Add a basic project tree model

This commit is contained in:
Veronica Berglyd Olsen
2024-11-17 19:25:55 +01:00
parent 7a9da5f219
commit cbe557507f
4 changed files with 271 additions and 2 deletions
+30 -1
View File
@@ -33,6 +33,7 @@ from typing import TYPE_CHECKING, Literal, overload
from novelwriter.common import isHandle
from novelwriter.constants import nwFiles
from novelwriter.core.item import NWItem
from novelwriter.core.itemmodel import ProjectModel, ProjectNode
from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType
from novelwriter.error import logException
@@ -62,7 +63,7 @@ class NWTree:
also used for file names.
"""
__slots__ = ("_project", "_tree", "_order", "_roots", "_trash", "_changed")
__slots__ = ("_project", "_tree", "_order", "_roots", "_model", "_nodes", "_trash", "_changed")
def __init__(self, project: NWProject) -> None:
@@ -72,6 +73,9 @@ class NWTree:
self._order: list[str] = [] # The order of the tree items in the tree view
self._roots: dict[str, NWItem] = {} # The root items of the tree
self._model = ProjectModel(self)
self._nodes: dict[str, ProjectNode] = {}
self._trash = None # The handle of the trash root folder
self._changed = False # True if tree structure has changed
@@ -86,6 +90,10 @@ class NWTree:
"""Return the handle of the trash folder, or None."""
return self._trash
@property
def model(self) -> ProjectModel:
return self._model
##
# Class Methods
##
@@ -196,6 +204,27 @@ class NWTree:
nwItem.saveInitialCount()
return
def buildModel(self) -> None:
""""""
root = ProjectNode(NWItem(self._project, ""))
for item in self._tree.values():
node = ProjectNode(item)
self._nodes[item.itemHandle] = node
if pHandle := item.itemParent:
if parent := self._nodes.get(pHandle):
parent.addChild(node)
else:
logger.error("Could not add item '%s'", item.itemHandle)
else:
root.addChild(node)
self._model.beginInsertRows(self._model.index(0, 0), 0, 0)
self._model.setRoot(root)
self._model.endInsertRows()
self._model.layoutChanged.emit()
return
def checkConsistency(self, prefix: str) -> tuple[int, int]:
"""Check the project tree consistency. Also check the content
folder and add back files that were discovered but were not