Skip to content

Commit

Permalink
Add test coverage of the gui project tree
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo committed Nov 24, 2024
1 parent 32d899d commit 0449cf8
Show file tree
Hide file tree
Showing 6 changed files with 905 additions and 960 deletions.
8 changes: 8 additions & 0 deletions novelwriter/core/itemmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ def moveChild(self, source: int, target: int) -> None:
self._refreshChildrenPos()
return

def setExpanded(self, state: bool) -> None:
"""Set the node's expanded state."""
if state and self._children:
self._item.setExpanded(True)
else:
self._item.setExpanded(False)
return

##
# Internal Functions
##
Expand Down
50 changes: 29 additions & 21 deletions novelwriter/gui/projtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,22 @@ def __init__(self, parent: QWidget) -> None:
self.keyGoPrev = QShortcut(self.projTree)
self.keyGoPrev.setKey("Alt+Up")
self.keyGoPrev.setContext(Qt.ShortcutContext.WidgetShortcut)
self.keyGoPrev.activated.connect(self.projTree.moveSiblingUp)
self.keyGoPrev.activated.connect(self.projTree.goToSiblingUp)

self.keyGoNext = QShortcut(self.projTree)
self.keyGoNext.setKey("Alt+Down")
self.keyGoNext.setContext(Qt.ShortcutContext.WidgetShortcut)
self.keyGoNext.activated.connect(self.projTree.moveSiblingDown)
self.keyGoNext.activated.connect(self.projTree.goToSiblingDown)

self.keyGoUp = QShortcut(self.projTree)
self.keyGoUp.setKey("Alt+Left")
self.keyGoUp.setContext(Qt.ShortcutContext.WidgetShortcut)
self.keyGoUp.activated.connect(self.projTree.moveToParent)
self.keyGoUp.activated.connect(self.projTree.goToParent)

self.keyGoDown = QShortcut(self.projTree)
self.keyGoDown.setKey("Alt+Right")
self.keyGoDown.setContext(Qt.ShortcutContext.WidgetShortcut)
self.keyGoDown.activated.connect(self.projTree.moveToFirstChild)
self.keyGoDown.activated.connect(self.projTree.goToFirstChild)

self.keyContext = QShortcut(self.projTree)
self.keyContext.setKey("Ctrl+.")
Expand Down Expand Up @@ -415,8 +415,6 @@ def processTemplateDocuments(self, tHandle: str, change: nwChange) -> None:
self.mTemplates.addUpdate(tHandle, item.itemName, item.getMainIcon())
elif tHandle in self.mTemplates:
self.mTemplates.remove(tHandle)
elif change == nwChange.DELETE and tHandle in self.mTemplates:
self.mTemplates.remove(tHandle)
return

