From bbe83e74b2c1335a0a726c2f993bb2d8c47ffedc Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 14 Oct 2024 09:54:47 -0300 Subject: Functioning context drawer --- kodereviewer/models/file.py | 75 +++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 30 deletions(-) (limited to 'kodereviewer/models') diff --git a/kodereviewer/models/file.py b/kodereviewer/models/file.py index 4f0d139..43fcbe4 100644 --- a/kodereviewer/models/file.py +++ b/kodereviewer/models/file.py @@ -22,8 +22,11 @@ logger = logging.getLogger(__name__) @dataclass class FileItem: - filename: str + path: str + icon: str + name: str parent: Self | None + patch: str | None = None children: list[Self] = field(default_factory=list) def append(self, child: Self): @@ -48,8 +51,11 @@ class FileItem: """Only the filename.""" return 1 + def is_file(self) -> bool: + return self.patch is not None + def __str__(self) -> str: - return f'{self.filename} | {id(self.parent)} | {[x.filename for x in self.children]}' + return f'{self.name} | {id(self.parent)} | {[x.name for x in self.children]}' @QmlElement @@ -60,18 +66,23 @@ class TreeFileModel(QAbstractItemModel): root_node: FileItem _pull_request: Optional[PullRequest] + class Roles(IntEnum): + Filename = Qt.ItemDataRole.UserRole + 1 + Path = auto() + IconName = auto() + Patch = auto() + IsFile = auto() + def __init__(self): super().__init__() - self.root_node = FileItem('./', None) + self.root_node = FileItem('./', '', './', parent=None) self._pull_request = None - def load_files(self, filenames: list[str]): - logger.info(f'Loading {filenames}') - self.filenames = filenames + def load_files(self, files: list[ChangedFile]): self.dir_mapping: dict[str, FileItem] = {} - root_node = FileItem('./', None) - for file in self.filenames: - p = Path(file) + root_node = FileItem('./', '', './', parent=None) + for file in files: + p = Path(file.filename) directories = p.parts[:-1] fname = p.name current_path = Path('') @@ -79,15 +90,13 @@ class TreeFileModel(QAbstractItemModel): for dir in directories: current_path = current_path / dir if str(current_path) not in self.dir_mapping: - logger.info(f'Creating {current_path}') self.dir_mapping[str(current_path)] = FileItem( - str(current_path), parent + path=str(current_path), name=current_path.name, icon='inode-directory-symbolic', parent=parent ) - logger.info(f'Appending to {parent}') parent.append(self.dir_mapping[str(current_path)]) parent = self.dir_mapping[str(current_path)] - file_item = FileItem(fname, parent) + file_item = FileItem(path=str(p), name=fname, icon='document-open-symbolic', patch=file.patch, parent=parent) parent.append(file_item) self.root_node = root_node logger.info(self.root_node) @@ -116,20 +125,19 @@ class TreeFileModel(QAbstractItemModel): def _reset_model(self) -> None: logger.info('reseting model') self.beginResetModel() - filenames = [f.filename for f in self._pull_request.files] - self.load_files(filenames) + self.load_files(self._pull_request.files) self.endResetModel() def index(self, row: int, column: int, - index: QModelIndex = QModelIndex()) -> QModelIndex: + parent: QModelIndex = QModelIndex()) -> QModelIndex: """Returns the index of the item in the model specified by the given row, column and parent index.""" - if not self.hasIndex(row, column, index): + if not self.hasIndex(row, column, parent): return QModelIndex() item: FileItem - if not index.isValid(): + if not parent.isValid(): item = self.root_node else: - item = index.internalPointer() + item = parent.internalPointer() if item.child(row): return self.createIndex(row, column, item.child(row)) @@ -166,25 +174,32 @@ class TreeFileModel(QAbstractItemModel): def columnCount(self, parent: QModelIndex = QModelIndex()) -> int: return 1 - FilenameRole = Qt.ItemDataRole.UserRole + 1 - def data(self, index: QModelIndex, role: int) -> Any: if not index.isValid(): return '' - filename = index.internalPointer().filename - name = Path(filename).name - logger.info(index) - logger.info(f'Role: {role} | {self.FilenameRole}') + item: FileItem = index.internalPointer() + if role == Qt.ItemDataRole.DisplayRole: - return name - if role == self.FilenameRole: - logger.info(name) - return name + return item.name + if role == self.Roles.Filename: + return item.name + if role == self.Roles.Path: + return item.path + if role == self.Roles.IconName: + return item.icon + if role == self.Roles.Patch: + return item.patch + if role == self.Roles.IsFile: + return item.is_file() return None def roleNames(self) -> dict[int, QByteArray]: return { - self.FilenameRole: QByteArray(b'filename'), + self.Roles.Filename: QByteArray(b'filename'), + self.Roles.IconName: QByteArray(b'iconName'), + self.Roles.Path: QByteArray(b'path'), + self.Roles.Patch: QByteArray(b'patch'), + self.Roles.IsFile: QByteArray(b'isFile'), } -- cgit v1.2.3-70-g09d2