diff options
Diffstat (limited to 'gui/tab_drivers.py')
-rw-r--r-- | gui/tab_drivers.py | 96 |
1 files changed, 94 insertions, 2 deletions
diff --git a/gui/tab_drivers.py b/gui/tab_drivers.py index b6571db..d74e07e 100644 --- a/gui/tab_drivers.py +++ b/gui/tab_drivers.py @@ -104,6 +104,84 @@ class DriverBrowser(QTableView): self.setSortingEnabled(True) self.selectionModel().selectionChanged.connect(self.on_selection) +@dataclass +class DBNationality(): + id: int + name: str + icon: bytes + +class NationalityEditorWidget(QWidget): + cursor: mariadb.Cursor + parent_update_fn: callable + selected_driver_id: int = 1 + layout: QGridLayout + button_add: QPushButton + nationalities: [DBNationality] + + def remove_nationality(self, nationality_id): + print(f"removing nationality id {nationality_id} from driver {self.selected_driver_id}") + self.cursor.execute("delete from `formula1`.`membernationality` where `memberID` = ? and `nationalityID` = ?", (self.selected_driver_id,nationality_id,)) + self.update(False) + + def add_nationality(self, nationality_id): + print(f"adding nationality id {nationality_id} from driver {self.selected_driver_id}") + self.cursor.execute("insert into `formula1`.`membernationality` (`memberID`, `nationalityID`) values (?, ?)", (self.selected_driver_id,nationality_id,)) + self.update(False) + + def update(self, cascade=True): + for i in reversed(range(self.layout.count())): + self.layout.itemAt(i).widget().deleteLater() + self.cursor.execute("select `nationality`.`ID`, `nationality`.`country` from nationality") + all_nationalities = self.cursor.fetchall() + + nationalities = list() + self.cursor.execute("select `nationality`.`ID`, `nationality`.`country`, `nationality`.`flag` from nationality join membernationality on membernationality.nationalityID = nationality.ID where membernationality.memberID = ?", (self.selected_driver_id,)) + last_row = 0 + buttons = list() + for i, record in enumerate(self.cursor.fetchall()): + nationality = DBNationality(*record) + if nationality.icon != None: + pixmap = QPixmap() + pixmap.loadFromData(nationality.icon) + flag_label = QLabel() + flag_label.setPixmap(pixmap) + self.layout.addWidget(flag_label, i, 0) + self.layout.addWidget(QLabel(nationality.name), i, 1) + buttons.insert(i, QPushButton("remove")) + temp = nationality.id + buttons[i].clicked.connect(lambda: self.remove_nationality(temp)) # idk this works + self.layout.addWidget(buttons[i], i, 2) + nationalities.append(nationality) + last_row = i + new_nationality_picker = QComboBox() + for nationality in all_nationalities: + new_nationality_picker.addItem(nationality[1], nationality[0]) + new_nationality_picker.setCurrentIndex(-1) + new_nationality_picker.currentIndexChanged.connect(lambda i: self.add_nationality(new_nationality_picker.itemData(i))) + self.layout.addWidget(new_nationality_picker, last_row + 1, 0, 1, 3) + + def parent_update(self): + if self.parent_update_fn != None: + self.parent_update_fn() + + def set_driver_id(self, id): + self.selected_driver_id = id + self.update(False) + + def set_parent_update(self, fn): + self.parent_update_fn = fn + + def save_edits(self): + self.parent_update() + + def __init__(self, cursor: mariadb.Cursor, parent=None): + super(NationalityEditorWidget, self).__init__(parent) + + self.cursor = cursor + + self.layout = QGridLayout() + self.setLayout(self.layout) + self.update(False) class DriverDetailsWidget(QWidget): cursor: mariadb.Cursor @@ -117,6 +195,7 @@ class DriverDetailsWidget(QWidget): line_edit_name_first: QLineEdit line_edit_name_middle: QLineEdit line_edit_name_last: QLineEdit + nationality_editor: NationalityEditorWidget push_button_save: QPushButton def update(self, cascade=True): @@ -133,6 +212,8 @@ class DriverDetailsWidget(QWidget): self.line_edit_name_first.setText(self.driver_details.first_name) self.line_edit_name_middle.setText(self.driver_details.middle_name) self.line_edit_name_last.setText(self.driver_details.last_name) + if cascade == False: return + self.nationality_editor.update() def parent_update(self): if self.parent_update_fn != None: @@ -140,6 +221,7 @@ class DriverDetailsWidget(QWidget): def set_driver_id(self, id): self.selected_driver_id = id + self.nationality_editor.set_driver_id(id) self.update(False) def set_parent_update(self, fn): @@ -161,9 +243,8 @@ class DriverDetailsWidget(QWidget): layout = QVBoxLayout(self) layout.setAlignment(Qt.AlignmentFlag.AlignTop) - label_portrait = QLabel("Driver portrait") + layout.addWidget(QLabel("Driver portrait")) self.img_driver = QLabel() - layout.addWidget(label_portrait) layout.addWidget(self.img_driver) details_form = QFormLayout(self) @@ -177,6 +258,17 @@ class DriverDetailsWidget(QWidget): details_form.addRow("Last name", self.line_edit_name_last) layout.addLayout(details_form) + layout.addWidget(QLabel("Nationalities")) + self.nationality_editor = NationalityEditorWidget(self.cursor, self) + layout.addWidget(self.nationality_editor) + + layout.addWidget(QLabel("Function")) + function_combobox = QComboBox() + self.cursor.execute("select `function`, `ID` from `function`") + for record in self.cursor.fetchall(): + function_combobox.addItem(record[0], record[1]) + layout.addWidget(function_combobox) + self.push_button_save = QPushButton("Save edits") self.push_button_save.clicked.connect(self.save_edits) layout.addWidget(self.push_button_save) |