Skip to content

Commit

Permalink
Add test coverage of the item model
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo committed Nov 23, 2024
1 parent be6888f commit 01b2b53
Show file tree
Hide file tree
Showing 11 changed files with 657 additions and 36 deletions.
31 changes: 14 additions & 17 deletions novelwriter/core/itemmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

logger = logging.getLogger(__name__)

INV_ROOT = "invisibleRoot"
C_FACTOR = 0x0100

C_LABEL_TEXT = 0x0000 | Qt.ItemDataRole.DisplayRole
Expand Down Expand Up @@ -244,7 +245,7 @@ def _updateRelationships(self, child: ProjectNode) -> None:
child.item.setParent(self._item.itemHandle)
child.item.setRoot(self._item.itemRoot)
child.item.setClassDefaults(self._item.itemClass)
child._flags |= Qt.ItemFlag.ItemIsDragEnabled
child._flags = NODE_FLAGS | Qt.ItemFlag.ItemIsDragEnabled
else:
child.item.setParent(None)
child.item.setRoot(child.item.itemHandle)
Expand All @@ -259,12 +260,12 @@ class ProjectModel(QAbstractItemModel):
def __init__(self, tree: NWTree) -> None:
super().__init__()
self._tree = tree
self._root = ProjectNode(NWItem(tree._project, "invisibleRoot"))
self._root = ProjectNode(NWItem(tree._project, INV_ROOT))
self._root.item.setName("Invisible Root")
logger.debug("Ready: ProjectModel")
return

def __del__(self) -> None:
def __del__(self) -> None: # pragma: no cover
logger.debug("Delete: ProjectModel")
return

Expand Down Expand Up @@ -390,10 +391,6 @@ def indexFromNode(self, node: ProjectNode, column: int = 0) -> QModelIndex:
"""Get the index representing a node in the model."""
return self.createIndex(node.row(), column, node)

def rootIndex(self) -> QModelIndex:
"""Get the index representing the root."""
return self.createIndex(0, 0, self._root)

##
# Model Edit
##
Expand Down Expand Up @@ -432,15 +429,6 @@ def internalMove(self, index: QModelIndex, step: int) -> None:
self.endMoveRows()
return

def trashSelection(self, indices: list[QModelIndex]) -> bool:
"""Check if a selection of indices are all in trash or not."""
for index in indices:
if index.isValid():
node: ProjectNode = index.internalPointer()
if node.item.itemClass != nwItemClass.TRASH:
return False
return True

