/
PomodoroClockView.py
404 lines (363 loc) · 15.7 KB
/
PomodoroClockView.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#/usr/bin/env python
import math
from PyQt5.QtCore import (
QEasingCurve,
QPoint, Qt, QTime, QTimer, QPropertyAnimation, QRect, QRectF, QPointF,
QState, QStateMachine,
pyqtSignal, pyqtProperty,
)
from PyQt5.QtGui import QColor, QPainter, QPolygon, QPen, QBrush, QPalette
from PyQt5.QtWidgets import QApplication, QWidget, QToolBox, QGraphicsView, QDialog, QGraphicsEllipseItem, QGraphicsScene, QGraphicsObject, QGraphicsDropShadowEffect
from OverlayGraphicsView import OverlayGraphicsView
class QColorThemedGraphicsObject(QGraphicsObject):
"""
For all our graphics objects that need a color.
"""
def get_color(self):
return self._color
def set_color(self,c):
self._color = c
self.update()
color = pyqtProperty(QColor, get_color, set_color)
class ClockHandShape(QColorThemedGraphicsObject):
"""A single hand of a single clock. By default, this starts at 12
O'Clock; use setRotation to change.
Args: (x, y, length of the hand, width of the hand)
"""
def __init__(self, x,y, sz, width=5, parent=None):
super(ClockHandShape, self).__init__(parent)
self.setPos(QPoint(x,y))
self._sz = sz
self._width = width
self._color = QColor(0, 0, 0)
def paint(self, painter, option, widget):
#print(self.transformOrigin())
painter.setBrush(Qt.NoBrush)
painter.setPen(QPen(self._color,
self._width))
painter.drawLine(0, self._sz*0.1, 0, -self._sz)
W = self._width
painter.setBrush(self._color)
painter.setPen(Qt.NoPen)
painter.drawEllipse(QRectF(-1.*W, -1.*W, 2.*W, 2.*W))
def boundingRect(self):
return QRectF(-2*self._width, -self._sz,
4*self._width, self._width+self._sz*1.1)
class SynchronizedClockHand(ClockHandShape):
"""
This is a clock hand that automatically moves in time!
"""
def __init__(self, mode, *args, **kw):
super(SynchronizedClockHand,self).__init__(*args, **kw)
self.mode = mode
self.timer = QTimer(self)
self.timer.timeout.connect(self.handle_time_update)
self.timer.start(1000)
self.handle_time_update()
def handle_time_update(self):
time = QTime.currentTime()
if self.mode == "hour":
self.setRotation(time.hour() * 360. / 12.
+ time.minute() * 360. / (60*12))
elif self.mode == "minute":
self.setRotation(time.minute() * 360. / 60.
+ time.second() / 60. * 360. / 60.)
elif self.mode == "second":
self.setRotation(time.second() * 360. / 60.)
class ClockBack(QColorThemedGraphicsObject):
"""
Draw a nice back face of the clock.
This is just the white circle for now.
"""
def get_alpha(self):
return self._alpha
def set_alpha(self,c):
self._alpha = c
self.update()
alpha = pyqtProperty(float, get_alpha, set_alpha)
def get_line_width(self):
return self._line_width
def set_line_width(self,c):
self._line_width = c
self.update()
line_width = pyqtProperty(float, get_line_width, set_line_width)
def set_outline_color(self, outline_color):
self._outline_color = outline_color
def __init__(self, size, *args, **kw):
super(ClockBack,self).__init__(*args, **kw)
self.size = size
self.bbox = QRectF(0, 0, self.size, self.size)
self._color = QColor(255,0,0,255)
self._outline_color = QColor(255,0,0,255)
self._alpha = 255
self._line_width = 3
def paint(self, painter, option, widget):
# Draw background
painter.setPen(QPen(QColor(self._outline_color.red(),
self._outline_color.green(),
self._outline_color.blue(),
0.5*255 + 0.5*self._alpha
), self._line_width))
# How much white to mix in?
# At low alpha, I want the background to be super close to
# pure white.
A=(1 - 0.07*(self._alpha/255.))
B=1-A
painter.setBrush(QColor(255*A+B*self._color.red(),
255*A+B*self._color.green(),
255*A+B*self._color.blue(),
self._alpha
))
center = self.size/2
painter.drawEllipse(self.bbox)
## Draw four ticks along the sides
#pen = QPen(QColor(self._color.red(),
# self._color.green(),
# self._color.blue(),
# 128), 2)
#painter.setPen(pen)
#START, END = 0.40, 0.43
#painter.drawLine(QPointF(center, center + START*self.size),
# QPointF(center, center + END*self.size))
#painter.drawLine(QPointF(center, center - START*self.size),
# QPointF(center, center - END*self.size))
#painter.drawLine(QPointF(center + START*self.size, center),
# QPointF(center + END*self.size, center))
#painter.drawLine(QPointF(center - START*self.size, center),
# QPointF(center - END*self.size, center))
def boundingRect(self):
return QRectF(-self._line_width,
-self._line_width,
self.size + 2*self.line_width,
self.size + 2*self.line_width)
class TimeElapsedView(QColorThemedGraphicsObject):
"""Draws the ever-updating "Time Elapsed" graph. This takes a starting
time (in just minutes), an ending time (in just minutes), and will
draw a constantly updating current time. The assumption is the
ending time should be less than 60 minutes after starting time,
and the current time should also be less than 60 minutes after
starting.
"""
def __init__(self, sz, thickness, pomodoro_control, parent=None):
super(TimeElapsedView, self).__init__(parent)
self._sz = sz
self._thickness = thickness
self._color = Qt.blue
self._seconds_elapsed = 0
self._seconds_remaining = 0
# Recieve view updates from the Pomodoro Controller, horray~~
self.pomodoro_control = pomodoro_control
self.pomodoro_control.time_update.connect(self.update_time)
# Must periodically repaint everything!
self.timer = QTimer(self)
self.timer.timeout.connect(self.update)
self.timer.start(1000)
def update_time(self, seconds_elapsed, seconds_remaining):
self._seconds_elapsed = seconds_elapsed
self._seconds_remaining = seconds_remaining
print "time update: %d, %d"%(seconds_elapsed, seconds_remaining)
self.update() # schedule repaint
def boundingRect(self):
return QRectF(-self._thickness,
-self._thickness,
self._sz + 2*self._thickness,
self._sz + 2*self._thickness)
def paint(self, painter, option, widget):
time = QTime.currentTime()
minuteNow = time.minute() + (time.second() / 60.0)
angleSecondsElapsed = 16*(360*self._seconds_elapsed / 60. / 60.)
angleSecondsRemaining = 16*(360*self._seconds_remaining / 60. / 60.)
thetaNow = 16*(90 - 360 * minuteNow / 60.)
PADDING = 0.5*self._thickness
# Time left
painter.setPen(QPen(QColor(self._color.red(),
self._color.green(),
self._color.blue(),
255),
self._thickness,
Qt.SolidLine,
#Qt.FlatCap if abs(theta3-thetaNow) < 16*20 else Qt.RoundCap,
Qt.FlatCap
#Qt.RoundCap
))
painter.setBrush(Qt.NoBrush)
painter.drawArc(PADDING, PADDING,
self._sz-2*PADDING, self._sz-2*PADDING,
thetaNow, -angleSecondsRemaining)
# Elapsed time
painter.setPen(QPen(QColor(self._color.red(),
self._color.green(),
self._color.blue(),
64),
self._thickness,
Qt.SolidLine,
Qt.FlatCap,
))
painter.drawArc(PADDING, PADDING,
self._sz-2*PADDING, self._sz-2*PADDING,
thetaNow, angleSecondsElapsed)
class CircleObstruction(QColorThemedGraphicsObject):
"""
Useful for notifications, I...guess?
"""
def get_thickness(self):
return self._thickness
def set_thickness(self,c):
self._thickness = c
self.update()
thickness = pyqtProperty(float, get_thickness, set_thickness)
def __init__(self, sz, thickness, parent=None):
super(CircleObstruction, self).__init__(parent)
self._sz = sz
self._thickness = thickness
self._color = Qt.blue
def boundingRect(self):
return QRectF(-self._thickness,
-self._thickness,
self._sz + 2*self._thickness,
self._sz + 2*self._thickness)
def paint(self, painter, option, widget):
# painter.setPen(QPen(self._color,
# self._thickness))
painter.setBrush(self._color)
painter.setPen(Qt.NoPen)
painter.drawEllipse(QRectF(
self._sz/2.0 - self._thickness,
self._sz/2.0 - self._thickness,
2*self._thickness,
2*self._thickness,
))
def show_anim(self):
self.anim = QPropertyAnimation(self, "thickness")
self.anim.setDuration(2000)
self.anim.setStartValue(self.get_thickness())
self.anim.setEndValue(50.0)
self.anim.setEasingCurve(QEasingCurve.OutElastic)
self.anim.start()
def hide_anim(self):
self.anim = QPropertyAnimation(self, "thickness")
self.anim.setDuration(500)
self.anim.setStartValue(self.get_thickness())
self.anim.setEndValue(0.0)
self.anim.setEasingCurve(QEasingCurve.InBack)
self.anim.start()
class PomodoroClockView(OverlayGraphicsView, QColorThemedGraphicsObject):
pomodoro_begin_requested = pyqtSignal()
pomodoro_pause_requested = pyqtSignal()
def set_color(self, color, outline_color):
super(PomodoroClockView, self).set_color(color)
self.hour_hand.set_color(outline_color)
self.minute_hand.set_color(outline_color)
self.clock_back.set_color(self._color)
self.clock_back.set_outline_color(outline_color)
self.time_elapsed_view.set_color(self._color)
self.obstruction.set_color(self._color)
def __init__(self, pomodoro_control, parent=None):
super(PomodoroClockView, self).__init__(parent)
self.dragPosition = None
self.pomodoro_control = pomodoro_control
# Hide scrollbars
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setStyleSheet("background: transparent")
self.setRenderHint(QPainter.Antialiasing)
# A scene containing all the parts of the clock
scene = QGraphicsScene()
self.setScene(scene)
self.clock_back = ClockBack(100)
self.hour_hand = SynchronizedClockHand("hour", 50, 50, 37*0.66, 2.3)
self.minute_hand = SynchronizedClockHand("minute", 50, 50, 37, 2)
self.time_elapsed_view = TimeElapsedView(100, 15, self.pomodoro_control)
self.obstruction = CircleObstruction(100, 0)
scene.addItem(self.clock_back)
scene.addItem(self.hour_hand)
scene.addItem(self.minute_hand)
scene.addItem(self.time_elapsed_view)
scene.addItem(self.obstruction)
self.set_color(QColor(0,0,0), QColor(0,0,0))
"""
Turns out I can't use a QStateMachine.
Why?
- I want this class to be *completely* subservient to the internal state
of the PomodoroControl. When that changes its state, this class
*reacts* to that change.
- QStateMachine runs in its own event loop, which does not start until after
the app's event loop runs. The state machine will refuse to catch signals
until the app is already running. This means that *this class* must know
whether it's starting at a started or stopped state.
"""
self.pomodoro_control.pomodoro_begin.connect(self.pomodoro_begin)
self.pomodoro_control.pomodoro_complete.connect(self.pomodoro_complete)
if self.pomodoro_control.is_running:
self.pomodoro_begin()
else:
self.pomodoro_complete()
def pomodoro_begin(self):
"""
Animation for beginning a pomodoro.
"""
self.obstruction.hide_anim()
def pomodoro_complete(self):
"""
Animation for completing a pomodoro.
"""
self.obstruction.show_anim()
# def pp(x): print x
# self.pomodoro_control.pomodoro_begin.connect(lambda: pp("Pomodoro is actually beginning."))
# self.stopped_state.entered.connect(lambda: pp("STOPPED"))
# self.running_state.entered.connect(lambda: pp("RUNNING"))
# self.wait_for_start_state = QState()
# self.wait_for_start_state.addTransition(self.pomodoro_control.pomodoro_begin,
# self.running_state)
# self.running_state.addTransition(self.pomodoro_control.pomodoro_complete,
# self.wait_for_start_state)
# self.wait_for_start_state.addTransition(
# self.mousePressEvent,
# self.pomodoro_begin_requested)
# self.running_state.addTransition(
# self.mousePressEvent,
# self.pomodoro_pause_requested)
#self.anim = QPropertyAnimation(self.obstruction, "thickness")
#self.anim.setDuration(2000)
#self.anim.setStartValue(0.0)
#self.anim.setEndValue(50.0)
#self.anim.setEasingCurve(QEasingCurve.OutElastic)
#self.anim.start()
#self.anim2 = QPropertyAnimation(self.clock_back, "line_width")
#self.anim2.setDuration(1000)
#self.anim2.setStartValue(1.0)
#self.anim2.setEndValue(4.0)
#self.anim2.setEasingCurve(QEasingCurve.OutExpo)
#self.anim2.start()
#self.anim3 = QPropertyAnimation(scene, "zoom")
#self.anim3.setDuration(1000)
#self.anim3.setStartValue(0.0)
#self.anim3.setEndValue(1.0)
#self.anim3.setEasingCurve(QEasingCurve.OutBounce)
#self.anim3.start()
def resizeEvent(self, evt):
#super(PomodoroClockView, self).resize(w, h)
w = evt.size().width()
h = evt.size().height()
# Transformation: let's let (50, 50) be the center and range from [0,100].
self.fitInView(QRectF(-10, -10, 120, 120))
self.setSceneRect(QRectF(0, 0, 100, 100))
def mousePressEvent(self, evt):
"""
Users can move the clock by dragging it aruond.
"""
self.is_dragging = False
if evt.button() == Qt.LeftButton:
self.dragPosition = evt.globalPos() - self.frameGeometry().topLeft()
evt.accept()
def mouseMoveEvent(self, evt):
self.is_dragging = True
if Qt.LeftButton & evt.buttons():
self.move(evt.globalPos() - self.dragPosition)
evt.accept()
def mouseReleaseEvent(self, evt):
if not self.is_dragging:
if self.pomodoro_control.is_running:
self.pomodoro_pause_requested.emit()
else:
self.pomodoro_begin_requested.emit()