From 7c643ea698ba21271ed293b029ee636af6f7a0ef Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 9 Jan 2021 19:53:42 +0100 Subject: [PATCH 1/4] Make sure document edit status is not set on non-text changes --- nw/gui/doceditor.py | 12 ++++++++++-- nw/guimain.py | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index f782fa62..c2978b00 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -316,7 +316,6 @@ class GuiDocEditor(QTextEdit): self.lastEdit = time() self._runCounter() self.wcTimer.start() - self.setDocumentChanged(False) self.theHandle = tHandle self.setReadOnly(False) @@ -341,6 +340,9 @@ class GuiDocEditor(QTextEdit): self.docFooter.updateLineCount() self.lengthLast = self.qDocument.characterCount() + qApp.processEvents() + self.setDocumentChanged(False) + qApp.restoreOverrideCursor() return True @@ -373,8 +375,8 @@ class GuiDocEditor(QTextEdit): qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) self.setPlainText(theText) - self.setDocumentChanged(True) self.updateDocMargins() + self.setDocumentChanged(True) qApp.restoreOverrideCursor() return True @@ -450,11 +452,17 @@ class GuiDocEditor(QTextEdit): lM = max(cM, fH) self.setViewportMargins(tM, uM, tM, lM) + docChanged = self.docChanged if self.mainConf.scrollPastEnd: docFrame = self.qDocument.rootFrame().frameFormat() docFrame.setBottomMargin(max(0, 0.6*(wH - uM - lM - 4*tB))) self.qDocument.rootFrame().setFrameFormat(docFrame) + # This is needed as the setFrameFormat function itself will + # trigger the contetsChanged signal which sets docChanged, so we + # set it back to whatever it was before. + self.setDocumentChanged(docChanged) + return def updateDocInfo(self, tHandle): diff --git a/nw/guimain.py b/nw/guimain.py index c7ad7e55..11ebbdff 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -432,6 +432,7 @@ class GuiMain(QMainWindow): # Restore previously open documents, if any if self.theProject.lastEdited is not None: self.openDocument(self.theProject.lastEdited, doScroll=True) + if self.theProject.lastViewed is not None: self.viewDocument(self.theProject.lastViewed) @@ -439,6 +440,12 @@ class GuiMain(QMainWindow): if self.theIndex.indexBroken: self.rebuildIndex() + # Make sure the changed status is set to false on all that was + # just opened + qApp.processEvents() + self.docEditor.setDocumentChanged(False) + self.theProject.setProjectChanged(False) + logger.debug("Project load complete") return True From a6cbdfc559e58e85b0debe1be81c244d7e985ecf Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 9 Jan 2021 20:07:09 +0100 Subject: [PATCH 2/4] Fix initial setting of project word count --- nw/core/project.py | 5 ++++- nw/guimain.py | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/nw/core/project.py b/nw/core/project.py index d6b6da78..75d86aaa 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -606,10 +606,13 @@ class NWProject(): self.theParent.setStatus("Opened Project: %s" % self.projName) self._scanProjectFolder() - self.setProjectChanged(False) + + self.currWCount = self.lastWCount self.projOpened = time() self.projAltered = False + self._writeLockFile() + self.setProjectChanged(False) return True diff --git a/nw/guimain.py b/nw/guimain.py index 11ebbdff..b9816c42 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -428,6 +428,7 @@ class GuiMain(QMainWindow): self.docEditor.setSpellCheck(self.theProject.spellCheck) self.mainMenu.setAutoOutline(self.theProject.autoOutline) self.statusBar.setRefTime(self.theProject.projOpened) + self.statusBar.setStats(self.theProject.currWCount, 0) # Restore previously open documents, if any if self.theProject.lastEdited is not None: From e2d553495998bb4ee0a388a9fe37a7c967ea6c6d Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 9 Jan 2021 20:21:32 +0100 Subject: [PATCH 3/4] Some minor code cleanup and redundant code removed --- nw/gui/statusbar.py | 24 +++++++----------------- nw/guimain.py | 2 +- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/nw/gui/statusbar.py b/nw/gui/statusbar.py index 8fceafa5..bbebb7cb 100644 --- a/nw/gui/statusbar.py +++ b/nw/gui/statusbar.py @@ -51,9 +51,6 @@ class GuiMainStatus(QStatusBar): self.theTheme = theParent.theTheme self.refTime = None - self.projWords = 0 - self.sessWords = 0 - colNone = QColor(*self.theTheme.statNone) colTrue = QColor(*self.theTheme.statUnsaved) colFalse = QColor(*self.theTheme.statSaved) @@ -182,26 +179,19 @@ class GuiMainStatus(QStatusBar): def setStats(self, pWC, sWC): """Set the current project statistics. """ - self.projWords = pWC - self.sessWords = sWC - self._updateStats() + self.statsText.setToolTip( + "Project word count (session change)" + ) + self.statsText.setText( + f"Words: {pWC:n} ({sWC:+n})" + ) + return ## # Internal Functions ## - def _updateStats(self): - """Update statistics. - """ - self.statsText.setToolTip( - "Project word count (session change)" - ) - self.statsText.setText( - f"Words: {self.projWords:n} ({self.sessWords:+n})" - ) - return - def _updateTime(self): """Update the session clock. """ diff --git a/nw/guimain.py b/nw/guimain.py index b9816c42..cc8f61bd 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -467,7 +467,7 @@ class GuiMain(QMainWindow): return False self.treeView.saveTreeOrder() - self.theProject.saveProject(autoSave) + self.theProject.saveProject(autoSave=autoSave) self.theIndex.saveIndex() return True From 73cd7f7513e7eb9ed9baf08ca77f11cea881af2d Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 9 Jan 2021 20:26:05 +0100 Subject: [PATCH 4/4] Improve doc changed checking and some code cleanup --- nw/gui/doceditor.py | 11 ++++++++--- nw/gui/statusbar.py | 9 ++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index c2978b00..3421276c 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -903,12 +903,13 @@ class GuiDocEditor(QTextEdit): ## @pyqtSlot(int, int, int) - def _docChange(self, thePos, charsRemoved, charsAdded): + def _docChange(self, thePos, chrRem, chrAdd): """Triggered by QTextDocument->contentsChanged. This also triggers the syntax highlighter. """ self.lastEdit = time() self.lastFind = None + if self.qDocument.characterCount() > nwConst.MAX_DOCSIZE: self.theParent.makeAlert(( "The document has grown too big and you cannot add more text to it. " @@ -916,12 +917,16 @@ class GuiDocEditor(QTextEdit): ) % (nwConst.MAX_DOCSIZE/1.0e6), nwAlert.ERROR) self.undo() return + if not self.docChanged: - self.setDocumentChanged(True) + self.setDocumentChanged(chrRem != 0 or chrAdd != 0) + if not self.wcTimer.isActive(): self.wcTimer.start() - if self.doReplace and charsAdded == 1: + + if self.doReplace and chrAdd == 1: self._docAutoReplace(self.qDocument.findBlock(thePos)) + return @pyqtSlot("QPoint") diff --git a/nw/gui/statusbar.py b/nw/gui/statusbar.py index bbebb7cb..e1738447 100644 --- a/nw/gui/statusbar.py +++ b/nw/gui/statusbar.py @@ -179,13 +179,8 @@ class GuiMainStatus(QStatusBar): def setStats(self, pWC, sWC): """Set the current project statistics. """ - self.statsText.setToolTip( - "Project word count (session change)" - ) - self.statsText.setText( - f"Words: {pWC:n} ({sWC:+n})" - ) - + self.statsText.setText(f"Words: {pWC:n} ({sWC:+n})") + self.statsText.setToolTip("Project word count (session change)") return ##