summaryrefslogtreecommitdiff
path: root/kodereviewer/models/file.py
blob: 246bd5b0adb7494b78b36c654fe344a71dadcb74 (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
89
90
91
92
93
94
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'),
        }