implement generic table model
implement generic table model which reads table info from db and constructs table view
This commit is contained in:
+1
-1
@@ -6,7 +6,7 @@ from pathlib import Path
|
||||
|
||||
import yaml
|
||||
from PySide6.QtGui import QAction, QIcon
|
||||
from PySide6.QtWidgets import QWidget, QVBoxLayout, QMenu, QMessageBox, QTabWidget, QTableView, QHBoxLayout, QCheckBox
|
||||
from PySide6.QtWidgets import QWidget, QVBoxLayout, QMenu, QMessageBox, QTabWidget, QTableView
|
||||
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow
|
||||
from platformdirs import PlatformDirs
|
||||
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
from datetime import datetime
|
||||
|
||||
import mariadb
|
||||
from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt
|
||||
from PySide6.QtGui import QColor
|
||||
|
||||
|
||||
class MediaFileTableModel(QAbstractTableModel):
|
||||
|
||||
def __init__(self, db_config, main_window):
|
||||
super().__init__()
|
||||
self.main_window = main_window
|
||||
self._data = []
|
||||
self.mariadb_conn = mariadb.connect(
|
||||
host=db_config['mariadb']['host'],
|
||||
port=db_config['mariadb']['port'],
|
||||
user=db_config['mariadb']['user'],
|
||||
password=db_config['mariadb']['password'],
|
||||
database=db_config['mariadb']['database']
|
||||
)
|
||||
self.refresh()
|
||||
|
||||
def refresh(self):
|
||||
data = []
|
||||
cursor = self.mariadb_conn.cursor()
|
||||
filter_rule = ""
|
||||
print(self.main_window.filter["download"].isChecked())
|
||||
if self.main_window.filter["download"].isChecked():
|
||||
print(self.main_window.filter["download"].isChecked())
|
||||
filter_rule = "WHERE should_download is true"
|
||||
if self.main_window.filter["review"].isChecked():
|
||||
if len(filter_rule) > 0:
|
||||
filter_rule += " AND "
|
||||
else:
|
||||
filter_rule += "WHERE "
|
||||
filter_rule += "review is true"
|
||||
print(f"{filter_rule=}")
|
||||
cursor.execute(f"SELECT id, url, review, should_download, file_name, cloud_link FROM media_file {filter_rule}")
|
||||
rows = cursor.fetchall()
|
||||
print(len(rows))
|
||||
if len(rows) > 0:
|
||||
self.beginResetModel()
|
||||
for row in rows:
|
||||
data.append(list(row))
|
||||
self._data = data
|
||||
self.endResetModel()
|
||||
else:
|
||||
self._data = None
|
||||
self.layoutChanged.emit()
|
||||
self.main_window.statusBar.showMessage(f"{len(rows)} Einträge geladen", 3000)
|
||||
|
||||
def rowCount(self, parent=QModelIndex()):
|
||||
# The length of the outer list.
|
||||
if self._data is None:
|
||||
return 0
|
||||
return len(self._data)
|
||||
|
||||
def headerData(self, col, orientation, role=Qt.ItemDataRole.DisplayRole):
|
||||
if orientation == Qt.Orientation.Horizontal and role == Qt.ItemDataRole.DisplayRole:
|
||||
match col:
|
||||
case 0:
|
||||
return "ID"
|
||||
case 1:
|
||||
return "URL"
|
||||
case 2:
|
||||
return "Review"
|
||||
case 3:
|
||||
return "Download"
|
||||
case 4:
|
||||
return "Filename"
|
||||
case 5:
|
||||
return "Cloud Link"
|
||||
if orientation == Qt.Orientation.Vertical and role == Qt.ItemDataRole.DisplayRole:
|
||||
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()]
|
||||
if role == Qt.ItemDataRole.DisplayRole:
|
||||
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.main_window.tick
|
||||
else:
|
||||
return self.main_window.cross
|
||||
return value
|
||||
if role == Qt.ItemDataRole.DecorationRole:
|
||||
if isinstance(value, bytes):
|
||||
# print('{}: {}'.format(value, type(value)))
|
||||
if value == b'\x01':
|
||||
return self.main_window.tick
|
||||
else:
|
||||
return self.main_window.cross
|
||||
|
||||
def columnCount(self, index=QModelIndex()):
|
||||
# The following takes the first sub-list, and returns
|
||||
# the length (only works if all rows are an equal length)
|
||||
if self._data is None:
|
||||
return 5
|
||||
return len(self._data[0])
|
||||
|
||||
def setData(self, index, value, role=Qt.ItemDataRole.EditRole):
|
||||
if role == Qt.ItemDataRole.EditRole:
|
||||
self._data[index.row()][index.column()] = value
|
||||
if role == Qt.ItemDataRole.CheckStateRole:
|
||||
checked = value == Qt.CheckState.Checked
|
||||
self._data[index.row()][index.column()] = checked
|
||||
return True
|
||||
|
||||
def flags(self, index):
|
||||
return Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsEditable | Qt.ItemFlag.ItemIsUserTristate
|
||||
Reference in New Issue
Block a user