-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM_Operate_GUI_Elements.py
296 lines (224 loc) · 10 KB
/
M_Operate_GUI_Elements.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
from PySide6.QtWidgets import (QVBoxLayout, QHBoxLayout, QCheckBox, QStackedWidget, QListView,
QComboBox, QTreeWidgetItem, QWidget, QSpacerItem, QSizePolicy, QLabel, QFrame
)
from PySide6.QtCore import Qt
from PySide6.QtGui import QStandardItemModel, QStandardItem
from M_Fonts import MyFont
def CleanStackedWidget(QStackedWidget):
#Remove all existing pages from the stacked widget
while QStackedWidget.count() > 0:
widget = QStackedWidget.widget(0)
QStackedWidget.removeWidget(widget)
def getLayoutsFromLayout(layout):
layouts = []
for i in range(layout.count()):
item = layout.itemAt(i)
if isinstance(item.layout(), QVBoxLayout) or isinstance(item.layout(), QHBoxLayout):
layouts.append(item.layout())
return layouts
def getWidgetsFromLayout(layout):
widgets = []
for i in range(layout.count()):
widget = layout.itemAt(i).widget()
if widget:
widgets.append(widget)
return widgets
def handleSingleChoiceSelection(button_group):
selected_button = button_group.checkedButton()
for button in button_group.buttons():
if button != selected_button:
button.setChecked(False)
def handleMultipleChoiceSelection(layout):
selected_checkboxes = [widget for widget in getWidgetsFromLayout(layout) if isinstance(widget, QCheckBox) and widget.isChecked()]
for widget in getWidgetsFromLayout(layout):
if isinstance(widget, QCheckBox) and widget not in selected_checkboxes:
widget.setChecked(False)
def expand_all_tree_items(tree_widget):
def expand_recursive(item):
item.setExpanded(True)
for i in range(item.childCount()):
child_item = item.child(i)
expand_recursive(child_item)
for i in range(tree_widget.topLevelItemCount()):
top_level_item = tree_widget.topLevelItem(i)
expand_recursive(top_level_item)
def access_page_by_name(stacked_widget: QStackedWidget, page_name: str):
"""
Accesses a page in a stacked widget by its name.
Args:
stacked_widget: The stacked widget containing the pages.
page_name (str): The name of the page to be accessed.
"""
# Iterate over each page in the stacked widget
for index in range(stacked_widget.count()):
page = stacked_widget.widget(index)
# Check if the page name matches the desired page name
if page.property("pageName") == page_name:
# Set the current page to the found page
stacked_widget.setCurrentIndex(index)
break
############### QList OPERATIONS ###############
def updateQListView(ListView: QListView, Model: QStandardItemModel, Data: list, Exclude=None):
"""
Update a QListView with the provided data.
Args:
ListView (QListView): The QListView to be updated.
Model (QStandardItemModel): The model for the QListView.
Data (list): The data to populate the model.
Exclude (Any, optional): An item to exclude from the data. Defaults to None.
"""
Model.clear()
# Check if the model is empty
if Model.rowCount() == 0:
# Populate the model with data
for item in Data:
if item != Exclude:
item = QStandardItem(item)
item.setCheckable(True)
item.setCheckState(Qt.Unchecked)
Model.appendRow(item)
ListView.setModel(Model)
ListView.show()
def getQListSelection(QListModel: QStandardItemModel):
"""
Get the list of text of the checked items in the QListModel.
Args:
QListModel: The QListModel object containing the items.
Returns:
A list of text of the checked items.
"""
# Get the number of items in the table model
num_items = QListModel.rowCount()
selected_items = []
for row in range(num_items):
item = QListModel.item(row)
if item and item.checkState() == Qt.Checked:
selected_items.append(item.text())
return selected_items
################################################
def updatePerformanceTablesViews(PerformanceTableData):
"""
Updates the views for the performance tables.
Args:
PerformanceTableData (list): A list of tuples containing the model and
its data for updating the views.
"""
for model, _ in PerformanceTableData:
model.select()
def updateQComboBox(ComboBox: QComboBox, Data: list):
"""
Clears the given QComboBox and adds the items from the provided list.
Parameters:
ComboBox (QComboBox): The QComboBox to update.
Data (list): The list of items to add to the QComboBox.
"""
ComboBox.clear()
ComboBox.addItems(Data)
"""
CLASSES - SPECIAL ELEMENTS
"""
class DatabaseItem(QTreeWidgetItem):
def __init__(self, db_row_id, column_name, parent=None):
super(DatabaseItem, self).__init__(parent)
self.db_row_id = db_row_id
self.column_name = column_name
class NotExpandableSimpleElement(QWidget):
def __init__(self, label_text = ""):
super().__init__()
self.expanded = True
self.label_text = label_text
#self.setStyleSheet("border: 1px solid #BB0A21;")
self.setup_ui()
def setup_ui(self):
self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
self.layout = QVBoxLayout(self)
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(0)
'''Create header of the expandable element'''
header_layout = QVBoxLayout()
header_layout.setContentsMargins(0, 0, 0, 0)
header_layout.setSpacing(0)
# Create a horizontal layout for the header of the expandable element
header_labels_layout = QHBoxLayout()
# Create a label for the element's text label
label = QLabel(self.label_text)
label.setFont(MyFont(10, True))
label.setMargin(0)
# Create a horizontal spacer to push self.label to the left
label_h_spacer = QSpacerItem(20, 10, QSizePolicy.Expanding, QSizePolicy.Preferred)
# Add the labels to the header_labels_layout
header_labels_layout.addWidget(label)
header_labels_layout.addItem(label_h_spacer)
# Add the header labels layout to the header_layout
header_layout.addLayout(header_labels_layout)
# Create a simple horizontal line and add to header_layout
line = QFrame()
line.setFrameShape(QFrame.HLine)
line.setFrameShadow(QFrame.Plain)
header_layout.addWidget(line)
self.layout.addLayout(header_layout)
self.content_layout = QVBoxLayout()
self.content_layout.setContentsMargins(4,0,4,0)
self.content_layout.setSpacing(4)
self.layout.addLayout(self.content_layout)
class ExpandableSimpleElement(QFrame):
def __init__(self, label_text = ""):
super().__init__()
self.expanded = True
self.label_text = label_text
#self.setStyleSheet("border: 1px solid #BB0A21;")
self.setup_ui()
def setup_ui(self):
self.layout = QVBoxLayout(self)
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(0)
'''Create header of the expandable element'''
header_layout = QVBoxLayout()
header_layout.setContentsMargins(0, 0, 0, 0)
header_layout.setSpacing(0)
# Create a horizontal layout for the header of the expandable element
header_labels_layout = QHBoxLayout()
# Create a label for the element's text label
label = QLabel(self.label_text)
label.setFont(MyFont(10, True))
# Create a horizontal spacer to push self.label to the left
label_h_spacer = QSpacerItem(20, 10, QSizePolicy.Expanding, QSizePolicy.Preferred)
# Create a label for the expand/collapse arrow (self.expand_label)
self.expand_label = QLabel("▲") # Use ▼ for down arrow and ▲ for up arrow
self.expand_label.setAlignment(Qt.AlignCenter)
self.expand_label.setCursor(Qt.PointingHandCursor)
self.expand_label.mousePressEvent = self.toggle_properties
# Add the labels to the header_labels_layout
header_labels_layout.addWidget(label)
header_labels_layout.addItem(label_h_spacer)
header_labels_layout.addWidget(self.expand_label)
# Add the header labels layout to the header_layout
header_layout.addLayout(header_labels_layout)
# Create a simple horizontal line and add to header_layout
line = QFrame()
line.setFrameShape(QFrame.HLine)
line.setFrameShadow(QFrame.Plain)
header_layout.addWidget(line)
self.layout.addLayout(header_layout)
'''Create content layout of the expandable element'''
self.content_layout = QVBoxLayout()
self.content_layout.setContentsMargins(4,0,4,0)
self.layout.addLayout(self.content_layout)
# self.layout.addItem(QSpacerItem(40, 20, QSizePolicy.Minimum, QSizePolicy.Expanding))
def toggle_properties(self, event):
self.expanded = not self.expanded
#self.content_widget.setVisible(self.expanded)
if self.expanded:
self.expand_label.setText("▲") # Change to up arrow when expanded
else:
self.expand_label.setText("▼") # Change to down arrow when collapsed
for i in range(self.content_layout.count()):
item = self.content_layout.itemAt(i)
if isinstance(item.layout(), QVBoxLayout) or isinstance(item.layout(), QHBoxLayout):
layout = item.layout()
for j in range(layout.count()):
widget = layout.itemAt(j).widget()
if widget:
widget.setVisible(self.expanded)
elif isinstance(item.widget(), QWidget):
item.widget().setVisible(self.expanded)