From 5e8047f528331e6ed4f8e16a8b435031da230906 Mon Sep 17 00:00:00 2001 From: Mark Watkins Date: Sat, 11 Oct 2014 13:07:29 +1000 Subject: [PATCH] Invalidate hour cache when enabling/disabling sessions --- sleepyhead/SleepLib/day.cpp | 2 +- sleepyhead/SleepLib/profiles.cpp | 33 ++++++++++++++++++++++++++++++++ sleepyhead/SleepLib/profiles.h | 1 + sleepyhead/SleepLib/session.cpp | 11 +++++++++++ sleepyhead/SleepLib/session.h | 2 +- 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/sleepyhead/SleepLib/day.cpp b/sleepyhead/SleepLib/day.cpp index 7da68f9b..01f5c4b8 100644 --- a/sleepyhead/SleepLib/day.cpp +++ b/sleepyhead/SleepLib/day.cpp @@ -791,7 +791,7 @@ qint64 Day::total_time(MachineType type) Session &sess = *(*it); int slicesize = sess.m_slices.size(); - if ((sess.type() == type) && sess.enabled()) { + if (sess.enabled() && (sess.type() == type)) { first = sess.first(); last = sess.last(); diff --git a/sleepyhead/SleepLib/profiles.cpp b/sleepyhead/SleepLib/profiles.cpp index f016cffd..fb1a7455 100644 --- a/sleepyhead/SleepLib/profiles.cpp +++ b/sleepyhead/SleepLib/profiles.cpp @@ -1260,6 +1260,39 @@ EventDataType Profile::calcBelowThreshold(ChannelID code, EventDataType threshol return val; } +Day * Profile::findSessionDay(Session * session) +{ + MachineType mt = session->type(); + + QDate start = LastGoodDay(mt); + QDate end = LastGoodDay(mt); + + QDate date = start; + + if (date.isNull()) { + return 0; + } + + double val = 0; + int cnt = 0; + + do { + Day *day = FindGoodDay(date, mt); + + if (day) { + for (int i=0; isize(); i++) { + Session * s = day->sessions.at(i); + if (s == session) { + return day; + } + } + } + + date = date.addDays(1); + } while (date <= end); + return nullptr; +} + EventDataType Profile::calcAvg(ChannelID code, MachineType mt, QDate start, QDate end) { diff --git a/sleepyhead/SleepLib/profiles.h b/sleepyhead/SleepLib/profiles.h index 2b482907..2db57c8d 100644 --- a/sleepyhead/SleepLib/profiles.h +++ b/sleepyhead/SleepLib/profiles.h @@ -181,6 +181,7 @@ class Profile : public Preferences QDate start = QDate(), QDate end = QDate()); + Day * findSessionDay(Session * session); // XML load components virtual void ExtraLoad(QDomElement &root); diff --git a/sleepyhead/SleepLib/session.cpp b/sleepyhead/SleepLib/session.cpp index 4f27d145..168dab76 100644 --- a/sleepyhead/SleepLib/session.cpp +++ b/sleepyhead/SleepLib/session.cpp @@ -86,6 +86,17 @@ void Session::TrashEvents() eventlist.squeeze(); } +void Session::setEnabled(bool b) +{ + s_enabled = b; + // not so simple.. we have to invalidate the hours cache in the day record.. + + Day * day = p_profile->findSessionDay(this); + if (day) { + day->invalidate(); + } +} + QString Session::eventFile() const { return s_machine->getEventsPath()+QString().sprintf("%08lx.001", s_session); diff --git a/sleepyhead/SleepLib/session.h b/sleepyhead/SleepLib/session.h index a072a5a0..90f6305a 100644 --- a/sleepyhead/SleepLib/session.h +++ b/sleepyhead/SleepLib/session.h @@ -111,7 +111,7 @@ class Session inline bool enabled() const { return s_enabled; } //! \brief Sets whether or not session is being used. - void setEnabled(bool b) { s_enabled = b; } + void setEnabled(bool b); inline qint64 realFirst() const { return s_first; } inline qint64 realLast() const { return s_last; }