From 78b97ce8fb9213c80be379984c1930f3b3c42aff Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 21 May 2022 22:10:25 +0200 Subject: [PATCH] Remove name as a required parameter for new root folders since no part of the GUI uses it --- novelwriter/core/project.py | 42 +++++++++---------- novelwriter/gui/projtree.py | 5 +-- .../coreProject_NewRoot_nwProject.nwx | 12 +++--- tests/test_core/test_core_index.py | 2 +- tests/test_core/test_core_project.py | 16 +++---- tests/tools.py | 16 +++---- 6 files changed, 44 insertions(+), 49 deletions(-) diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index 0557ce6e..3341e578 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -120,32 +120,34 @@ class NWProject(): # Item Methods ## - def newRoot(self, rootName, rootClass): - """Add a new root item. + def newRoot(self, itemClass, label=None): + """Add a new root item. If label is None, use the class label. """ + if label is None: + label = trConst(nwLabels.CLASS_NAME[itemClass]) newItem = NWItem(self) - newItem.setName(rootName) + newItem.setName(label) newItem.setType(nwItemType.ROOT) - newItem.setClass(rootClass) + newItem.setClass(itemClass) self.projTree.append(None, None, newItem) self.projTree.updateItemData(newItem.itemHandle) return newItem.itemHandle - def newFolder(self, folderName, pHandle): - """Add a new folder with a given name and parent item. + def newFolder(self, label, pHandle): + """Add a new folder with a given label and parent item. """ newItem = NWItem(self) - newItem.setName(folderName) + newItem.setName(label) newItem.setType(nwItemType.FOLDER) self.projTree.append(None, pHandle, newItem) self.projTree.updateItemData(newItem.itemHandle) return newItem.itemHandle - def newFile(self, fileName, pHandle): - """Add a new file with a given name and parent item. + def newFile(self, label, pHandle): + """Add a new file with a given label and parent item. """ newItem = NWItem(self) - newItem.setName(fileName) + newItem.setName(label) newItem.setType(nwItemType.FILE) self.projTree.append(None, pHandle, newItem) self.projTree.updateItemData(newItem.itemHandle) @@ -264,15 +266,13 @@ class NWProject(): self.setBookTitle(projTitle) self.setBookAuthors(projAuthors) - hNovelRoot = self.newRoot( - trConst(nwLabels.CLASS_NAME[nwItemClass.NOVEL]), nwItemClass.NOVEL - ) + hNovelRoot = self.newRoot(nwItemClass.NOVEL) + hTitlePage = self.newFile(self.tr("Title Page"), hNovelRoot) titlePage = "#! %s\n\n" % (self.bookTitle if self.bookTitle else self.projName) if self.bookAuthors: titlePage = "%s>> %s %s <<\n" % (titlePage, self.tr("By"), self.getAuthors()) - hTitlePage = self.newFile(self.tr("Title Page"), hNovelRoot) aDoc = NWDoc(self, hTitlePage) aDoc.writeDocument(titlePage) @@ -287,12 +287,10 @@ class NWProject(): aDoc = NWDoc(self, hScene) aDoc.writeDocument("### %s\n\n" % self.tr("New Scene")) - minClasses = [ - nwItemClass.PLOT, nwItemClass.CHARACTER, - nwItemClass.WORLD, nwItemClass.ARCHIVE - ] - for minClass in minClasses: - self.newRoot(trConst(nwLabels.CLASS_NAME[minClass]), minClass) + self.newRoot(nwItemClass.PLOT) + self.newRoot(nwItemClass.CHARACTER) + self.newRoot(nwItemClass.WORLD) + self.newRoot(nwItemClass.ARCHIVE) elif popCustom: # Create a project structure based on selected root folders @@ -340,7 +338,7 @@ class NWProject(): addNotes = projData.get("addNotes", False) for newRoot in projData.get("addRoots", []): if newRoot in nwItemClass: - rHandle = self.newRoot(trConst(nwLabels.CLASS_NAME[newRoot]), newRoot) + rHandle = self.newRoot(newRoot) if addNotes: aHandle = self.newFile(noteTitles[newRoot], rHandle) ntTag = simplified(noteTitles[newRoot]).replace(" ", "") @@ -348,7 +346,7 @@ class NWProject(): aDoc.writeDocument(f"# {noteTitles[newRoot]}\n\n@tag: {ntTag}\n\n") # Also add the archive and trash folders - self.newRoot(trConst(nwLabels.CLASS_NAME[nwItemClass.ARCHIVE]), nwItemClass.ARCHIVE) + self.newRoot(nwItemClass.ARCHIVE) self.trashFolder() # Finalise diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index 0339ec5f..5d40aeae 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -37,7 +37,6 @@ from PyQt5.QtWidgets import ( from novelwriter.core import NWDoc from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert -from novelwriter.constants import trConst, nwLabels logger = logging.getLogger(__name__) @@ -176,9 +175,7 @@ class GuiProjectTree(QTreeWidget): if itemType == nwItemType.ROOT and isinstance(itemClass, nwItemClass): - tHandle = self.theProject.newRoot( - trConst(nwLabels.CLASS_NAME[itemClass]), itemClass - ) + tHandle = self.theProject.newRoot(itemClass) elif itemType in (nwItemType.FILE, nwItemType.FOLDER): diff --git a/tests/reference/coreProject_NewRoot_nwProject.nwx b/tests/reference/coreProject_NewRoot_nwProject.nwx index 02930a4e..cd25c1cf 100644 --- a/tests/reference/coreProject_NewRoot_nwProject.nwx +++ b/tests/reference/coreProject_NewRoot_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project New Novel @@ -83,11 +83,11 @@ - Character + Characters - World + Locations @@ -95,15 +95,15 @@ - Object + Objects - Custom1 + Custom - Custom2 + Custom diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index c3e6ef47..93ec35c6 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -280,7 +280,7 @@ def testCoreIndex_ScanText(nwMinimal, mockGUI): assert theIndex.scanText(xHandle, "Hello World!") is False # Create the archive root - aHandle = theProject.newRoot("Archive", nwItemClass.ARCHIVE) + aHandle = theProject.newRoot(nwItemClass.ARCHIVE) assert theProject.projTree[aHandle] is not None xItem.setParent(aHandle) theProject.projTree.updateItemData(xItem.itemHandle) diff --git a/tests/test_core/test_core_project.py b/tests/test_core/test_core_project.py index 4f71b787..e6e5092d 100644 --- a/tests/test_core/test_core_project.py +++ b/tests/test_core/test_core_project.py @@ -259,14 +259,14 @@ def testCoreProject_NewRoot(fncDir, outDir, refDir, mockGUI, mockRnd): assert theProject.closeProject() is True assert theProject.openProject(projFile) is True - assert isinstance(theProject.newRoot("Novel", nwItemClass.NOVEL), str) - assert isinstance(theProject.newRoot("Plot", nwItemClass.PLOT), str) - assert isinstance(theProject.newRoot("Character", nwItemClass.CHARACTER), str) - assert isinstance(theProject.newRoot("World", nwItemClass.WORLD), str) - assert isinstance(theProject.newRoot("Timeline", nwItemClass.TIMELINE), str) - assert isinstance(theProject.newRoot("Object", nwItemClass.OBJECT), str) - assert isinstance(theProject.newRoot("Custom1", nwItemClass.CUSTOM), str) - assert isinstance(theProject.newRoot("Custom2", nwItemClass.CUSTOM), str) + assert isinstance(theProject.newRoot(nwItemClass.NOVEL), str) + assert isinstance(theProject.newRoot(nwItemClass.PLOT), str) + assert isinstance(theProject.newRoot(nwItemClass.CHARACTER), str) + assert isinstance(theProject.newRoot(nwItemClass.WORLD), str) + assert isinstance(theProject.newRoot(nwItemClass.TIMELINE), str) + assert isinstance(theProject.newRoot(nwItemClass.OBJECT), str) + assert isinstance(theProject.newRoot(nwItemClass.CUSTOM), str) + assert isinstance(theProject.newRoot(nwItemClass.CUSTOM), str) assert theProject.projChanged is True assert theProject.saveProject() is True diff --git a/tests/tools.py b/tests/tools.py index bfdb5140..fcdd3817 100644 --- a/tests/tools.py +++ b/tests/tools.py @@ -147,14 +147,14 @@ def buildTestProject(theObject, projPath): # Creating a minimal project with a few root folders and a # single chapter folder with a single file. xHandle = {} - xHandle[1] = theProject.newRoot(theProject.tr("Novel"), nwItemClass.NOVEL) - xHandle[2] = theProject.newRoot(theProject.tr("Plot"), nwItemClass.PLOT) - xHandle[3] = theProject.newRoot(theProject.tr("Characters"), nwItemClass.CHARACTER) - xHandle[4] = theProject.newRoot(theProject.tr("World"), nwItemClass.WORLD) - xHandle[5] = theProject.newFile(theProject.tr("Title Page"), xHandle[1]) - xHandle[6] = theProject.newFolder(theProject.tr("New Chapter"), xHandle[1]) - xHandle[7] = theProject.newFile(theProject.tr("New Chapter"), xHandle[6]) - xHandle[8] = theProject.newFile(theProject.tr("New Scene"), xHandle[6]) + xHandle[1] = theProject.newRoot(nwItemClass.NOVEL, "Novel") + xHandle[2] = theProject.newRoot(nwItemClass.PLOT, "Plot") + xHandle[3] = theProject.newRoot(nwItemClass.CHARACTER, "Characters") + xHandle[4] = theProject.newRoot(nwItemClass.WORLD, "World") + xHandle[5] = theProject.newFile("Title Page", xHandle[1]) + xHandle[6] = theProject.newFolder("New Chapter", xHandle[1]) + xHandle[7] = theProject.newFile("New Chapter", xHandle[6]) + xHandle[8] = theProject.newFile("New Scene", xHandle[6]) aDoc = NWDoc(theProject, xHandle[5]) aDoc.writeDocument("#! New Novel\n\n>> By Jane DOe <<\n")