summaryrefslogtreecommitdiff
path: root/kodereviewer/pull_request_model.py
diff options
context:
space:
mode:
Diffstat (limited to 'kodereviewer/pull_request_model.py')
-rw-r--r--kodereviewer/pull_request_model.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/kodereviewer/pull_request_model.py b/kodereviewer/pull_request_model.py
new file mode 100644
index 0000000..e08bf6e
--- /dev/null
+++ b/kodereviewer/pull_request_model.py
@@ -0,0 +1,66 @@
+import json
+from os import path
+from typing import Any, Optional
+
+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
+from kodereviewer.data import PullRequest
+
+QML_IMPORT_NAME = "org.deprecated.kodereviewer"
+QML_IMPORT_MAJOR_VERSION = 1
+
+@QmlElement
+class PullRequestModel(QAbstractListModel):
+
+ _project: Optional[Project]
+
+ NumberRole = Qt.ItemDataRole.UserRole + 1
+ TitleRole = NumberRole + 1
+
+ def __init__(self):
+ super().__init__()
+
+ def data(self,
+ index: QModelIndex | QPersistentModelIndex,
+ role: int = Qt.ItemDataRole.DisplayRole) -> object:
+ if self._project is None:
+ return None
+
+ pull_request = self._project.pullRequests[index.row()]
+ if role == self.NumberRole:
+ return pull_request.number
+ if role == self.TitleRole:
+ return pull_request.title
+ if role == Qt.ItemDataRole.DisplayRole:
+ return f'{pull_request.number} - {pull_request.title}'
+ return None
+
+ def rowCount(self, parent: QModelIndex | QPersistentModelIndex = QModelIndex()) -> int:
+ if self._project is not None:
+ return len(self._project.pullRequests)
+ return 0
+
+ def roleNames(self) -> dict[int, QByteArray]:
+ return {
+ self.NumberRole: QByteArray(b"number"),
+ self.TitleRole: QByteArray(b"title"),
+ }
+
+ def get_project(self) -> Optional[Project]:
+ return self._project
+
+ def set_project(self, project: Optional[Project])-> None:
+ if project is None:
+ return
+ self._project = project
+ print("Connecting!")
+ self._project.pullRequestChanged.connect(self._reset_model)
+
+ project = Property(Project, fget=get_project, fset=set_project)
+
+ def _reset_model(self) -> None:
+ print("Reseting pull request model")
+ self.beginResetModel()
+ self.endResetModel()