summaryrefslogtreecommitdiff
path: root/kodereviewer/models/comments.py
blob: 0d92e235d3c24782d3ca3ed2016ba3093d56c2a5 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from enum import auto, IntEnum
from typing import Optional

from PySide6.QtCore import QAbstractListModel, QByteArray, QModelIndex, QObject, QPersistentModelIndex, QStandardPaths, QUrl, Qt, Signal, Slot, Property
from PySide6.QtQml import QmlElement

from kodereviewer.data import Comment, PullRequest

QML_IMPORT_NAME = "org.deprecated.kodereviewer"
QML_IMPORT_MAJOR_VERSION = 1


@QmlElement
class CommentModel(QAbstractListModel):

    _pull_request: Optional[PullRequest]

    class Roles(IntEnum):
        Body = Qt.ItemDataRole.UserRole + 1
        CreatedAt = auto()
        UpdatedAt = auto()
        Username = auto()
        AvatarUrl = auto()


    def __init__(self):
        super().__init__()
        self._pull_request = None

    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.commentsLoaded.connect(self._reset_model)
        self.endResetModel()
        self.pullRequestChanged.emit()

    pullRequestChanged = Signal()
    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

        comment: Comment = self._pull_request._comments[index.row()]

        if role == self.Roles.Body:
            return comment.body
        if role == self.Roles.CreatedAt:
            return comment.created_at.strftime("%Y-%M-%d %H:%m")
        if role == self.Roles.UpdatedAt:
            return comment.updated_at.strftime("%Y-%M-%d %H:%m")
        if role == self.Roles.Username:
            return comment.user.username
        if role == self.Roles.AvatarUrl:
            return comment.user.avatar_url
        if role == Qt.ItemDataRole.DisplayRole:
            return comment.body
        return None

    def rowCount(self, parent: QModelIndex | QPersistentModelIndex = QModelIndex()) -> int:
        if self._pull_request is None:
            return 0
        return len(self._pull_request._comments)

    def roleNames(self) -> dict[int, QByteArray]:
        return {
            self.Roles.Body: QByteArray(b'body'),
            self.Roles.CreatedAt: QByteArray(b'createdAt'),
            self.Roles.UpdatedAt: QByteArray(b'updatedAt'),
            self.Roles.Username: QByteArray(b'username'),
            self.Roles.AvatarUrl: QByteArray(b'avatarUrl'),
        }