Add test coverage of the gui project tree

This commit is contained in:
Veronica Berglyd Olsen
2024-11-24 18:39:23 +01:00
parent 32d899dbfb
commit 0449cf824d
6 changed files with 910 additions and 965 deletions
+8
View File
@@ -240,6 +240,14 @@ class ProjectNode:
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
##
+29 -21
View File
@@ -101,22 +101,22 @@ class GuiProjectView(QWidget):
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+.")
@@ -415,8 +415,6 @@ class GuiProjectToolBar(QWidget):
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
##
@@ -680,7 +678,8 @@ class GuiProjectTree(QTreeView):
tHandle = SHARED.project.newFolder(newLabel, sHandle, pos)
# Select the new item automatically
self.setSelectedHandle(tHandle)
if tHandle:
self.setSelectedHandle(tHandle)
return
@@ -793,6 +792,7 @@ class GuiProjectTree(QTreeView):
dHandles = docDup.duplicate(itemTree)
if len(dHandles) != len(itemTree):
SHARED.warn(self.tr("Could not duplicate all items."))
self.restoreExpandedState()
return
##
@@ -807,7 +807,7 @@ class GuiProjectTree(QTreeView):
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(
@@ -841,7 +841,7 @@ class GuiProjectTree(QTreeView):
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()):
@@ -849,7 +849,7 @@ class GuiProjectTree(QTreeView):
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()):
@@ -857,7 +857,7 @@ class GuiProjectTree(QTreeView):
return
@pyqtSlot()
def moveToParent(self) -> None:
def goToParent(self) -> None:
"""Move to parent item."""
if (
(model := self._getModel())
@@ -868,7 +868,7 @@ class GuiProjectTree(QTreeView):
return
@pyqtSlot()
def moveToFirstChild(self) -> None:
def goToFirstChild(self) -> None:
"""Move to first child item."""
if (
(model := self._getModel())
@@ -893,7 +893,9 @@ class GuiProjectTree(QTreeView):
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]
@@ -1002,20 +1004,26 @@ class GuiProjectTree(QTreeView):
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]
@@ -1162,9 +1170,8 @@ class _TreeContextMenu(QMenu):
# 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
@@ -1321,9 +1328,10 @@ class _TreeContextMenu(QMenu):
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")
+1 -2
View File
@@ -419,8 +419,7 @@ class _FilterTab(NFixedPage):
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
@@ -34,7 +34,7 @@
<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">
@@ -42,39 +42,39 @@
<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>
+10
View File
@@ -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):
File diff suppressed because it is too large Load Diff