From 0aaf42de21f9e5f01d8a7c3797dd320eeddc5809 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 8 Feb 2024 21:49:07 +0100 Subject: [PATCH] Connect the bits for the file template feature --- novelwriter/gui/projtree.py | 29 +++++++++---------- sample/content/2a60782759c6f.nwd | 11 +++++-- sample/nwProject.nwx | 10 +++---- .../coreProject_NewFileFolder_nwProject.nwx | 10 +++++-- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index e7516ab3..08291baf 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -135,6 +135,7 @@ class GuiProjectView(QWidget): # Signals self.selectedItemChanged.connect(self.projBar.treeSelectionChanged) self.projTree.itemRefreshed.connect(self.projBar.treeItemRefreshed) + self.projBar.newDocumentFromTemplate.connect(self.createFileFromTemplate) # Function Mappings self.emptyTrash = self.projTree.emptyTrash @@ -215,10 +216,17 @@ class GuiProjectView(QWidget): @pyqtSlot(str) def updateItemValues(self, tHandle: str) -> None: - """Update tree item""" + """Update tree item.""" self.projTree.setTreeItemValues(tHandle) return + @pyqtSlot(str) + def createFileFromTemplate(self, tHandle: str) -> None: + """Create a new document from a template.""" + logger.debug("Template selected: '%s'", tHandle) + self.projTree.newTreeItem(nwItemType.FILE, copyDoc=tHandle) + return + @pyqtSlot(str, int, int, int) def updateCounts(self, tHandle: str, cCount: int, wCount: int, pCount: int) -> None: """Slot for updating the word count of a specific item.""" @@ -310,7 +318,7 @@ class GuiProjectToolBar(QWidget): self.mTemplates = _UpdatableMenu(self.mAdd) self.mTemplates.setActionsVisible(False) - self.mTemplates.menuItemTriggered.connect(self._templateSelected) + self.mTemplates.menuItemTriggered.connect(lambda h: self.newDocumentFromTemplate.emit(h)) self.mAdd.addMenu(self.mTemplates) self.mAddRoot = self.mAdd.addMenu(trConst(nwLabels.ITEM_DESCRIPTION["root"])) @@ -443,17 +451,6 @@ class GuiProjectToolBar(QWidget): self.aAddScene.setVisible(allowDoc) return - ## - # Private Slots - ## - - @pyqtSlot(str) - def _templateSelected(self, tHandle: str) -> None: - """Forward the template menu signal.""" - logger.debug("Template selected: '%s'", tHandle) - self.newDocumentFromTemplate.emit(tHandle) - return - ## # Internal Functions ## @@ -622,7 +619,7 @@ class GuiProjectTree(QTreeWidget): return False def newTreeItem(self, itemType: nwItemType, itemClass: nwItemClass | None = None, - hLevel: int = 1, isNote: bool = False) -> bool: + hLevel: int = 1, isNote: bool = False, copyDoc: str | None = None) -> bool: """Add new item to the tree, with a given itemType (and itemClass if Root), and attach it to the selected handle. Also make sure the item is added in a place it can be added, and that @@ -710,7 +707,9 @@ class GuiProjectTree(QTreeWidget): return True # Handle new file creation - if itemType == nwItemType.FILE and hLevel > 0: + if itemType == nwItemType.FILE and copyDoc: + SHARED.project.copyFileContent(tHandle, copyDoc) + elif itemType == nwItemType.FILE and hLevel > 0: SHARED.project.writeNewFile(tHandle, hLevel, not isNote) # Add the new item to the project tree diff --git a/sample/content/2a60782759c6f.nwd b/sample/content/2a60782759c6f.nwd index 677e68d9..ac23227a 100644 --- a/sample/content/2a60782759c6f.nwd +++ b/sample/content/2a60782759c6f.nwd @@ -1,7 +1,12 @@ %%~name: Chapter %%~path: f4ed1ae756a1f/2a60782759c6f -%%~kind: TEMPLATE/NOTE -%%~hash: d09e26b73443ca5592973b954fb8d34b406765cc -%%~date: 2024-02-04 12:13:08/2024-02-08 00:02:03 +%%~kind: TEMPLATE/DOCUMENT +%%~hash: da17bc023f9fcf9d739f19ce4b49a4f95dd7acc2 +%%~date: 2024-02-04 12:13:08/2024-02-08 21:45:26 ## Chapter +@pov: Name + +%Synopsis: A brief note about the chapter content. + +Text diff --git a/sample/nwProject.nwx b/sample/nwProject.nwx index d3d9cdb5..8f980bad 100644 --- a/sample/nwProject.nwx +++ b/sample/nwProject.nwx @@ -1,6 +1,6 @@ - - + + Sample Project Jane Smith @@ -35,7 +35,7 @@ Main - + Novel @@ -141,11 +141,11 @@ Templates - + Scene - + Chapter diff --git a/tests/reference/coreProject_NewFileFolder_nwProject.nwx b/tests/reference/coreProject_NewFileFolder_nwProject.nwx index 99b5a57c..2422fc02 100644 --- a/tests/reference/coreProject_NewFileFolder_nwProject.nwx +++ b/tests/reference/coreProject_NewFileFolder_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project Jane Doe @@ -22,13 +22,13 @@ Finished - New + New Minor Major Main - + Novel @@ -73,5 +73,9 @@ Jane + + + John +