forked from fzls/djc_helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqt_wrapper.py
363 lines (247 loc) · 9.42 KB
/
qt_wrapper.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
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
from __future__ import annotations
import datetime
import math
from PyQt5.QtCore import Qt, QTimer, pyqtSignal
from PyQt5.QtGui import QValidator, QWheelEvent
from PyQt5.QtWidgets import (
QCheckBox,
QComboBox,
QDoubleSpinBox,
QFormLayout,
QFrame,
QGridLayout,
QHBoxLayout,
QLabel,
QLayout,
QLineEdit,
QMessageBox,
QPushButton,
QScrollArea,
QSpinBox,
QVBoxLayout,
QWidget,
)
from log import logger
from qt_collapsible_box import CollapsibleBox
from util import get_now, padLeftRight
class QHLine(QFrame):
def __init__(self):
super().__init__()
self.setFrameShape(QFrame.HLine)
self.setFrameShadow(QFrame.Sunken)
class QVLine(QFrame):
def __init__(self):
super().__init__()
self.setFrameShape(QFrame.VLine)
self.setFrameShadow(QFrame.Sunken)
class MySpinbox(QSpinBox):
def __init__(self, parent=None):
super().__init__(parent)
self.setFocusPolicy(Qt.StrongFocus)
def wheelEvent(self, event: QWheelEvent) -> None:
if self.hasFocus():
super().wheelEvent(event)
else:
event.ignore()
class MyDoubleSpinbox(QDoubleSpinBox):
def __init__(self, parent=None):
super().__init__(parent)
self.setFocusPolicy(Qt.StrongFocus)
def wheelEvent(self, event: QWheelEvent) -> None:
if self.hasFocus():
super().wheelEvent(event)
else:
event.ignore()
class MyComboBox(QComboBox):
clicked = pyqtSignal()
def showPopup(self):
self.clicked.emit()
super().showPopup()
def wheelEvent(self, event: QWheelEvent) -> None:
if self.hasFocus():
super().wheelEvent(event)
else:
event.ignore()
def create_pushbutton(text, color="", tooltip="") -> QPushButton:
btn = QPushButton(text)
btn.setStyleSheet(f"background-color: {color}; font-weight: bold; font-family: Microsoft YaHei")
btn.setToolTip(tooltip)
return btn
def create_checkbox(val=False, name="") -> QCheckBox:
checkbox = QCheckBox(name)
checkbox.setChecked(val)
return checkbox
def create_spin_box(value: int, maximum: int = 99999, minimum: int = 0) -> MySpinbox:
spinbox = MySpinbox()
spinbox.setMaximum(maximum)
spinbox.setMinimum(minimum)
spinbox.setValue(value)
return spinbox
def create_double_spin_box(value: float, maximum: float = 1.0, minimum: float = 0.0) -> MyDoubleSpinbox:
spinbox = MyDoubleSpinbox()
spinbox.setMaximum(maximum)
spinbox.setMinimum(minimum)
spinbox.setValue(value)
return spinbox
def create_combobox(current_val: str, values: list[str] | None = None) -> MyComboBox:
combobox = MyComboBox()
combobox.setFocusPolicy(Qt.StrongFocus)
if values is not None:
combobox.addItems(values)
combobox.setCurrentText(current_val)
return combobox
def create_lineedit(current_text: str, placeholder_text="") -> QLineEdit:
lineedit = QLineEdit(current_text)
lineedit.setPlaceholderText(placeholder_text)
return lineedit
def add_form_seperator(form_layout: QFormLayout, title: str):
add_row(form_layout, f"=== {title} ===", QHLine())
def add_vbox_seperator(vbox_layout: QVBoxLayout, title: str):
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(QLabel(title))
hbox.addStretch(1)
vbox_layout.addWidget(QHLine())
vbox_layout.addLayout(hbox)
vbox_layout.addWidget(QHLine())
def add_row(form_layout: QFormLayout, row_name: str, row_widget: QWidget, minium_row_name_size=0):
if minium_row_name_size > 0:
row_name = padLeftRight(row_name, minium_row_name_size, mode="left")
form_layout.addRow(row_name, row_widget)
def make_scroll_layout(inner_layout: QLayout):
widget = QWidget()
widget.setLayout(inner_layout)
scroll = QScrollArea()
scroll.setWidgetResizable(True)
scroll.setWidget(widget)
scroll_layout = QVBoxLayout()
scroll_layout.addWidget(scroll)
return scroll_layout
def create_collapsible_box_with_sub_form_layout_and_add_to_parent_layout(
title: str, parent_layout: QLayout, fold: bool = True, title_backgroup_color=""
) -> tuple[CollapsibleBox, QFormLayout]:
collapsible_box = CollapsibleBox(title, title_backgroup_color=title_backgroup_color)
parent_layout.addWidget(collapsible_box)
form_layout = QFormLayout()
collapsible_box.setContentLayout(form_layout)
collapsible_box.set_fold(fold)
return collapsible_box, form_layout
def create_collapsible_box_add_to_parent_layout(
title: str, parent_layout: QLayout, title_backgroup_color=""
) -> CollapsibleBox:
collapsible_box = CollapsibleBox(title, title_backgroup_color=title_backgroup_color)
parent_layout.addWidget(collapsible_box)
return collapsible_box
def init_collapsible_box_size(parent_widget: QWidget):
# 尝试更新各个折叠区域的大小
for attr_name in dir(parent_widget):
if not attr_name.startswith("collapsible_box_"):
continue
collapsible_box: CollapsibleBox = getattr(parent_widget, attr_name)
collapsible_box.try_adjust_size()
def list_to_str(vlist: list[str]):
return ",".join(str(v) for v in vlist)
def str_to_list(str_list: str):
str_list = str_list.strip(" ,")
if str_list == "":
return []
return [s.strip() for s in str_list.split(",")]
class QQListValidator(QValidator):
def validate(self, text: str, pos: int) -> tuple[QValidator.State, str, int]:
sl = str_to_list(text)
for qq in sl:
if not qq.isnumeric():
return (QValidator.Invalid, text, pos)
return (QValidator.Acceptable, text, pos)
class QQValidator(QValidator):
def validate(self, text: str, pos: int) -> tuple[QValidator.State, str, int]:
qq = text
if qq != "" and not qq.isnumeric():
return (QValidator.Invalid, text, pos)
return (QValidator.Acceptable, text, pos)
def show_message(title: str, text: str, disabled_seconds=0, is_text_selectable=False, show_log=True):
if show_log:
logger.info(f"{title} {text}")
message_box = ConfirmMessageBox()
message_box.setWindowTitle(title)
message_box.setText(text)
if is_text_selectable:
message_box.setTextInteractionFlags(Qt.TextSelectableByMouse)
message_box.setStandardButtons(QMessageBox.Ok)
if disabled_seconds > 0:
message_box.set_disabled_duration(disabled_seconds, [0])
message_box.exec_()
# based on https://gitee.com/i_melon/DNFCalculating/blob/master/PublicReference/view/NotificationButton.py
class ConfirmMessageBox(QMessageBox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def set_disabled_duration(self, seconds: float, btn_indexes: list[int]):
self.end_time = get_now() + datetime.timedelta(seconds=seconds)
self.btn_indexes = btn_indexes
self.old_names = {}
for btn_idx in self.btn_indexes:
btn = self.buttons()[btn_idx]
self.old_names[btn_idx] = btn.text()
btn.setEnabled(False)
self.time = QTimer(self)
self.time.setInterval(100)
self.time.timeout.connect(self.Refresh)
self.time.start()
self.Refresh()
def Refresh(self):
if get_now() < self.end_time:
remaining_time = self.end_time - get_now()
for btn_idx in self.btn_indexes:
self.buttons()[btn_idx].setText(
self.old_names[btn_idx] + f"({math.ceil(remaining_time.total_seconds())} 秒后可以点击)"
)
else:
self.time.stop()
for btn_idx in self.btn_indexes:
btn = self.buttons()[btn_idx]
btn.setText(self.old_names[btn_idx])
btn.setEnabled(True)
def show_confirm_message_box(
title: str,
message: str,
confirm_duration: int = 3,
) -> int:
message_box = ConfirmMessageBox()
message_box.setWindowTitle(title)
message_box.setText(message)
message_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
message_box.set_disabled_duration(confirm_duration, [0])
return message_box.exec_()
class MyPushButtonGridLayout(QGridLayout):
def __init__(self, color="Cyan"):
super().__init__()
self.color = color
self.buttons: list[QPushButton] = []
def add_button(self, btn: QPushButton, row: int, col: int):
btn.setStyleSheet(f"QPushButton::checked {{ background-color: {self.color}; }}")
btn.setCheckable(True)
if len(self.buttons) == 0:
# 默认选中第一个选项
btn.setChecked(True)
self.addWidget(btn, row, col)
def clicked():
self.on_click(btn)
btn.clicked.connect(clicked)
self.buttons.append(btn)
def on_click(self, clicked_btn: QPushButton):
for btn in self.buttons:
btn.setChecked(False)
clicked_btn.setChecked(True)
def get_active_radio_text(self) -> str:
for btn in self.buttons:
if btn.isChecked():
return btn.text()
return ""
def create_push_button_grid_layout(items: list[str], color="Cyan", width=3) -> MyPushButtonGridLayout:
grid_layout = MyPushButtonGridLayout(color)
for idx, item in enumerate(items):
row = math.floor(idx / width)
col = idx % width
btn = create_pushbutton(item)
grid_layout.add_button(btn, row, col)
return grid_layout