-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedoThermo.py
151 lines (120 loc) · 5.82 KB
/
speedoThermo.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import math
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import Qt, QRectF, QSize, QPoint
from PyQt5.QtGui import QPen, QPainter, QPainterPath, QTransform, QFont
from PyQt5.QtGui import QColor
from PyQt5.QtGui import QPolygon
class SpeedoThermo(QWidget):
def __init__(self, x, y, radius, width, min_temp, max_temp, show, name):
super().__init__()
self.origin_x = x
self.origin_y = y
self.radius = radius
self.app_width = width
self.min_temp = min_temp
self.max_temp = max_temp
self.show_temps = show
self.name = name
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.angle_range = 56
self.initUI()
def initUI(self):
self.angle = 0
self.setThermoTemp(self.min_temp)
self.setFixedWidth(1920)
self.setFixedHeight(1080)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
green = QColor(46, 125, 50)
# magic nums to draw the lil segments
self.drawArc(painter, self.origin_x, self.origin_y, green, self.radius, -self.angle_range, 6, self.app_width)
self.drawArc(painter, self.origin_x, self.origin_y, green, self.radius, -self.angle_range + 14, 38, self.app_width)
self.drawArc(painter, self.origin_x, self.origin_y, green, self.radius, 4, 38, self.app_width)
self.drawArc(painter, self.origin_x, self.origin_y, Qt.red, self.radius, self.angle_range-6, 6, self.app_width)
if self.show_temps:
label_font = QFont("Futura", 32)
label_font.setBold(True)
painter.setFont(label_font)
font_color = QColor(255, 255, 255)
painter.setPen(font_color)
(x, y) = self.get_radial_point(self.origin_x, self.origin_y, self.radius + (self.app_width * 2.4), -56)
label_rect = QRectF(x-40, y, 100, 40)
painter.drawText(label_rect, Qt.AlignCenter, str(self.min_temp))
(x, y) = self.get_radial_point(self.origin_x, self.origin_y, self.radius + (self.app_width * 2.4), 56)
label_rect = QRectF(x-60, y, 100, 40)
painter.drawText(label_rect, Qt.AlignCenter, str(self.max_temp))
# lil circle
d = int(self.radius * .7)
x = int(self.origin_x - (d / 2))
y = int(self.origin_y - (d / 2))
painter.setBrush(QColor(Qt.yellow))
painter.setPen(QPen(QColor(Qt.black), 1, Qt.SolidLine))
painter.drawEllipse(x, y, d, d)
# bottom label with name
label_font = QFont("Futura", 22)
label_font.setBold(True)
painter.setFont(label_font)
painter.setPen(QPen(QColor(Qt.white), 1, Qt.SolidLine))
label_rect = QRectF(x, y + d, 100, 50)
painter.drawText(label_rect, Qt.AlignCenter, str(self.name))
# Create a transformation matrix for rotation
transform = QTransform()
transform.translate(self.origin_x, self.origin_y)
transform.rotate(self.angle)
transform.translate(-self.origin_x, -self.origin_y)
painter.setTransform(transform)
# draw the triangle
triangle = QPolygon([
QPoint(self.origin_x, int(self.origin_y - self.radius - (self.app_width/2))),
QPoint(self.origin_x - 8, self.origin_y),
QPoint(self.origin_x + 8, self.origin_y)
])
painter.setBrush(QColor(255, 143, 0))
painter.drawPolygon(triangle)
def drawArc(self, painter, origin_x, origin_y, color, radius, start_angle, span_angle, width):
painter.setPen(QPen(color, 1, Qt.SolidLine))
# Calculate points for the arc
arc_points1 = self.calculate_arc_points(origin_x, origin_y, radius, start_angle, span_angle)
arc_points2 = self.calculate_arc_points(origin_x, origin_y, radius + width, start_angle, span_angle)
arc_points2.reverse()
path = QPainterPath()
path.moveTo(arc_points1[0])
for point in arc_points1:
path.lineTo(point)
path.lineTo(arc_points2[0])
for point in arc_points2:
path.lineTo(point)
path.lineTo(arc_points1[0])
painter.setBrush(color)
painter.drawPath(path)
def setThermoTemp(self, angle):
# self.angle = 0 is straight up
r = self.max_temp - self.min_temp
self.angle = self.map_range(angle, self.min_temp, self.max_temp, (-1 * self.angle_range), self.angle_range)
self.update()
def map_range(self, x, in_min, in_max, out_min, out_max):
return ((x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min)
def get_radial_point(self, x, y, radius, angle):
angle += 270
x = int(x + radius * math.cos(angle * math.pi / 180))
y = int(y + radius * math.sin(angle * math.pi / 180))
return(x, y)
def calculate_arc_points(self, origin_x, origin_y, radius, start_angle, span_angle):
start_angle += 270
# Calculate the points on the arc
arc_points = []
angle_step = 1 # Increment angle by 1 degree
if span_angle < 0:
for angle in range(start_angle, -(start_angle + span_angle + 1), -angle_step):
x = int(origin_x + radius * math.cos(angle * math.pi / 180))
y = int(origin_y + radius * math.sin(angle * math.pi / 180))
arc_points.append(QPoint(x, y)) # Create QPoint object and append
else:
for angle in range(start_angle, start_angle + span_angle + 1, angle_step):
x = int(origin_x + radius * math.cos(angle * math.pi / 180))
y = int(origin_y + radius * math.sin(angle * math.pi / 180))
arc_points.append(QPoint(x, y)) # Create QPoint object and append
return arc_points