diff options
Diffstat (limited to 'kodereviewer/models/file.py')
-rw-r--r-- | kodereviewer/models/file.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/kodereviewer/models/file.py b/kodereviewer/models/file.py new file mode 100644 index 0000000..246bd5b --- /dev/null +++ b/kodereviewer/models/file.py @@ -0,0 +1,95 @@ +from copy import copy +from enum import IntEnum, auto +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 ChangedFile, Label, PullRequest + +QML_IMPORT_NAME = "org.deprecated.kodereviewer" +QML_IMPORT_MAJOR_VERSION = 1 + + +@QmlElement +class FileModel(QAbstractListModel): + + _pull_request: Optional[PullRequest] + + class Roles(IntEnum): + Sha = Qt.ItemDataRole.UserRole + 1 + Filename = auto() + Status = auto() + Additions = auto() + Deletions = auto() + Changes = auto() + Patch = auto() + + def __init__(self): + super().__init__() + self._pull_request = None + + pullRequestChanged = Signal() + + def get_pull_request(self) -> Optional[PullRequest]: + return self._pull_request + + def set_pull_request(self, pull_request: Optional[PullRequest]) -> None: + if pull_request is None: + return + + if self._pull_request is not None and self._pull_request == pull_request: + return + + self.beginResetModel() + self._pull_request = pull_request + self._pull_request.filesLoaded.connect(self._reset_model) + self.endResetModel() + self.pullRequestChanged.emit() + + pullRequest = Property(PullRequest, fget=get_pull_request, fset=set_pull_request, + notify=pullRequestChanged) + + def _reset_model(self) -> None: + self.beginResetModel() + self.endResetModel() + + def data(self, + index: QModelIndex | QPersistentModelIndex, + role: int = Qt.ItemDataRole.DisplayRole) -> object: + if self._pull_request is None: + return None + + file: ChangedFile = self._pull_request.files[index.row()] + + if role == self.Roles.Sha: + return file.sha + if role == self.Roles.Filename: + return file.filename + if role == self.Roles.Additions: + return file.additions + if role == self.Roles.Deletions: + return file.deletions + if role == self.Roles.Changes: + return file.changes + if role == self.Roles.Patch: + return file.patch + if role == Qt.ItemDataRole.DisplayRole: + return file.filename + return None + + def rowCount(self, parent: QModelIndex | QPersistentModelIndex = QModelIndex()) -> int: + if self._pull_request is None: + return 0 + return len(self._pull_request.files) + + def roleNames(self) -> dict[int, QByteArray]: + return { + self.Roles.Sha: QByteArray(b'sha'), + self.Roles.Filename: QByteArray(b'filename'), + self.Roles.Additions: QByteArray(b'additions'), + self.Roles.Deletions: QByteArray(b'deletions'), + self.Roles.Changes: QByteArray(b'changes'), + self.Roles.Patch: QByteArray(b'patch'), + } |