From d349e5014fc60a8ae140a56c457c3f3258959582 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 21 Oct 2024 09:57:10 -0300 Subject: Adding something Add reviewer data --- kodereviewer/models/__init__.py | 2 ++ kodereviewer/models/reviewer.py | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 kodereviewer/models/reviewer.py (limited to 'kodereviewer/models') diff --git a/kodereviewer/models/__init__.py b/kodereviewer/models/__init__.py index ad675bf..810d403 100644 --- a/kodereviewer/models/__init__.py +++ b/kodereviewer/models/__init__.py @@ -4,6 +4,7 @@ from kodereviewer.models.line_model import LineModel from kodereviewer.models.project import ProjectModel from kodereviewer.models.pull_request import PullRequestModel from kodereviewer.models.label import LabelModel +from kodereviewer.models.reviewer import ReviewerModel __all__ = [ 'CommentModel', @@ -13,4 +14,5 @@ __all__ = [ 'LineModel', 'ProjectModel', 'PullRequestModel', + 'ReviewerModel', ] diff --git a/kodereviewer/models/reviewer.py b/kodereviewer/models/reviewer.py new file mode 100644 index 0000000..8e9c21a --- /dev/null +++ b/kodereviewer/models/reviewer.py @@ -0,0 +1,74 @@ +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 PullRequest, User + +QML_IMPORT_NAME = "org.deprecated.kodereviewer" +QML_IMPORT_MAJOR_VERSION = 1 + + +@QmlElement +class ReviewerModel(QAbstractListModel): + """Model to show pull request labels.""" + + _pull_request: Optional[PullRequest] + + class Roles(IntEnum): + Username = Qt.ItemDataRole.UserRole + 1 + Avatar = 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.endResetModel() + self.pullRequestChanged.emit() + + pullRequest = Property(PullRequest, fget=get_pull_request, fset=set_pull_request, + notify=pullRequestChanged) + + def data(self, + index: QModelIndex | QPersistentModelIndex, + role: int = Qt.ItemDataRole.DisplayRole) -> object: + if self._pull_request is None: + return None + + reviewer: User = self._pull_request.reviewers[index.row()] + + if role == self.Roles.Username: + return reviewer.username + if role == self.Roles.Avatar: + return reviewer.avatar_url + if role == Qt.ItemDataRole.DisplayRole: + return reviewer.username + return None + + def rowCount(self, parent: QModelIndex | QPersistentModelIndex = QModelIndex()) -> int: + if self._pull_request is None: + return 0 + return len(self._pull_request.reviewers) + + def roleNames(self) -> dict[int, QByteArray]: + return { + self.Roles.Username: QByteArray(b'username'), + self.Roles.Avatar: QByteArray(b'avatar'), + } -- cgit v1.2.3-70-g09d2