##
Expand Down Expand Up @@ -680,7 +678,8 @@ def newTreeItem(
tHandle = SHARED.project.newFolder(newLabel, sHandle, pos)

# Select the new item automatically
self.setSelectedHandle(tHandle)
if tHandle:
self.setSelectedHandle(tHandle)

return

Expand Down Expand Up @@ -793,6 +792,7 @@ def duplicateFromHandle(self, tHandle: str) -> None:
dHandles = docDup.duplicate(itemTree)
if len(dHandles) != len(itemTree):
SHARED.warn(self.tr("Could not duplicate all items."))
self.restoreExpandedState()
return

##
Expand All @@ -807,7 +807,7 @@ def mousePressEvent(self, event: QMouseEvent) -> None:
super().mousePressEvent(event)
if event.button() == QtMouseLeft:
if not self.indexAt(event.pos()).isValid():
self.selectionModel().clearCurrentIndex()
self._clearSelection()
elif event.button() == QtMouseMiddle:
if (node := self._getNode(self.indexAt(event.pos()))) and node.item.isFileType():
self.projView.openDocumentRequest.emit(
Expand Down Expand Up @@ -841,23 +841,23 @@ def moveItemDown(self) -> None:
return

@pyqtSlot()
def moveSiblingUp(self) -> None:
def goToSiblingUp(self) -> None:
"""Skip to the previous sibling."""
if (node := self._getNode(self.currentIndex())) and (parent := node.parent()):
if (move := parent.child(node.row() - 1)) and (model := self._getModel()):
self.setCurrentIndex(model.indexFromNode(move))
return

@pyqtSlot()
def moveSiblingDown(self) -> None:
def goToSiblingDown(self) -> None:
"""Skip to the next sibling."""
if (node := self._getNode(self.currentIndex())) and (parent := node.parent()):
if (move := parent.child(node.row() + 1)) and (model := self._getModel()):
self.setCurrentIndex(model.indexFromNode(move))
return

@pyqtSlot()
def moveToParent(self) -> None:
def goToParent(self) -> None:
"""Move to parent item."""
if (
(model := self._getModel())
Expand All @@ -868,7 +868,7 @@ def moveToParent(self) -> None:
return

@pyqtSlot()
def moveToFirstChild(self) -> None:
def goToFirstChild(self) -> None:
"""Move to first child item."""
if (
(model := self._getModel())
Expand All @@ -893,7 +893,9 @@ def collapseFromIndex(self, index: QModelIndex) -> None:
return

@pyqtSlot()
def processDeleteRequest(self, handles: list[str] = [], askFirst: bool = True) -> None:
def processDeleteRequest(
self, handles: list[str] | None = None, askFirst: bool = True
) -> None:
"""Move selected items to Trash."""
if handles and (model := self._getModel()):
indices = [model.indexFromHandle(handle) for handle in handles]
Expand Down Expand Up @@ -1002,20 +1004,26 @@ def _onDoubleClick(self, index: QModelIndex) -> None:
def _onNodeCollapsed(self, index: QModelIndex) -> None:
"""Capture a node collapse, and pass it to the model."""
if node := self._getNode(index):
node.item.setExpanded(False)
node.setExpanded(False)
return

@pyqtSlot(QModelIndex)
def _onNodeExpanded(self, index: QModelIndex) -> None:
"""Capture a node expand, and pass it to the model."""
if node := self._getNode(index):
node.item.setExpanded(True)
node.setExpanded(True)
return

##
# Internal Functions
##

def _clearSelection(self) -> None:
"""Clear the currently selected items."""
self.clearSelection()
self.selectionModel().clearCurrentIndex()
return

def _selectedRows(self) -> list[QModelIndex]:
"""Return all column 0 indexes."""
return [i for i in self.selectedIndexes() if i.column() == 0]
Expand Down Expand Up @@ -1162,9 +1170,8 @@ def buildSingleSelectMenu(self) -> None:
# Process Item
if self._children:
self._expandCollapse()
if isFile:
action = self.addAction(self.tr("Duplicate"))
action.triggered.connect(qtLambda(self._tree.duplicateFromHandle, self._handle))
action = self.addAction(self.tr("Duplicate"))
action.triggered.connect(qtLambda(self._tree.duplicateFromHandle, self._handle))
self._deleteOrTrash()

return
Expand Down Expand Up @@ -1321,9 +1328,10 @@ def _expandCollapse(self) -> None:

def _deleteOrTrash(self) -> None:
"""Add move to Trash action."""
if self._model.trashSelection(self._indices):
text = self.tr("Delete Permanently")
elif len(self._indices) == 1 and self._item.isRootType():
if (
self._model.trashSelection(self._indices)
or len(self._indices) == 1 and self._item.isRootType()
):
text = self.tr("Delete Permanently")
else:
text = self.tr("Move to Trash")
Expand Down
3 changes: 1 addition & 2 deletions novelwriter/tools/manussettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,7 @@ def _populateTree(self) -> None:
logger.debug("Building project tree")
self._treeMap = {}
self.optTree.clear()
for node in SHARED.project.tree.model.root.allChildren():
nwItem = node.item
for nwItem in SHARED.project.tree:
tHandle = nwItem.itemHandle
pHandle = nwItem.itemParent
rHandle = nwItem.itemRoot
Expand Down
20 changes: 10 additions & 10 deletions tests/reference/guiEditor_Main_Final_nwProject.nwx
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,47 @@
<name status="s000000" import="i000004">Novel</name>
</item>
<item handle="000000000000c" parent="0000000000008" root="0000000000008" order="0" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="yes" heading="H1" charCount="20" wordCount="5" paraCount="1" cursorPos="0" />
<meta expanded="no" heading="H1" charCount="20" wordCount="5" paraCount="1" cursorPos="0" />
<name status="s000000" import="i000004" active="yes">Title Page</name>
</item>
<item handle="000000000000d" parent="0000000000008" root="0000000000008" order="1" type="FOLDER" class="NOVEL">
<meta expanded="yes" />
<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" />
<meta expanded="no" heading="H2" charCount="11" wordCount="2" paraCount="0" cursorPos="0" />
<name status="s000000" import="i000004" active="yes">New Chapter</name>
</item>
<item handle="000000000000f" parent="000000000000d" root="0000000000008" order="1" type="FILE" class="NOVEL" layout="DOCUMENT">
<meta expanded="yes" heading="H1" charCount="1003" wordCount="172" paraCount="17" cursorPos="1259" />
<meta expanded="no" heading="H1" charCount="1003" wordCount="172" paraCount="17" cursorPos="1259" />
<name status="s000000" import="i000004" active="yes">New Scene</name>
</item>
<item handle="0000000000009" parent="None" root="0000000000009" order="1" type="ROOT" class="PLOT">
<meta expanded="yes" />
<meta expanded="no" />
<name status="s000000" import="i000004">Plot</name>
</item>
<item handle="0000000000012" parent="0000000000009" root="0000000000009" order="0" type="FILE" class="PLOT" layout="NOTE">
<meta expanded="yes" heading="H1" charCount="48" wordCount="10" paraCount="1" cursorPos="76" />
<meta expanded="no" heading="H1" charCount="48" wordCount="10" paraCount="1" cursorPos="76" />
<name status="s000000" import="i000004" active="yes">New Note</name>
</item>
<item handle="000000000000a" parent="None" root="000000000000a" order="2" type="ROOT" class="CHARACTER">
<meta expanded="yes" />
<meta expanded="no" />
<name status="s000000" import="i000004">Characters</name>
</item>
<item handle="0000000000011" parent="000000000000a" root="000000000000a" order="0" type="FILE" class="CHARACTER" layout="NOTE">
<meta expanded="yes" heading="H1" charCount="34" wordCount="8" paraCount="1" cursorPos="51" />
<meta expanded="no" heading="H1" charCount="34" wordCount="8" paraCount="1" cursorPos="51" />
<name status="s000000" import="i000004" active="yes">New Note</name>
</item>
<item handle="000000000000b" parent="None" root="000000000000b" order="3" type="ROOT" class="WORLD">
<meta expanded="yes" />
<meta expanded="no" />
<name status="s000000" import="i000004">Locations</name>
</item>
<item handle="0000000000013" parent="000000000000b" root="000000000000b" order="0" type="FILE" class="WORLD" layout="NOTE">
<meta expanded="yes" heading="H1" charCount="51" wordCount="9" paraCount="1" cursorPos="68" />
<meta expanded="no" heading="H1" charCount="51" wordCount="9" paraCount="1" cursorPos="68" />
<name status="s000000" import="i000004" active="yes">New Note</name>
</item>
<item handle="0000000000010" parent="None" root="0000000000010" order="4" type="ROOT" class="TRASH">
<meta expanded="yes" />
<meta expanded="no" />
<name status="s000000" import="i000004">Trash</name>
</item>
</content>
Expand Down
10 changes: 10 additions & 0 deletions tests/test_core/test_core_itemmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ def testCoreItemModel_ProjectNode_Children(mockGUI, mockRnd, fncPath):
assert child010.parent() is child01
assert child011.parent() is child01

# Expand
child0.setExpanded(True)
child1.setExpanded(True)
child2.setExpanded(True)
child3.setExpanded(True)
assert child0.item.isExpanded is True # Only one with children
assert child1.item.isExpanded is False
assert child2.item.isExpanded is False
assert child3.item.isExpanded is False


@pytest.mark.core
def testCoreItemModel_ProjectNode_Modify(mockGUI, mockRnd, fncPath):
Expand Down
Loading

0 comments on commit 0449cf8

Please sign in to comment.