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]
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
+4 -2
View File
@@ -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)
+4 -20
View File
@@ -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()
+30 -2
View File
@@ -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
+5 -3
View File
@@ -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