-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserverledge_items_descriptor_widgets.py
More file actions
403 lines (339 loc) · 14.9 KB
/
serverledge_items_descriptor_widgets.py
File metadata and controls
403 lines (339 loc) · 14.9 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, pyqtSignal, QMutex
from PyQt5.QtGui import QPixmap, QFont, QMouseEvent, QCursor, QPixmap, QIcon
from PyQt5.QtWidgets import QLabel, QHBoxLayout, QWidget, QPushButton
#Custom widget label that can emit clicked signal
class ClickableLabel(QLabel):
clicked = pyqtSignal(QMouseEvent)
def __init__(self, parent=None):
super().__init__(parent)
def mousePressEvent(self, event):
self.clicked.emit(event)
class NodeWidget(QWidget):
#Class variables for sizes
LOGO_WIDTH = 60
LOGO_HEIGHT = 60
ICON_WIDTH = 140
ICON_HEIGHT = 140
MIN_ELEMENT_WIDTH = 350
MIN_ELEMENT_HEIGHT = 160
MAX_ELEMENT_WIDTH = 350
MAX_ELEMENT_HEIGHT = 150
def __init__(self, url : str):
super().__init__()
#ServerLedge node infos
self.url = url
self.available_cpu = 0
self.available_memory = 0
self.online = True
#Used for pull get requests
#Memorize dict with structure:
# {
# pull_id
# params
# }
self.pull_ids = []
#For concurrency
self.node_mutex = QMutex()
#Widget elements
self.text_box = QLabel()
self.icon = ClickableLabel(parent=self)
#For printing spacial characters we must:
self.text_box.setFont(QFont("Noto Sans Cuneiform", 12))
# For easy customization we define a name for each child widget and the widget itself
self.setObjectName("Element")
self.text_box.setObjectName("Text_Box")
self.icon.setObjectName("Icon")
#Add image to icon label
pixmap = QPixmap("./images/server2.png")
self.icon.setPixmap(pixmap.scaled(NodeWidget.LOGO_WIDTH, NodeWidget.LOGO_HEIGHT))
#Set default text to the text_box
self.text_box.setText(f"""<p style="font-size: 20px; color: white; font-family: 'JetBrains Mono'">
<center>{self.url.capitalize().replace("Http://","").split(":")[0]}<br><br>
<img src="./images/cpu1.png" width="30" height="30"></img>: {self.available_cpu}<br>
<img src="./images/ram1.png" width="33" height="36">: {self.available_memory}
</p></center>
""")
#Setting size for elements
self.setFixedSize(NodeWidget.MIN_ELEMENT_WIDTH,NodeWidget.MIN_ELEMENT_HEIGHT)
self.icon.setMaximumSize(NodeWidget.ICON_WIDTH,NodeWidget.ICON_HEIGHT)
#Define layout: horizontal
layout = QHBoxLayout()
layout.setContentsMargins(15,0,0,0)
layout.setSpacing(0)
layout.addWidget(self.icon)
layout.addWidget(self.text_box)
self.setLayout(layout)
#Apply default css
self.apply_css_default()
self.set_online()
def apply_css_default(self):
# MUST: By default, for efficiency reason, css on custom widget is disabled
# to enable it:
self.setAttribute(Qt.WA_StyledBackground, True)
#Css
self.setStyleSheet("""
QWidget#Element{
background-color: rgb(67,110,144);
margin: 0px;
}
QLabel#Text_Box{
font-size: 20px;
color: white;
margin: 0px 0px 0px 10px;
border: 0px;
}
""")
def set_online(self):
self.online = True
#Change cursor
self.icon.setCursor(QCursor(Qt.PointingHandCursor))
#Change the background color of the icon to green
self.icon.setStyleSheet("""
text-align: center;
background-color: #32a852;
qproperty-alignment: AlignCenter;
border: 2px solid white;
padding: 20px 30px;
border-radius: 25px;
""")
def set_offline(self):
self.online = False
# Change the background color of the icon to grey
self.icon.setStyleSheet("""
text-align: center;
background-color: #909992;
qproperty-alignment: AlignCenter;
border: 2px solid white;
padding: 20px 30px;
border-radius: 25px;
""")
def set_node_text(self, available_cpu : str, available_memory : str):
# Set text to the text_box
self.text_box.setText(f"""<p style="font-size: 24px; color: white; font-family: 'JetBrains Mono'">
<center>{self.url.capitalize().replace("Http://","").split(":")[0]}<br><br>
<img src="./images/cpu1.png" width="30" height="30"></img>: {available_cpu}<br>
<img src="./images/ram1.png" width="33" height="36">: {available_memory}
</p></center>
""")
def update_node_status(self, available_cpu : str, available_memory : str):
self.set_node_text(available_cpu,available_memory)
def select(self):
self.setStyleSheet("""
QWidget#Element{
background-color: rgb(3,63,107);
margin: 0px;
}
QLabel#Text_Box{
font-size: 20px;
color: white;
margin: 0px 0px 0px 10px;
border: 0px;
}
""")
def deselect(self):
self.setStyleSheet("""
QWidget#Element{
background-color: rgb(67,110,144);
margin: 0px;
}
QLabel#Text_Box{
font-size: 20px;
color: white;
margin: 0px 0px 0px 10px;
border: 0px;
}
""")
def get_pull_ids(self):
self.node_mutex.lock()
pull_ids_copy = self.pull_ids.copy()
self.node_mutex.unlock()
return pull_ids_copy
def extend_pull_ids(self, new_pull_ids : list):
self.node_mutex.lock()
self.pull_ids.extend(new_pull_ids)
self.node_mutex.unlock()
def remove_pull_ids(self, unsatisfied_pull_ids : list):
self.node_mutex.lock()
pull_ids_copy = self.pull_ids.copy()
for pull_id in pull_ids_copy:
if pull_id not in unsatisfied_pull_ids:
self.pull_ids.remove(pull_id)
self.node_mutex.unlock()
class EtcdWidget(QWidget):
def __init__(self):
super().__init__()
#Attributes
self.etcd_status_online = False
self.etcd_ip = ""
self.number_nodes = 0
#Widget within
self.etcd_icon = QLabel()
self.etcd_status_text = QLabel()
#For added customization, we define an object name for each widget
self.setObjectName("EtcdWidget")
self.etcd_icon.setObjectName("EtcdIcon")
self.etcd_status_text.setObjectName("EtcdText")
#Defining font
font = QFont("JetBrains Mono",10)
self.etcd_status_text.setFont(font)
#Adding images
self.etcd_icon.setPixmap(QPixmap("./images/etcd.png").scaled(70,70))
#Default text
self.etcd_status_text.setText(f"""<center><p style="font-size: 22px; color: white; font-family: 'JetBrains Mono'">
{self.etcd_ip}<br>
<br>NO NODES<br>AVAILABLE<br>
</p></center>
""")
# Setting size for elements
self.setFixedSize(NodeWidget.MIN_ELEMENT_WIDTH, NodeWidget.MIN_ELEMENT_HEIGHT+20)
self.etcd_icon.setFixedSize(NodeWidget.ICON_WIDTH-1,NodeWidget.ICON_HEIGHT-1)
#Layout: Horizontal
layout = QHBoxLayout()
layout.setSpacing(10)
layout.setContentsMargins(15, 30, 0, 20)
#Add elements
#Fist row
layout.addWidget(self.etcd_icon)
layout.addWidget(self.etcd_status_text)
#Setting layout
self.setLayout(layout)
#Apply default css
self.apply_css()
def apply_css(self):
#MUST: By default, for efficiency reason, css on custom widget is disabled
#to enable it:
self.setAttribute(Qt.WA_StyledBackground, True)
self.setStyleSheet("""
EtcdWidget{
background-color: #06538c;
}
QLabel#EtcdIcon{
background-color: #909992;
qproperty-alignment: AlignCenter;
border: 2px solid white;
padding: 10px;
border-radius: 25px;
}
""")
def update_etcd_ip(self, etcd_ip : str):
self.etcd_ip = etcd_ip
self.update_text()
def set_online(self):
self.etcd_icon.setStyleSheet("""
background-color: #32a852;
qproperty-alignment: AlignCenter;
border: 2px solid white;
padding: 10px;
border-radius: 25px;
""")
def set_offline(self):
self.etcd_icon.setStyleSheet("""
background-color: #909992;
qproperty-alignment: AlignCenter;
border: 2px solid white;
padding: 10px;
border-radius: 25px;
""")
def plus_node(self):
self.number_nodes += 1
self.update_text()
def update_text(self):
# Set text to the text_box
self.etcd_status_text.setText(f"""<center><p style="font-size: 24px; color: white; font-family: 'JetBrains Mono'">
{self.etcd_ip}<br><br>
<img src="./images/server2.png" width="50" height="50"></img>: {self.number_nodes}<br>
</p></center>
""")
class FunctionWidget(QWidget):
FUNCTION_NUMBER = 0
def __init__(self, function_name, memory_demand, cpu_demand):
super().__init__()
# Increment number of functions
FunctionWidget.FUNCTION_NUMBER += 1
# Attributes
self.function_name = function_name
self.memory_demand = memory_demand
self.cpu_demand = cpu_demand
# Widget definition
self.function_icon = QLabel()
self.invoke_button = QPushButton()
self.function_info_text = QLabel()
# For more customization we define an object name
self.setObjectName("Widget")
self.function_icon.setObjectName("Icon")
self.invoke_button.setObjectName("InvokeButton")
self.function_info_text.setObjectName("Text")
# Setting function icon & invoke button
self.function_icon.setPixmap(QPixmap("./images/function.png").scaled(100,100))
self.invoke_button.setIcon(QIcon("./images/invoke.png"))
self.invoke_button.setIconSize(QtCore.QSize(60,60))
# Sizes
self.setFixedHeight(160)
self.function_icon.setFixedWidth(120)
self.function_icon.setFixedHeight(120)
self.invoke_button.setFixedSize(120, 120)
# Layout: Horizontal
layout = QHBoxLayout()
layout.setSpacing(10)
layout.setContentsMargins(20,0,20,0)
# Adding widgets
layout.addWidget(self.function_icon)
layout.addWidget(self.function_info_text)
layout.addWidget(self.invoke_button)
# Setting layout
self.setLayout(layout)
# Apply css
self.apply_css()
self.update_text()
def update_text(self):
self.function_info_text.setText(f"""
<p style="font-size: 20px; color: white; font-family: 'JetBrains Mono'">
Name: {self.function_name}<br><br>
<img src="./images/cpu1.png" width="30" height="30"></img>: {self.cpu_demand}<br>
<img src="./images/ram1.png" width="30" height="30"></img>: {self.memory_demand}
</p>
""")
def apply_css(self):
# MUST: By default, for efficiency reason, css on custom widget is disabled
# to enable it:
self.setAttribute(Qt.WA_StyledBackground, True)
self.setStyleSheet("""
QWidget#Widget{
background-color: rgb(3,63,107);
border-radius: 15px;
}
QLabel#Icon{
qproperty-alignment: AlignCenter;
background-color: white;
border-radius: 15px;
}
QPushButton#InvokeButton{
background-color: #044b80;
border-radius: 15px;
border: 2px solid white;
}
QPushButton#InvokeButton:hover{
background-color: #06538c;
}
""")
if FunctionWidget.FUNCTION_NUMBER % 2 == 0:
self.setStyleSheet("""
QWidget#Widget{
background-color: #06538c;
border-radius: 15px;
}
QLabel#Icon{
qproperty-alignment: AlignCenter;
background-color: white;
border-radius: 15px;
}
QPushButton#InvokeButton{
background-color: #044b80;
border-radius: 15px;
border: 2px solid white;
}
QPushButton#InvokeButton:hover{
background-color: #06538c;
}
""")