-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM_PerformanceSetup.py
675 lines (515 loc) · 29.2 KB
/
M_PerformanceSetup.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
from PySide6.QtWidgets import (QHBoxLayout, QVBoxLayout, QPushButton, QWidget, QSpacerItem, QSizePolicy, QDialog, QLabel,
QMessageBox, QFrame, QCheckBox, QRadioButton, QLabel, QButtonGroup, QComboBox, QTableView, QMenu,
QStyledItemDelegate, QHeaderView, QScrollArea, QAbstractItemView, QStyleOptionViewItem
)
from PySide6.QtCore import Qt, Signal
from PySide6.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
from PySide6.QtGui import QAction
from M_IndicatorsSelection import IndicatorsSelection, flatten_dict
from M_OperateDatabases import fetch_table_from_database
from M_Fonts import MyFont
import pandas as pd
import re
class IndicatorsSelection(QWidget):
IndicatorsSelectionChanged = Signal(pd.DataFrame)
def __init__(self,
IndicatorsClassesLibrary: pd.DataFrame,
IndicatorsLibrary: pd.DataFrame,
IndicatorsSetup: pd.DataFrame,
study_db: QSqlDatabase):
super().__init__()
self.indicators_classes_library = IndicatorsClassesLibrary
self.indicators_library = IndicatorsLibrary
self.indicators_setup = IndicatorsSetup.copy(deep = True)
self.study_db = study_db
self.radio_button_groups = {} # Dictionary to manage radio button groups for each class
self.selected_indicators = load_selected_indicators(self.indicators_setup)
self.init_ui()
def init_ui(self):
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
top_text = "Select performance indicators:"
top_label = QLabel(top_text)
top_label.setFont(MyFont(9, True))
layout.addWidget(top_label)
# Create the UI elements and check if indicators are already selected
for class_id, class_prop in self.indicators_classes_library.iterrows():
class_name = class_prop['IndicatorClassName']
exclusive = class_prop['Exclusive']
label = QLabel(class_name)
label.setFont(MyFont(10, True))
layout.addWidget(label)
if class_id not in self.selected_indicators:
self.selected_indicators[class_id] = []
# Create a frame to group the radio buttons and checkboxes
frame = QFrame()
frame.setFrameShape(QFrame.Box)
frame.setObjectName(class_name)
#frame.setFrameShadow(QFrame.Raised)
frame.setStyleSheet("background-color: rgb(255, 255, 255);")
# Create a layout for the frame
frame_layout = QVBoxLayout(frame)
if exclusive == 'YES':
# Create a button group to manage radio buttons for each class
button_group = QButtonGroup()
button_group.setObjectName(class_name)
button_group.setExclusive(False)
self.radio_button_groups[class_name] = button_group
for indicator_id, indicator in self.indicators_library.iterrows():
if indicator["IndicatorClass"] == class_name:
indicator_name = indicator["ShowName"]
if exclusive == 'YES':
radiobutton = QRadioButton(text = indicator_name, parent = frame)
radiobutton.setProperty("indicator_id", indicator_id) # Set the IndicatorID property
radiobutton.clicked.connect(self.handle_radio_clicked)
radiobutton.toggled.connect(lambda checked, id=indicator_id, class_id = class_id: self.handle_indicator_selection(id, checked, class_id))
button_group.addButton(radiobutton)
frame_layout.addWidget(radiobutton)
# Check if the indicator is previously selected and set the radio button accordingly
if indicator_id in self.selected_indicators[class_id]:
radiobutton.setChecked(True)
else:
checkbox = QCheckBox(text = indicator_name, parent = frame)
checkbox.setProperty("indicator_id", indicator_id) # Set the IndicatorID property
checkbox.stateChanged.connect(lambda state, id=indicator_id, class_id = class_id: self.handle_indicator_selection(id, state == 2, class_id))
frame_layout.addWidget(checkbox)
# Check if the indicator is previously selected and set the checkbox accordingly
if indicator_id in self.selected_indicators[class_id]:
checkbox.setChecked(True)
layout.addWidget(frame)
layout.addItem(QSpacerItem(20, 20, QSizePolicy.Minimum, QSizePolicy.Expanding))
def handle_radio_clicked(self):
sender = self.sender()
button_group = self.radio_button_groups.get(sender.parent().objectName()) # Get the button group of the sender
if sender.isChecked():
# Uncheck all other radio buttons in the same group
for button in button_group.buttons():
if button is not sender:
button.setChecked(False)
def handle_indicator_selection(self, indicator_id, selected, class_id):
if selected:
if indicator_id not in self.selected_indicators[class_id]:
self.selected_indicators[class_id].append(indicator_id)
else:
if indicator_id in self.selected_indicators[class_id]:
self.selected_indicators[class_id].remove(indicator_id)
self.update_indicators_state()
self.IndicatorsSelectionChanged.emit(self.indicators_setup)
#print(self.selected_indicators)
def update_indicators_state(self, ):
try:
query = QSqlQuery(self.study_db)
selected_ids = flatten_dict(self.selected_indicators)
# Create a dictionary where keys are IndicatorIDs and values are 'True' or 'False'
selected_states = {indicator_id: 1 if indicator_id in selected_ids else 0 for indicator_id in self.indicators_library.index.to_list()}
# Execute the SQL UPDATE statement for each IndicatorID
for indicator_id, selected_state in selected_states.items():
query.prepare("UPDATE IndicatorsSetup SET SelectedState = :selectedState WHERE IndicatorID = :indicatorID")
query.bindValue(":selectedState", selected_state)
query.bindValue(":indicatorID", indicator_id)
if not query.exec():
print("Update failed:", query.lastError().text())
break
self.study_db.transaction()
if not self.study_db.commit():
print("Commit failed in update_indicators_state", self.study_db.lastError().text())
except Exception as e:
print("Error:", str(e))
for indicator_id, indicator in self.indicators_setup.iterrows():
if indicator_id in flatten_dict(self.selected_indicators):
self.indicators_setup.at[indicator_id, "SelectedState"] = 1
else:
self.indicators_setup.at[indicator_id, "SelectedState"] = 0
#print(self.indicators_setup)
def load_selected_indicators(IndicatorsSetup: pd.DataFrame):
selected_indicators = {}
selected_df = IndicatorsSetup[IndicatorsSetup["SelectedState"] == 1]
for indicator_id, indicator in selected_df.iterrows():
class_id = re.sub(r'\d', '', indicator_id)
if class_id not in selected_indicators:
selected_indicators[class_id] = []
selected_indicators[class_id].append(indicator_id)
return selected_indicators
def flatten_dict(dict):
flattened_list = [item for sublist in dict.values() for item in sublist]
return flattened_list
class PerformanceSetup(QWidget):
def __init__(self,
Methodology_Database: QSqlDatabase,
Study_Database: QSqlDatabase,
IndicatorsSelector: IndicatorsSelection):
super().__init__()
self.indicators_library = fetch_table_from_database(Methodology_Database, "IndicatorsLibrary")
self.indicators_library.set_index("IndicatorID", inplace=True)
self.indicators_classes = fetch_table_from_database(Methodology_Database, "IndicatorsClassesLibrary")
self.indicators_classes.set_index("IndicatorClassID", inplace=True)
self.indicators_setup = fetch_table_from_database(Study_Database, "IndicatorsSetup")
self.indicators_setup.set_index("IndicatorID", inplace=True)
self.indicator_selector = IndicatorsSelector
self.study_db = Study_Database
if not self.study_db.isValid():
QMessageBox.critical(self, "Database Error", "Invalid database connection.")
return
if not self.study_db.isOpen():
if not self.study_db.open():
QMessageBox.critical(self, "Database Error", "Failed to open the database in Ui_PerformanceSetup.")
return
#get selected indicators dict
self.selected_indicators = load_selected_indicators(self.indicators_setup)
self.classes_widgets = {}
self.setupUi()
# Load exisiting classes of the database to self.classes_widgets and create respective custom expandable widgets
# do not forget the deaulft widget of "No indicators selected within this category"
self.load_classes_setup()
# Verify which class has selected indicators and which indicators selected within each class
# show widget of selecteced indicators and hide the others by calling the self.classes_widgets .show() or .hide()
# this function is also called when the user closes the indicators selection window
self.filter_selected_indicators()
self.indicator_selector.IndicatorsSelectionChanged.connect(self.update_indicators_setup)
def setupUi(self):
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(5)
top_text = "Set indicators settings:"
top_label = QLabel(top_text)
top_label.setFont(MyFont(9, True))
layout.addWidget(top_label)
self.scroll_content_widget = QWidget()
self.indicators_scroll_layout = QVBoxLayout(self.scroll_content_widget) # to add things
#self.indicators_scroll_layout.setContentsMargins(10, 0, 20, 0)
self.indicators_scroll_layout.setSpacing(10)
self.indicators_scroll_layout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
self.indicators_scroll_area = QScrollArea()
self.indicators_scroll_area.setWidget(self.scroll_content_widget)
self.indicators_scroll_area.setWidgetResizable(True)
self.indicators_scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.indicators_scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.indicators_scroll_area.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
#self.indicators_scroll_area.setFrameShape(QFrame.StyledPanel)
layout.addWidget(self.indicators_scroll_area)
def load_classes_setup(self):
for class_id, setting in self.indicators_classes.iterrows():
class_name = setting['IndicatorClassName']
expandable_element = ExpandableClassSetup(label_text = class_name)
SRP_check = False
for indicator_id, indicator in self.indicators_setup.iterrows():
indicator_class_id = re.sub(r'\d', '', indicator_id)
if indicator_class_id == class_id:
indicator_widget = self.create_indicators_setup_widgets(indicator_id)
if SRP_check == False or indicator_class_id != "SRP":
expandable_element.properties_layout.addWidget(indicator_widget)
self.classes_widgets[class_id] = expandable_element
if indicator_class_id == "SRP":
SRP_check = True
# If one of SRP indicators is selected, only show the unit info once (evit repeating)
# if indicator_id in ["SRP1", "SRP2", "SRP3"]:
# break
expandable_element.setVisible(True)
# Add widget to layout
self.indicators_scroll_layout.insertWidget(self.indicators_scroll_layout.count() - 1, expandable_element)
# debugging purpose - do not delete
# for i in range(self.indicators_scroll_layout.count()):
# element = self.indicators_scroll_layout.itemAt(i)
# widget = element.widget()
# layout = element.layout()
# print(f"Item {i}: Widget - {widget}, Layout - {layout}")
def create_indicators_setup_widgets(self, indicator_id):
indicator_widget = QWidget()
indicator_widget.setObjectName(indicator_id)
indicator_layout = QVBoxLayout(indicator_widget)
indicator_layout.setContentsMargins(0, 0, 0, 0)
referece_text = self.indicators_library.at[indicator_id, "Reference"]
indicator_text = f'Methodology: {referece_text}'
indicator_label = QLabel(indicator_text)
indicator_label.setFont(MyFont(10, True))
indicator_layout.addWidget(indicator_label)
possible_units = self.indicators_library.at[indicator_id, "PossibleUnits"].split("; ")
unit_layout = QHBoxLayout()
indicator_layout.addLayout(unit_layout)
#set the model
model = QSqlTableModel(db = self.study_db)
model.setTable("IndicatorsSetup")
model.select()
# Find the corresponding unit in the model
model_column_name = "SelectedUnit"
if len(possible_units) > 1:
unit_text = 'Select the data unit:'
unit_label = QLabel(unit_text)
unit_layout.addWidget(unit_label)
unit_combo_box = QComboBox()
unit_combo_box.addItems(possible_units)
model_row = find_model_row(model, 'IndicatorID', indicator_id)
if model_row >= 0:
# Get the unit value from the model
initial_unit_value = model.data(model.index(model_row, model.fieldIndex(model_column_name)))
if initial_unit_value and initial_unit_value in possible_units:
initial_index = possible_units.index(initial_unit_value)
unit_combo_box.setCurrentIndex(initial_index)
else:
update_model(indicator_id, possible_units[0], model, model_column_name)
unit_combo_box.currentIndexChanged.connect(lambda index: update_model(indicator_id, possible_units[index], model, model_column_name))
unit_layout.addWidget(unit_combo_box)
else:
update_model(indicator_id, possible_units[0], model, model_column_name)
unit_text = f'Data unit: {possible_units[0]}'
unit_label = QLabel(unit_text)
unit_layout.addWidget(unit_label)
if indicator_id in ["P1", "P2", "V1"]:
pass
elif indicator_id in ["B1"]:
self.CustomBuilding = B1UsesSetupTable(self.study_db)
indicator_layout.addWidget(self.CustomBuilding)
# CustomWaterHeigts = B1WaterHeightsSetupTable(self.study_db)
# indicator_layout.addWidget(CustomWaterHeigts)
elif indicator_id in ["SRP1", "SRP2", "SRP3"]:
pass
return indicator_widget
def set_selected_indicators(self):
self.indicators_classes.sort_values(by = "Order")
SRP_control = False
for class_id, _ in self.indicators_classes.iterrows():
layout = self.classes_widgets[class_id].properties_layout
for i in range(layout.count()):
item = layout.itemAt(i)
if item.widget().objectName() in flatten_dict(self.selected_indicators):
item.widget().show()
if item.widget().objectName() in ["SRP1", "SRP2", "SRP3"] and not SRP_control:
SRP_control = True
elif item.widget().objectName() in ["SRP1", "SRP2", "SRP3"] and SRP_control:
item.widget().hide()
else:
item.widget().hide()
if class_id not in self.selected_indicators.keys():
self.classes_widgets[class_id].default_label.show()
def update_indicators_setup(self, IndicatorsSetup: pd.DataFrame):
self.indicators_setup = IndicatorsSetup
self.filter_selected_indicators()
def filter_selected_indicators(self):
self.selected_indicators = load_selected_indicators(self.indicators_setup)
self.indicators_classes.sort_values(by = "Order")
SRP_control = False
for class_id, _ in self.indicators_classes.iterrows():
layout = self.classes_widgets[class_id].properties_layout
for i in range(layout.count()):
item = layout.itemAt(i)
if item.widget().objectName() in flatten_dict(self.selected_indicators):
item.widget().show()
if item.widget().objectName() in ["SRP1", "SRP2", "SRP3"] and not SRP_control:
SRP_control = True
elif item.widget().objectName() in ["SRP1", "SRP2", "SRP3"] and SRP_control:
item.widget().hide()
else:
item.widget().hide()
if class_id not in self.selected_indicators.keys():
self.classes_widgets[class_id].default_label.show()
def ValidateAnswerText(text: str, list: list):
if not text.strip():
message = "Scenario name must not be empty!"
return False, message
elif not re.match(r'^[a-zA-Z0-9_]+$', text.strip()):
message = "Scenario name must not have special characters!"
return False, message
elif text in list:
message = "Scenario name must be unique!"
return False, message
else:
message = "OK!"
return True, message
class ExpandableClassSetup(QWidget):
def __init__(self, label_text: str):
super().__init__()
self.setup_ui(label_text)
self.expanded = False
def setup_ui(self, label_text):
# layout = QVBoxLayout(self)
# layout.setContentsMargins(0, 0, 0, 0)
# Create a frame for the header labels and a simple horizontal line
#Main_frame = QFrame(self)
Main_layout = QVBoxLayout(self)
Main_layout.setContentsMargins(0, 0, 0, 0)
Main_layout.setSpacing(0)
# Create a horizontal layout for the header labels (self.label, self.edit_label, self.expand_label)
header_labels_layout = QHBoxLayout()
# Create a label for the element's text (self.label)
self.label = QLabel(label_text, self)
self.label.setFont(MyFont(10, True))
# Create a horizontal spacer to push self.label to the left
label_spacer = QSpacerItem(20, 40, QSizePolicy.Expanding, QSizePolicy.Minimum)
# Create a label for the expand/collapse arrow (self.expand_label)
self.expand_label = QLabel("▼", self) # 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(self.label)
header_labels_layout.addItem(label_spacer)
header_labels_layout.addWidget(self.expand_label)
Main_layout.addLayout(header_labels_layout)
# Create a simple horizontal line
line = QFrame(self)
line.setFrameShape(QFrame.HLine)
Main_layout.addWidget(line)
# Create a widget for the expandable properties
self.properties_widget = QWidget(self)
# self.properties_widget.setVisible(True)
self.properties_layout = QVBoxLayout(self.properties_widget)
self.properties_layout.setContentsMargins(0, 0, 0, 0)
self.default_label = QLabel("No performance indicator selected within this class.", self)
self.default_label.setObjectName("DefaultLabel")
self.properties_layout.addWidget(self.default_label)
# self.default_label.setVisible(True)
self.properties_layout.setSpacing(5) # Adjust vertical spacing as needed
Main_layout.addWidget(self.properties_widget)
def toggle_properties(self, event):
self.expanded = not self.expanded
self.properties_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
class WarningDialog(QDialog):
def __init__(self, text: str, parent=None):
super().__init__(parent)
self.setWindowTitle("Warning")
layout = QVBoxLayout(self)
label = QLabel(f"{text}", self)
layout.addWidget(label)
button_container = QWidget(self) # Create a container for the button
button_layout = QHBoxLayout(button_container) # Create a layout for the button container
ok_button = QPushButton("OK", self)
ok_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) # Set size policy for the button
ok_button.clicked.connect(self.accept)
button_layout.addWidget(ok_button)
layout.addWidget(button_container) # Add the button container to the main layout
class B1UsesSetupTable(QTableView):
#SET THESE SIGNALS ON THE MAIN WINDOW TO UPDATE THE B1 TABLES ACCORDINGLY
building_uses_modified = Signal(str, str)
def __init__(self, study_db: QSqlDatabase):
super().__init__()
self.db = study_db
# Create models for tables
self.createTable_UserUses()
def createTable_UserUses(self):
# Ensure that the database connection is open before creating the model
if not self.db.isOpen():
QMessageBox.critical(self, "Database Error", "ANSWERS_DB is not open. - B1UsesSetup")
return
# Define and set the model for UserUses_Table
self.user_uses_model = QSqlTableModel(db = self.db)
self.user_uses_model.setTable("B1UsesSetup")
self.user_uses_model.setEditStrategy(QSqlTableModel.OnManualSubmit)
# Fetch the data from the table
if not self.user_uses_model.select():
QMessageBox.critical(self, "Database Error", "Failed to fetch data from the table B1UsesSetup.")
return
self.user_uses_model.setHeaderData(0, Qt.Horizontal, "Custom building use")
self.user_uses_model.setHeaderData(1, Qt.Horizontal, "Total size")
self.user_uses_model.setHeaderData(2, Qt.Horizontal, "Methodology corresponding use")
self.setModel(self.user_uses_model)
self.resizeColumnsToContents()
self.resizeColumnsToContents()
self.setEditTriggers(QAbstractItemView.DoubleClicked)
# Set the delegate for the third column to use ComboBox
self.setItemDelegateForColumn(2, B1ComboBoxDelegate(self))
#self.setItemDelegate(B1ComboBoxDelegate(self))
self.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
# Create context menu
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showContextMenu)
self.user_uses_model.dataChanged.connect(self.handleDataChanged)
def handleDataChanged(self, top_left, bottom_right, roles):
# This is called after the user has finished editing a cell
if top_left.column() == 0:
row = top_left.row()
old_value = self.retrieveOldValue(row, "CustomUse")
new_value = self.user_uses_model.data(top_left, Qt.DisplayRole)
if old_value and old_value != new_value:
self.user_uses_model.submitAll()
self.building_uses_modified.emit(old_value, new_value)
def retrieveOldValue(self, row, column_name):
query = QSqlQuery(self.db)
if query.exec(f"SELECT {column_name} FROM B1UsesSetup WHERE rowid = {row+1}"):
if query.next():
return query.value(0)
return None
def addNewRow(self):
# Add a new row to the table
row = self.user_uses_model.rowCount()
record = self.user_uses_model.record()
record.setValue("CustomUse", "Edit here...")
self.user_uses_model.insertRecord(row, record)
self.reset()
self.user_uses_model.submitAll()
self.user_uses_model.select()
# self.user_uses_model.database().commit()
self.building_uses_modified.emit('', record.value("CustomUse"))
def removeCurrentRow(self):
# Remove the current row from the table
current_row = self.clicked_index.row()
old_value = self.user_uses_model.data(self.clicked_index, Qt.DisplayRole)
if current_row >= 0:
self.user_uses_model.removeRow(current_row)
self.user_uses_model.submitAll()
self.user_uses_model.select()
self.user_uses_model.database().commit()
self.building_uses_modified.emit(old_value, '')
def showContextMenu(self, pos):
table = self.sender() # Identify the table that triggered the event
if table == self:
model = self.user_uses_model
add_label = "Add New Use"
remove_label = "Remove Current Use"
else:
return
global_pos = table.viewport().mapToGlobal(pos)
self.clicked_index = table.indexAt(pos)
context_menu = QMenu(self)
add_new_use_action = QAction(add_label, self)
remove_current_use_action = QAction(remove_label, self)
context_menu.addAction(add_new_use_action)
context_menu.addAction(remove_current_use_action)
# Connect context menu actions to slots
add_new_use_action.triggered.connect(lambda: self.addNewRow())
remove_current_use_action.triggered.connect(lambda: self.removeCurrentRow())
context_menu.exec(global_pos)
class B1ComboBoxDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super().__init__(parent)
self.options = ["Residential", "Commercial", "Industrial"]
def createEditor(self, parent: QWidget, option: QStyleOptionViewItem, index: int):
if index.column() == 2: # Only create an editor for the third column
editor = QComboBox(parent)
editor.addItems(self.options)
return editor
return super().createEditor(parent, option, index)
def setEditorData(self, editor: QComboBox, index: str):
if index.column() == 2: # Set the current index of the ComboBox based on the item data
current_data = index.data(Qt.DisplayRole)
if current_data in self.options:
editor.setCurrentIndex(self.options.index(current_data))
else:
super().setEditorData(editor, index)
def setModelData(self, editor: QComboBox, model: QSqlTableModel, index: int):
if index.column() == 2: # Set the item data based on the current index of the ComboBox
model.setData(index, editor.currentText(), Qt.EditRole)
else:
super().setModelData(editor, model, index)
model.submitAll()
model.database().commit()
# def sizeHint(self, option, index):
# if index.column() == 2: # Adjust the size hint for the ComboBox in the third column
# return QSize(100, 30)
# return super().sizeHint(option, index)
def update_model(IndicatorID, content, model, model_column):
model_row = find_model_row(model, 'IndicatorID', IndicatorID)
if model_row >= 0:
model.setData(model.index(model_row, model.fieldIndex(model_column)), content)
model.submitAll()
def find_model_row(model, model_column_name, search_name):
# Find the row in the model where the model_column_name value is equal to search_name
row = -1
for i in range(model.rowCount()):
if model.data(model.index(i, model.fieldIndex(model_column_name))) == search_name:
row = i
break
return row