From 61f5425662ccf5b7c3ac80c76127fa0f49d7e236 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 16 Apr 2022 20:55:54 +0200 Subject: [PATCH] Update new item functions in project class --- novelwriter/core/item.py | 25 ++++++++++++++++--- novelwriter/core/project.py | 43 ++++++++++++--------------------- novelwriter/core/tree.py | 2 +- novelwriter/dialogs/docmerge.py | 2 +- novelwriter/dialogs/docsplit.py | 12 +++------ novelwriter/gui/projtree.py | 4 +-- 6 files changed, 45 insertions(+), 43 deletions(-) diff --git a/novelwriter/core/item.py b/novelwriter/core/item.py index 1edd93b8..af189597 100644 --- a/novelwriter/core/item.py +++ b/novelwriter/core/item.py @@ -294,14 +294,33 @@ class NWItem(): stIcon = self.theProject.importItems.icon(self._import) return stName, stIcon - def setImportStatus(self, theLabel): + def setImportStatus(self, value): """Update the importance or status value based on class. This is a wrapper setter for setStatus and setImport. """ if self._class in nwLists.CLS_NOVEL: - self.setStatus(theLabel) + self.setStatus(value) else: - self.setImport(theLabel) + self.setImport(value) + return + + def setClassDefaults(self, itemClass): + """Set the default values based on the item's class and the + project settings. + """ + self.setClass(itemClass) + + if self._class in nwLists.CLS_NOVEL: + self._layout = nwItemLayout.DOCUMENT + else: + self._layout = nwItemLayout.NOTE + + if self._status is None: + self.setStatus("New") # This forces a default value lookup + + if self._import is None: + self.setImport("New") # This forces a default value lookup + return ## diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index 0d71b81f..28aba590 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -127,36 +127,26 @@ class NWProject(): newItem.setName(rootName) newItem.setType(nwItemType.ROOT) newItem.setClass(rootClass) - newItem.setStatus(0) self.projTree.append(None, None, newItem) self.projTree.updateItemData(newItem.itemHandle) return newItem.itemHandle - def newFolder(self, folderName, folderClass, pHandle): - """Add a new folder with a given name and class and parent item. + def newFolder(self, folderName, pHandle): + """Add a new folder with a given name and parent item. """ newItem = NWItem(self) newItem.setName(folderName) newItem.setType(nwItemType.FOLDER) - newItem.setClass(folderClass) - newItem.setStatus(0) self.projTree.append(None, pHandle, newItem) self.projTree.updateItemData(newItem.itemHandle) return newItem.itemHandle - def newFile(self, fileName, fileClass, pHandle): - """Add a new file with a given name and class, and set a layout - based on the class. DOCUMENT for NOVEL, otherwise NOTE. + def newFile(self, fileName, pHandle): + """Add a new file with a given name and parent item. """ newItem = NWItem(self) newItem.setName(fileName) newItem.setType(nwItemType.FILE) - if fileClass == nwItemClass.NOVEL: - newItem.setLayout(nwItemLayout.DOCUMENT) - else: - newItem.setLayout(nwItemLayout.NOTE) - newItem.setClass(fileClass) - newItem.setStatus(0) self.projTree.append(None, pHandle, newItem) self.projTree.updateItemData(newItem.itemHandle) return newItem.itemHandle @@ -286,10 +276,10 @@ class NWProject(): xHandle[2] = self.newRoot(self.tr("Plot"), nwItemClass.PLOT) xHandle[3] = self.newRoot(self.tr("Characters"), nwItemClass.CHARACTER) xHandle[4] = self.newRoot(self.tr("World"), nwItemClass.WORLD) - xHandle[5] = self.newFile(self.tr("Title Page"), nwItemClass.NOVEL, xHandle[1]) - xHandle[6] = self.newFolder(self.tr("New Chapter"), nwItemClass.NOVEL, xHandle[1]) - xHandle[7] = self.newFile(self.tr("New Chapter"), nwItemClass.NOVEL, xHandle[6]) - xHandle[8] = self.newFile(self.tr("New Scene"), nwItemClass.NOVEL, xHandle[6]) + xHandle[5] = self.newFile(self.tr("Title Page"), xHandle[1]) + xHandle[6] = self.newFolder(self.tr("New Chapter"), xHandle[1]) + xHandle[7] = self.newFile(self.tr("New Chapter"), xHandle[6]) + xHandle[8] = self.newFile(self.tr("New Scene"), xHandle[6]) aDoc = NWDoc(self, xHandle[5]) aDoc.writeDocument(titlePage) @@ -312,8 +302,7 @@ class NWProject(): self.newRoot(trConst(nwLabels.CLASS_NAME[newRoot]), newRoot) # Create a title page - tHandle = self.newFile(self.tr("Title Page"), nwItemClass.NOVEL, nHandle) - self.projTree.setFileItemLayout(tHandle, nwItemLayout.DOCUMENT) + tHandle = self.newFile(self.tr("Title Page"), nHandle) aDoc = NWDoc(self, tHandle) aDoc.writeDocument(titlePage) @@ -329,10 +318,9 @@ class NWProject(): chTitle = self.tr("Chapter {0}").format(f"{ch+1:d}") pHandle = nHandle if chFolders: - pHandle = self.newFolder(chTitle, nwItemClass.NOVEL, nHandle) + pHandle = self.newFolder(chTitle, nHandle) - cHandle = self.newFile(chTitle, nwItemClass.NOVEL, pHandle) - self.projTree.setFileItemLayout(cHandle, nwItemLayout.DOCUMENT) + cHandle = self.newFile(chTitle, pHandle) aDoc = NWDoc(self, cHandle) aDoc.writeDocument("## %s\n\n" % chTitle) @@ -341,7 +329,7 @@ class NWProject(): if numScenes > 0: for sc in range(numScenes): scTitle = self.tr("Scene {0}").format(f"{ch+1:d}.{sc+1:d}") - sHandle = self.newFile(scTitle, nwItemClass.NOVEL, pHandle) + sHandle = self.newFile(scTitle, pHandle) aDoc = NWDoc(self, sHandle) aDoc.writeDocument("### %s\n\n" % scTitle) @@ -350,7 +338,7 @@ class NWProject(): elif numScenes > 0: for sc in range(numScenes): scTitle = self.tr("Scene {0}").format(f"{sc+1:d}") - sHandle = self.newFile(scTitle, nwItemClass.NOVEL, nHandle) + sHandle = self.newFile(scTitle, nHandle) aDoc = NWDoc(self, sHandle) aDoc.writeDocument("### %s\n\n" % scTitle) @@ -480,8 +468,9 @@ class NWProject(): # documents and one for project notes. Introduced in # version 1.5. # 1.4 : Introduces a more compact format for storing items. All - # settings aside from name are now attributes. Introduced - # in version 1.7. + # settings aside from name are now attributes. This format + # also changes the way satus and importance labels are + # stored and handled. Introduced in version 1.7. if fileVersion not in ("1.0", "1.1", "1.2", "1.3", "1.4"): self.theParent.makeAlert(self.tr( diff --git a/novelwriter/core/tree.py b/novelwriter/core/tree.py index ddef88b7..e1b68220 100644 --- a/novelwriter/core/tree.py +++ b/novelwriter/core/tree.py @@ -222,7 +222,7 @@ class NWTree(): for _ in range(nwConst.MAX_DEPTH + 1): if iItem.itemParent is None: tItem.setRoot(iItem.itemHandle) - tItem.setClass(iItem.itemClass) + tItem.setClassDefaults(iItem.itemClass) return True else: iItem = self.__getitem__(iItem.itemParent) diff --git a/novelwriter/dialogs/docmerge.py b/novelwriter/dialogs/docmerge.py index f7695b9a..c082dd3b 100644 --- a/novelwriter/dialogs/docmerge.py +++ b/novelwriter/dialogs/docmerge.py @@ -130,7 +130,7 @@ class GuiDocMerge(QDialog): self.theParent.makeAlert(self.tr("Internal error."), nwAlert.ERROR) return False - nHandle = self.theProject.newFile(srcItem.itemName, srcItem.itemClass, srcItem.itemParent) + nHandle = self.theProject.newFile(srcItem.itemName, srcItem.itemParent) newItem = self.theProject.projTree[nHandle] newItem.setStatus(srcItem.itemStatus) newItem.setImport(srcItem.itemImport) diff --git a/novelwriter/dialogs/docsplit.py b/novelwriter/dialogs/docsplit.py index cdf0757a..b650671d 100644 --- a/novelwriter/dialogs/docsplit.py +++ b/novelwriter/dialogs/docsplit.py @@ -33,7 +33,7 @@ from PyQt5.QtWidgets import ( ) from novelwriter.core import NWDoc -from novelwriter.enum import nwAlert, nwItemType, nwItemClass, nwItemLayout +from novelwriter.enum import nwAlert, nwItemType from novelwriter.constants import nwConst from novelwriter.gui.custom import QHelpLabel @@ -186,22 +186,16 @@ class GuiDocSplit(QDialog): return False # Create the folder - fHandle = self.theProject.newFolder( - srcItem.itemName, srcItem.itemClass, srcItem.itemParent - ) + fHandle = self.theProject.newFolder(srcItem.itemName, srcItem.itemParent) self.theParent.treeView.revealNewTreeItem(fHandle) logger.verbose("Creating folder '%s'", fHandle) # Loop through, and create the files for wTitle, iStart, iEnd in finalOrder: - isNovel = srcItem.itemClass == nwItemClass.NOVEL - itemLayout = nwItemLayout.DOCUMENT if isNovel else nwItemLayout.NOTE - wTitle = wTitle.lstrip("#").strip() - nHandle = self.theProject.newFile(wTitle, srcItem.itemClass, fHandle) + nHandle = self.theProject.newFile(wTitle, fHandle) newItem = self.theProject.projTree[nHandle] - newItem.setLayout(itemLayout) newItem.setStatus(srcItem.itemStatus) newItem.setImport(srcItem.itemImport) logger.verbose( diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index 01a8f707..584a281c 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -250,7 +250,7 @@ class GuiProjectTree(QTreeWidget): # If we're still here, add the file or folder if itemType == nwItemType.FILE: - tHandle = self.theProject.newFile(self.tr("New File"), itemClass, pHandle) + tHandle = self.theProject.newFile(self.tr("New File"), pHandle) elif itemType == nwItemType.FOLDER: if len(parTree) >= nwConst.MAX_DEPTH - 1: @@ -261,7 +261,7 @@ class GuiProjectTree(QTreeWidget): "Maximum folder depth has been reached." ), nwAlert.ERROR) return False - tHandle = self.theProject.newFolder(self.tr("New Folder"), itemClass, pHandle) + tHandle = self.theProject.newFolder(self.tr("New Folder"), pHandle) else: logger.error("Failed to add new item")