Make the add new item feature a little smarter

This commit is contained in:
Veronica Berglyd Olsen
2022-09-11 17:06:16 +02:00
parent 01b66bc62e
commit 5cba10151a
+22 -10
View File
@@ -41,7 +41,7 @@ from PyQt5.QtWidgets import (
from novelwriter.core import NWDoc from novelwriter.core import NWDoc
from novelwriter.enum import nwDocMode, nwItemType, nwItemClass, nwItemLayout, nwAlert from novelwriter.enum import nwDocMode, nwItemType, nwItemClass, nwItemLayout, nwAlert
from novelwriter.constants import trConst, nwLabels from novelwriter.constants import nwHeaders, trConst, nwLabels
from novelwriter.dialogs.editlabel import GuiEditLabel from novelwriter.dialogs.editlabel import GuiEditLabel
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -447,16 +447,11 @@ class GuiProjectTree(QTreeWidget):
), nwAlert.ERROR) ), nwAlert.ERROR)
return False return False
# If the selected item is a file, the new item will be a # Collect some information about the selected item that
# sibling if the file has no children, otherwise a child
pItem = self.theProject.tree[sHandle] pItem = self.theProject.tree[sHandle]
qItem = self._getTreeItem(sHandle) qItem = self._getTreeItem(sHandle)
if pItem.itemType == nwItemType.FILE and qItem.childCount() == 0: sLevel = nwHeaders.H_LEVEL.get(self.theProject.index.getHandleHeaderLevel(sHandle), 0)
nHandle = sHandle sIsParent = False if qItem is None else qItem.childCount() > 0
sHandle = pItem.itemParent
if sHandle is None:
logger.error("Internal error") # Bug
return False
if self.theProject.tree.isTrash(sHandle): if self.theProject.tree.isTrash(sHandle):
self.mainGui.makeAlert(self.tr( self.mainGui.makeAlert(self.tr(
@@ -464,19 +459,36 @@ class GuiProjectTree(QTreeWidget):
), nwAlert.ERROR) ), nwAlert.ERROR)
return False return False
# Ask for label # Set default label and determine if new item is to be added
# as child or sibling to the selected item
if itemType == nwItemType.FILE: if itemType == nwItemType.FILE:
if isNote: if isNote:
newLabel = self.tr("New Note") newLabel = self.tr("New Note")
asChild = sIsParent
elif hLevel == 2: elif hLevel == 2:
newLabel = self.tr("New Chapter") newLabel = self.tr("New Chapter")
asChild = sIsParent and pItem.isDocumentLayout() and sLevel < 2
elif hLevel == 3: elif hLevel == 3:
newLabel = self.tr("New Scene") newLabel = self.tr("New Scene")
asChild = sIsParent and pItem.isDocumentLayout() and sLevel < 3
else: else:
newLabel = self.tr("New Document") newLabel = self.tr("New Document")
asChild = sIsParent and pItem.isDocumentLayout()
else: else:
newLabel = self.tr("New Folder") newLabel = self.tr("New Folder")
asChild = False
if not (asChild or pItem.isFolderType() or pItem.isRootType()):
# Move to the parent item so that the new item is added
# as a sibling instead
nHandle = sHandle
sHandle = pItem.itemParent
if sHandle is None:
# Bug: We have a condition that is unhandled
logger.error("Internal error")
return False
# Ask for label
newLabel, dlgOk = GuiEditLabel.getLabel(self, text=newLabel) newLabel, dlgOk = GuiEditLabel.getLabel(self, text=newLabel)
if not dlgOk: if not dlgOk:
logger.info("New item creation cancelled by user") logger.info("New item creation cancelled by user")