13 |
13 |
14 |
14 |
15 class SnapshotTimer(QWidget): |
15 class SnapshotTimer(QWidget): |
16 """ |
16 """ |
17 Class implementing the snapshot timer widget. |
17 Class implementing the snapshot timer widget. |
18 |
18 |
19 @signal timeout() emitted after the timer timed out |
19 @signal timeout() emitted after the timer timed out |
20 """ |
20 """ |
|
21 |
21 timeout = pyqtSignal() |
22 timeout = pyqtSignal() |
22 |
23 |
23 def __init__(self): |
24 def __init__(self): |
24 """ |
25 """ |
25 Constructor |
26 Constructor |
26 """ |
27 """ |
27 super().__init__(None) |
28 super().__init__(None) |
28 |
29 |
29 self.setWindowFlags( |
30 self.setWindowFlags( |
30 Qt.WindowType.WindowStaysOnTopHint | |
31 Qt.WindowType.WindowStaysOnTopHint |
31 Qt.WindowType.FramelessWindowHint | |
32 | Qt.WindowType.FramelessWindowHint |
32 Qt.WindowType.X11BypassWindowManagerHint |
33 | Qt.WindowType.X11BypassWindowManagerHint |
33 ) |
34 ) |
34 |
35 |
35 self.__timer = QTimer() |
36 self.__timer = QTimer() |
36 self.__textRect = QRect() |
37 self.__textRect = QRect() |
37 self.__time = 0 |
38 self.__time = 0 |
38 self.__length = 0 |
39 self.__length = 0 |
39 self.__toggle = True |
40 self.__toggle = True |
40 |
41 |
41 # text is taken from paintEvent with maximum number plus some margin |
42 # text is taken from paintEvent with maximum number plus some margin |
42 try: |
43 try: |
43 fmWidth = self.fontMetrics().horizontalAdvance(self.tr( |
44 fmWidth = self.fontMetrics().horizontalAdvance( |
44 "Snapshot will be taken in %n seconds", "", 99)) |
45 self.tr("Snapshot will be taken in %n seconds", "", 99) |
|
46 ) |
45 except AttributeError: |
47 except AttributeError: |
46 fmWidth = self.fontMetrics().width(self.tr( |
48 fmWidth = self.fontMetrics().width( |
47 "Snapshot will be taken in %n seconds", "", 99)) |
49 self.tr("Snapshot will be taken in %n seconds", "", 99) |
|
50 ) |
48 self.resize(fmWidth + 6, self.fontMetrics().height() + 4) |
51 self.resize(fmWidth + 6, self.fontMetrics().height() + 4) |
49 |
52 |
50 self.__timer.timeout.connect(self.__bell) |
53 self.__timer.timeout.connect(self.__bell) |
51 |
54 |
52 def start(self, seconds): |
55 def start(self, seconds): |
53 """ |
56 """ |
54 Public method to start the timer. |
57 Public method to start the timer. |
55 |
58 |
56 @param seconds timeout value (integer) |
59 @param seconds timeout value (integer) |
57 """ |
60 """ |
58 screenGeom = QApplication.screens()[0].geometry() |
61 screenGeom = QApplication.screens()[0].geometry() |
59 self.move(screenGeom.width() // 2 - self.size().width() // 2, |
62 self.move(screenGeom.width() // 2 - self.size().width() // 2, screenGeom.top()) |
60 screenGeom.top()) |
|
61 self.__toggle = True |
63 self.__toggle = True |
62 self.__time = 0 |
64 self.__time = 0 |
63 self.__length = seconds |
65 self.__length = seconds |
64 self.__timer.start(1000) |
66 self.__timer.start(1000) |
65 self.show() |
67 self.show() |
66 |
68 |
67 def stop(self): |
69 def stop(self): |
68 """ |
70 """ |
69 Public method to stop the timer. |
71 Public method to stop the timer. |
70 """ |
72 """ |
71 self.setVisible(False) |
73 self.setVisible(False) |
72 self.hide() |
74 self.hide() |
73 self.__timer.stop() |
75 self.__timer.stop() |
74 |
76 |
75 def __bell(self): |
77 def __bell(self): |
76 """ |
78 """ |
77 Private slot handling timer timeouts. |
79 Private slot handling timer timeouts. |
78 """ |
80 """ |
79 if self.__time == self.__length - 1: |
81 if self.__time == self.__length - 1: |
80 self.hide() |
82 self.hide() |
81 else: |
83 else: |
82 if self.__time == self.__length: |
84 if self.__time == self.__length: |
83 self.__timer.stop() |
85 self.__timer.stop() |
84 self.timeout.emit() |
86 self.timeout.emit() |
85 |
87 |
86 self.__time += 1 |
88 self.__time += 1 |
87 self.__toggle = not self.__toggle |
89 self.__toggle = not self.__toggle |
88 self.update() |
90 self.update() |
89 |
91 |
90 def paintEvent(self, evt): |
92 def paintEvent(self, evt): |
91 """ |
93 """ |
92 Protected method handling paint events. |
94 Protected method handling paint events. |
93 |
95 |
94 @param evt paint event (QPaintEvent) |
96 @param evt paint event (QPaintEvent) |
95 """ |
97 """ |
96 painter = QPainter(self) |
98 painter = QPainter(self) |
97 |
99 |
98 if self.__time < self.__length: |
100 if self.__time < self.__length: |
99 pal = QToolTip.palette() |
101 pal = QToolTip.palette() |
100 textBackgroundColor = pal.color(QPalette.ColorGroup.Active, |
102 textBackgroundColor = pal.color( |
101 QPalette.ColorRole.Base) |
103 QPalette.ColorGroup.Active, QPalette.ColorRole.Base |
|
104 ) |
102 if self.__toggle: |
105 if self.__toggle: |
103 textColor = pal.color(QPalette.ColorGroup.Active, |
106 textColor = pal.color( |
104 QPalette.ColorRole.Text) |
107 QPalette.ColorGroup.Active, QPalette.ColorRole.Text |
|
108 ) |
105 else: |
109 else: |
106 textColor = pal.color(QPalette.ColorGroup.Active, |
110 textColor = pal.color( |
107 QPalette.ColorRole.Base) |
111 QPalette.ColorGroup.Active, QPalette.ColorRole.Base |
|
112 ) |
108 painter.setPen(textColor) |
113 painter.setPen(textColor) |
109 painter.setBrush(textBackgroundColor) |
114 painter.setBrush(textBackgroundColor) |
110 helpText = self.tr("Snapshot will be taken in %n seconds", "", |
115 helpText = self.tr( |
111 self.__length - self.__time) |
116 "Snapshot will be taken in %n seconds", "", self.__length - self.__time |
|
117 ) |
112 textRect = painter.boundingRect( |
118 textRect = painter.boundingRect( |
113 self.rect().adjusted(2, 2, -2, -2), |
119 self.rect().adjusted(2, 2, -2, -2), |
114 Qt.AlignmentFlag.AlignHCenter | Qt.TextFlag.TextSingleLine, |
120 Qt.AlignmentFlag.AlignHCenter | Qt.TextFlag.TextSingleLine, |
115 helpText) |
121 helpText, |
|
122 ) |
116 painter.drawText( |
123 painter.drawText( |
117 textRect, |
124 textRect, |
118 Qt.AlignmentFlag.AlignHCenter | Qt.TextFlag.TextSingleLine, |
125 Qt.AlignmentFlag.AlignHCenter | Qt.TextFlag.TextSingleLine, |
119 helpText) |
126 helpText, |
120 |
127 ) |
|
128 |
121 def enterEvent(self, evt): |
129 def enterEvent(self, evt): |
122 """ |
130 """ |
123 Protected method handling the mouse cursor entering the widget. |
131 Protected method handling the mouse cursor entering the widget. |
124 |
132 |
125 @param evt enter event (QEvent) |
133 @param evt enter event (QEvent) |
126 """ |
134 """ |
127 screenGeom = QApplication.screens()[0].geometry() |
135 screenGeom = QApplication.screens()[0].geometry() |
128 if self.x() == screenGeom.left(): |
136 if self.x() == screenGeom.left(): |
129 self.move( |
137 self.move( |
130 screenGeom.x() + |
138 screenGeom.x() + (screenGeom.width() // 2 - self.size().width() // 2), |
131 (screenGeom.width() // 2 - self.size().width() // 2), |
139 screenGeom.top(), |
132 screenGeom.top()) |
140 ) |
133 else: |
141 else: |
134 self.move(screenGeom.topLeft()) |
142 self.move(screenGeom.topLeft()) |