-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings_widgets.py
More file actions
198 lines (167 loc) · 7.56 KB
/
settings_widgets.py
File metadata and controls
198 lines (167 loc) · 7.56 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
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QWidget, QTextEdit, QLabel, QPushButton, QLineEdit, QSpinBox
from PyQt5.QtGui import QFont, QRegExpValidator
from PyQt5.QtCore import QRegExp
class StackWidget(QWidget):
def __init__(self, widget_top : QWidget, widget_bottom : QWidget):
super().__init__()
#Widget definition
self.widget_top = widget_top
self.widget_bottom = widget_bottom
#Layout: Vertical
layout = QVBoxLayout()
layout.setSpacing(0)
layout.setContentsMargins(0, 0, 0, 0)
#Add widgets
layout.addWidget(self.widget_top)
layout.addWidget(self.widget_bottom)
#Set layout
self.setLayout(layout)
class SettingsFormWidget(QWidget):
def __init__(self):
super().__init__()
#Widget definition
self.etcd_ip_form = QLineEdit()
self.etcd_port_form = QLineEdit()
self.frequency_update_form = QSpinBox()
self.submit_button = QPushButton()
self.text_etcd_ip = QLabel()
self.text_etcd_port = QLabel()
self.freq = QLabel()
#Button text
self.submit_button.setText("Apply")
#Label text
self.text_etcd_ip.setText("""<p style="font-size: 26px; color: white; font-family: 'JetBrains Mono'">Etcd ip (Ipv4):</p>""")
self.text_etcd_port.setText("""<p style="font-size: 26px; color: white; font-family: 'JetBrains Mono'">Etcd port:</p>""")
self.freq.setText("""<p style="font-size: 26px; color: white; font-family: 'JetBrains Mono'">Update frequency:</p>""")
# Stacking widgets
stack1 = StackWidget(self.text_etcd_ip, self.etcd_ip_form)
stack2 = StackWidget(self.text_etcd_port, self.etcd_port_form)
stack3 = StackWidget(self.freq, self.frequency_update_form)
#Sizes
self.setFixedHeight(700)
self.etcd_ip_form.setFixedHeight(50)
self.etcd_port_form.setFixedHeight(50)
self.frequency_update_form.setFixedHeight(50)
self.submit_button.setFixedSize(120, 50)
stack1.setFixedHeight(100)
stack2.setFixedHeight(100)
stack3.setFixedHeight(100)
# Input font sizes
font = QFont("Jetbrains mono", 15)
self.etcd_ip_form.setFont(font)
self.etcd_port_form.setFont(font)
self.frequency_update_form.setFont(font)
# Input centering
self.etcd_ip_form.setAlignment(Qt.AlignCenter)
self.etcd_port_form.setAlignment(Qt.AlignCenter)
self.frequency_update_form.setAlignment(Qt.AlignCenter)
# Text centering
self.text_etcd_ip.setAlignment(Qt.AlignCenter)
self.text_etcd_port.setAlignment(Qt.AlignCenter)
self.freq.setAlignment(Qt.AlignCenter)
#Validator - regular expression
self.etcd_ip_form.setValidator(QRegExpValidator(QRegExp("^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}$")))
self.etcd_port_form.setValidator(QRegExpValidator(QRegExp("^[0-9]{1,5}$")))
#Layout: Vertical
layout = QVBoxLayout()
layout.setSpacing(0)
layout.setContentsMargins(20, 0, 20, 0)
#Add widgets
layout.addWidget(stack1)
layout.addWidget(stack2)
layout.addWidget(stack3)
layout.addWidget(self.submit_button)
#Set layout
self.setLayout(layout)
#Apply 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("""
background-color: #06538c;
border-top-left-radius: 15px;
border-bottom-left-radius: 15px;
""")
self.etcd_ip_form.setStyleSheet("""
background-color: white;
border-radius: 15px;
""")
self.etcd_port_form.setStyleSheet("""
background-color: white;
border-radius: 15px;
""")
self.frequency_update_form.setStyleSheet("""
QSpinBox{
background-color: white;
border-radius: 15px;
}
QSpinBox::up-button {
border-bottom: 0px;
}
QSpinBox::down-button {
border-bottom: 0px;
}
""")
self.submit_button.setStyleSheet("""
QPushButton{
color: white;
font-size: 20px;
border-radius: 10px;
background-color: #044b80;
border: 3px solid white;
}
QPushButton:hover{
background-color: #b30412;
}
""")
def set_text_forms(self, etcd_ip : str, etcd_port : str, delay_seconds : int):
self.etcd_ip_form.setText(etcd_ip)
self.etcd_port_form.setText(etcd_port)
self.frequency_update_form.setValue(delay_seconds)
class SettingsWidget(QWidget):
def __init__(self):
super().__init__()
#Widget definition
self.form = SettingsFormWidget()
self.log = QTextEdit()
#Sizes
self.log.setMaximumSize(500,700)
self.form.setMinimumWidth(450)
#Tweak: Cant interact
self.log.setReadOnly(True)
#Layout: Horizontal
layout = QHBoxLayout()
layout.setSpacing(0)
layout.setContentsMargins(50, 0, 50, 0)
#Adding widgets and spacer for centering
layout.addWidget(self.form)
layout.addWidget(self.log)
#Set layout
self.setLayout(layout)
#Apply css
self.apply_css()
def apply_css(self):
self.log.setStyleSheet("""
background-color: rgb(67,110,144);
border: 0px;
border-top-right-radius: 15px;
border-bottom-right-radius: 15px;
""")
def write_outcome(self, conf_written : bool):
icon = "!"
text = "Failed to overwrite configuration file"
if conf_written is True:
icon = "✓"
text = "All done!"
self.log.setHtml(f"""
<br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br>
<center style="color: white; font-size: 26px;">{icon}<br>{text}</center>
""")
#After a short while clear the log
QTimer.singleShot(2000, self.clear_log)
def clear_log(self):
self.log.setHtml("")