Remove name as a required parameter for new root folders since no part of the GUI uses it

This commit is contained in:
Veronica Berglyd Olsen
2022-05-21 22:10:25 +02:00
parent aeaaaaf3af
commit 78b97ce8fb
6 changed files with 44 additions and 49 deletions
+20 -22
View File
@@ -120,32 +120,34 @@ class NWProject():
# Item Methods # Item Methods
## ##
def newRoot(self, rootName, rootClass): def newRoot(self, itemClass, label=None):
"""Add a new root item. """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 = NWItem(self)
newItem.setName(rootName) newItem.setName(label)
newItem.setType(nwItemType.ROOT) newItem.setType(nwItemType.ROOT)
newItem.setClass(rootClass) newItem.setClass(itemClass)
self.projTree.append(None, None, newItem) self.projTree.append(None, None, newItem)
self.projTree.updateItemData(newItem.itemHandle) self.projTree.updateItemData(newItem.itemHandle)
return newItem.itemHandle return newItem.itemHandle
def newFolder(self, folderName, pHandle): def newFolder(self, label, pHandle):
"""Add a new folder with a given name and parent item. """Add a new folder with a given label and parent item.
""" """
newItem = NWItem(self) newItem = NWItem(self)
newItem.setName(folderName) newItem.setName(label)
newItem.setType(nwItemType.FOLDER) newItem.setType(nwItemType.FOLDER)
self.projTree.append(None, pHandle, newItem) self.projTree.append(None, pHandle, newItem)
self.projTree.updateItemData(newItem.itemHandle) self.projTree.updateItemData(newItem.itemHandle)
return newItem.itemHandle return newItem.itemHandle
def newFile(self, fileName, pHandle): def newFile(self, label, pHandle):
"""Add a new file with a given name and parent item. """Add a new file with a given label and parent item.
""" """
newItem = NWItem(self) newItem = NWItem(self)
newItem.setName(fileName) newItem.setName(label)
newItem.setType(nwItemType.FILE) newItem.setType(nwItemType.FILE)
self.projTree.append(None, pHandle, newItem) self.projTree.append(None, pHandle, newItem)
self.projTree.updateItemData(newItem.itemHandle) self.projTree.updateItemData(newItem.itemHandle)
@@ -264,15 +266,13 @@ class NWProject():
self.setBookTitle(projTitle) self.setBookTitle(projTitle)
self.setBookAuthors(projAuthors) self.setBookAuthors(projAuthors)
hNovelRoot = self.newRoot( hNovelRoot = self.newRoot(nwItemClass.NOVEL)
trConst(nwLabels.CLASS_NAME[nwItemClass.NOVEL]), nwItemClass.NOVEL hTitlePage = self.newFile(self.tr("Title Page"), hNovelRoot)
)
titlePage = "#! %s\n\n" % (self.bookTitle if self.bookTitle else self.projName) titlePage = "#! %s\n\n" % (self.bookTitle if self.bookTitle else self.projName)
if self.bookAuthors: if self.bookAuthors:
titlePage = "%s>> %s %s <<\n" % (titlePage, self.tr("By"), self.getAuthors()) titlePage = "%s>> %s %s <<\n" % (titlePage, self.tr("By"), self.getAuthors())
hTitlePage = self.newFile(self.tr("Title Page"), hNovelRoot)
aDoc = NWDoc(self, hTitlePage) aDoc = NWDoc(self, hTitlePage)
aDoc.writeDocument(titlePage) aDoc.writeDocument(titlePage)
@@ -287,12 +287,10 @@ class NWProject():
aDoc = NWDoc(self, hScene) aDoc = NWDoc(self, hScene)
aDoc.writeDocument("### %s\n\n" % self.tr("New Scene")) aDoc.writeDocument("### %s\n\n" % self.tr("New Scene"))
minClasses = [ self.newRoot(nwItemClass.PLOT)
nwItemClass.PLOT, nwItemClass.CHARACTER, self.newRoot(nwItemClass.CHARACTER)
nwItemClass.WORLD, nwItemClass.ARCHIVE self.newRoot(nwItemClass.WORLD)
] self.newRoot(nwItemClass.ARCHIVE)
for minClass in minClasses:
self.newRoot(trConst(nwLabels.CLASS_NAME[minClass]), minClass)
elif popCustom: elif popCustom:
# Create a project structure based on selected root folders # Create a project structure based on selected root folders
@@ -340,7 +338,7 @@ class NWProject():
addNotes = projData.get("addNotes", False) addNotes = projData.get("addNotes", False)
for newRoot in projData.get("addRoots", []): for newRoot in projData.get("addRoots", []):
if newRoot in nwItemClass: if newRoot in nwItemClass:
rHandle = self.newRoot(trConst(nwLabels.CLASS_NAME[newRoot]), newRoot) rHandle = self.newRoot(newRoot)
if addNotes: if addNotes:
aHandle = self.newFile(noteTitles[newRoot], rHandle) aHandle = self.newFile(noteTitles[newRoot], rHandle)
ntTag = simplified(noteTitles[newRoot]).replace(" ", "") ntTag = simplified(noteTitles[newRoot]).replace(" ", "")
@@ -348,7 +346,7 @@ class NWProject():
aDoc.writeDocument(f"# {noteTitles[newRoot]}\n\n@tag: {ntTag}\n\n") aDoc.writeDocument(f"# {noteTitles[newRoot]}\n\n@tag: {ntTag}\n\n")
# Also add the archive and trash folders # Also add the archive and trash folders
self.newRoot(trConst(nwLabels.CLASS_NAME[nwItemClass.ARCHIVE]), nwItemClass.ARCHIVE) self.newRoot(nwItemClass.ARCHIVE)
self.trashFolder() self.trashFolder()
# Finalise # Finalise
+1 -4
View File
@@ -37,7 +37,6 @@ from PyQt5.QtWidgets import (
from novelwriter.core import NWDoc from novelwriter.core import NWDoc
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
from novelwriter.constants import trConst, nwLabels
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -176,9 +175,7 @@ class GuiProjectTree(QTreeWidget):
if itemType == nwItemType.ROOT and isinstance(itemClass, nwItemClass): if itemType == nwItemType.ROOT and isinstance(itemClass, nwItemClass):
tHandle = self.theProject.newRoot( tHandle = self.theProject.newRoot(itemClass)
trConst(nwLabels.CLASS_NAME[itemClass]), itemClass
)
elif itemType in (nwItemType.FILE, nwItemType.FOLDER): elif itemType in (nwItemType.FILE, nwItemType.FOLDER):
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="1.7-beta1" hexVersion="0x010700b1" fileVersion="1.4" timeStamp="2022-05-21 17:07:33"> <novelWriterXML appVersion="1.7-beta1" hexVersion="0x010700b1" fileVersion="1.4" timeStamp="2022-05-21 22:07:28">
<project> <project>
<name>New Project</name> <name>New Project</name>
<title>New Novel</title> <title>New Novel</title>
@@ -83,11 +83,11 @@
</item> </item>
<item handle="000000000002a" parent="None" root="000000000002a" order="0" type="ROOT" class="CHARACTER"> <item handle="000000000002a" parent="None" root="000000000002a" order="0" type="ROOT" class="CHARACTER">
<meta expanded="False"/> <meta expanded="False"/>
<name status="s000008" import="i00000c">Character</name> <name status="s000008" import="i00000c">Characters</name>
</item> </item>
<item handle="000000000002b" parent="None" root="000000000002b" order="0" type="ROOT" class="WORLD"> <item handle="000000000002b" parent="None" root="000000000002b" order="0" type="ROOT" class="WORLD">
<meta expanded="False"/> <meta expanded="False"/>
<name status="s000008" import="i00000c">World</name> <name status="s000008" import="i00000c">Locations</name>
</item> </item>
<item handle="000000000002c" parent="None" root="000000000002c" order="0" type="ROOT" class="TIMELINE"> <item handle="000000000002c" parent="None" root="000000000002c" order="0" type="ROOT" class="TIMELINE">
<meta expanded="False"/> <meta expanded="False"/>
@@ -95,15 +95,15 @@
</item> </item>
<item handle="000000000002d" parent="None" root="000000000002d" order="0" type="ROOT" class="OBJECT"> <item handle="000000000002d" parent="None" root="000000000002d" order="0" type="ROOT" class="OBJECT">
<meta expanded="False"/> <meta expanded="False"/>
<name status="s000008" import="i00000c">Object</name> <name status="s000008" import="i00000c">Objects</name>
</item> </item>
<item handle="000000000002e" parent="None" root="000000000002e" order="0" type="ROOT" class="CUSTOM"> <item handle="000000000002e" parent="None" root="000000000002e" order="0" type="ROOT" class="CUSTOM">
<meta expanded="False"/> <meta expanded="False"/>
<name status="s000008" import="i00000c">Custom1</name> <name status="s000008" import="i00000c">Custom</name>
</item> </item>
<item handle="000000000002f" parent="None" root="000000000002f" order="0" type="ROOT" class="CUSTOM"> <item handle="000000000002f" parent="None" root="000000000002f" order="0" type="ROOT" class="CUSTOM">
<meta expanded="False"/> <meta expanded="False"/>
<name status="s000008" import="i00000c">Custom2</name> <name status="s000008" import="i00000c">Custom</name>
</item> </item>
</content> </content>
</novelWriterXML> </novelWriterXML>
+1 -1
View File
@@ -280,7 +280,7 @@ def testCoreIndex_ScanText(nwMinimal, mockGUI):
assert theIndex.scanText(xHandle, "Hello World!") is False assert theIndex.scanText(xHandle, "Hello World!") is False
# Create the archive root # Create the archive root
aHandle = theProject.newRoot("Archive", nwItemClass.ARCHIVE) aHandle = theProject.newRoot(nwItemClass.ARCHIVE)
assert theProject.projTree[aHandle] is not None assert theProject.projTree[aHandle] is not None
xItem.setParent(aHandle) xItem.setParent(aHandle)
theProject.projTree.updateItemData(xItem.itemHandle) theProject.projTree.updateItemData(xItem.itemHandle)
+8 -8
View File
@@ -259,14 +259,14 @@ def testCoreProject_NewRoot(fncDir, outDir, refDir, mockGUI, mockRnd):
assert theProject.closeProject() is True assert theProject.closeProject() is True
assert theProject.openProject(projFile) is True assert theProject.openProject(projFile) is True
assert isinstance(theProject.newRoot("Novel", nwItemClass.NOVEL), str) assert isinstance(theProject.newRoot(nwItemClass.NOVEL), str)
assert isinstance(theProject.newRoot("Plot", nwItemClass.PLOT), str) assert isinstance(theProject.newRoot(nwItemClass.PLOT), str)
assert isinstance(theProject.newRoot("Character", nwItemClass.CHARACTER), str) assert isinstance(theProject.newRoot(nwItemClass.CHARACTER), str)
assert isinstance(theProject.newRoot("World", nwItemClass.WORLD), str) assert isinstance(theProject.newRoot(nwItemClass.WORLD), str)
assert isinstance(theProject.newRoot("Timeline", nwItemClass.TIMELINE), str) assert isinstance(theProject.newRoot(nwItemClass.TIMELINE), str)
assert isinstance(theProject.newRoot("Object", nwItemClass.OBJECT), str) assert isinstance(theProject.newRoot(nwItemClass.OBJECT), str)
assert isinstance(theProject.newRoot("Custom1", nwItemClass.CUSTOM), str) assert isinstance(theProject.newRoot(nwItemClass.CUSTOM), str)
assert isinstance(theProject.newRoot("Custom2", nwItemClass.CUSTOM), str) assert isinstance(theProject.newRoot(nwItemClass.CUSTOM), str)
assert theProject.projChanged is True assert theProject.projChanged is True
assert theProject.saveProject() is True assert theProject.saveProject() is True
+8 -8
View File
@@ -147,14 +147,14 @@ def buildTestProject(theObject, projPath):
# Creating a minimal project with a few root folders and a # Creating a minimal project with a few root folders and a
# single chapter folder with a single file. # single chapter folder with a single file.
xHandle = {} xHandle = {}
xHandle[1] = theProject.newRoot(theProject.tr("Novel"), nwItemClass.NOVEL) xHandle[1] = theProject.newRoot(nwItemClass.NOVEL, "Novel")
xHandle[2] = theProject.newRoot(theProject.tr("Plot"), nwItemClass.PLOT) xHandle[2] = theProject.newRoot(nwItemClass.PLOT, "Plot")
xHandle[3] = theProject.newRoot(theProject.tr("Characters"), nwItemClass.CHARACTER) xHandle[3] = theProject.newRoot(nwItemClass.CHARACTER, "Characters")
xHandle[4] = theProject.newRoot(theProject.tr("World"), nwItemClass.WORLD) xHandle[4] = theProject.newRoot(nwItemClass.WORLD, "World")
xHandle[5] = theProject.newFile(theProject.tr("Title Page"), xHandle[1]) xHandle[5] = theProject.newFile("Title Page", xHandle[1])
xHandle[6] = theProject.newFolder(theProject.tr("New Chapter"), xHandle[1]) xHandle[6] = theProject.newFolder("New Chapter", xHandle[1])
xHandle[7] = theProject.newFile(theProject.tr("New Chapter"), xHandle[6]) xHandle[7] = theProject.newFile("New Chapter", xHandle[6])
xHandle[8] = theProject.newFile(theProject.tr("New Scene"), xHandle[6]) xHandle[8] = theProject.newFile("New Scene", xHandle[6])
aDoc = NWDoc(theProject, xHandle[5]) aDoc = NWDoc(theProject, xHandle[5])
aDoc.writeDocument("#! New Novel\n\n>> By Jane DOe <<\n") aDoc.writeDocument("#! New Novel\n\n>> By Jane DOe <<\n")