|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the snapshot timer widget. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import pyqtSignal, Qt, QTimer, QRect |
|
11 from PyQt4.QtGui import QWidget, QApplication, QPainter, QToolTip, QPalette |
|
12 |
|
13 |
|
14 class SnapshotTimer(QWidget): |
|
15 """ |
|
16 Class implementing the snapshot timer widget. |
|
17 |
|
18 @signal timeout() emitted after the timer timed out |
|
19 """ |
|
20 timeout = pyqtSignal() |
|
21 |
|
22 def __init__(self): |
|
23 """ |
|
24 Constructor |
|
25 """ |
|
26 super().__init__(None) |
|
27 |
|
28 self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | |
|
29 Qt.X11BypassWindowManagerHint) |
|
30 |
|
31 self.__timer = QTimer() |
|
32 self.__textRect = QRect() |
|
33 self.__time = 0 |
|
34 self.__length = 0 |
|
35 self.__toggle = True |
|
36 |
|
37 # text is taken from paintEvent with maximum number plus some margin |
|
38 self.resize( |
|
39 self.fontMetrics().width( |
|
40 self.trUtf8("Snapshot will be taken in %n seconds", "", 99)) + 6, |
|
41 self.fontMetrics().height() + 4) |
|
42 |
|
43 self.__timer.timeout.connect(self.__bell) |
|
44 |
|
45 def start(self, seconds): |
|
46 """ |
|
47 Public method to start the timer. |
|
48 |
|
49 @param seconds timeout value (integer) |
|
50 """ |
|
51 screenGeom = QApplication.desktop().screenGeometry() |
|
52 self.move(screenGeom.width() // 2 - self.size().width() // 2, screenGeom.top()) |
|
53 self.__toggle = True |
|
54 self.__time = 0 |
|
55 self.__length = seconds |
|
56 self.__timer.start(1000) |
|
57 self.show() |
|
58 |
|
59 def stop(self): |
|
60 """ |
|
61 Public method to stop the timer. |
|
62 """ |
|
63 self.setVisible(False) |
|
64 self.hide() |
|
65 self.__timer.stop() |
|
66 |
|
67 def __bell(self): |
|
68 """ |
|
69 Private slot handling timer timeouts. |
|
70 """ |
|
71 if self.__time == self.__length - 1: |
|
72 self.hide() |
|
73 else: |
|
74 if self.__time == self.__length: |
|
75 self.__timer.stop() |
|
76 self.timeout.emit() |
|
77 |
|
78 self.__time += 1 |
|
79 self.__toggle = not self.__toggle |
|
80 self.update() |
|
81 |
|
82 def paintEvent(self, evt): |
|
83 """ |
|
84 Protected method handling paint events. |
|
85 |
|
86 @param evt paint event (QPaintEvent) |
|
87 """ |
|
88 painter = QPainter(self) |
|
89 |
|
90 if self.__time < self.__length: |
|
91 pal = QToolTip.palette() |
|
92 textBackgroundColor = pal.color(QPalette.Active, QPalette.Base) |
|
93 if self.__toggle: |
|
94 textColor = pal.color(QPalette.Active, QPalette.Text) |
|
95 else: |
|
96 textColor = pal.color(QPalette.Active, QPalette.Base) |
|
97 painter.setPen(textColor) |
|
98 painter.setBrush(textBackgroundColor) |
|
99 helpText = self.trUtf8("Snapshot will be taken in %n seconds", "", |
|
100 self.__length - self.__time) |
|
101 textRect = painter.boundingRect(self.rect().adjusted(2, 2, -2, -2), |
|
102 Qt.AlignHCenter | Qt.TextSingleLine, helpText) |
|
103 painter.drawText(textRect, Qt.AlignHCenter | Qt.TextSingleLine, helpText) |
|
104 |
|
105 def enterEvent(self, evt): |
|
106 """ |
|
107 Protected method handling the mouse cursor entering the widget. |
|
108 |
|
109 @param evt enter event (QEvent) |
|
110 """ |
|
111 screenGeom = QApplication.desktop().screenGeometry() |
|
112 if self.x() == screenGeom.left(): |
|
113 self.move( |
|
114 screenGeom.x() + (screenGeom.width() // 2 - self.size().width() // 2), |
|
115 screenGeom.top()) |
|
116 else: |
|
117 self.move(screenGeom.topLeft()) |