-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_widget.py
332 lines (214 loc) · 9.85 KB
/
number_widget.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
#!/usr/bin/python3
# -*- coding: utf-8 -*
from PyQt5.Qt import *
from hierarchy_qs import material_code
from main_datas import number_setting_file, number_setting_datas, component_code
from tools import settings_read, settings_save, move_window_tool
from ui_number import Ui_Number
nb_max_txt = 20
class SeparatorHighlighter(QSyntaxHighlighter):
def __init__(self, separator, parent=None):
super().__init__(parent)
self.separator = separator
def highlightBlock(self, text):
if self.separator == "":
return
separator_format_before = QTextCharFormat()
separator_format_before.setForeground(QColor("blue"))
separator_format_after = QTextCharFormat()
separator_format_after.setForeground(QColor("orange"))
if self.separator in text:
index = text.index(self.separator)
self.setFormat(0, index, separator_format_before)
self.setFormat(index + 1, len(text) - index - 1, separator_format_after)
else:
self.setFormat(0, len(text), separator_format_before)
def update_separator(self, separator):
self.separator = separator
self.rehighlight()
class WidgetNumber(QWidget):
save_item = pyqtSignal(list, str, str)
def __init__(self):
super().__init__()
# Création de l'interface
self.ui = Ui_Number()
self.ui.setupUi(self)
self.type_element = material_code
self.nom_onglet = ""
self.ui.spin.setMaximum(nb_max_txt)
# -----------------------------------------------
# Settings
# -----------------------------------------------
number_datas = settings_read(number_setting_file)
self.ismaximized_on = number_datas.get("ismaximized_on", False)
if not isinstance(self.ismaximized_on, bool):
self.ismaximized_on = False
width = number_datas.get("width", number_setting_datas["width"])
if not isinstance(width, int):
width = number_setting_datas["width"]
height = number_datas.get("height", number_setting_datas["height"])
if not isinstance(height, int):
width = number_setting_datas["height"]
self.resize(width, height)
# -----------------------------------------------
number = number_datas.get("number", number_setting_datas["number"])
if not isinstance(number, str):
number = number_setting_datas["number"]
self.ui.spin.setValue(number)
# -----------------------------------------------
description = number_datas.get("description", number_setting_datas["description"])
if not isinstance(description, str):
description = number_setting_datas["number"]
self.ui.description_chk.setChecked(description)
# -----------------------------------------------
separator = number_datas.get("separator", number_setting_datas["separator"])
if not isinstance(separator, str):
separator = number_setting_datas["separator"]
self.ui.separator.setText(separator)
self.highlighter = SeparatorHighlighter(separator, self.ui.titles.document())
# -----------------------------------------------
self.ui.titles.textChanged.connect(self.gestion_titres)
self.ui.description_chk.clicked.connect(self.description_option_changed)
self.ui.separator.textEdited.connect(self.separator_changed)
self.ui.bt_valider.clicked.connect(self.save)
self.ui.bt_quitter.clicked.connect(self.close)
def personnaliser(self, type_element: str, nom_onglet=""):
self.ui.spin.setValue(1)
self.ui.titles.clear()
self.type_element = type_element
self.nom_onglet = nom_onglet
self.ui.spin.setFocus()
self.ui.spin.selectAll()
self.show()
def gestion_titres(self):
texte = self.ui.titles.toPlainText().strip()
if texte == "":
self.ui.spin.setEnabled(True)
return
self.ui.spin.setEnabled(False)
if "\n" not in texte:
self.ui.spin.setValue(1)
return
liste_texte = self.creation_liste()
nb_texte = len(liste_texte)
self.ui.spin.setValue(nb_texte)
if nb_texte > nb_max_txt:
self.ui.spin.setValue(nb_max_txt)
self.ui.titles.blockSignals(True)
self.ui.titles.setPlainText("\n".join(liste_texte[:nb_max_txt]))
self.ui.titles.blockSignals(False)
self.ui.titles.moveCursor(QTextCursor.End)
else:
self.ui.spin.setValue(nb_texte)
def creation_liste(self) -> list:
texte = self.ui.titles.toPlainText().strip()
liste = texte.split("\n")
liste_upper = list()
liste_clean = list()
separator = self.ui.separator.text()
if not self.ui.description_chk.isChecked():
separator = ""
for title in liste:
title_upper = title.strip().upper()
description = ""
if title_upper == "":
continue
# --------------------------------------
if separator == "" or separator not in title:
if self.type_element != component_code and title_upper not in liste_upper:
liste_clean.append([title, description])
liste_upper.append(title_upper)
continue
if self.type_element == component_code:
liste_clean.append([title, description])
continue
continue
# --------------------------------------
title_datas = title.split(separator, maxsplit=1)
if len(title_datas) != 2:
if self.type_element != component_code and title_upper not in liste_upper:
liste_clean.append([title, description])
liste_upper.append(title_upper)
continue
if self.type_element == component_code:
liste_clean.append([title, description])
continue
continue
# --------------------------------------
title, description = title_datas
if not isinstance(title, str):
continue
if not isinstance(description, str):
description = ""
title = title.strip()
description = description.strip()
title_upper = title.upper()
if self.type_element != component_code and title_upper in liste_upper:
continue
liste_clean.append([title, description])
liste_upper.append(title_upper)
return liste_clean
def save(self):
nombre = self.ui.spin.value()
self.close()
texte = self.ui.titles.toPlainText().strip()
if texte == "":
liste = ["" for _ in range(nombre)]
else:
liste = self.creation_liste()
self.save_item.emit(liste, self.type_element, self.nom_onglet)
def number_settings_save(self):
datas_config = settings_read(file_name=number_setting_file)
# -----------------------------------------------
datas_config["number"] = self.ui.spin.value()
datas_config["description"] = self.ui.description_chk.isChecked()
datas_config["separator"] = self.ui.separator.text()
# -----------------------------------------------
if self.isMaximized():
screennumber = QApplication.desktop().screenNumber(self)
screen = QApplication.desktop().screenGeometry(screennumber)
if isinstance(screen, QRect):
datas_config["height"] = screen.height()
datas_config["width"] = screen.width()
datas_config["ismaximized_on"] = True
settings_save(file_name=number_setting_file, config_datas=datas_config)
return
datas_config["height"] = self.size().height()
datas_config["width"] = self.size().width()
datas_config["ismaximized_on"] = False
# -----------------------------------------------
settings_save(file_name=number_setting_file, config_datas=datas_config)
@staticmethod
def a___________________description______():
pass
def description_option_changed(self):
self.ui.separator.setEnabled(self.ui.description_chk.isChecked())
if self.ui.description_chk.isChecked():
self.highlighter.update_separator(self.ui.separator.text())
else:
self.highlighter.update_separator("")
def separator_changed(self):
self.highlighter.update_separator(self.ui.separator.text())
@staticmethod
def a___________________event______():
pass
def keyPressEvent(self, event: QKeyEvent):
if event.key() == Qt.Key_Escape:
self.hide()
elif event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
self.save()
super().keyPressEvent(event)
def changeEvent(self, event: QEvent):
if event.type() == QEvent.WindowStateChange:
if self.isMaximized():
self.ismaximized_on = True
else:
self.ismaximized_on = False
move_window_tool(widget_parent=self, widget_current=self)
super().changeEvent(event)
def closeEvent(self, event: QCloseEvent):
self.number_settings_save()
super().closeEvent(event)
@staticmethod
def a___________________end______():
pass