Add a local, per-project time-spent tracker (#576)

Adds a ProjectUsageTracker (sources/project/) that accumulates how
long a project has been the active tab, using QElapsedTimer so the
value is computed on demand rather than via polling. It's hosted on
ProjectPropertiesHandler per that class's own stated design intent
("all new properties should be managed by this class").

The accumulated time is persisted as a new <usage time_spent="N"
enabled="true|false"/> element, a sibling of <properties> in the
project XML, written/read by new QETProject::writeUsageXml()/
readUsageXml(). It rides along on the existing autosave path for
free, since writeBackup() already serializes the full project via
toXml().

QETDiagramEditor::subWindowActivated() now pauses every open
project's tracker except the one whose tab just became current, so
switching between several open projects keeps each project's tracked
time isolated.

Surfaced in the existing Project Properties "Général" page: a
"Temps passé sur ce projet" display, a "Réinitialiser" button, and
an opt-out checkbox ("uniquement enregistré localement dans ce
fichier" - this is local-only, never transmitted anywhere).

Verified beyond compiling: full CMake build, then an actual runtime
session confirming the saved XML's time_spent value, that closing
and reopening the project round-trips and resumes timing, that the
reset button works, and - the key correctness check - that with two
projects open, the inactive one's time_spent stays frozen while the
active one accumulates real elapsed time, over the same interval.

See discussion #576.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ispyisail
2026-07-31 09:52:32 +12:00
parent 120ca08209
commit 464d516537
11 changed files with 302 additions and 1 deletions
+27
View File
@@ -29,6 +29,9 @@
#include "elementspanelwidget.h"
#include "factory/qetgraphicstablefactory.h"
#include "print/projectprintwindow.h"
#include "project/projectpropertieshandler.h"
#include "projectview.h"
#include "qetproject.h"
#include "qetgraphicsitem/ViewItem/qetgraphicstableitem.h"
#include "qetgraphicsitem/conductortextitem.h"
#include "qetgraphicsitem/dynamicelementtextitem.h"
@@ -2466,6 +2469,30 @@ void QETDiagramEditor::subWindowActivated(QMdiSubWindow *subWindows)
slot_updateActions();
slot_updateWindowsMenu();
emit syncElementsPanel();
updateUsageTrackersActiveState();
}
/**
@brief QETDiagramEditor::updateUsageTrackersActiveState
Mark the currently active project's usage tracker (time spent on this
project) as active, and every other opened project's tracker as
inactive. Called whenever the current MDI subwindow changes.
Known limitation: this only accounts for tab switches within this
QETDiagramEditor window. If the same project were ever shown as the
active tab in two different windows at once, its tracked time could be
double-counted -- today QETApp only ever gives a project one ProjectView,
so this doesn't happen in practice.
*/
void QETDiagramEditor::updateUsageTrackersActiveState()
{
QETProject *active_project = currentProject();
const QList<ProjectView *> project_views = openedProjects();
for (ProjectView *project_view : project_views) {
if (QETProject *project = project_view->project()) {
project->projectPropertiesHandler().usageTracker().setActive(project == active_project);
}
}
}
/**