From f1b31f26d07211bcb7d4d3d593b1a9d813cda59f Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 9 Feb 2021 18:54:44 +0100 Subject: [PATCH] Add preferences for controlling session timer behaviour --- nw/config.py | 11 +++++++++++ nw/gui/doceditor.py | 25 +++++++++++++++---------- nw/gui/preferences.py | 31 +++++++++++++++++++++++++++++++ nw/gui/statusbar.py | 9 ++++++++- nw/guimain.py | 6 +++++- 5 files changed, 70 insertions(+), 12 deletions(-) diff --git a/nw/config.py b/nw/config.py index 028574ca..58229ea5 100644 --- a/nw/config.py +++ b/nw/config.py @@ -144,6 +144,9 @@ class Config: self.allowOpenDQuote = True # Allow open-ended double quotes self.highlightEmph = True # Add colour to text emphasis + self.stopWhenIdle = True # Stop the status bar clock when the user is idle + self.userIdleTime = 300 # Time of inactivity to consider user idle + ## User-Selected Symbols self.fmtApostrophe = nwUnicode.U_RSQUO self.fmtSingleQuotes = [nwUnicode.U_LSQUO, nwUnicode.U_RSQUO] @@ -532,6 +535,12 @@ class Config: self.highlightEmph = self._parseLine( cnfParse, cnfSec, "highlightemph", self.CNF_BOOL, self.highlightEmph ) + self.stopWhenIdle = self._parseLine( + cnfParse, cnfSec, "stopwhenidle", self.CNF_BOOL, self.stopWhenIdle + ) + self.userIdleTime = self._parseLine( + cnfParse, cnfSec, "useridletime", self.CNF_INT, self.userIdleTime + ) ## Backup cnfSec = "Backup" @@ -672,6 +681,8 @@ class Config: cnfParse.set(cnfSec, "allowopensquote", str(self.allowOpenSQuote)) cnfParse.set(cnfSec, "allowopendquote", str(self.allowOpenDQuote)) cnfParse.set(cnfSec, "highlightemph", str(self.highlightEmph)) + cnfParse.set(cnfSec, "stopwhenidle", str(self.stopWhenIdle)) + cnfParse.set(cnfSec, "useridletime", str(self.userIdleTime)) ## Backup cnfSec = "Backup" diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 9ebb400a..abafc6fb 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -91,6 +91,7 @@ class GuiDocEditor(QTextEdit): self.wordCount = 0 # Word count self.paraCount = 0 # Paragraph count self.lastEdit = 0 # Time stamp of last edit + self.lastActive = 0 # Time stamp of last activity self.lastFind = None # Position of the last found search word self.bigDoc = False # Flag for very large document size self.doReplace = False # Switch to temporarily disable auto-replace @@ -169,15 +170,16 @@ class GuiDocEditor(QTextEdit): self.clear() self.wcTimer.stop() - self.theHandle = None - self.charCount = 0 - self.wordCount = 0 - self.paraCount = 0 - self.lastEdit = 0 - self.lastFind = None - self.bigDoc = False - self.doReplace = False - self.queuePos = None + self.theHandle = None + self.charCount = 0 + self.wordCount = 0 + self.paraCount = 0 + self.lastEdit = 0 + self.lastActive = 0 + self.lastFind = None + self.bigDoc = False + self.doReplace = False + self.queuePos = None self.setDocumentChanged(False) self.docHeader.setTitleFromHandle(self.theHandle) @@ -319,6 +321,7 @@ class GuiDocEditor(QTextEdit): logger.debug("Document highlighted in %.3f ms" % (1000*(afTime-bfTime))) self.lastEdit = time() + self.lastActive = time() self._runCounter() self.wcTimer.start() self.theHandle = tHandle @@ -722,6 +725,7 @@ class GuiDocEditor(QTextEdit): return False self._allowAutoReplace(True) + self.lastActive = time() return True @@ -839,6 +843,7 @@ class GuiDocEditor(QTextEdit): * The undo/redo/select all sequences bypasses the docAction pathway from the menu, so we redirect them back from here. """ + self.lastActive = time() isReturn = keyEvent.key() == Qt.Key_Return isReturn |= keyEvent.key() == Qt.Key_Enter if isReturn and self.docSearch.anyFocus(): @@ -1083,7 +1088,7 @@ class GuiDocEditor(QTextEdit): logger.verbose("Word counter is busy") return - if time() - self.lastEdit < 5*self.wcInterval: + if time() - self.lastEdit < 5 * self.wcInterval: logger.verbose("Running word counter") self.theParent.threadPool.start(self.wCounter) diff --git a/nw/gui/preferences.py b/nw/gui/preferences.py index b7be1d05..4913be25 100644 --- a/nw/gui/preferences.py +++ b/nw/gui/preferences.py @@ -358,6 +358,33 @@ class GuiPreferencesProjects(QWidget): "If off, backups will run in the background." ) + # Session Timer + # ============= + self.mainForm.addGroupLabel("Session Timer") + + ## Pause when idle + self.stopWhenIdle = QSwitch() + self.stopWhenIdle.setChecked(self.mainConf.stopWhenIdle) + self.mainForm.addRow( + "Pause the session timer when not writing", + self.stopWhenIdle, + "Also pauses when the application window does not have focus." + ) + + ## Inactive time for idle + self.userIdleTime = QDoubleSpinBox() + self.userIdleTime.setMinimum(0.5) + self.userIdleTime.setMaximum(600.0) + self.userIdleTime.setSingleStep(0.5) + self.userIdleTime.setDecimals(1) + self.userIdleTime.setValue(self.mainConf.userIdleTime/60.0) + self.mainForm.addRow( + "Editor inactive time before pausing timer", + self.userIdleTime, + "User activity includes typing and changing the content.", + theUnit="minutes" + ) + return def saveValues(self): @@ -372,6 +399,10 @@ class GuiPreferencesProjects(QWidget): self.mainConf.backupOnClose = self.backupOnClose.isChecked() self.mainConf.askBeforeBackup = self.askBeforeBackup.isChecked() + # Session Timer + self.mainConf.stopWhenIdle = self.stopWhenIdle.isChecked() + self.mainConf.userIdleTime = round(self.userIdleTime.value() * 60) + self.mainConf.confChanged = True return diff --git a/nw/gui/statusbar.py b/nw/gui/statusbar.py index b7723963..441cacda 100644 --- a/nw/gui/statusbar.py +++ b/nw/gui/statusbar.py @@ -182,6 +182,9 @@ class GuiMainStatus(QStatusBar): def setUserIdle(self, userIdle): """Change the idle status icon. """ + if not self.mainConf.stopWhenIdle: + userIdle = False + if self.userIdle != userIdle: if userIdle: self.timeIcon.setPixmap(self.idlePixmap) @@ -198,7 +201,11 @@ class GuiMainStatus(QStatusBar): if self.refTime is None: self.timeText.setText("00:00:00") else: - self.timeText.setText(formatTime(round(time() - self.refTime - idleTime))) + if self.mainConf.stopWhenIdle: + sessTime = round(time() - self.refTime - idleTime) + else: + sessTime = round(time() - self.refTime) + self.timeText.setText(formatTime(sessTime)) return # END Class GuiMainStatus diff --git a/nw/guimain.py b/nw/guimain.py index 344bafbd..c72c6d46 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -420,7 +420,11 @@ class GuiMain(QMainWindow): self.closeDocument() self.docViewer.clearNavHistory() self.projView.closeOutline() + self.theProject.closeProject(self.idleTime) + self.idleRefTime = time() + self.idleTime = 0.0 + self.theIndex.clearIndex() self.clearGUI() self.hasProject = False @@ -1432,7 +1436,7 @@ class GuiMain(QMainWindow): return currTime = time() - editIdle = currTime - self.docEditor.lastEdit > 30.0 + editIdle = currTime - self.docEditor.lastActive > self.mainConf.userIdleTime userIdle = qApp.applicationState() != Qt.ApplicationActive if editIdle or userIdle: