diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index 40828056..9b1718c7 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -544,7 +544,7 @@ class NWProject(QObject): # Update recent projects self.mainConf.updateRecentCache( - self.projPath, self._data.name, self._data.getLastCount("total"), time() + self.projPath, self._data.name, sum(self._data.initCounts), time() ) self.mainConf.saveRecentCache() @@ -611,7 +611,7 @@ class NWProject(QObject): # Update recent projects self.mainConf.updateRecentCache( - self.projPath, self._data.name, self._data.getCurrCount("total"), saveTime + self.projPath, self._data.name, sum(self._data.currCounts), saveTime ) self.mainConf.saveRecentCache() @@ -949,10 +949,8 @@ class NWProject(QObject): def updateWordCounts(self): """Update the total word count values. """ - wcNovel, wcNotes = self._projTree.sumWords() - self._data.setCurrCount(wcNovel, "novel") - self._data.setCurrCount(wcNotes, "notes") - self._data.setCurrCount(wcNovel + wcNotes, "total") + novel, notes = self._projTree.sumWords() + self._data.setCurrCounts(novel=novel, notes=notes) return def countStatus(self): @@ -1211,9 +1209,10 @@ class NWProject(QObject): isFile = os.path.isfile(sessionFile) nowTime = time() - lastCount = self._data.getLastCount("total") - currCount = self._data.getCurrCount("total") - sessDiff = currCount - lastCount + iNovel, iNotes = self._data.initCounts + cNovel, cNotes = self._data.currCounts + iTotal = iNovel + iNotes + sessDiff = cNovel + cNotes - iTotal sessTime = nowTime - self._projOpened logger.info("The session lasted %d sec and added %d words", int(sessTime), sessDiff) @@ -1225,8 +1224,8 @@ class NWProject(QObject): with open(sessionFile, mode="a+", encoding="utf-8") as outFile: if not isFile: # It's a new file, so add a header - if lastCount > 0: - outFile.write("# Offset %d\n" % lastCount) + if iTotal > 0: + outFile.write("# Offset %d\n" % iTotal) outFile.write("# %-17s %-19s %8s %8s %8s\n" % ( "Start Time", "End Time", "Novel", "Notes", "Idle" )) @@ -1234,8 +1233,8 @@ class NWProject(QObject): outFile.write("%-19s %-19s %8d %8d %8d\n" % ( formatTimeStamp(self._projOpened), formatTimeStamp(nowTime), - self._data.getCurrCount("novel"), - self._data.getCurrCount("notes"), + cNovel, + cNotes, int(idleTime), )) @@ -1341,8 +1340,8 @@ class NWProjectData: self._spellLang = None # Project Dictionaries - self._lastCount: dict[str, int] = {} - self._currCount: dict[str, int] = {} + self._initCounts = [0, 0] + self._currCounts = [0, 0] self._lastHandle: dict[str, str | None] = { "editor": None, "viewer": None, @@ -1407,6 +1406,14 @@ class NWProjectData: def spellLang(self): return self._spellLang + @property + def initCounts(self): + return tuple(self._initCounts) + + @property + def currCounts(self): + return tuple(self._currCounts) + @property def lastHandle(self): return self._lastHandle @@ -1461,16 +1468,6 @@ class NWProjectData: """ return self._lastHandle.get(component, None) - def getLastCount(self, type): - """Retrieve the last word count for a given type. - """ - return self._lastCount.get(type, 0) - - def getCurrCount(self, type): - """Retrieve the current word count for a given type. - """ - return self._currCount.get(type, 0) - def getTitleFormat(self, kind): """Retrieve the title format string for a given kind of header. """ @@ -1580,19 +1577,22 @@ class NWProjectData: self.theProject.setProjectChanged(True) return - def setLastCount(self, value, type): - """Set the word counts from last session. + def setInitCounts(self, novel=None, notes=None): + """Set the worc count totals for novel and note files. """ - self._lastCount[type] = checkInt(value, 0) - self.theProject.setProjectChanged(True) + if novel is not None: + self._initCounts[0] = checkInt(novel, 0) + if notes is not None: + self._initCounts[1] = checkInt(notes, 0) return - def setCurrCount(self, value, type): - """Set the current word counts. + def setCurrCounts(self, novel=None, notes=None): + """Set the worc count totals for novel and note files. """ - if value != self._currCount.get(type, 0): - self._currCount[type] = checkInt(value, 0) - self.theProject.setProjectChanged(True) + if novel is not None: + self._currCounts[0] = checkInt(novel, 0) + if notes is not None: + self._currCounts[1] = checkInt(notes, 0) return def setAutoReplace(self, value): diff --git a/novelwriter/core/projectxml.py b/novelwriter/core/projectxml.py index 1c3f525d..66dfb294 100644 --- a/novelwriter/core/projectxml.py +++ b/novelwriter/core/projectxml.py @@ -266,12 +266,10 @@ class ProjectXMLReader: projData.setSpellCheck(xItem.text) elif xItem.tag == "spellLang": projData.setSpellLang(xItem.text) - elif xItem.tag == "totalWordCount": - projData.setLastCount(xItem.text, "total") elif xItem.tag == "novelWordCount": - projData.setLastCount(xItem.text, "novel") + projData.setInitCounts(novel=xItem.text) elif xItem.tag == "notesWordCount": - projData.setLastCount(xItem.text, "notes") + projData.setInitCounts(notes=xItem.text) elif xItem.tag == "status": self._parseStatusImport(xItem, projData.itemStatus) elif xItem.tag in ("import", "importance"): @@ -289,18 +287,7 @@ class ProjectXMLReader: else: # Pre 1.4 format projData.setTitleFormat(self._parseDictTagText(xItem)) else: - if self._version < 0x0104: - # Convert some deprecated fields - if xItem.tag == "lastEdited": # Discontinued in 1.4 - projData.setLastHandle(xItem.text, "editor") - elif xItem.tag == "lastViewed": # Discontinued in 1.4 - projData.setLastHandle(xItem.text, "viewer") - elif xItem.tag == "lastWordCount": # Renamed in 1.4 - projData.setLastCount(xItem.text, "total") - else: - logger.warning("Ignored in xml", xItem.tag) - else: - logger.warning("Ignored in xml", xItem.tag) + logger.warning("Ignored in xml", xItem.tag) return True @@ -495,9 +482,8 @@ class ProjectXMLWriter: self._packSingleValue(xSettings, "language", projData.language) self._packSingleValue(xSettings, "spellCheck", projData.spellCheck) self._packSingleValue(xSettings, "spellLang", projData.spellLang) - self._packSingleValue(xSettings, "totalWordCount", projData.getCurrCount("total")) - self._packSingleValue(xSettings, "novelWordCount", projData.getCurrCount("novel")) - self._packSingleValue(xSettings, "notesWordCount", projData.getCurrCount("notes")) + self._packSingleValue(xSettings, "novelWordCount", projData.currCounts[0]) + self._packSingleValue(xSettings, "notesWordCount", projData.currCounts[1]) self._packDictKeyValue(xSettings, "lastHandle", projData.lastHandle) self._packDictKeyValue(xSettings, "autoReplace", projData.autoReplace) self._packDictKeyValue(xSettings, "titleFormat", projData.titleFormat) diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py index 9f776210..f984d5d2 100644 --- a/novelwriter/guimain.py +++ b/novelwriter/guimain.py @@ -1559,13 +1559,13 @@ class GuiMain(QMainWindow): self.theProject.updateWordCounts() if self.mainConf.incNotesWCount: - currWords = self.theProject.data.getCurrCount("total") - diffWords = currWords - self.theProject.data.getLastCount("total") + iTotal = sum(self.theProject.data.initCounts) + cTotal = sum(self.theProject.data.currCounts) + self.mainStatus.setProjectStats(cTotal, cTotal - iTotal) else: - currWords = self.theProject.data.getCurrCount("novel") - diffWords = currWords - self.theProject.data.getLastCount("novel") - - self.mainStatus.setProjectStats(currWords, diffWords) + iNovel, _ = self.theProject.data.initCounts + cNovel, _ = self.theProject.data.currCounts + self.mainStatus.setProjectStats(cNovel, cNovel - iNovel) return diff --git a/sample/nwProject.nwx b/sample/nwProject.nwx index d6eeee62..1dba26ca 100644 --- a/sample/nwProject.nwx +++ b/sample/nwProject.nwx @@ -1,20 +1,19 @@ - + Sample Project Sample Project Jane Smith Jay Doh - 1407 + 1409 236 - 69424 + 69427 False en_GB True None - 1363 954 409 diff --git a/tests/reference/coreProject_NewCustomA_nwProject.nwx b/tests/reference/coreProject_NewCustomA_nwProject.nwx index ca7b5891..76509948 100644 --- a/tests/reference/coreProject_NewCustomA_nwProject.nwx +++ b/tests/reference/coreProject_NewCustomA_nwProject.nwx @@ -1,5 +1,5 @@ - + Test Custom Test Novel @@ -14,7 +14,6 @@ None False None - 0 0 0 diff --git a/tests/reference/coreProject_NewCustomB_nwProject.nwx b/tests/reference/coreProject_NewCustomB_nwProject.nwx index 404b0128..afd7627c 100644 --- a/tests/reference/coreProject_NewCustomB_nwProject.nwx +++ b/tests/reference/coreProject_NewCustomB_nwProject.nwx @@ -1,5 +1,5 @@ - + Test Custom Test Novel @@ -14,7 +14,6 @@ None False None - 0 0 0 diff --git a/tests/reference/coreProject_NewFileFolder_nwProject.nwx b/tests/reference/coreProject_NewFileFolder_nwProject.nwx index 646317f3..b1370820 100644 --- a/tests/reference/coreProject_NewFileFolder_nwProject.nwx +++ b/tests/reference/coreProject_NewFileFolder_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project New Novel @@ -13,7 +13,6 @@ None False None - 13 10 3 diff --git a/tests/reference/coreProject_NewMinimal_nwProject.nwx b/tests/reference/coreProject_NewMinimal_nwProject.nwx index 15907d72..d43aee14 100644 --- a/tests/reference/coreProject_NewMinimal_nwProject.nwx +++ b/tests/reference/coreProject_NewMinimal_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project None @@ -12,7 +12,6 @@ None False None - 0 0 0 diff --git a/tests/reference/coreProject_NewRoot_nwProject.nwx b/tests/reference/coreProject_NewRoot_nwProject.nwx index 6aff4336..ed50e9ca 100644 --- a/tests/reference/coreProject_NewRoot_nwProject.nwx +++ b/tests/reference/coreProject_NewRoot_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project New Novel @@ -13,7 +13,6 @@ None False None - 9 9 0 diff --git a/tests/reference/guiEditor_Main_Final_nwProject.nwx b/tests/reference/guiEditor_Main_Final_nwProject.nwx index e5c1a1ac..853ea1d9 100644 --- a/tests/reference/guiEditor_Main_Final_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Final_nwProject.nwx @@ -1,19 +1,18 @@ - + New Project New Novel Jane Doe 4 2 - 4 + 3 True None True None - 163 136 27 diff --git a/tests/reference/guiEditor_Main_Initial_nwProject.nwx b/tests/reference/guiEditor_Main_Initial_nwProject.nwx index 9dfb7b59..8108fba8 100644 --- a/tests/reference/guiEditor_Main_Initial_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Initial_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project New Novel @@ -13,7 +13,6 @@ None False None - 9 9 0 diff --git a/tests/test_core/test_core_project.py b/tests/test_core/test_core_project.py index 66fa4082..710d8160 100644 --- a/tests/test_core/test_core_project.py +++ b/tests/test_core/test_core_project.py @@ -1003,8 +1003,8 @@ def testCoreProject_Methods(monkeypatch, mockGUI, tmpDir, fncDir, mockRnd): assert theProject.tree.handles() == oldOrder # Session stats - theProject.data.setCurrCount(200, "total") - theProject.data.setLastCount(100, "total") + theProject._data._initCounts = [50, 50] + theProject._data._currCounts = [100, 100] with monkeypatch.context() as mp: mp.setattr("os.path.isdir", lambda *a, **k: False) assert not theProject._appendSessionStats(idleTime=0) @@ -1019,8 +1019,7 @@ def testCoreProject_Methods(monkeypatch, mockGUI, tmpDir, fncDir, mockRnd): statsFile = os.path.join(theProject.projMeta, nwFiles.SESS_STATS) theProject._projOpened = 1600002000 - theProject._data.setCurrCount(200, "novel") - theProject._data.setCurrCount(100, "notes") + theProject._data._currCounts = [200, 100] with monkeypatch.context() as mp: mp.setattr("novelwriter.core.project.time", lambda: 1600005600)