diff options
Diffstat (limited to 'kodereviewer/models/pull_request.py')
-rw-r--r-- | kodereviewer/models/pull_request.py | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/kodereviewer/models/pull_request.py b/kodereviewer/models/pull_request.py index 7d08efa..99e3495 100644 --- a/kodereviewer/models/pull_request.py +++ b/kodereviewer/models/pull_request.py @@ -1,5 +1,4 @@ -import json -from os import path +from copy import copy from typing import Any, Optional from PySide6.QtCore import QAbstractListModel, QByteArray, QModelIndex, QObject, QPersistentModelIndex, QStandardPaths, QUrl, Qt, Signal, Slot, Property @@ -15,9 +14,17 @@ QML_IMPORT_MAJOR_VERSION = 1 class PullRequestModel(QAbstractListModel): _project: Optional[Project] + _pull_requests: list[PullRequest] + + # This attribute holds a reference to a reference to a newly + # copied PullRequest so Qt don't frees the memory of the pull + # request on _pull_requests + _current_pull_request: PullRequest NumberRole = Qt.ItemDataRole.UserRole + 1 TitleRole = NumberRole + 1 + StateRole = TitleRole + 1 + DraftRole = StateRole + 1 def __init__(self): super().__init__() @@ -33,6 +40,10 @@ class PullRequestModel(QAbstractListModel): return pull_request.number if role == self.TitleRole: return pull_request.title + if role == self.StateRole: + return pull_request.state + if role == self.DraftRole: + return pull_request.draft if role == Qt.ItemDataRole.DisplayRole: return f'{pull_request.number} - {pull_request.title}' return None @@ -46,6 +57,8 @@ class PullRequestModel(QAbstractListModel): return { self.NumberRole: QByteArray(b"number"), self.TitleRole: QByteArray(b"title"), + self.StateRole: QByteArray(b"state"), + self.DraftRole: QByteArray(b"draft"), } def get_project(self) -> Optional[Project]: @@ -64,12 +77,14 @@ class PullRequestModel(QAbstractListModel): @Slot(int, result=PullRequest) def get(self, index: int) -> Optional[PullRequest]: - if self._project is not None: - return self._project.pullRequests[index] - return None + self._current_pull_request = self._pull_requests[index].copy() + + return self._current_pull_request def _reset_model(self) -> None: print("Reseting pull request model") self.beginResetModel() + if self._project is not None: + self._pull_requests = self._project.pullRequests self.endResetModel() |