-
Notifications
You must be signed in to change notification settings - Fork 2
/
compass.py
95 lines (70 loc) · 2.89 KB
/
compass.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
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class CompassWidget(QWidget):
angleChanged = pyqtSignal(float)
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self._angle = 0.0
self._margins = 10
self._pointText = {0: "N", 45: "NE", 90: "E", 135: "SE", 180: "S",
225: "SW", 270: "W", 315: "NW"}
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.fillRect(event.rect(), self.palette().brush(QPalette.Window))
self.drawMarkings(painter)
self.drawNeedle(painter)
painter.end()
def drawMarkings(self, painter):
painter.save()
painter.translate(self.width()/2, self.height()/2)
scale = min((self.width() - self._margins)/120.0,
(self.height() - self._margins)/120.0)
painter.scale(scale, scale)
font = QFont(self.font())
font.setPixelSize(10)
metrics = QFontMetricsF(font)
painter.setFont(font)
painter.setPen(Qt.white) #self.palette().color(QPalette.Shadow)
i = 0
while i < 360:
if i % 45 == 0:
painter.drawLine(0, -40, 0, -50)
painter.drawText(-metrics.width(self._pointText[i])/2.0, -52,
self._pointText[i])
else:
painter.drawLine(0, -45, 0, -50)
painter.rotate(15)
i += 15
painter.restore()
def drawNeedle(self, painter):
painter.save()
painter.translate(self.width()/2, self.height()/2)
painter.rotate(self._angle)
scale = min((self.width() - self._margins)/120.0,
(self.height() - self._margins)/120.0)
painter.scale(scale, scale)
painter.setPen(QPen(Qt.NoPen))
painter.setBrush(Qt.black) #stand er colour
painter.drawPolygon(
QPolygon([QPoint(-10, 0), QPoint(0, -45), QPoint(10, 0),
QPoint(0, 45), QPoint(-10, 0)])
)
painter.setBrush(Qt.blue) #self.palette().brush(QPalette.Blue)
painter.drawPolygon(
QPolygon([QPoint(-5, -25), QPoint(0, -45), QPoint(5, -25),
QPoint(0, -30), QPoint(-5, -25)])
)
painter.restore()
def sizeHint(self):
return QSize(150, 150)
def angle(self):
return self._angle
def setAngle(self, angle):
if angle != self._angle:
self._angle = angle
self.angleChanged.emit(angle)
self.update()