Update document layout during editor save process

This commit is contained in:
Veronica K. B. Olsen
2021-01-29 01:09:04 +01:00
parent 59cfb14a19
commit 4c27d2600a
2 changed files with 54 additions and 2 deletions
+47 -1
View File
@@ -33,10 +33,36 @@ from time import time
from nw.core.item import NWItem
from nw.common import checkHandle
from nw.constants import nwFiles, nwItemType, nwItemClass, nwItemLayout, nwConst
from nw.constants import (
nwFiles, nwItemType, nwItemClass, nwItemLayout, nwConst, nwLists
)
logger = logging.getLogger(__name__)
# Translation map for item layouts
NOVEL_MAP = {
nwItemLayout.SCENE: {
"H1": nwItemLayout.PARTITION,
"H2": nwItemLayout.CHAPTER,
"H4": nwItemLayout.SCENE,
},
nwItemLayout.CHAPTER: {
"H1": nwItemLayout.PARTITION,
"H3": nwItemLayout.SCENE,
"H4": nwItemLayout.SCENE,
},
nwItemLayout.UNNUMBERED: {
"H1": nwItemLayout.PARTITION,
"H3": nwItemLayout.SCENE,
"H4": nwItemLayout.SCENE,
},
nwItemLayout.PARTITION: {
"H2": nwItemLayout.CHAPTER,
"H3": nwItemLayout.SCENE,
"H4": nwItemLayout.SCENE,
},
}
class NWTree():
def __init__(self, theProject):
@@ -202,6 +228,26 @@ class NWTree():
novelWords += tItem.wordCount
return novelWords, noteWords
def updateItemLayout(self, tHandle, hLevel):
"""Check if the item layout needs updating based on the header
given level.
"""
tItem = self.__getitem__(tHandle)
if tItem is None:
return False
if tItem.itemClass not in nwLists.CLS_NOVEL:
return False
if hLevel not in ("H1", "H2", "H3", "H4"):
return False
iLayout = tItem.itemLayout
if iLayout in NOVEL_MAP:
if hLevel in NOVEL_MAP[iLayout]:
tItem.itemLayout = NOVEL_MAP[iLayout][hLevel]
return True
return False
##
# Tree Structure Methods
##
+7 -1
View File
@@ -393,6 +393,7 @@ class GuiDocEditor(QTextEdit):
return False
docText = self.getText()
tHandle = theItem.itemHandle
cC, wC, pC = countWords(docText)
self._updateCounts(cC, wC, pC)
@@ -405,7 +406,12 @@ class GuiDocEditor(QTextEdit):
self.nwDocument.saveDocument(docText)
self.setDocumentChanged(False)
self.theParent.theIndex.scanText(theItem.itemHandle, docText)
self.theParent.theIndex.scanText(tHandle, docText)
hLevel, _ = self.theParent.theIndex.getFirstTitle(tHandle)
if self.theProject.projTree.updateItemLayout(tHandle, hLevel):
self.theParent.treeView.setTreeItemValues(tHandle)
self.nwDocument.saveDocument(docText)
return True