forked from anak10thn/qt5-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigiflip.py
348 lines (281 loc) · 10.9 KB
/
digiflip.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
#############################################################################
##
## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
## Contact: Qt Software Information ([email protected])
##
## This file is part of the Graphics Dojo project on Qt Labs.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 or 3.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file. Please review the following information to ensure GNU
## General Public Licensing requirements will be met:
## http://www.fsf.org/licensing/licenses/info/GPLv2.html and
## http://www.gnu.org/copyleft/gpl.html.
##
## If you are unsure which license is appropriate for your use, please
## contact the sales department at [email protected].
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
#############################################################################
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Digits(QWidget):
Slide, Flip, Rotate = range(3)
def __init__(self, parent):
QWidget.__init__(self, parent)
self.m_number = 0
self.m_transition = self.Slide
self.m_pixmap = QPixmap()
self.m_lastPixmap = QPixmap()
self.m_animator = QTimeLine()
self.setAttribute(Qt.WA_OpaquePaintEvent, True)
self.setAttribute(Qt.WA_NoSystemBackground, True)
self.connect(self.m_animator, SIGNAL("frameChanged(int)"), SLOT("update()"))
self.m_animator.setFrameRange(0, 100)
self.m_animator.setDuration(600)
self.m_animator.setCurveShape(QTimeLine.EaseInOutCurve)
def setTransition(self, tr):
self.m_transition = tr
def transition(self):
return self.m_transition
def setNumber(self, n):
if self.m_number != n:
self.m_number = max(0, min(n, 99))
self.preparePixmap()
self.update()
def flipTo(self, n):
if self.m_number != n:
self.m_number = max(0, min(n, 99))
self.m_lastPixmap = self.m_pixmap
self.preparePixmap()
self.m_animator.stop()
self.m_animator.start()
def drawFrame(self, p, rect):
p.setPen(Qt.NoPen)
gradient = QLinearGradient(QPointF(rect.topLeft()), QPointF(rect.bottomLeft()))
gradient.setColorAt(0.00, QColor(245, 245, 245))
gradient.setColorAt(0.49, QColor(192, 192, 192))
gradient.setColorAt(0.51, QColor(245, 245, 245))
gradient.setColorAt(1.00, QColor(192, 192, 192))
p.setBrush(gradient)
r = rect
p.drawRoundedRect(r, 15, 15, Qt.RelativeSize)
r.adjust(1, 4, -1, -4)
p.setPen(QColor(181, 181, 181))
p.setBrush(Qt.NoBrush)
p.drawRoundedRect(r, 15, 15, Qt.RelativeSize)
p.setPen(QColor(159, 159, 159))
y = rect.top() + rect.height() / 2 - 1
p.drawLine(rect.left(), y, rect.right(), y)
def drawDigits(self, n, rect):
s = "%02i" % n
font = QFont()
font.setFamily("Helvetica")
fontHeight = 2 * 0.55 * rect.height()
font.setPixelSize(fontHeight)
font.setBold(True)
pixmap = QPixmap(rect.size() * 2)
pixmap.fill(Qt.transparent)
gradient = QLinearGradient(QPointF(0, 0), QPointF(0, pixmap.height()))
gradient.setColorAt(0.00, QColor(128, 128, 128))
gradient.setColorAt(0.49, QColor(64, 64, 64))
gradient.setColorAt(0.51, QColor(128, 128, 128))
gradient.setColorAt(1.00, QColor(16, 16, 16))
p = QPainter()
p.begin(pixmap)
p.setFont(font)
pen = QPen()
pen.setBrush(QBrush(gradient))
p.setPen(pen)
p.drawText(pixmap.rect(), Qt.AlignCenter, s)
p.end()
return pixmap.scaledToWidth(self.width(), Qt.SmoothTransformation)
def preparePixmap(self):
self.m_pixmap = QPixmap(self.size())
self.m_pixmap.fill(Qt.transparent)
p = QPainter()
p.begin(self.m_pixmap)
p.drawPixmap(0, 0, self.drawDigits(self.m_number, self.rect()))
p.end()
def resizeEvent(self, event):
self.preparePixmap()
self.update()
def paintStatic(self):
p = QPainter(self)
p.fillRect(self.rect(), Qt.black)
pad = self.width() / 10
self.drawFrame(p, self.rect().adjusted(pad, pad, -pad, -pad))
p.drawPixmap(0, 0, self.m_pixmap)
def paintSlide(self):
p = QPainter(self)
p.fillRect(self.rect(), Qt.black)
pad = self.width() / 10
fr = self.rect().adjusted(pad, pad, -pad, -pad)
self.drawFrame(p, fr)
p.setClipRect(fr)
y = self.height() * self.m_animator.currentFrame() / 100
p.drawPixmap(0, y, self.m_lastPixmap)
p.drawPixmap(0, y - self.height(), self.m_pixmap)
def paintFlip(self):
p = QPainter(self)
p.setRenderHint(QPainter.SmoothPixmapTransform, True)
p.setRenderHint(QPainter.Antialiasing, True)
p.fillRect(self.rect(), Qt.black)
hw = self.width() / 2
hh = self.height() / 2
# behind is the new pixmap
pad = self.width() / 10
fr = self.rect().adjusted(pad, pad, -pad, -pad)
self.drawFrame(p, fr)
p.drawPixmap(0, 0, self.m_pixmap)
index = self.m_animator.currentFrame()
if index <= 50:
# the top part of the old pixmap is flipping
angle = -180 * index / 100
transform = QTransform()
transform.translate(hw, hh)
transform.rotate(angle, Qt.XAxis)
p.setTransform(transform)
self.drawFrame(p, fr.adjusted(-hw, -hh, -hw, -hh))
p.drawPixmap(-hw, -hh, self.m_lastPixmap)
# the bottom part is still the old pixmap
p.resetTransform()
p.setClipRect(0, hh, self.width(), hh)
self.drawFrame(p, fr)
p.drawPixmap(0, 0, self.m_lastPixmap)
else:
p.setClipRect(0, hh, self.width(), hh)
# the bottom part is still the old pixmap
self.drawFrame(p, fr)
p.drawPixmap(0, 0, self.m_lastPixmap)
# the bottom part of the new pixmap is flipping
angle = 180 - 180 * self.m_animator.currentFrame() / 100
transform = QTransform()
transform.translate(hw, hh)
transform.rotate(angle, Qt.XAxis)
p.setTransform(transform)
self.drawFrame(p, fr.adjusted(-hw, -hh, -hw, -hh))
p.drawPixmap(-hw, -hh, self.m_pixmap)
def paintRotate(self):
p = QPainter(self)
pad = self.width() / 10
fr = self.rect().adjusted(pad, pad, -pad, -pad)
self.drawFrame(p, fr)
p.setClipRect(fr)
angle1 = -180 * self.m_animator.currentFrame() / 100
angle2 = 180 - 180 * self.m_animator.currentFrame() / 100
if self.m_animator.currentFrame() <= 50:
angle = angle1
else:
angle = angle2
if self.m_animator.currentFrame() <= 50:
pix = self.m_lastPixmap
else:
pix = self.m_pixmap
transform = QTransform()
transform.translate(self.width() / 2, self.height() / 2)
transform.rotate(angle, Qt.XAxis)
p.setTransform(transform)
p.setRenderHint(QPainter.SmoothPixmapTransform, True)
p.drawPixmap(-self.width() / 2, -self.height() / 2, pix)
def paintEvent(self, event):
if self.m_animator.state() == QTimeLine.Running:
if self.m_transition == self.Slide:
self.paintSlide()
if self.m_transition == self.Flip:
self.paintFlip()
if self.m_transition == self.Rotate:
self.paintRotate()
else:
self.paintStatic()
class DigiFlip(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.m_ticker = QBasicTimer()
self.m_hour = Digits(self)
self.m_hour.show()
self.m_minute = Digits(self)
self.m_minute.show()
pal = QPalette(self.palette())
pal.setColor(QPalette.Window, Qt.black)
self.setPalette(pal)
self.m_ticker.start(1000, self)
t = QTime.currentTime()
self.m_hour.setNumber(t.hour())
self.m_minute.setNumber(t.minute())
self.updateTime()
slideAction = QAction("Slide", self)
flipAction = QAction("Flip", self)
rotateAction = QAction("Rotate", self)
self.connect(slideAction, SIGNAL("triggered()"), self.chooseSlide)
self.connect(flipAction, SIGNAL("triggered()"), self.chooseFlip)
self.connect(rotateAction, SIGNAL("triggered()"), self.chooseRotate)
#if defined(Q_OS_SYMBIAN)
# menuBar().addAction(slideAction)
# menuBar().addAction(flipAction)
# menuBar().addAction(rotateAction)
#else
self.addAction(slideAction)
self.addAction(flipAction)
self.addAction(rotateAction)
self.setContextMenuPolicy(Qt.ActionsContextMenu)
#endif
def updateTime(self):
t = QTime.currentTime()
self.m_hour.flipTo(t.hour())
self.m_minute.flipTo(t.minute())
if self.m_hour.transition() == Digits.Slide:
s = u"Slide: "
if self.m_hour.transition() == Digits.Flip:
s = u"Flip: "
if self.m_hour.transition() == Digits.Rotate:
s = u"Rotate: "
self.setWindowTitle(s + t.toString("hh:mm:ss"))
def switchTransition(self, delta):
i = (self.m_hour.transition() + delta + 3) % 3
self.m_hour.setTransition(i)
self.m_minute.setTransition(i)
self.updateTime()
def resizeEvent(self, event):
digitsWidth = self.width() / 2
digitsHeight = digitsWidth * 1.2
y = (self.height() - digitsHeight) / 3
self.m_hour.resize(digitsWidth, digitsHeight)
self.m_hour.move(0, y)
self.m_minute.resize(digitsWidth, digitsHeight)
self.m_minute.move(self.width() / 2, y)
def timerEvent(self, event):
self.updateTime()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Right:
self.switchTransition(1)
event.accept()
if event.key() == Qt.Key_Left:
self.switchTransition(-1)
event.accept()
def chooseSlide(self):
self.m_hour.setTransition(0)
self.m_minute.setTransition(0)
self.updateTime()
def chooseFlip(self):
self.m_hour.setTransition(1)
self.m_minute.setTransition(1)
self.updateTime()
def chooseRotate(self):
self.m_hour.setTransition(2)
self.m_minute.setTransition(2)
self.updateTime()
if __name__ == "__main__":
app = QApplication(sys.argv)
time = DigiFlip()
#if defined(Q_OS_SYMBIAN)
# time.showMaximized()
#else
time.resize(320, 240)
time.show()
#endif
sys.exit(app.exec_())