Make the new item function better at guessing header level of new files

This commit is contained in:
Veronica Berglyd Olsen
2022-06-05 17:41:29 +02:00
parent 88976d6800
commit 92924b3288
4 changed files with 17 additions and 4 deletions
+5
View File
@@ -502,6 +502,11 @@ class NWIndex:
"""
return self._itemIndex.mainItemHeader(tHandle)
def getHandleHeaderIntLevel(self, tHandle):
"""Get the integer header level of the first header of a handle.
"""
return H_LEVEL.get(self._itemIndex.mainItemHeader(tHandle), 0)
def getTableOfContents(self, maxDepth, skipExcl=True):
"""Generate a table of contents up to a maximum depth.
"""
+8 -3
View File
@@ -38,6 +38,7 @@ from PyQt5.QtWidgets import (
from novelwriter.core import NWDoc
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
from novelwriter.common import minmax
from novelwriter.dialogs.itemeditor import GuiItemEditor
logger = logging.getLogger(__name__)
@@ -188,9 +189,11 @@ class GuiProjectTree(QTreeWidget):
), nwAlert.ERROR)
return False
# If the selected item is a file, the new item will be a sibling
# If the selected item is a file, the new item will be a
# sibling if the file has no children, otherwise a child
pItem = self.theProject.tree[sHandle]
if pItem.itemType == nwItemType.FILE:
qItem = self._getTreeItem(sHandle)
if pItem.itemType == nwItemType.FILE and qItem.childCount() == 0:
nHandle = sHandle
sHandle = pItem.itemParent
if sHandle is None:
@@ -233,7 +236,9 @@ class GuiProjectTree(QTreeWidget):
newDoc = NWDoc(self.theProject, tHandle)
if not newDoc.readDocument():
if nwItem.itemLayout == nwItemLayout.DOCUMENT:
newText = f"### {nwItem.itemName}\n\n"
iLvl = self.theProject.index.getHandleHeaderIntLevel(sHandle)
hLvl = "#"*minmax(iLvl + 1, 2, 4)
newText = f"{hLvl} {nwItem.itemName}\n\n"
else:
newText = f"# {nwItem.itemName}\n\n"