def multiMove(self, indices: list[QModelIndex], target: QModelIndex, pos: int = -1) -> None:
"""Move multiple items to a new location."""
if target.isValid():
Expand All @@ -458,7 +446,7 @@ def multiMove(self, indices: list[QModelIndex], target: QModelIndex, pos: int =
if node.item.isRootType() is False and handle not in handles:
pruned.append(node)
handles.add(handle)
for node in pruned:
for node in (reversed(pruned) if pos >= 0 else pruned):
if node.item.itemParent not in handles:
index = self.indexFromNode(node)
if temp := self.removeChild(index.parent(), index.row()):
Expand All @@ -485,3 +473,12 @@ def allExpanded(self) -> list[QModelIndex]:
if node._item.isExpanded:
expanded.append(self.createIndex(node.row(), 0, node))
return expanded

def trashSelection(self, indices: list[QModelIndex]) -> bool:
"""Check if a selection of indices are all in trash or not."""
for index in indices:
if index.isValid():
node: ProjectNode = index.internalPointer()
if node.item.itemClass != nwItemClass.TRASH:
return False
return True
6 changes: 5 additions & 1 deletion novelwriter/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class NWProjectState(Enum):

class NWProject:

__slots__ = (
"_options", "_storage", "_data", "_tree", "_index", "_session",
"_langData", "_changed", "_valid", "_state", "tr",
)

def __init__(self) -> None:

# Core Elements
Expand Down Expand Up @@ -433,7 +438,6 @@ def closeProject(self, idleTime: float = 0.0) -> None:
self._tree.writeToCFile()
self._session.appendSession(idleTime)
self._storage.closeSession()
self._lockedBy = None
return

def backupProject(self, doNotify: bool) -> bool:
Expand Down
5 changes: 1 addition & 4 deletions novelwriter/core/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ class NWTree:
also used for file names.
"""

__slots__ = (
"_project", "_tree", "_order", "_roots",
"_model", "_items", "_nodes", "_trash", "_changed",
)
__slots__ = ("_project", "_model", "_items", "_nodes", "_trash")

def __init__(self, project: NWProject) -> None:
self._project = project
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/coreProject_NewFileFolder_nwProject.nwx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.6b1" hexVersion="0x020600b1" fileVersion="1.5" fileRevision="4" timeStamp="2024-11-23 18:40:02">
<novelWriterXML appVersion="2.6b1" hexVersion="0x020600b1" fileVersion="1.5" fileRevision="4" timeStamp="2024-11-23 23:32:55">
<project id="d0f3fe10-c6e6-4310-8bfd-181eb4224eed" saveCount="1" autoCount="1" editTime="0">
<name>New Project</name>
<author>Jane Doe</author>
Expand Down Expand Up @@ -39,7 +39,7 @@
</item>
<item handle="000000000000d" parent="0000000000008" root="0000000000008" order="1" type="FOLDER" class="NOVEL">
<meta expanded="no" />
<name status="s000000" import="i000004">New Chapter</name>
<name status="s000000" import="i000004">New Folder</name>
</item>
<item handle="000000000000e" parent="000000000000d" root="0000000000008" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="no" heading="H2" charCount="11" wordCount="2" paraCount="0" cursorPos="0" />
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/coreProject_NewRoot_nwProject.nwx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.6b1" hexVersion="0x020600b1" fileVersion="1.5" fileRevision="4" timeStamp="2024-11-23 18:44:06">
<novelWriterXML appVersion="2.6b1" hexVersion="0x020600b1" fileVersion="1.5" fileRevision="4" timeStamp="2024-11-23 23:32:55">
<project id="d0f3fe10-c6e6-4310-8bfd-181eb4224eed" saveCount="1" autoCount="1" editTime="0">
<name>New Project</name>
<author>Jane Doe</author>
Expand Down Expand Up @@ -39,7 +39,7 @@
</item>
<item handle="000000000000d" parent="0000000000008" root="0000000000008" order="1" type="FOLDER" class="NOVEL">
<meta expanded="no" />
<name status="s000000" import="i000004">New Chapter</name>
<name status="s000000" import="i000004">New Folder</name>
</item>
<item handle="000000000000e" parent="000000000000d" root="0000000000008" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="no" heading="H2" charCount="11" wordCount="2" paraCount="0" cursorPos="0" />
Expand Down
8 changes: 4 additions & 4 deletions tests/reference/coreTools_DocDuplicator_nwProject.nwx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.6b1" hexVersion="0x020600b1" fileVersion="1.5" fileRevision="4" timeStamp="2024-11-23 19:08:29">
<novelWriterXML appVersion="2.6b1" hexVersion="0x020600b1" fileVersion="1.5" fileRevision="4" timeStamp="2024-11-23 23:37:19">
<project id="d0f3fe10-c6e6-4310-8bfd-181eb4224eed" saveCount="1" autoCount="1" editTime="0">
<name>New Project</name>
<author>Jane Doe</author>
Expand Down Expand Up @@ -39,7 +39,7 @@
</item>
<item handle="000000000000d" parent="0000000000008" root="0000000000008" order="1" type="FOLDER" class="NOVEL">
<meta expanded="no" />
<name status="s000000" import="i000004">New Chapter</name>
<name status="s000000" import="i000004">New Folder</name>
</item>
<item handle="000000000000e" parent="000000000000d" root="0000000000008" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="no" heading="H2" charCount="11" wordCount="2" paraCount="0" cursorPos="0" />
Expand All @@ -63,7 +63,7 @@
</item>
<item handle="0000000000011" parent="0000000000008" root="0000000000008" order="2" type="FOLDER" class="NOVEL">
<meta expanded="no" />
<name status="s000000" import="i000004">New Chapter</name>
<name status="s000000" import="i000004">New Folder</name>
</item>
<item handle="0000000000012" parent="0000000000011" root="0000000000008" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="no" heading="H2" charCount="11" wordCount="2" paraCount="0" cursorPos="0" />
Expand All @@ -83,7 +83,7 @@
</item>
<item handle="0000000000016" parent="0000000000014" root="0000000000014" order="1" type="FOLDER" class="NOVEL">
<meta expanded="no" />
<name status="s000000" import="i000004">New Chapter</name>
<name status="s000000" import="i000004">New Folder</name>
</item>
<item handle="0000000000017" parent="0000000000016" root="0000000000014" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="no" heading="H2" charCount="11" wordCount="2" paraCount="0" cursorPos="0" />
Expand Down
6 changes: 3 additions & 3 deletions tests/reference/guiEditor_Main_Final_nwProject.nwx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.6b1" hexVersion="0x020600b1" fileVersion="1.5" fileRevision="4" timeStamp="2024-11-23 18:27:11">
<project id="d0f3fe10-c6e6-4310-8bfd-181eb4224eed" saveCount="3" autoCount="2" editTime="5">
<novelWriterXML appVersion="2.6b1" hexVersion="0x020600b1" fileVersion="1.5" fileRevision="4" timeStamp="2024-11-23 23:35:44">
<project id="d0f3fe10-c6e6-4310-8bfd-181eb4224eed" saveCount="3" autoCount="2" editTime="4">
<name>New Project</name>
<author>Jane Doe</author>
</project>
Expand Down Expand Up @@ -39,7 +39,7 @@
</item>
<item handle="000000000000d" parent="0000000000008" root="0000000000008" order="1" type="FOLDER" class="NOVEL">
<meta expanded="yes" />
<name status="s000000" import="i000004">New Chapter</name>
<name status="s000000" import="i000004">New Folder</name>
</item>
<item handle="000000000000e" parent="000000000000d" root="0000000000008" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="yes" heading="H2" charCount="11" wordCount="2" paraCount="0" cursorPos="0" />
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/guiEditor_Main_Initial_nwProject.nwx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</item>
<item handle="000000000000d" parent="0000000000008" root="0000000000008" order="1" type="FOLDER" class="NOVEL">
<meta expanded="no" />
<name status="s000000" import="i000004">New Chapter</name>
<name status="s000000" import="i000004">New Folder</name>
</item>
<item handle="000000000000e" parent="000000000000d" root="0000000000008" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="no" heading="H2" charCount="11" wordCount="2" paraCount="0" cursorPos="0" />
Expand Down
Loading

0 comments on commit 01b2b53

Please sign in to comment.