refactor project by using enums for recurring strings

This commit is contained in:
Thomas Peetz
2025-02-06 16:33:15 +01:00
parent 7dc18b10cb
commit ed0e108599
6 changed files with 62 additions and 63 deletions
+4 -3
View File
@@ -1,5 +1,6 @@
from PySide6.QtCore import Signal, QSortFilterProxyModel
from PySide6.QtWidgets import QWidget, QVBoxLayout, QTabWidget, QTableView, QMdiSubWindow, QHeaderView
from PySide6.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QTabWidget, QMenu, QTableView, QMdiSubWindow, \
QHeaderView
from gui.model_config import KontorModelConfig
from gui.table_model import KontorTableModel
@@ -53,11 +54,11 @@ class ComicWindow(QMdiSubWindow):
self.data_views.append(model)
data_tab.setLayout(layout)
table_view = QTableView()
header = table_view.horizontalHeader()
header.setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
proxy_model = QSortFilterProxyModel()
proxy_model.setSourceModel(model)
table_view.setSortingEnabled(True)
header = table_view.horizontalHeader()
header.setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
table_view.setModel(proxy_model)
layout.addLayout(table_config.get_filter_layout())
layout.addWidget(table_view)
+1 -3
View File
@@ -57,11 +57,9 @@ class MediaWindow(QMdiSubWindow):
proxy_model = QSortFilterProxyModel()
proxy_model.setSourceModel(model)
table_view.setSortingEnabled(True)
# header = table_view.horizontalHeader()
# header.setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
#table_view.setModel(model)
table_view.setModel(proxy_model)
layout.addLayout(table_config.get_filter_layout())
layout.addWidget(table_view)
model.refresh()
table_view.resizeColumnToContents(0)
return data_tab
+4 -4
View File
@@ -57,11 +57,11 @@ class MetaDataWindow(QMdiSubWindow):
# proxy_model = QSortFilterProxyModel()
# proxy_model.setSourceModel(model)
table_view.setSortingEnabled(True)
header = table_view.horizontalHeader()
header.setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
# table_view.setModel(proxy_model)
table_view.setModel(model)
# header = table_view.horizontalHeader()
# header.setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
table_view.setModel(proxy_model)
layout.addLayout(table_config.get_filter_layout())
layout.addWidget(table_view)
model.refresh()
table_view.resizeColumnToContents(0)
return data_tab
+1 -2
View File
@@ -1,6 +1,5 @@
from PySide6.QtWidgets import QHBoxLayout, QCheckBox, QMdiSubWindow
from kontor_schema import KontorDB
from kontor_schema.database import ColumnEntry
from kontor_schema import KontorDB, ColumnEntry
class KontorModelConfig:
+51 -50
View File
@@ -1,12 +1,45 @@
from datetime import datetime
from typing import Any
from PySide6.QtCore import QAbstractTableModel, QModelIndex
from PySide6.QtGui import Qt
from PySide6.QtGui import Qt, QColor
from kontor_schema.database import ColumnEntry
from .model_config import KontorModelConfig
def get_display_value(value: Any, column_config: dict, window) -> str:
if isinstance(value, datetime):
return value.strftime("%Y-%m-%d %M:%M:%S")
if column_config[ColumnEntry.COLUMN_TYPE] == 'BOOLEAN':
if value == 1:
return window.tick
else:
return window.cross
if value is None:
return ""
# window.log.info(f"unknown type: {column_config[ColumnEntry.COLUMN_TYPE]} - {type(value)}")
return str(value)
def get_edit_value(value, column_config, window):
# window.log.info(f"edit value {value}")
return str(value)
def get_decoration_value(value: Any, column_config: dict, window):
if column_config[ColumnEntry.COLUMN_TYPE] == 'BOOLEAN':
if value == 1:
return window.tick
else:
return window.cross
def get_background_value(value: Any, column_config: dict, window):
if value is None:
return QColor('lightgrey')
class KontorTableModel(QAbstractTableModel):
def __init__(self, model_config: KontorModelConfig):
@@ -47,73 +80,41 @@ class KontorTableModel(QAbstractTableModel):
if orientation == Qt.Orientation.Horizontal and role == Qt.ItemDataRole.DisplayRole:
return self._config.header[col][ColumnEntry.COLUMN_LABEL]
if orientation == Qt.Orientation.Vertical and role == Qt.ItemDataRole.DisplayRole:
return str(col+1)
return str(col + 1)
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
if self._data is None:
return None
value = self._data[index.row()][index.column()]
# print('{}:: {}:: {}: {}'.format(index, role, value, type(value)))
row = index.row()
column = index.column()
column_type = self._config.header[column][ColumnEntry.COLUMN_TYPE]
# self.log.info(f"{row}-{column}: {column_type}")
if role == Qt.ItemDataRole.DisplayRole or role == Qt.ItemDataRole.EditRole:
if column_type == "BOOLEAN":
if isinstance(value, bytes):
if value == b'\x01':
return self._config.main_window.tick
else:
return self._config.main_window.cross
if isinstance(value, int):
# print('{}:: {}: {}'.format(index, value, type(value)))
if value == 1:
return self._config.main_window.tick
else:
return self._config.main_window.cross
if isinstance(value, bool):
if value:
return self._config.main_window.tick
else:
return self._config.main_window.cross
if isinstance(value, datetime):
return value.strftime("%Y-%m-%d %M:%M:%S")
if isinstance(value, str):
return value
return str(value)
if role == Qt.ItemDataRole.DecorationRole:
if isinstance(value, bytes):
if value == b'\x01':
return self._config.main_window.tick
else:
return self._config.main_window.cross
if isinstance(value, int):
if value == 1:
return self._config.main_window.tick
else:
return self._config.main_window.cross
if isinstance(value, bool):
if value:
return self._config.main_window.tick
else:
return self._config.main_window.cross
# print('{}:: {}:: {}:: {}: {}'.format(index, role, self._config.header[index.column()][ColumnEntry.COLUMN_TYPE], value, type(value)))
match role:
case Qt.ItemDataRole.DisplayRole:
return get_display_value(value, self._config.header[index.column()], self._config.main_window)
case Qt.ItemDataRole.EditRole:
return get_edit_value(value, self._config.header[index.column()], self._config.main_window)
case Qt.ItemDataRole.DecorationRole:
return get_decoration_value(value, self._config.header[index.column()], self._config.main_window)
case Qt.ItemDataRole.BackgroundRole:
return get_background_value(value, self._config.header[index.column()], self._config.main_window)
def columnCount(self, index=QModelIndex()):
# self.log.info("rowCount %s: %d", self, len(self._config.header))
return len(self._config.header)
def setData(self, index, value, role: int) -> bool:
# print(index, role)
def setData(self, index, value, role=Qt.ItemDataRole.EditRole) -> bool:
# self._config.log.info(f"{index}: {role}")
if role == Qt.ItemDataRole.EditRole:
self._data[index.row()][index.column()] = value
# print(self._data[index.row()][index.column()])
# self._config.log.info(f"{index.row()}-{index.column()}: {self._data[index.row()][index.column()]}")
self.dataChanged.emit(index, index)
return True
if role == Qt.ItemDataRole.CheckStateRole:
# print("role == Qt.ItemDataRole.CheckStateRole")
print("role == Qt.ItemDataRole.CheckStateRole")
checked = value == Qt.CheckState.Checked
self._data[index.row()][index.column()] = checked
return False
def flags(self, index):
if self._config.header[index.column()][ColumnEntry.COLUMN_NAME] == 'id':
return Qt.ItemFlag.ItemIsEnabled
return Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsEditable | Qt.ItemFlag.ItemIsUserTristate
@@ -7,4 +7,4 @@ from .metadata import MetaDataTable, MetaDataColumn
from .tysc import Card, CardSet, Sport, Team, FieldPosition, Rooster, Player, Vendor
from .media import MediaFile, MediaArticle, MediaVideo
from .base import Base
from .database import KontorDB
from .database import KontorDB, ColumnEntry