summaryrefslogtreecommitdiff
path: root/kodereviewer/models/reviewer.py
blob: 8e9c21ac774b79a5b76baa54cd566a30f8a81a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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'),
        }