Make novel structure function in index class an iterable function instead

This commit is contained in:
Veronica K. B. Olsen
2021-01-02 21:37:46 +01:00
parent 21d91193a2
commit e4d160ee0b
4 changed files with 45 additions and 56 deletions
+7 -9
View File
@@ -549,12 +549,11 @@ class NWIndex():
# Extract Data # Extract Data
## ##
def getNovelStructure(self, skipExcluded=True): def novelStructure(self, skipExcluded=True):
"""Builds a list of all titles in the novel, in the correct """Iterate over all titles in the novel, in the correct order as
order as they appear in the tree view and in the respective they appear in the tree view and in the respective document
document files, but skipping all note files. files, but skipping all note files.
""" """
theStructure = []
for tItem in self.theProject.projTree: for tItem in self.theProject.projTree:
if tItem is not None: if tItem is not None:
if not tItem.isExported and skipExcluded: if not tItem.isExported and skipExcluded:
@@ -562,10 +561,9 @@ class NWIndex():
tHandle = tItem.itemHandle tHandle = tItem.itemHandle
if tHandle not in self.novelIndex: if tHandle not in self.novelIndex:
continue continue
for sTitle in sorted(self.novelIndex[tHandle].keys()): for sTitle in sorted(self.novelIndex[tHandle]):
theStructure.append("%s:%s" % (tHandle, sTitle)) tKey = "%s:%s" % (tHandle, sTitle)
yield tKey, tHandle, sTitle, self.novelIndex[tHandle][sTitle]
return theStructure
def getCounts(self, tHandle, sTitle=None): def getCounts(self, tHandle, sTitle=None):
"""Returns the counts for a file, or a section of a file """Returns the counts for a file, or a section of a file
+8 -23
View File
@@ -34,6 +34,7 @@ from PyQt5.QtCore import Qt, QSize
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView
from nw.constants import nwKeyWords from nw.constants import nwKeyWords
from nw.common import checkInt
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -209,10 +210,7 @@ class GuiNovelTree(QTreeWidget):
""" """
theData = tItem.data(self.C_TITLE, Qt.UserRole) theData = tItem.data(self.C_TITLE, Qt.UserRole)
tHandle = theData[0] tHandle = theData[0]
try: tLine = checkInt(theData[1], 1)
tLine = int(theData[1])
except Exception:
tLine = 1
logger.verbose("User selected entry with handle %s on line %s" % (tHandle, tLine)) logger.verbose("User selected entry with handle %s on line %s" % (tHandle, tLine))
self.theParent.openDocument(tHandle, tLine=tLine-1, doScroll=True) self.theParent.openDocument(tHandle, tLine=tLine-1, doScroll=True)
@@ -239,23 +237,12 @@ class GuiNovelTree(QTreeWidget):
""" """
self.clearTree() self.clearTree()
for titleKey in self.theIndex.getNovelStructure(skipExcluded=True): for tKey, tHandle, sTitle, novIdx in self.theIndex.novelStructure(skipExcluded=True):
if len(titleKey) < 16: tItem = self._createTreeItem(tHandle, sTitle, tKey, novIdx)
continue self._treeMap[tKey] = tItem
tHandle = titleKey[:13]
sTitle = titleKey[14:]
if tHandle not in self.theIndex.novelIndex:
continue
if sTitle not in self.theIndex.novelIndex[tHandle]:
continue
tLevel = self.theIndex.novelIndex[tHandle][sTitle]["level"]
tItem = self._createTreeItem(tHandle, sTitle, tLevel, titleKey)
self._treeMap[titleKey] = tItem
tLevel = novIdx["level"]
if tLevel == "H1": if tLevel == "H1":
currTitle = tItem currTitle = tItem
self.addTopLevelItem(tItem) self.addTopLevelItem(tItem)
@@ -292,13 +279,11 @@ class GuiNovelTree(QTreeWidget):
return return
def _createTreeItem(self, tHandle, sTitle, tLevel, titleKey): def _createTreeItem(self, tHandle, sTitle, titleKey, novIdx):
"""Populate a tree item with all the column values. """Populate a tree item with all the column values.
""" """
novIdx = self.theIndex.novelIndex[tHandle][sTitle]
newItem = QTreeWidgetItem() newItem = QTreeWidgetItem()
hIcon = "doc_%s" % tLevel.lower() hIcon = "doc_%s" % novIdx["level"].lower()
theData = (tHandle, sTitle[1:].lstrip("0"), titleKey) theData = (tHandle, sTitle[1:].lstrip("0"), titleKey)
wC = int(novIdx["wCount"]) wC = int(novIdx["wCount"])
+7 -20
View File
@@ -375,23 +375,12 @@ class GuiOutline(QTreeWidget):
currChapter = None currChapter = None
currScene = None currScene = None
for titleKey in self.theIndex.getNovelStructure(skipExcluded=True): for tKey, tHandle, sTitle, novIdx in self.theIndex.novelStructure(skipExcluded=True):
if len(titleKey) < 16: tItem = self._createTreeItem(tHandle, sTitle, novIdx)
continue self.treeMap[tKey] = tItem
tHandle = titleKey[:13]
sTitle = titleKey[14:]
if tHandle not in self.theIndex.novelIndex:
continue
if sTitle not in self.theIndex.novelIndex[tHandle]:
continue
tLevel = self.theIndex.novelIndex[tHandle][sTitle]["level"]
tItem = self._createTreeItem(tHandle, sTitle, tLevel)
self.treeMap[titleKey] = tItem
tLevel = novIdx["level"]
if tLevel == "H1": if tLevel == "H1":
currTitle = tItem currTitle = tItem
self.addTopLevelItem(tItem) self.addTopLevelItem(tItem)
@@ -428,14 +417,12 @@ class GuiOutline(QTreeWidget):
return return
def _createTreeItem(self, tHandle, sTitle, tLevel): def _createTreeItem(self, tHandle, sTitle, novIdx):
"""Populate a tree item with all the column values. """Populate a tree item with all the column values.
""" """
nwItem = self.theProject.projTree[tHandle] nwItem = self.theProject.projTree[tHandle]
novIdx = self.theIndex.novelIndex[tHandle][sTitle]
newItem = QTreeWidgetItem() newItem = QTreeWidgetItem()
hIcon = "doc_%s" % tLevel.lower() hIcon = "doc_%s" % novIdx["level"].lower()
cC = int(novIdx["cCount"]) cC = int(novIdx["cCount"])
wC = int(novIdx["wCount"]) wC = int(novIdx["wCount"])
+23 -4
View File
@@ -441,13 +441,32 @@ def testCoreIndex_ExtractData(nwMinimal, dummyGUI):
)) ))
# The novel structure should contain the pointer to the novel file header # The novel structure should contain the pointer to the novel file header
assert theIndex.getNovelStructure() == ["%s:T000001" % nHandle] theKeys = []
for aKey, _, _, _ in theIndex.novelStructure():
theKeys.append(aKey)
assert theKeys == ["%s:T000001" % nHandle]
# Check that excluded files can be skipped # Check that excluded files can be skipped
theProject.projTree[nHandle].setExported(False) theProject.projTree[nHandle].setExported(False)
assert theIndex.getNovelStructure(skipExcluded=False) == ["%s:T000001" % nHandle]
assert theIndex.getNovelStructure(skipExcluded=True) == [] theKeys = []
assert theIndex.getNovelStructure() == [] for aKey, _, _, _ in theIndex.novelStructure(skipExcluded=False):
theKeys.append(aKey)
assert theKeys == ["%s:T000001" % nHandle]
theKeys = []
for aKey, _, _, _ in theIndex.novelStructure(skipExcluded=True):
theKeys.append(aKey)
assert theKeys == []
theKeys = []
for aKey, _, _, _ in theIndex.novelStructure():
theKeys.append(aKey)
assert theKeys == []
# The novel file should have the correct counts # The novel file should have the correct counts
cC, wC, pC = theIndex.getCounts(nHandle) cC, wC, pC = theIndex.getCounts(nHandle)