-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
287 lines (235 loc) · 11.7 KB
/
gui.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
import os
import sys
import datetime
from PyQt5.QtCore import QThread, QSize, Qt, pyqtSignal
from PyQt5.QtGui import QFont, QIcon, QPixmap, QColor
from PyQt5.QtWidgets import QApplication, QGridLayout, QWidget, QTextEdit, QTableWidget, QTableWidgetItem, \
QTableView, QAbstractItemView, QHeaderView, QLabel, QPushButton
import opt_live_predictor as predictor
class Threader(QThread):
authResult = pyqtSignal(object)
updateStart = pyqtSignal(object)
def __init__(self, main_window):
super().__init__()
self.main_window = main_window
self.predictor_result = []
def run(self):
while True:
self.updateStart.emit('Updating the results table...')
predictions, matches_found, error_code = predictor.get_predicts_results()
self.sleep(2)
# - "Available live-matches" case -
# error_code = 0
# matches_found = True
# predictions = ['Team1:Team2:Map1:W2', 'Team1:Team2:Map2:W2', 'Team1:Team2:Map3:W1',
# 'Team3:Team4:Map1:W2', 'Team3:Team4:Map2:W1', 'Team3:Team4:Map3:W2',
# 'Team3:Team4:Map4:W2', 'Team3:Team4:Map5:W1']
# - "No live-matches" case -
# error_code = 0
# matches_found = False
# predictions = []
# - "No internet connection" case -
# error_code = 2
# matches_found = False
# predictions = []
# - "Absence of .json-file with a neural net weights" case -
# error_code = 3
# matches_found = False
# predictions = []
# - "Problem occurred while getting active maps" case -
# error_code = 4
# matches_found = False
# predictions = []
if matches_found is False:
if error_code == 2:
self.authResult.emit(['Network currently down!', 2])
elif error_code == 3:
self.authResult.emit(['Weights.json not found!', 3])
elif error_code == 4:
self.authResult.emit(['Can\'t get active duty maps!', 4])
else:
self.authResult.emit(['Matches are not found!', 0])
else:
self.authResult.emit(predictions)
# A pause, preventing Error1015
self.sleep(300)
class Window(QWidget):
def __init__(self):
super().__init__(flags=Qt.FramelessWindowHint)
self.offset = None
self.predictions = []
self.push_button_quit = QPushButton(self)
self.push_button_minimize = QPushButton(self)
self.text_edit_title = QTextEdit(self)
self.text_edit_matches_found = QTextEdit(self)
self.text_edit_last_update_time = QTextEdit(self)
self.table_widget = QTableWidget(0, 4, self)
self.label = QLabel(self)
self.label.setPixmap(QPixmap(os.path.dirname(__file__) + '/images/title_bar_icon.png'))
self.label.resize(720, 50)
self.label.move(0, -5)
self.label.stackUnder(self.table_widget)
self.label = QLabel(self)
self.label.setPixmap(QPixmap(os.path.dirname(__file__) + '/images/title_icon.png'))
self.label.resize(35, 35)
self.label.move(5, 5)
self.label.stackUnder(self.table_widget)
self.bg_label = QLabel(self)
self.bg_label.setPixmap(QPixmap(os.path.dirname(__file__) + '/images/bg_image.png'))
self.bg_label.resize(720, 690)
self.bg_label.move(0, 0)
self.bg_label.stackUnder(self.label)
self.bg_label.stackUnder(self.text_edit_last_update_time)
self.bg_label.stackUnder(self.text_edit_matches_found)
self.thread = Threader(self)
self.thread.authResult.connect(self.handleAuthResult)
self.thread.updateStart.connect(self.handleStartUpdate)
self.thread.start()
self.initUi()
self.setWindowIcon(QIcon(os.path.dirname(__file__) + '/images/taskbar_icon.png'))
self.setWindowTitle('The Predictor')
self.setMinimumSize(QSize(720, 690))
self.setMaximumSize(QSize(720, 690))
def initUi(self):
font = QFont('Calibri', 14)
stylesheet = '''
QTextEdit#matches_found, QTextEdit#update_time {
color: rgb(154,157,161);
background-color: rgb(42,62,97);
border: none;
}
QTextEdit#title {
color: white;
}
QPushButton#quit {
border: none;
}
QPushButton#quit:hover {
background-image: url(os.path.dirname(__file__) + "/images/red-x.png");
background-repeat: no-repeat;
}
QPushButton#minimize {
border: none;
}
QPushButton#minimize:hover {
background-color: rgb(40, 67, 115);
}
QTableView::item
{
background: black;
}
'''
layout = QGridLayout()
layout.setSpacing(50)
self.push_button_quit.setObjectName('quit')
self.push_button_quit.move(678, 3)
self.push_button_quit.resize(40, 40)
self.push_button_quit.setStyleSheet(stylesheet)
self.push_button_quit.stackUnder(self.table_widget)
self.push_button_quit.setIcon(QIcon(os.path.dirname(__file__) + '/images/x.png'))
self.push_button_quit.clicked.connect(self.button_quit)
self.push_button_quit.setToolTip('Close app')
self.push_button_quit.toolTip()
self.push_button_minimize.setObjectName('minimize')
self.push_button_minimize.move(640, 5)
self.push_button_minimize.resize(34, 34)
self.push_button_minimize.setStyleSheet(stylesheet)
self.push_button_minimize.stackUnder(self.table_widget)
self.push_button_minimize.setIcon(QIcon(os.path.dirname(__file__) + '/images/-.png'))
self.push_button_minimize.clicked.connect(self.button_minimize)
self.push_button_minimize.setToolTip('Minimize app\'s window')
self.text_edit_title.setObjectName('title')
self.text_edit_title.setText('The Predictor')
self.text_edit_title.setFont(QFont('Calibri', 20))
self.text_edit_title.setDisabled(True)
self.text_edit_title.setStyleSheet('color:rgb(183, 206, 247);background: transparent;border: none;')
self.text_edit_title.move(40, 0)
self.text_edit_title.resize(270, 60)
self.text_edit_title.stackUnder(self.table_widget)
self.text_edit_title.show()
self.text_edit_matches_found.setObjectName('matches_found')
self.text_edit_matches_found.setFont(font)
self.text_edit_matches_found.setDisabled(True)
self.text_edit_matches_found.setStyleSheet(stylesheet)
self.text_edit_matches_found.move(70, 68)
self.text_edit_matches_found.resize(270, 40)
self.text_edit_matches_found.setToolTip('Message box, which signalising about updating \n'
'of the results table or showing reason \n'
'why a results table is empty.')
self.text_edit_matches_found.setToolTipDuration(5000)
self.text_edit_matches_found.hide()
self.text_edit_last_update_time.setObjectName('update_time')
self.text_edit_last_update_time.setFont(font)
self.text_edit_last_update_time.setDisabled(True)
self.text_edit_last_update_time.setStyleSheet(stylesheet)
self.text_edit_last_update_time.move(380, 68)
self.text_edit_last_update_time.resize(270, 40)
self.text_edit_last_update_time.setToolTip('Message box, showing a timestamp of last check or \n'
'update of a results table.')
self.text_edit_last_update_time.setToolTipDuration(5000)
self.text_edit_last_update_time.hide()
self.setLayout(layout)
layout.setContentsMargins(60, 130, 60, 60)
self.table_widget.setHorizontalHeaderLabels(('Team 1', 'Team 2', 'Map', 'Prediction'))
self.table_widget.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)
self.table_widget.horizontalHeader().setHighlightSections(False)
self.table_widget.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.table_widget.horizontalHeader().setStyleSheet('::section{color:rgb(154,157,161); '
'background: rgb(24, 39, 66);}')
self.table_widget.setStyleSheet('QTableView{background: rgb(31, 53, 89);border: none;}')
for i in range(4):
self.table_widget.setColumnWidth(i, 150)
self.table_widget.setSelectionBehavior(QTableView.SelectRows)
self.table_widget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.table_widget.verticalHeader().setVisible(False)
layout.addWidget(self.table_widget)
def button_minimize(self):
self.showMinimized()
def button_quit(self):
self.close()
def handleAuthResult(self, result):
"""Function receives signals from a predicting thread and renew a result`s table"""
self.table_widget.setRowCount(0)
self.text_edit_last_update_time.setText(datetime.datetime.now().strftime('%H:%M:%S %d-%m-%Y'))
self.text_edit_last_update_time.setAlignment(Qt.AlignCenter)
self.text_edit_last_update_time.show()
if len(result) == 2 and 'int' in str(type(result[1])):
self.text_edit_matches_found.setText(result[0])
self.text_edit_matches_found.setAlignment(Qt.AlignCenter)
self.text_edit_matches_found.show()
else:
self.text_edit_matches_found.hide()
for n, item in enumerate(result):
split_item = item.split(':')
self.table_widget.insertRow(n)
r, g, b = (40, 90, 150) if n % 2 == 0 else (65, 85, 145)
# Put predictions into the table
for cell in range(4):
self.table_widget.setItem(n, cell, QTableWidgetItem(str(split_item[cell])))
self.table_widget.item(n, cell).setBackground(QColor(r, g, b))
self.table_widget.item(n, cell).setTextAlignment(Qt.AlignCenter)
self.table_widget.item(n, 3).setToolTip('W1 means The Predictor predicts that "Team 1" will win, \n'
'otherwise W2 means The Predictor predicts \n'
'that "Team 2" will win.')
def handleStartUpdate(self, result):
"""Signals a predictions update"""
self.text_edit_matches_found.setText(result)
self.text_edit_matches_found.show()
# Next 3 mouse event handlers are used to allow a window dragging
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.offset = event.pos()
else:
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.offset is not None and event.buttons() == Qt.LeftButton:
self.move(self.pos() + event.pos() - self.offset)
else:
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
if __name__ == '__main__':
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())