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
|
import os
import mariadb
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtCore import Qt
from split_view_layout import *
from dataclasses import dataclass
@dataclass
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", "middle name", "last name"]
_data: [DBMember] = []
def update(self):
self.beginResetModel()
self.cursor.execute("select `ID`, `firstName`, `middleName`, `lastName` from `member`")
self._data = list()
for result in self.cursor.fetchall():
self._data.append(DBMember(*result))
self.endResetModel()
def __init__(self, cursor):
super().__init__()
self.cursor = cursor
self.update()
def rowCount(self, index=0):
return len(self._data)
def columnCount(self, index=0):
return len(self._header)
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return self._header[section]
else:
return self._data[section].id
def data(self, index, role):
if role == Qt.DisplayRole:
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)
self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
@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 = self.sender().property("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):
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
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)
button = QPushButton("remove")
button.setProperty("id", nationality.id)
button.clicked.connect(self.remove_nationality) # idk this works
self.layout.addWidget(button, 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.setPlaceholderText("Add nationality...")
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
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
nationality_editor: NationalityEditorWidget
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)
if cascade == False: return
self.nationality_editor.update()
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.nationality_editor.set_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.addWidget(QLabel("Driver portrait"))
self.img_driver = QLabel()
layout.addWidget(self.img_driver)
details_form = QFormLayout(self)
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)
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)
self.setLayout(layout)
class TabDrivers(QWidget):
layout: SplitViewLayout
cursor: mariadb.Cursor
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 set_driver_id(self, id):
print("not implemented")
def __init__(self, cursor: mariadb.Cursor, parent=None):
super(TabDrivers, self).__init__(parent)
self.cursor = cursor
self.layout = SplitViewLayout(self)
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.cursor, self)
self.driver_details.set_parent_update(self.child_update)
self.layout.rightWidget(self.driver_details)
self.setLayout(self.layout)
|