From e9c7736ff83b9003eba960edebd2653bd5a4021f Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Fri, 4 Oct 2024 18:30:17 -0300 Subject: Fix a lot of warnings and add file view --- kodereviewer/models/__init__.py | 2 + kodereviewer/models/file.py | 95 +++++++++++++++++++++++++++++++++++++++ kodereviewer/models/line_model.py | 23 ++++++---- 3 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 kodereviewer/models/file.py (limited to 'kodereviewer/models') diff --git a/kodereviewer/models/__init__.py b/kodereviewer/models/__init__.py index b566981..b9082d1 100644 --- a/kodereviewer/models/__init__.py +++ b/kodereviewer/models/__init__.py @@ -1,4 +1,5 @@ from kodereviewer.models.comments import CommentModel +from kodereviewer.models.file import FileModel from kodereviewer.models.line_model import LineModel from kodereviewer.models.project import ProjectModel from kodereviewer.models.pull_request import PullRequestModel @@ -6,6 +7,7 @@ from kodereviewer.models.label import LabelModel __all__ = [ 'CommentModel', + 'FileModel', 'LabelModel' 'LineModel', 'ProjectModel', diff --git a/kodereviewer/models/file.py b/kodereviewer/models/file.py new file mode 100644 index 0000000..246bd5b --- /dev/null +++ b/kodereviewer/models/file.py @@ -0,0 +1,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'), + } diff --git a/kodereviewer/models/line_model.py b/kodereviewer/models/line_model.py index 1814829..03d14c6 100644 --- a/kodereviewer/models/line_model.py +++ b/kodereviewer/models/line_model.py @@ -1,7 +1,9 @@ """Model used for line numbers in Editor.qml""" +import logging from enum import auto, IntEnum from typing import Optional +import rich from PySide6.QtCore import QAbstractListModel, QByteArray, QModelIndex, QObject, QPersistentModelIndex, QStandardPaths, QUrl, Qt, Signal, Slot, Property from PySide6.QtQuick import QQuickTextDocument from PySide6.QtQml import QmlElement @@ -9,6 +11,8 @@ from PySide6.QtQml import QmlElement QML_IMPORT_NAME = "org.deprecated.kodereviewer" QML_IMPORT_MAJOR_VERSION = 1 +logger = logging.getLogger(__name__) + @QmlElement class LineModel(QAbstractListModel): @@ -27,15 +31,17 @@ class LineModel(QAbstractListModel): return self._document def set_document(self, document): - print('setting document') + logger.debug('setting document') if document == self._document: + logger.debug(f'Document {document} == {self._document}') return + logger.debug(f'Setting document {document}') self._document = document self.documentChanged.emit() self.resetModel() documentChanged = Signal() - document = Property(QQuickTextDocument, fget=get_document, fset=set_document, + document = Property(QObject, fget=get_document, fset=set_document, notify=documentChanged) def data(self, @@ -43,29 +49,28 @@ class LineModel(QAbstractListModel): role: int = Qt.ItemDataRole.DisplayRole) -> object: if not index.isValid(): - print('index not valid') + logger.debug('index not valid') return if self._document is None: - print('document none') + logger.debug('document none') return row = index.row() if row < 0 or row > self.rowCount(): - print(f'row: {row}') + logger.debug(f'row: {row}') return if role == self.Roles.LineHeight: text_doc = self._document.textDocument() return int(text_doc.documentLayout().blockBoundingRect(text_doc.findBlockByNumber(row)).height()) - print('found role ?') + logger.debug('found role ?') def rowCount(self, parent: QModelIndex | QPersistentModelIndex = QModelIndex()) -> int: if self._document is None: - print('document is none: row count 0') + logger.debug('rowCount: _document is None') return 0 - print(f'returning {self._document.textDocument().blockCount()}') return self._document.textDocument().blockCount(); def roleNames(self) -> dict[int, QByteArray]: @@ -76,6 +81,6 @@ class LineModel(QAbstractListModel): @Slot() def resetModel(self) -> None: - print('reseting model?') + logger.debug('reseting model?') self.beginResetModel() self.endResetModel() -- cgit v1.2.3-70-g09d2