From 996a09afe7725bca4e6422108c5071d59ea30c06 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 10 Apr 2023 11:09:24 +0200 Subject: more driver tab --- gui/gui.todo | 4 +- gui/main.py | 2 +- gui/main_window.py | 30 +++++++++- gui/tab_drivers.py | 167 +++++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 173 insertions(+), 30 deletions(-) diff --git a/gui/gui.todo b/gui/gui.todo index 720da30..95501d8 100644 --- a/gui/gui.todo +++ b/gui/gui.todo @@ -1,8 +1,8 @@ [x] login dialog box (like mac system settings with padlock) (username + password) [ ] drivers - - [ ] {show} {set} full name + - [x] {show} {set} full name - [ ] {show} {set} nationality (both as text and as flag) - - [ ] {show} portrait + - [x] {show} portrait - [ ] {show} {set} team - [ ] {show} {set} role/function [ ] teams diff --git a/gui/main.py b/gui/main.py index ee84dfa..b3c7e34 100755 --- a/gui/main.py +++ b/gui/main.py @@ -20,7 +20,7 @@ if __name__ == '__main__': break except: print("Login failed, please try again") - win = MainWindow(db.cursor(buffered=True)) + win = MainWindow(db.cursor(buffered=True), db) win.show() app.exec() diff --git a/gui/main_window.py b/gui/main_window.py index d30633d..c503951 100644 --- a/gui/main_window.py +++ b/gui/main_window.py @@ -10,6 +10,7 @@ from tab_races import * class MainWindow(QMainWindow): cursor: mariadb.Cursor = None + db: mariadb.Connection = None menu_bar: QMenuBar _tab_drivers: TabDrivers @@ -18,9 +19,25 @@ class MainWindow(QMainWindow): _tab_circuits: TabCircuits _tab_races: TabRaces - def set_cursor(self, cursor): + def set_cursor(self, cursor: mariadb.Cursor): self.cursor = cursor + def set_connection(self, db: mariadb.Connection): + self.db = db + + def commit(self): + self.db.commit() + + def exit(self, commit=False): + if commit: self.commit() + self.db.close() + self.close() + + def exit_no_commit(self): + self.exit(False) + def exit_commit(self): + self.exit(True) + def call_update_flags(self): folder = QFileDialog().getExistingDirectory(self, "Open directory", "/var/dab2/") self.cursor.execute("call spUpdateFlags(?)", (folder,)) @@ -32,10 +49,11 @@ class MainWindow(QMainWindow): def call_delete_flags(self): self.cursor.execute("call spDeleteFlags()") - def __init__(self, cursor: mariadb.Cursor, parent=None): + def __init__(self, cursor: mariadb.Cursor, db: mariadb.Connection, parent=None): super(MainWindow, self).__init__(parent) self.set_cursor(cursor) + self.set_connection(db) self.setWindowTitle("[floating] dab2 eindopdracht main window") self.setMinimumHeight(500) @@ -54,6 +72,11 @@ class MainWindow(QMainWindow): main_layout.addTab(self._tab_races, "races") self.menu_bar = QMenuBar(self) + menu_procedures = self.menu_bar.addMenu("file") + sp_update_flags = menu_procedures.addAction("Exit (commit changes)") + sp_update_flags.triggered.connect(self.exit_commit) + sp_update_flags = menu_procedures.addAction("Exit (don't commit changes)") + sp_update_flags.triggered.connect(self.exit_no_commit) menu_procedures = self.menu_bar.addMenu("procedures") sp_update_flags = menu_procedures.addAction("Import/update flags") sp_update_flags.triggered.connect(self.call_update_flags) @@ -61,6 +84,9 @@ class MainWindow(QMainWindow): sp_update_persons.triggered.connect(self.call_update_persons) sp_delete_flags = menu_procedures.addAction("Delete flags") sp_delete_flags.triggered.connect(self.call_delete_flags) + menu_procedures = self.menu_bar.addMenu("database") + sp_update_flags = menu_procedures.addAction("Commit changes") + sp_update_flags.triggered.connect(self.commit) self.setMenuBar(self.menu_bar) self.setCentralWidget(main_layout) diff --git a/gui/tab_drivers.py b/gui/tab_drivers.py index a94ee14..b6571db 100644 --- a/gui/tab_drivers.py +++ b/gui/tab_drivers.py @@ -10,23 +10,25 @@ from split_view_layout import * from dataclasses import dataclass @dataclass -class DBDriver(): - id: int - first_name: str - last_name: str +class DBMember(): + id: int = 0 + first_name: str = "" + middle_name: str = "" + last_name: str = "" + photo: bytes = b"" class DriverModel(QAbstractTableModel): cursor: mariadb.Cursor - _header = ["first name", "last name"] - _data: [DBDriver] = [] + _header = ["first name", "middle name", "last name"] + _data: [DBMember] = [] def update(self): self.beginResetModel() - self.cursor.execute("select `ID`, `firstName`, `lastName` from `member`") + self.cursor.execute("select `ID`, `firstName`, `middleName`, `lastName` from `member`") self._data = list() for result in self.cursor.fetchall(): - self._data.append(DBDriver(*result)) + self._data.append(DBMember(*result)) self.endResetModel() def __init__(self, cursor): @@ -52,52 +54,167 @@ class DriverModel(QAbstractTableModel): driver = self._data[index.row()] return ( driver.first_name, + driver.middle_name, driver.last_name, )[index.column()] +class DriverBrowser(QTableView): + model_table: DriverModel + model_proxy: QSortFilterProxyModel + parent_update_fn: callable + selected_driver_id: int = 1 + ignore_update: bool = False + temp_selection_index: int = 0 + + def update(self, cascade=True): + self.model_table.update() + self.ignore_update = True + self.selectRow(self.temp_selection_index) + self.ignore_update = False + + def parent_update(self): + if self.parent_update_fn != None: + self.parent_update_fn() + + def set_parent_update(self, fn): + self.parent_update_fn = fn + + def on_selection(self): + if self.ignore_update: return + rows = self.selectionModel().selectedRows() + if len(rows) == 0: return + row = rows[0].row() + self.temp_selection_index = row + index = self.model_proxy.mapToSource(self.model_proxy.index(row, 0)) + new_driver_id = self.model_table.headerData(index.row(), Qt.Vertical, Qt.DisplayRole) + if self.selected_driver_id == new_driver_id: return + self.selected_driver_id = new_driver_id + self.parent_update() + + def __init__(self, cursor: mariadb.Cursor, parent=None): + super(DriverBrowser, self).__init__(parent) + + self.cursor = cursor + + self.setSelectionBehavior(QAbstractItemView.SelectRows) + self.model_table = DriverModel(self.cursor) + self.model_proxy = QSortFilterProxyModel() + self.model_proxy.setSourceModel(self.model_table) + self.setModel(self.model_proxy) + self.setSortingEnabled(True) + self.selectionModel().selectionChanged.connect(self.on_selection) + + class DriverDetailsWidget(QWidget): + cursor: mariadb.Cursor + parent_update_fn: callable + selected_driver_id: int = 1 + + driver_details: DBMember + + label_id: QLabel + img_driver: QLabel + line_edit_name_first: QLineEdit + line_edit_name_middle: QLineEdit + line_edit_name_last: QLineEdit + push_button_save: QPushButton + + def update(self, cascade=True): + self.cursor.execute("select `ID`, `firstName`, `middleName`, `lastName`, `photo` from `member` where `ID` = ?", (self.selected_driver_id,)) + self.driver_details = DBMember(*self.cursor.fetchone()) + + pixmap = QPixmap() + pixmap.loadFromData(self.driver_details.photo) + self.label_id.setText(str(self.driver_details.id)) + self.img_driver.setScaledContents(True) + # self.img_driver.setSizePolicy(QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Ignored) + # self.img_driver.setPixmap(pixmap.scaled(self.img_driver.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) + self.img_driver.setPixmap(pixmap) + 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) + + 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): + first_name = self.line_edit_name_first.text() + middle_name = self.line_edit_name_middle.text() + last_name = self.line_edit_name_last.text() + self.cursor.execute("update `member` set `firstName` = ?, `middleName` = ?, `lastName` = ? where `ID` = ?", (first_name, middle_name, last_name, self.selected_driver_id,)) + self.parent_update() + def __init__(self, cursor: mariadb.Cursor, parent=None): super(DriverDetailsWidget, self).__init__(parent) + self.cursor = cursor + + self.driver_details = DBMember() layout = QVBoxLayout(self) - # layout.setAlignment(Qt.AlignmentFlag.AlignTop) + layout.setAlignment(Qt.AlignmentFlag.AlignTop) label_portrait = QLabel("Driver portrait") + self.img_driver = QLabel() layout.addWidget(label_portrait) + layout.addWidget(self.img_driver) details_form = QFormLayout(self) - details_form.addRow("First name", QLineEdit("hoi")) - details_form.addRow("Middle name", QLineEdit("hoi")) - details_form.addRow("Last name", QLineEdit("hoi")) + self.label_id = QLabel() + self.line_edit_name_first = QLineEdit() + self.line_edit_name_middle = QLineEdit() + self.line_edit_name_last = QLineEdit() + details_form.addRow("ID", self.label_id) + details_form.addRow("First name", self.line_edit_name_first) + details_form.addRow("Middle name", self.line_edit_name_middle) + details_form.addRow("Last name", self.line_edit_name_last) layout.addLayout(details_form) + self.push_button_save = QPushButton("Save edits") + self.push_button_save.clicked.connect(self.save_edits) + layout.addWidget(self.push_button_save) + self.setLayout(layout) class TabDrivers(QWidget): layout: SplitViewLayout cursor: mariadb.Cursor - widget_driver_table: QTableView - model_driver_table: DriverModel - model_proxy: QSortFilterProxyModel - + driver_browser: DriverBrowser driver_details: DriverDetailsWidget + selected_driver_id: int = 1 + + def update(self, cascade=True): + print("update TabDrivers") + + if not cascade: return + self.driver_browser.update(True) + self.driver_details.update(True) + + def child_update(self): + self.selected_driver_id = self.driver_browser.selected_driver_id + self.driver_browser.update(False) + self.driver_details.set_driver_id(self.selected_driver_id) + def __init__(self, cursor: mariadb.Cursor, parent=None): super(TabDrivers, self).__init__(parent) self.cursor = cursor self.layout = SplitViewLayout(self) - self.widget_driver_table = QTableView(self) - self.widget_driver_table.setSelectionBehavior(QAbstractItemView.SelectRows) - self.model_driver_table = DriverModel(self.cursor) - self.model_proxy = QSortFilterProxyModel() - self.model_proxy.setSourceModel(self.model_driver_table) - self.widget_driver_table.setModel(self.model_proxy) - self.widget_driver_table.setSortingEnabled(True) - self.layout.leftWidget(self.widget_driver_table) + self.driver_browser = DriverBrowser(self.cursor, self) + self.driver_browser.set_parent_update(self.child_update) + self.layout.leftWidget(self.driver_browser) - self.driver_details = DriverDetailsWidget(self) + self.driver_details = DriverDetailsWidget(self.cursor, self) + self.driver_details.set_parent_update(self.child_update) self.layout.rightWidget(self.driver_details) self.setLayout(self.layout) -- cgit v1.2.3