summaryrefslogtreecommitdiff
path: root/kodereviewer/models/line_model.py
diff options
context:
space:
mode:
Diffstat (limited to 'kodereviewer/models/line_model.py')
-rw-r--r--kodereviewer/models/line_model.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/kodereviewer/models/line_model.py b/kodereviewer/models/line_model.py
new file mode 100644
index 0000000..1814829
--- /dev/null
+++ b/kodereviewer/models/line_model.py
@@ -0,0 +1,81 @@
+"""Model used for line numbers in Editor.qml"""
+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.QtQuick import QQuickTextDocument
+from PySide6.QtQml import QmlElement
+
+QML_IMPORT_NAME = "org.deprecated.kodereviewer"
+QML_IMPORT_MAJOR_VERSION = 1
+
+
+@QmlElement
+class LineModel(QAbstractListModel):
+
+ _document: QQuickTextDocument | None
+
+ class Roles(IntEnum):
+ LineHeight = Qt.ItemDataRole.UserRole + 1
+
+ def __init__(self, *args, **kwargs):
+ self._document = None
+ super().__init__(*args, **kwargs)
+
+
+ def get_document(self):
+ return self._document
+
+ def set_document(self, document):
+ print('setting document')
+ if document == self._document:
+ return
+ self._document = document
+ self.documentChanged.emit()
+ self.resetModel()
+
+ documentChanged = Signal()
+ document = Property(QQuickTextDocument, fget=get_document, fset=set_document,
+ notify=documentChanged)
+
+ def data(self,
+ index: QModelIndex | QPersistentModelIndex,
+ role: int = Qt.ItemDataRole.DisplayRole) -> object:
+
+ if not index.isValid():
+ print('index not valid')
+ return
+
+ if self._document is None:
+ print('document none')
+ return
+
+ row = index.row()
+ if row < 0 or row > self.rowCount():
+ print(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 ?')
+
+
+ def rowCount(self, parent: QModelIndex | QPersistentModelIndex = QModelIndex()) -> int:
+ if self._document is None:
+ print('document is none: row count 0')
+ return 0
+ print(f'returning {self._document.textDocument().blockCount()}')
+ return self._document.textDocument().blockCount();
+
+ def roleNames(self) -> dict[int, QByteArray]:
+ return {
+ self.Roles.LineHeight: QByteArray(b'lineHeight'),
+ }
+
+
+ @Slot()
+ def resetModel(self) -> None:
+ print('reseting model?')
+ self.beginResetModel()
+ self.endResetModel()