summaryrefslogtreecommitdiff
path: root/kodereviewer/pull_request_model.py
blob: e08bf6e7305c33ff8cd4cca6f0cb7913ad0e9224 (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
import json
from os import path
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

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

@QmlElement
class PullRequestModel(QAbstractListModel):

    _project: Optional[Project]

    NumberRole = Qt.ItemDataRole.UserRole + 1
    TitleRole = NumberRole + 1

    def __init__(self):
        super().__init__()

    def data(self,
             index: QModelIndex | QPersistentModelIndex,
             role: int = Qt.ItemDataRole.DisplayRole) -> object:
        if self._project is None:
            return None

        pull_request = self._project.pullRequests[index.row()]
        if role == self.NumberRole:
            return pull_request.number
        if role == self.TitleRole:
            return pull_request.title
        if role == Qt.ItemDataRole.DisplayRole:
            return f'{pull_request.number} - {pull_request.title}'
        return None

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

    def roleNames(self) -> dict[int, QByteArray]:
        return {
            self.NumberRole: QByteArray(b"number"),
            self.TitleRole: QByteArray(b"title"),
        }

    def get_project(self) -> Optional[Project]:
        return self._project

    def set_project(self, project: Optional[Project])-> None:
        if project is None:
            return
        self._project = project
        print("Connecting!")
        self._project.pullRequestChanged.connect(self._reset_model)

    project = Property(Project, fget=get_project, fset=set_project)

    def _reset_model(self) -> None:
        print("Reseting pull request model")
        self.beginResetModel()
        self.endResetModel()