-
Notifications
You must be signed in to change notification settings - Fork 4
/
manual.py
118 lines (88 loc) · 3.89 KB
/
manual.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf8 -*-
"""
manual.py - The view shown when controlling the car manually.
"""
from PySide.QtGui import *
from PySide.QtCore import *
import widgets
class ManualView(QGraphicsView):
def __init__(self, car, parent=None):
super(ManualView, self).__init__(parent)
self.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
scene = ManualScene(car=car, parent=self)
self.setScene(scene)
# Disabling scrollbars
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
def showEvent(self, event):
super(ManualView, self).showEvent(event)
def paintEvent(self, event):
super(ManualView, self).paintEvent(event)
class ManualScene(QGraphicsScene):
def __init__(self, car, parent=None):
super(ManualScene, self).__init__(parent)
self.car = car
# Defines the maximum speed of the car
self.maxSpeed = 250
# Gradient background
self.gradient = QLinearGradient(0, -200, 0, 600)
lightGray = 0.1
darkGray = 0.03
self.gradient.setColorAt(0, QColor.fromRgbF(darkGray, darkGray, darkGray, 1))
self.gradient.setColorAt(1, QColor.fromRgbF(lightGray, lightGray, lightGray, 1))
self.setBackgroundBrush(self.gradient)
self.w = 980
self.h = 700
# Rect
self.rectItem = QGraphicsRectItem(0, 0, self.w, self.h)
self.rectItem.setPen(QColor.fromRgbF(1., 0., 0., 0.))
self.rectItem.setFlags(QGraphicsItem.ItemClipsToShape)
self.rectItem.setZValue(0)
self.addItem(self.rectItem)
self.setSceneRect(self.rectItem.boundingRect())
# Text
self.titleItem = QGraphicsTextItem("Dashboard")
self.titleItem.setFont(QFont("Ubuntu-L.ttf", 35, QFont.Light))
# 'Dirty' centering of the text
self.titleItem.setPos(100 + (self.w - self.titleItem.boundingRect().width())/2, 10)
self.titleItem.setDefaultTextColor(QColor(210, 220, 250))
self.addItem(self.titleItem)
# Compass
self.compass = widgets.CarCompass(self.car)
self.compass.setPos(50, self.h - self.compass.boundingRect().height() - 50)
self.addItem(self.compass)
# Speed meter
self.speedmeter = widgets.CarSpeedMeter(self.car)
x = self.compass.pos().x() + self.compass.boundingRect().width() + 40
y = self.h - self.speedmeter.boundingRect().height() - 120
self.speedmeter.setPos(x, y)
self.addItem(self.speedmeter)
# Thermometer
self.thermometer = widgets.CarThermometer(self.car)
x = self.speedmeter.pos().x() + self.speedmeter.boundingRect().width() + 40
y = self.h - self.thermometer.boundingRect().height()
self.thermometer.setPos(x, y)
self.addItem(self.thermometer)
# "Obstacle warner"
self.obstacleWarning = widgets.ObstacleWarning(self.car)
self.addItem(self.obstacleWarning)
# List of pressed keys (to press multiple keys at the same time)
self.keylist = list()
self.firstrelease = True
def mousePressEvent(self, event):
super(ManualScene, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
super(ManualScene, self).mouseMoveEvent(event)
def keyPressEvent(self, event):
if event.key() == Qt.Key_L:
servoAngle = min(80, self.car.servoAngle + 10)
self.car.setServoAngle(servoAngle)
elif event.key() == Qt.Key_J:
servoAngle = max(-80, self.car.servoAngle - 10)
self.car.setServoAngle(servoAngle)
elif event.key() == Qt.Key_Plus:
self.car.setMaxSpeed(self.car.maxspeed + 10)
elif event.key() == Qt.Key_Minus:
self.car.setMaxSpeed(self.car.maxspeed - 10)
def processmultikeys(self,keyspressed):
print keyspressed