Move idle time records to the shared class

This commit is contained in:
Veronica Berglyd Olsen
2023-08-20 21:33:04 +02:00
parent 777b1a15fd
commit 0b4cfd0037
5 changed files with 44 additions and 29 deletions
+1 -2
View File
@@ -512,8 +512,7 @@ class NWProject(QObject):
tItem = self._tree[tHandle] tItem = self._tree[tHandle]
n += 1 n += 1
if tItem is None: if tItem is None:
# Technically a bug since treeOrder is built from the # Technically a bug
# same data as _projTree
continue continue
elif tItem.itemParent is None: elif tItem.itemParent is None:
# Item is a root, or already been identified as an # Item is a root, or already been identified as an
+4 -2
View File
@@ -229,8 +229,10 @@ class NWTree:
oParent = self.findRoot(oClass) oParent = self.findRoot(oClass)
if oParent is None: # Otherwise, add to the Novel root if oParent is None: # Otherwise, add to the Novel root
oParent = self.findRoot(nwItemClass.NOVEL) oParent = self.findRoot(nwItemClass.NOVEL)
if oParent is None: # If not, give up if oParent is None: # If not, create a new novel folder
continue 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 # Create a new item
newItem = NWItem(self._project, cHandle) newItem = NWItem(self._project, cHandle)
+4 -20
View File
@@ -116,8 +116,6 @@ class GuiMain(QMainWindow):
# Core Settings # Core Settings
self.isFocusMode = False self.isFocusMode = False
self.idleRefTime = time()
self.idleTime = 0.0
# Prepare Main Window # Prepare Main Window
self.resize(*CONFIG.mainWinSize) self.resize(*CONFIG.mainWinSize)
@@ -428,9 +426,7 @@ class GuiMain(QMainWindow):
self.itemDetails.clearDetails() self.itemDetails.clearDetails()
self.mainStatus.clearStatus() self.mainStatus.clearStatus()
SHARED.closeProject(self.idleTime) SHARED.closeProject()
self.idleRefTime = time()
self.idleTime = 0.0
self.docEditor.setDictionaries() self.docEditor.setDictionaries()
self._updateWindowTitle() self._updateWindowTitle()
@@ -489,10 +485,6 @@ class GuiMain(QMainWindow):
else: else:
return False return False
# Project is loaded
self.idleRefTime = time()
self.idleTime = 0.0
# Update GUI # Update GUI
self._updateWindowTitle(SHARED.project.data.name) self._updateWindowTitle(SHARED.project.data.name)
self.rebuildTrees() self.rebuildTrees()
@@ -1394,20 +1386,12 @@ class GuiMain(QMainWindow):
"""Process time tick of the main timer.""" """Process time tick of the main timer."""
if not SHARED.hasProject: if not SHARED.hasProject:
return return
currTime = time() currTime = time()
editIdle = currTime - self.docEditor.lastActive > CONFIG.userIdleTime editIdle = currTime - self.docEditor.lastActive > CONFIG.userIdleTime
userIdle = qApp.applicationState() != Qt.ApplicationActive userIdle = qApp.applicationState() != Qt.ApplicationActive
self.mainStatus.setUserIdle(editIdle or userIdle)
if editIdle or userIdle: SHARED.updateIdleTime(currTime, editIdle or userIdle)
self.idleTime += currTime - self.idleRefTime self.mainStatus.updateTime(idleTime=SHARED.idleTime)
self.mainStatus.setUserIdle(True)
else:
self.mainStatus.setUserIdle(False)
self.idleRefTime = currTime
self.mainStatus.updateTime(idleTime=self.idleTime)
return return
@pyqtSlot() @pyqtSlot()
+30 -2
View File
@@ -25,6 +25,7 @@ from __future__ import annotations
import logging import logging
from time import time
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from pathlib import Path from pathlib import Path
@@ -51,6 +52,8 @@ class SharedData(QObject):
self._project = None self._project = None
self._lockedBy = None self._lockedBy = None
self._alert = None self._alert = None
self._idleTime = 0.0
self._idleRedTime = time()
return return
@property @property
@@ -89,6 +92,11 @@ class SharedData(QObject):
"""Return a pointer to the last alert box.""" """Return a pointer to the last alert box."""
return self._alert return self._alert
@property
def idleTime(self) -> float:
"""Return the session idle time."""
return self._idleTime
## ##
# Methods # Methods
## ##
@@ -117,6 +125,8 @@ class SharedData(QObject):
self._lockedBy = self.project.lockStatus self._lockedBy = self.project.lockStatus
self._resetProject() self._resetProject()
self._resetIdleTimer()
return status return status
def saveProject(self, autoSave: bool = False) -> bool: def saveProject(self, autoSave: bool = False) -> bool:
@@ -126,10 +136,22 @@ class SharedData(QObject):
return False return False
return self.project.saveProject(autoSave=autoSave) return self.project.saveProject(autoSave=autoSave)
def closeProject(self, idleTime: float) -> None: def closeProject(self) -> None:
"""Close the current project.""" """Close the current project."""
self.project.closeProject(idleTime) self.project.closeProject(self._idleTime)
self._resetProject() 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 return
## ##
@@ -206,6 +228,12 @@ class SharedData(QObject):
self._project.statusMessage.connect(self._emitProjectStatusMeesage) self._project.statusMessage.connect(self._emitProjectStatusMeesage)
return return
def _resetIdleTimer(self) -> None:
"""Reset the timer data for the idle timer."""
self._idleRefTime = time()
self._idleTime = 0.0
return
# END Class SharedData # END Class SharedData
+5 -3
View File
@@ -354,10 +354,12 @@ def testCoreTree_CheckConsistency(caplog: pytest.LogCaptureFixture, mockGUI, fnc
assert itemX.itemClass == nwItemClass.NOVEL assert itemX.itemClass == nwItemClass.NOVEL
assert itemX.itemName == "[Recovered] Stuff" 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() theProject.tree.clear()
assert theProject.tree.checkConsistency("Recovered") == (4, 0) assert theProject.tree.checkConsistency("Recovered") == (4, 4)
assert len(theProject.tree) == 0 assert len(theProject.tree) == 5
nHandle = theProject.tree.findRoot(nwItemClass.NOVEL)
assert theProject.tree[nHandle].itemName == "Recovered" # type: ignore
# END Test testCoreTree_CheckConsistency # END Test testCoreTree_CheckConsistency