Improve how the novel tree is refreshed

This commit is contained in:
Veronica Berglyd Olsen
2022-11-14 18:45:36 +01:00
parent 992ed4f36c
commit ebc6654069
7 changed files with 76 additions and 135 deletions
+7 -22
View File
@@ -90,7 +90,6 @@ class GuiDocEditor(QTextEdit):
self._docChanged = False # Flag for changed status of document
self._docHandle = None # The handle of the open file
self._docHeaders = [] # Record of headers in the file
self._spellCheck = False # Flag for spell checking enabled
self._nonWord = "\"'" # Characters to not include in spell checking
@@ -417,8 +416,8 @@ class GuiDocEditor(QTextEdit):
self._queuePos = self._nwItem.cursorPos
else:
self.setCursorPosition(self._nwItem.cursorPos)
else:
self.setCursorLine(tLine)
elif isinstance(tLine, int):
self.setCursorLine(tLine - 1)
if self.mainConf.scrollPastEnd > 0:
fSize = QFontMetrics(self.font()).lineSpacing()
@@ -427,7 +426,6 @@ class GuiDocEditor(QTextEdit):
self.document().rootFrame().setFrameFormat(docFrame)
self.docFooter.updateLineCount()
self._docHeaders = self.theProject.index.getHandleHeaders(self._docHandle)
qApp.processEvents()
self.document().clearUndoRedoStacks()
@@ -533,14 +531,16 @@ class GuiDocEditor(QTextEdit):
self.setDocumentChanged(False)
oldHeader = self._nwItem.mainHeading
oldCount = self.theProject.index.getHandleHeaderCount(tHandle)
self.theProject.index.scanText(tHandle, docText)
newHeader = self._nwItem.mainHeading
newCount = self.theProject.index.getHandleHeaderCount(tHandle)
if self._nwItem.itemClass == nwItemClass.NOVEL:
if self._updateHeaders():
self.novelStructureChanged.emit()
else:
if oldCount == newCount:
self.novelItemMetaChanged.emit(tHandle)
else:
self.novelStructureChanged.emit()
# ToDo: This should be a signal
if oldHeader != newHeader:
@@ -2067,21 +2067,6 @@ class GuiDocEditor(QTextEdit):
return False
return True
def _updateHeaders(self):
"""Update the headers record and return True if anything
changed, if a check flag was provided.
"""
if self._docHandle is None:
return False
newHeaders = self.theProject.index.getHandleHeaders(self._docHandle)
newLev = [x[1] for x in newHeaders]
oldLev = [x[1] for x in self._docHeaders]
self._docHeaders = newHeaders
return newLev != oldLev
def _checkDocSize(self, theSize):
"""Check if document size crosses the big document limit set in
config. If so, we will set the big document flag to True.
+27 -36
View File
@@ -477,7 +477,7 @@ class GuiNovelTree(QTreeWidget):
return
def refreshTree(self, rootHandle=None, overRide=False):
"""Called whenever the Novel tab is activated.
"""Refresh the tree if it has been changed.
"""
logger.debug("Requesting refresh of the novel tree")
if rootHandle is None:
@@ -509,31 +509,16 @@ class GuiNovelTree(QTreeWidget):
if idxData is None:
return
logger.debug("Refreshing meta data for item '%s'", tHandle)
for sTitle, tHeading in idxData.items():
sKey = f"{tHandle}:{sTitle}"
trItem = self._treeMap.get(sKey, None)
if trItem is None:
logger.debug("Heading '%s' not in novel tree", sKey)
continue
self.refreshTree()
return
iLevel = nwHeaders.H_LEVEL.get(tHeading.level, 0)
if iLevel == 0:
continue
hDec = self.mainTheme.getHeaderDecoration(iLevel)
trItem.setData(self.C_TITLE, Qt.DecorationRole, hDec)
trItem.setText(self.C_TITLE, tHeading.title)
trItem.setFont(self.C_TITLE, self._hFonts[iLevel])
trItem.setText(self.C_WORDS, f"{tHeading.wordCount:n}")
trItem.setTextAlignment(self.C_WORDS, Qt.AlignRight)
trItem.setData(self.C_MORE, Qt.DecorationRole, self._pMore)
# Custom column
lastText, toolTip = self._getLastColumnText(tHandle, sTitle)
trItem.setText(self.C_EXTRA, lastText)
if lastText:
trItem.setToolTip(self.C_EXTRA, toolTip)
self._updateTreeItemValues(trItem, tHeading, tHandle, sTitle)
return
@@ -668,30 +653,16 @@ class GuiNovelTree(QTreeWidget):
novStruct = self.theProject.index.novelStructure(rootHandle=rootHandle, skipExcl=True)
for tKey, tHandle, sTitle, novIdx in novStruct:
iLevel = nwHeaders.H_LEVEL.get(novIdx.level, 0)
if iLevel == 0:
if novIdx.level == "H0":
continue
hDec = self.mainTheme.getHeaderDecoration(iLevel)
newItem = QTreeWidgetItem()
newItem.setData(self.C_TITLE, Qt.DecorationRole, hDec)
newItem.setText(self.C_TITLE, novIdx.title)
newItem.setData(self.C_TITLE, self.D_HANDLE, tHandle)
newItem.setData(self.C_TITLE, self.D_TITLE, sTitle)
newItem.setData(self.C_TITLE, self.D_KEY, tKey)
newItem.setFont(self.C_TITLE, self._hFonts[iLevel])
newItem.setText(self.C_WORDS, f"{novIdx.wordCount:n}")
newItem.setTextAlignment(self.C_WORDS, Qt.AlignRight)
newItem.setData(self.C_MORE, Qt.DecorationRole, self._pMore)
# Custom column
lastText, toolTip = self._getLastColumnText(tHandle, sTitle)
newItem.setText(self.C_EXTRA, lastText)
if lastText:
newItem.setToolTip(self.C_EXTRA, toolTip)
self._updateTreeItemValues(newItem, novIdx, tHandle, sTitle)
self._treeMap[tKey] = newItem
self.addTopLevelItem(newItem)
@@ -702,6 +673,26 @@ class GuiNovelTree(QTreeWidget):
return
def _updateTreeItemValues(self, trItem, idxItem, tHandle, sTitle):
"""Set the tree item values from the index entry.
"""
iLevel = nwHeaders.H_LEVEL.get(idxItem.level, 0)
hDec = self.mainTheme.getHeaderDecoration(iLevel)
trItem.setData(self.C_TITLE, Qt.DecorationRole, hDec)
trItem.setText(self.C_TITLE, idxItem.title)
trItem.setFont(self.C_TITLE, self._hFonts[iLevel])
trItem.setText(self.C_WORDS, f"{idxItem.wordCount:n}")
trItem.setData(self.C_MORE, Qt.DecorationRole, self._pMore)
# Custom column
lastText, toolTip = self._getLastColumnText(tHandle, sTitle)
trItem.setText(self.C_EXTRA, lastText)
if lastText:
trItem.setToolTip(self.C_EXTRA, toolTip)
return
def _getLastColumnText(self, tHandle, sTitle):
"""Generate the text for the last column based on user settings.
"""