summaryrefslogtreecommitdiff
path: root/kodereviewer/project_model.py
diff options
context:
space:
mode:
Diffstat (limited to 'kodereviewer/project_model.py')
-rw-r--r--kodereviewer/project_model.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/kodereviewer/project_model.py b/kodereviewer/project_model.py
new file mode 100644
index 0000000..c676157
--- /dev/null
+++ b/kodereviewer/project_model.py
@@ -0,0 +1,92 @@
+import json
+from os import path
+from typing import Any
+
+from PySide6.QtCore import QAbstractListModel, QByteArray, QModelIndex, QObject, QPersistentModelIndex, QStandardPaths, QUrl, Qt, Signal, Slot, Property
+from PySide6.QtQml import QmlElement
+
+from kodereviewer.project import Project
+
+QML_IMPORT_NAME = "org.deprecated.kodereviewer"
+QML_IMPORT_MAJOR_VERSION = 1
+
+
+@QmlElement
+class ProjectModel(QAbstractListModel):
+ """Projects list!"""
+
+ projects: list[Project]
+
+ NameRole = Qt.ItemDataRole.UserRole + 1
+ OwnerRole = NameRole + 1
+ UrlRole = OwnerRole + 1
+
+ def __init__(self):
+ super().__init__()
+ self.projects = []
+
+ project_config = self._project_file()
+ try:
+ with open(project_config) as fp:
+ data = json.load(fp)
+ if isinstance(data, list):
+ self._load_projects(data)
+ except OSError as e:
+ pass
+
+ def _project_file(self) -> str:
+ app_data = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation)
+ project_config = path.join(app_data, 'projects.json')
+ return project_config
+
+ def data(self,
+ index: QModelIndex | QPersistentModelIndex,
+ role: int = Qt.ItemDataRole.DisplayRole) -> object:
+ project = self.projects[index.row()]
+
+ if role == Qt.ItemDataRole.DisplayRole:
+ return project.name
+ if role == self.NameRole:
+ return project.name
+ if role == self.OwnerRole:
+ return project.owner
+ if role == self.UrlRole:
+ return project.url
+
+ return None
+
+ def rowCount(self, parent: QModelIndex | QPersistentModelIndex = QModelIndex()) -> int:
+ return len(self.projects)
+
+ def roleNames(self) -> dict[int, QByteArray]:
+ return {
+ self.NameRole: QByteArray(b"name"),
+ self.OwnerRole: QByteArray(b"owner"),
+ self.UrlRole: QByteArray(b"url")
+ }
+
+ @Slot(int, result=Project)
+ def get(self, index: int) -> Project:
+ return self.projects[index]
+
+ def _load_projects(self, data: list[dict[str, Any]]) -> None:
+ for project in data:
+ self.projects.append(Project(
+ project['name'],
+ project['owner'],
+ project['url']
+ ))
+
+ def _save_projects(self):
+ project_config = self._project_file()
+ data = [{'name': project.name, 'owner': project.owner, 'url': project.url} for project in self.projects]
+
+ with open(self._project_file(), 'w') as fp:
+ json.dump(data, fp)
+
+ @Slot(str, str, str)
+ def add(self, name: str, owner: str, url: str) -> None:
+ self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
+ self.projects.append(Project(name, owner, url))
+ self._save_projects()
+ self.endInsertRows()