refactor project by using enums for recurring strings
This commit is contained in:
@@ -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):
|
||||
@@ -46,68 +79,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)))
|
||||
if role == Qt.ItemDataRole.DisplayRole or role == Qt.ItemDataRole.EditRole:
|
||||
if isinstance(value, datetime):
|
||||
return value.strftime("%Y-%m-%d %M:%M:%S")
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
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
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user