-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtmainui.py
199 lines (171 loc) · 8.47 KB
/
qtmainui.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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import re
import sys
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QMessageBox
from callbacks import callback
from qt_material import apply_stylesheet
from qtui import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
self.call = callback()
super(MainWindow, self).__init__(parent=parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowIcon(QtGui.QIcon('shutdown.ico'))
self.setWindowFlag(QtCore.Qt.WindowMaximizeButtonHint, False)
self.btnlist = [self.ui.mins_5, self.ui.mins_10,
self.ui.mins_30, self.ui.mins_60]
# add listeners to buttons and fields
self.ui.clear_btn.clicked.connect(self.clear_fields)
self.ui.direct_mode.setChecked(True)
self.ui.timeout_box.setChecked(True)
self.ui.submit_btn.installEventFilter(self)
self.ui.extend_btn.installEventFilter(self)
self.ui.cancel_btn.installEventFilter(self)
self.ui.hours_field.installEventFilter(self)
self.ui.minuits_field.installEventFilter(self)
self.ui.seconds_field.installEventFilter(self)
self.ui.mins_5.installEventFilter(self)
self.ui.mins_10.installEventFilter(self)
self.ui.mins_30.installEventFilter(self)
self.ui.mins_60.installEventFilter(self)
self.ui.manual_mode.installEventFilter(self)
self.ui.direct_mode.installEventFilter(self)
self.ui.extend_btn.setToolTip(
"Extend the time by the time you have set")
self.ui.mode.setToolTip(
"Should manually choose the mode (Default: Direct mode)")
self.ui.timeout_box.setToolTip(
"This will make the display turn off after 60 seconds of inactivity")
self.ui.direct_mode.setToolTip("Starts a timer immediately")
self.ui.manual_mode.setToolTip(
"Adds the time to the current timer")
def get_numbers(self):
try:
self.hrs_val = int(self.ui.hours_field.toPlainText())
self.min_val = int(self.ui.minuits_field.toPlainText())
self.sec_val = int(self.ui.seconds_field.toPlainText())
self.finalseconds = self.hrs_val*3600+self.min_val*60+self.sec_val
except ValueError:
self.criticalmsg.setInformativeText(
'Characters are not allowed')
self.criticalmsg.exec_()
self.hrs_val = 0
self.min_val = 0
self.sec_val = 0
self.finalseconds = 1
self.ui.hours_field.setPlainText("0")
self.ui.minuits_field.setPlainText("0")
self.ui.seconds_field.setPlainText("0")
def clear_fields(self):
self.ui.hours_field.setPlainText("0")
self.ui.minuits_field.setPlainText("0")
self.ui.seconds_field.setPlainText("0")
# EVENT FILTER
# NOT RECOMMENDED in general to use the setplaintext function in a event filter directly as it will cause unknown errors , clear references must be made in future versions
def eventFilter(self, source, event):
# Alternative to the click.connect()
# handles mousevents
if event.type() == QtCore.QEvent.MouseButtonPress:
self.criticalmsg = QMessageBox()
self.criticalmsg.setIcon(QMessageBox.Critical)
self.criticalmsg.setInformativeText("Error")
self.criticalmsg.setWindowTitle("Error")
self.informationmsg = QMessageBox()
self.informationmsg.setIcon(QMessageBox.Information)
self.informationmsg.setGeometry(QtCore.QRect(800, 600, 651, 300))
self.informationmsg.setInformativeText("Information")
self.informationmsg.setWindowTitle("Information")
# Quickies actions
if source in self.btnlist:
hrs_check, mins_check = (re.search(r'hr', source.text())), re.search(
r'mins', source.text())
self.get_numbers()
to_add = 0
if self.ui.direct_mode.isChecked():
if hrs_check:
to_add = 60*60
self.call.submit(to_add)
self.informationmsg.setInformativeText(
f"Starting a timer for {to_add//60} mins")
if mins_check:
to_add = int(re.search(r'\d+', source.text()).group())
self.call.submit(to_add*60)
self.informationmsg.setInformativeText(
f"Starting a timer for {to_add} mins")
self.call.sleeper_action(self.ui.timeout_box.isChecked())
self.informationmsg.exec_()
if self.ui.manual_mode.isChecked():
if hrs_check:
to_add = 1
self.ui.hours_field.setPlainText(
str(int(re.search(r'\d+', source.text()).group())+self.hrs_val))
self.informationmsg.setInformativeText(
f"Added {to_add} hr to the timer")
if mins_check:
to_add = int(re.search(r'\d+', source.text()).group())
self.ui.minuits_field.setPlainText(
str(self.min_val+to_add))
self.informationmsg.setInformativeText(
f"Added {to_add} mins to the timer")
self.informationmsg.exec_()
# Normal mode
self.get_numbers()
if (self.finalseconds < 20 and (source is self.ui.submit_btn or source is self.ui.extend_btn)):
self.criticalmsg.setInformativeText(
'Minimum time is 20 seconds')
self.criticalmsg.exec_()
elif(source is self.ui.submit_btn):
self.get_numbers()
self.call.submit(self.finalseconds)
self.call.sleeper_action(self.ui.timeout_box.isChecked())
elif source is self.ui.extend_btn:
self.get_numbers()
self.extendstatus = self.call.extend(self.finalseconds)
if(not self.extendstatus):
self.criticalmsg.setInformativeText(
'Start the timer first')
self.criticalmsg.exec_()
else:
self.call.sleeper_action(self.ui.timeout_box.isChecked())
if source is self.ui.cancel_btn:
self.call.cancel()
self.call.sleep_reset()
# Clears the field value if the user enters the fields
if event.type() == QtCore.QEvent.Enter:
if type(source) is QtWidgets.QPlainTextEdit:
if source.toPlainText() == '0':
source.setPlainText("")
return True
if type(source) == QtWidgets.QPushButton:
if(self.ui.hours_field.toPlainText() == ''):
self.ui.hours_field.setPlainText("0")
if(self.ui.minuits_field.toPlainText() == ''):
self.ui.minuits_field.setPlainText("0")
if(self.ui.seconds_field.toPlainText() == ''):
self.ui.seconds_field.setPlainText("0")
# Sets the field value to 0 if the user leaves the fields
if event.type() == QtCore.QEvent.Leave:
if(type(source) is QtWidgets.QPlainTextEdit):
# if the user enters a letter in the hours field or if its empty
if(any(c.isalpha() for c in self.ui.hours_field.toPlainText())):
self.ui.hours_field.setPlainText("0")
if(any(c.isalpha() for c in self.ui.minuits_field.toPlainText())):
self.ui.minuits_field.setPlainText("0")
if(any(c.isalpha() for c in self.ui.seconds_field.toPlainText())):
self.ui.seconds_field.setPlainText("0")
return QtWidgets.QWidget.eventFilter(self, source, event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
apply_stylesheet(app, theme='dark_blue.xml')
w = MainWindow()
w.show()
sys.exit(app.exec_())