diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 627b4ff9..03f6b622 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -75,12 +75,14 @@ class GuiDocEditor(QTextEdit): self.mainConf = nw.CONFIG self.theParent = theParent self.theTheme = theParent.theTheme + self.theIndex = theParent.theIndex self.theProject = theParent.theProject self.nwDocument = NWDoc(self.theProject, self.theParent) self.docChanged = False # Flag for changed status of document self.spellCheck = False # Flag for spell checking enabled self.theHandle = None # The handle of the open file + self.theHeaders = [] # Record of headers in the file self.theDict = None # The current spell check dictionary self.nonWord = "\"'" # Characters to not include in spell checking @@ -342,6 +344,7 @@ class GuiDocEditor(QTextEdit): self.docFooter.updateLineCount() self.lengthLast = self.qDocument.characterCount() + self.theHeaders = self.theIndex.getHandleHeaders(self.theHandle) qApp.processEvents() self.setDocumentChanged(False) @@ -398,6 +401,7 @@ class GuiDocEditor(QTextEdit): return False docText = self.getText() + tHandle = theItem.itemHandle cC, wC, pC = countWords(docText) self._updateCounts(cC, wC, pC) @@ -410,7 +414,11 @@ class GuiDocEditor(QTextEdit): self.nwDocument.saveDocument(docText) self.setDocumentChanged(False) - self.theParent.theIndex.scanText(theItem.itemHandle, docText) + self.theIndex.scanText(tHandle, docText) + if self._updateHeaders(checkLevel=True): + self.theParent.novelView.refreshTree() + else: + self.theParent.novelView.updateWordCounts(tHandle) return True @@ -1219,6 +1227,30 @@ class GuiDocEditor(QTextEdit): return + def _updateHeaders(self, checkPos=False, checkLevel=False): + """Update the headers record and return True if anything + changed, if a check flag was provided. + """ + if self.theHandle is None: + return False + + newHeaders = self.theIndex.getHandleHeaders(self.theHandle) + if checkPos: + newPos = [x[0] for x in newHeaders] + oldPos = [x[0] for x in self.theHeaders] + if checkLevel: + newLev = [x[1] for x in newHeaders] + oldLev = [x[1] for x in self.theHeaders] + + self.theHeaders = newHeaders + + if checkPos: + return newPos != oldPos + if checkLevel: + return newLev != oldLev + + return False + def _replaceQuotes(self, sQuote, oQuote, cQuote): """Replace all straight quotes in the selected text. """ diff --git a/nw/gui/noveltree.py b/nw/gui/noveltree.py index d8c23b04..7a7990f8 100644 --- a/nw/gui/noveltree.py +++ b/nw/gui/noveltree.py @@ -150,6 +150,15 @@ class GuiNovelTree(QTreeWidget): return + def updateWordCounts(self, tHandle): + """Update the word count for a given handle. + """ + tHeaders = self.theIndex.getHandleWordCounts(tHandle) + for titleKey, wCount in tHeaders: + if titleKey in self._treeMap: + self._treeMap[titleKey].setText(self.C_WORDS, f"{wCount:n}") + return + def getColumnSizes(self): """Return the column widths for the tree columns. """