From 0b4cfd0037dcec979d7b1bd425312f2422b1d0c6 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 20 Aug 2023 21:33:04 +0200 Subject: [PATCH] Move idle time records to the shared class --- novelwriter/core/project.py | 3 +-- novelwriter/core/tree.py | 6 ++++-- novelwriter/guimain.py | 24 ++++------------------- novelwriter/shared.py | 32 +++++++++++++++++++++++++++++-- tests/test_core/test_core_tree.py | 8 +++++--- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index b0ed4ad2..b1abef91 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -512,8 +512,7 @@ class NWProject(QObject): tItem = self._tree[tHandle] n += 1 if tItem is None: - # Technically a bug since treeOrder is built from the - # same data as _projTree + # Technically a bug continue elif tItem.itemParent is None: # Item is a root, or already been identified as an diff --git a/novelwriter/core/tree.py b/novelwriter/core/tree.py index 31969a9d..a9643fc9 100644 --- a/novelwriter/core/tree.py +++ b/novelwriter/core/tree.py @@ -229,8 +229,10 @@ class NWTree: oParent = self.findRoot(oClass) if oParent is None: # Otherwise, add to the Novel root oParent = self.findRoot(nwItemClass.NOVEL) - if oParent is None: # If not, give up - continue + if oParent is None: # If not, create a new novel folder + oParent = self.create(prefix, None, nwItemType.ROOT, nwItemClass.NOVEL) + + assert oParent is not None # Otherwise there's an issue with self.create() # Create a new item newItem = NWItem(self._project, cHandle) diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py index d303491c..596ccb95 100644 --- a/novelwriter/guimain.py +++ b/novelwriter/guimain.py @@ -116,8 +116,6 @@ class GuiMain(QMainWindow): # Core Settings self.isFocusMode = False - self.idleRefTime = time() - self.idleTime = 0.0 # Prepare Main Window self.resize(*CONFIG.mainWinSize) @@ -428,9 +426,7 @@ class GuiMain(QMainWindow): self.itemDetails.clearDetails() self.mainStatus.clearStatus() - SHARED.closeProject(self.idleTime) - self.idleRefTime = time() - self.idleTime = 0.0 + SHARED.closeProject() self.docEditor.setDictionaries() self._updateWindowTitle() @@ -489,10 +485,6 @@ class GuiMain(QMainWindow): else: return False - # Project is loaded - self.idleRefTime = time() - self.idleTime = 0.0 - # Update GUI self._updateWindowTitle(SHARED.project.data.name) self.rebuildTrees() @@ -1394,20 +1386,12 @@ class GuiMain(QMainWindow): """Process time tick of the main timer.""" if not SHARED.hasProject: return - currTime = time() editIdle = currTime - self.docEditor.lastActive > CONFIG.userIdleTime userIdle = qApp.applicationState() != Qt.ApplicationActive - - if editIdle or userIdle: - self.idleTime += currTime - self.idleRefTime - self.mainStatus.setUserIdle(True) - else: - self.mainStatus.setUserIdle(False) - - self.idleRefTime = currTime - self.mainStatus.updateTime(idleTime=self.idleTime) - + self.mainStatus.setUserIdle(editIdle or userIdle) + SHARED.updateIdleTime(currTime, editIdle or userIdle) + self.mainStatus.updateTime(idleTime=SHARED.idleTime) return @pyqtSlot() diff --git a/novelwriter/shared.py b/novelwriter/shared.py index 20a4677f..6c115fca 100644 --- a/novelwriter/shared.py +++ b/novelwriter/shared.py @@ -25,6 +25,7 @@ from __future__ import annotations import logging +from time import time from typing import TYPE_CHECKING from pathlib import Path @@ -51,6 +52,8 @@ class SharedData(QObject): self._project = None self._lockedBy = None self._alert = None + self._idleTime = 0.0 + self._idleRedTime = time() return @property @@ -89,6 +92,11 @@ class SharedData(QObject): """Return a pointer to the last alert box.""" return self._alert + @property + def idleTime(self) -> float: + """Return the session idle time.""" + return self._idleTime + ## # Methods ## @@ -117,6 +125,8 @@ class SharedData(QObject): self._lockedBy = self.project.lockStatus self._resetProject() + self._resetIdleTimer() + return status def saveProject(self, autoSave: bool = False) -> bool: @@ -126,10 +136,22 @@ class SharedData(QObject): return False return self.project.saveProject(autoSave=autoSave) - def closeProject(self, idleTime: float) -> None: + def closeProject(self) -> None: """Close the current project.""" - self.project.closeProject(idleTime) + self.project.closeProject(self._idleTime) self._resetProject() + self._resetIdleTimer() + return + + def updateIdleTime(self, currTime: float, userIdle: bool) -> None: + """Update the idle time record. If the userIdle flag is True, + the user idle counter is updated with the time difference since + the last time this function was called. Otherwise, only the + reference time is updated. + """ + if userIdle: + self._idleTime += currTime - self._idleRefTime + self._idleRefTime = currTime return ## @@ -206,6 +228,12 @@ class SharedData(QObject): self._project.statusMessage.connect(self._emitProjectStatusMeesage) return + def _resetIdleTimer(self) -> None: + """Reset the timer data for the idle timer.""" + self._idleRefTime = time() + self._idleTime = 0.0 + return + # END Class SharedData diff --git a/tests/test_core/test_core_tree.py b/tests/test_core/test_core_tree.py index 2bac97da..cc5a6724 100644 --- a/tests/test_core/test_core_tree.py +++ b/tests/test_core/test_core_tree.py @@ -354,10 +354,12 @@ def testCoreTree_CheckConsistency(caplog: pytest.LogCaptureFixture, mockGUI, fnc assert itemX.itemClass == nwItemClass.NOVEL assert itemX.itemName == "[Recovered] Stuff" - # If the tree is empty, there is nowhere to add any of the 4 files + # If the tree is empty, a new root folder is created theProject.tree.clear() - assert theProject.tree.checkConsistency("Recovered") == (4, 0) - assert len(theProject.tree) == 0 + assert theProject.tree.checkConsistency("Recovered") == (4, 4) + assert len(theProject.tree) == 5 + nHandle = theProject.tree.findRoot(nwItemClass.NOVEL) + assert theProject.tree[nHandle].itemName == "Recovered" # type: ignore # END Test testCoreTree_CheckConsistency