/
posemaniac-timer.py
270 lines (227 loc) · 9.06 KB
/
posemaniac-timer.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, time
from PyQt4 import QtCore, QtGui
from pymouse import PyMouse
from pykeyboard import PyKeyboard
class ScreenSaverPreventer(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.key = PyKeyboard()
def doSomething(self):
self.key.press_key(self.key.control_l_key)
self.key.release_key(self.key.control_l_key)
def run(self):
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.doSomething)
self.timer.start(59000) # 59 sec
def stop(self):
self.timer.stop()
class TimerWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(TimerWindow, self).__init__(parent)
self.label = QtGui.QLabel()
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
font=QtGui.QFont()
font.setPixelSize(60)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.stopButton = QtGui.QPushButton("&Stop")
self.stopButton.setFixedWidth(60)
self.stopButton.setStyleSheet("margin-bottom: 10px;padding: 4px;")
self.stopButton.clicked.connect(self.stop)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.stopButton, alignment=QtCore.Qt.AlignHCenter)
self.setLayout(layout)
self.resize(400, 400)
self.screen_saver_preventer=ScreenSaverPreventer()
def run(self, sec, countdown3):
self.sec = sec
self.countdownConfig = countdown3
self.countdown3Init()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update)
self.timer.start(1001) # Magic number
self.screen_saver_preventer.run()
def stop(self):
self.timer.stop()
self.config_window = ConfigWindow()
self.config_window.move(self.pos())
self.config_window.show()
self.screen_saver_preventer.stop()
self.close()
def mainUpdateInit(self):
self.setStyleSheet("\
QPushButton {background-color:#fff; color:#49f;}\
TimerWindow {background-color:#fff; border-bottom:10px solid #49f}\
QLabel {color:#777;}")
self.now = self.sec
self.mode = 'mainCountdown'
self.update()
def update(self):
self.label.setText(str(self.now))
self.now -= 1
if self.now == -1: # When Time's up
if (self.mode == 'countdown3' or # If currently is counting 3 sec
self.countdownConfig == False): # If user set not countdown 3 sec
self.mainUpdateInit()
else:
self.countdown3Init()
if self.mode == 'mainCountdown':
if self.now == 60:
self.setStyleSheet("\
QPushButton {background-color:#fff; color:#af0;}\
TimerWindow {background-color:#af0;}\
QLabel {color:#fff;}")
elif self.now == 29:
self.setStyleSheet("\
QPushButton {background-color:#fff; color:#49f;}\
TimerWindow {background-color:#49f;}\
QLabel {color:#fff;}")
elif self.now == 19:
self.setStyleSheet("\
QPushButton {background-color:#fff; color:#fa0;}\
TimerWindow {background-color:#fa0;}\
QLabel {color:#fff;}")
elif self.now == 9:
self.setStyleSheet("\
QPushButton {background-color:#fff; color:#f00;}\
TimerWindow {background-color:#f00;}\
QLabel {color:#fff;}")
elif self.now == 2:
self.setStyleSheet("\
QPushButton {background-color:#fff; color:#700;}\
TimerWindow {background-color:#700}\
QLabel {color:#fff;}")
def countdown3Init(self):
self.setStyleSheet("background-color:#222;color:#eee;")
self.mode = 'countdown3'
self.now = 3
self.countdown3()
def countdown3(self):
self.label.setText(str(self.now))
self.now -= 1
if self.now == -1:
# Call main loop
self.update()
class ConfigWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(ConfigWindow, self).__init__(parent)
layout = QtGui.QGridLayout()
self.radio_buttons = QtGui.QButtonGroup(parent)
r10 = QtGui.QRadioButton("&10 Sec")
r15 = QtGui.QRadioButton("1&5 Sec")
r30 = QtGui.QRadioButton("&30 Sec")
r45 = QtGui.QRadioButton("&45 Sec")
r60 = QtGui.QRadioButton("&60 Sec")
r90 = QtGui.QRadioButton("&90 Sec")
self.radio_buttons.addButton(r10)
self.radio_buttons.addButton(r15)
self.radio_buttons.addButton(r30)
self.radio_buttons.addButton(r45)
self.radio_buttons.addButton(r60)
self.radio_buttons.addButton(r90)
layout.addWidget(r10, 0, 0)
layout.addWidget(r15, 0, 1)
layout.addWidget(r30, 0, 2)
layout.addWidget(r45, 1, 0)
layout.addWidget(r60, 1, 1)
layout.addWidget(r90, 1, 2)
r30.setChecked(True)
self.countdownConfig = QtGui.QCheckBox("&Countdown")
layout.addWidget(self.countdownConfig, 2, 0, 1, 3)
startButton = QtGui.QPushButton("&Start")
layout.addWidget(startButton, 3, 2)
startButton.clicked.connect(self.positionSelector)
startButton.setDefault(True)
quitButton = QtGui.QPushButton("&Quit")
layout.addWidget(quitButton, 3, 0)
quitButton.clicked.connect(self.close)
self.setWindowTitle("Posemaniac Timer")
self.setLayout(layout)
self.setStyleSheet('''
background-color: #fff;
color: #666;
''')
desktop = QtGui.QApplication.desktop()
self.move(desktop.width() / 2 - self.width() * 0.2,
desktop.height() / 2 - self.height() * 0.2,)
def positionSelector(self):
r = self.radio_buttons.checkedId()
if r == -2:
sec = 10
elif r == -3:
sec = 15
elif r == -4:
sec = 30
elif r == -5:
sec = 45
elif r == -6:
sec = 60
elif r == -7:
sec = 90
if self.countdownConfig.checkState() == 2:
countdown3 = True
else:
countdown3 = False
self.position_selection_ui = PositionSelectionUI(sec, countdown3, self.pos())
self.close() # 不太確定會不會有問題
class PositionSelectionUI (QtGui.QWidget):
def __init__ (self, sec, countdown3, configWindowPos, parent = None):
self.sec = sec
self.countdownConfig = countdown3
# Create TimerWindow Instance
self.timer_window = TimerWindow()
self.timer_window.move(configWindowPos)
super(PositionSelectionUI, self).__init__(parent)
self.setWindowOpacity(0.7)
self.setStyleSheet("background-color:rgba(0,0,0,180)")
# Init QLabel
self.label = PositionSelectionUILabel(self)
# Init QLayout
layout = QtGui.QHBoxLayout()
layout.addWidget(self.label)
layout.setMargin(0)
layout.setSpacing(0)
self.setLayout(layout)
self.show()
self.showFullScreen()
def callTimerAndRun(self):
# parent is ConfigWindow
self.timer_window.show()
# this will remove minimized status
# and restore window with keeping maximized/normal state
self.timer_window.setWindowState(self.timer_window.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
# this will activate the window
self.timer_window.activateWindow()
self.timer_window.run(self.sec, self.countdownConfig)
self.close()
class PositionSelectionUILabel (QtGui.QLabel):
def __init__ (self, parent = None):
super(PositionSelectionUILabel, self).__init__(parent)
self.parent = parent
self.setMouseTracking(True)
self.setTextLabelPosition(0, 0)
self.setAlignment(QtCore.Qt.AlignCenter)
def mouseMoveEvent (self, event):
self.setTextLabelPosition(event.x(), event.y())
QtGui.QWidget.mouseMoveEvent(self, event)
def mousePressEvent (self, event):
if event.button() == QtCore.Qt.LeftButton:
self.parent.hide()
pos=event.pos()
mouse = PyMouse()
time.sleep(0.2)
mouse.click(pos.x(), pos.y())
# parent is PositionSelectionUI
self.parent.callTimerAndRun()
self.parent.timer_window.move(pos.x() + 320, pos.y() - 380)
QtGui.QWidget.mousePressEvent(self, event)
def setTextLabelPosition (self, x, y):
self.x, self.y = x, y
self.setText('Please click at START button on Posemaniacs page. ( %d : %d )' % (self.x, self.y))
app = QtGui.QApplication(sys.argv)
config_window = ConfigWindow()
config_window.show()
app.exec_()