From 9ee92ecf486e7a0687d053ad79ff478a70853ac5 Mon Sep 17 00:00:00 2001
From: Mark Watkins <jedimark@users.sourceforge.net>
Date: Fri, 20 Jun 2014 12:06:57 +1000
Subject: [PATCH] Session::m_cnt should have been a float, fixes some ResMed
 summary issues

---
 sleepyhead/Graphs/gGraph.cpp                  |  7 ++--
 sleepyhead/Graphs/gGraphView.cpp              |  2 +-
 sleepyhead/Graphs/gSegmentChart.cpp           |  2 +-
 sleepyhead/SleepLib/day.cpp                   | 13 ++++---
 sleepyhead/SleepLib/day.h                     |  2 +-
 .../SleepLib/loader_plugins/resmed_loader.cpp | 34 +++++++++++++------
 .../SleepLib/loader_plugins/resmed_loader.h   |  2 +-
 sleepyhead/SleepLib/profiles.cpp              |  2 +-
 sleepyhead/SleepLib/session.cpp               | 19 ++++++-----
 sleepyhead/SleepLib/session.h                 |  8 ++---
 sleepyhead/mainwindow.cpp                     |  8 +----
 11 files changed, 55 insertions(+), 44 deletions(-)

diff --git a/sleepyhead/Graphs/gGraph.cpp b/sleepyhead/Graphs/gGraph.cpp
index d784c356..915da5a5 100644
--- a/sleepyhead/Graphs/gGraph.cpp
+++ b/sleepyhead/Graphs/gGraph.cpp
@@ -450,9 +450,10 @@ QPixmap gGraph::renderPixmap(int w, int h, bool printing)
 
     sg->setScaleY(1.0);
 
-    float dpr = sg->devicePixelRatio();
-    sg->setDevicePixelRatio(1);
+//    float dpr = sg->devicePixelRatio();
+//    sg->setDevicePixelRatio(1);
 
+//    bool b = sg->usePixmapCache();
     QPixmap pm(w,h);
 
     QPainter painter(&pm);
@@ -460,7 +461,7 @@ QPixmap gGraph::renderPixmap(int w, int h, bool printing)
     sg->renderGraphs(painter);
     painter.end();
 
-    sg->setDevicePixelRatio(dpr);
+//    sg->setDevicePixelRatio(dpr);
     //sg->doneCurrent();
     sg->trashGraphs();
 
diff --git a/sleepyhead/Graphs/gGraphView.cpp b/sleepyhead/Graphs/gGraphView.cpp
index a33ecb6b..a599bc1f 100644
--- a/sleepyhead/Graphs/gGraphView.cpp
+++ b/sleepyhead/Graphs/gGraphView.cpp
@@ -285,7 +285,7 @@ gGraphView::gGraphView(QWidget *parent, gGraphView *shared)
     m_limbo = false;
     m_fadedir = false;
     m_blockUpdates = false;
-    use_pixmap_cache = true;
+    use_pixmap_cache = p_profile->appearance->usePixmapCaching();
 
    // pixmapcache.setCacheLimit(10240*2);
 
diff --git a/sleepyhead/Graphs/gSegmentChart.cpp b/sleepyhead/Graphs/gSegmentChart.cpp
index 6140235f..ee486bd5 100644
--- a/sleepyhead/Graphs/gSegmentChart.cpp
+++ b/sleepyhead/Graphs/gSegmentChart.cpp
@@ -40,7 +40,7 @@ void gSegmentChart::SetDay(Day *d)
         m_values[c] = 0;
 
         for (QList<Session *>::iterator s = m_day->begin(); s != m_day->end(); ++s) {
-            if ((*s)->enabled() && (*s)->channelExists(m_codes[c])) {
+            if ((*s)->enabled() && (*s)->m_cnt.contains(m_codes[c])) {
                 int cnt = (*s)->count(m_codes[c]);
                 m_values[c] += cnt;
                 m_total += cnt;
diff --git a/sleepyhead/SleepLib/day.cpp b/sleepyhead/SleepLib/day.cpp
index 69618768..de5285d7 100644
--- a/sleepyhead/SleepLib/day.cpp
+++ b/sleepyhead/SleepLib/day.cpp
@@ -758,20 +758,20 @@ EventDataType Day::sph(ChannelID code)
     return sum;
 }
 
-int Day::count(ChannelID code)
+EventDataType Day::count(ChannelID code)
 {
-    int sum = 0;
+    EventDataType total = 0;
 
     QList<Session *>::iterator end = sessions.end();
     for (QList<Session *>::iterator it = sessions.begin(); it != end; ++it) {
         Session & sess = *(*it);
 
-        if (sess.enabled() && sess.channelExists(code)) {
-            sum += sess.count(code);
+        if (sess.enabled() && sess.m_cnt.contains(code)) {
+            total += sess.count(code);
         }
     }
 
-    return sum;
+    return total;
 }
 
 bool Day::summaryOnly()
@@ -849,6 +849,9 @@ bool Day::channelHasData(ChannelID id)
             if (sess.m_valuesummary.contains(id)) {
                 return true;
             }
+            if (sess.m_cnt.contains(id)) {
+                return true;
+            }
         }
     }
 
diff --git a/sleepyhead/SleepLib/day.h b/sleepyhead/SleepLib/day.h
index 4076927b..487ff5a1 100644
--- a/sleepyhead/SleepLib/day.h
+++ b/sleepyhead/SleepLib/day.h
@@ -44,7 +44,7 @@ class Day
     MachineType machine_type() const;
 
     //! \brief Returns the count of all this days sessions' events for this day
-    int count(ChannelID code);
+    EventDataType count(ChannelID code);
 
     //! \brief Returns the Minimum of all this sessions' events for this day
     EventDataType Min(ChannelID code);
diff --git a/sleepyhead/SleepLib/loader_plugins/resmed_loader.cpp b/sleepyhead/SleepLib/loader_plugins/resmed_loader.cpp
index dc437fe8..eb81077b 100644
--- a/sleepyhead/SleepLib/loader_plugins/resmed_loader.cpp
+++ b/sleepyhead/SleepLib/loader_plugins/resmed_loader.cpp
@@ -311,6 +311,8 @@ void ResmedLoader::ParseSTR(Machine *mach, QStringList strfiles)
                 QDateTime dontime = QDateTime::fromTime_t(ontime);
                 date = dontime.date();
                 R.date = date;
+
+                //CHECKME: Should I be taking noon day split time into account here?
                 strdate[date].push_back(&strsess.insert(ontime, R).value());
 
                 QDateTime dofftime = QDateTime::fromTime_t(offtime);
@@ -420,7 +422,7 @@ bool EDFParser::Parse()
         return false;
     }
 
-    dur_data_record = QString::fromLatin1(header.dur_data_records, 8).toDouble(&ok) * 1000.0;
+    dur_data_record = (QString::fromLatin1(header.dur_data_records, 8).toDouble(&ok) * 1000.0L);
 
     if (!ok) {
         return false;
@@ -1221,6 +1223,7 @@ int ResmedLoader::Open(QString path, Profile *profile)
     cnt=0;
     quint32 ignoreolder = PROFILE.session->ignoreOlderSessionsDate().toTime_t();
 
+    bool ignoreold = PROFILE.session->ignoreOlderSessions();
     // strsess end can change above.
     end = strsess.end();
 
@@ -1230,16 +1233,25 @@ int ResmedLoader::Open(QString path, Profile *profile)
 
 
     m->StartSaveThreads();
-    // Look for the nearest matching str record
+
+
+    /////////////////////////////////////////////////////////////////////////////////////////////
+    // Scan through unmatched strsess records, and attempt to get at summary data
+    /////////////////////////////////////////////////////////////////////////////////////////////
     for (it = strsess.begin(); it != end; ++it) {
         STRRecord & R = it.value();
 
-        if (R.maskon < ignoreolder) {
+        if (R.date == QDate(2013,6,20)) {
+            int i=5;
+        }
+
+        if (ignoreold && (R.maskon < ignoreolder)) {
             m->skipSaveTask();
             continue;
         }
 
         //Q_ASSERT(R.sessionid == 0);
+        // the following should not happen
         if (R.sessionid > 0) {
             m->skipSaveTask();
             continue;
@@ -1320,20 +1332,20 @@ int ResmedLoader::Open(QString path, Profile *profile)
 
                     // Add the time weighted proportion of the events counts
                     if (r->ai >= 0) {
-                        sess->setCount(CPAP_Obstructive, r->ai / ratio);
-                        sess->setCph(CPAP_Obstructive, (r->ai / ratio) / (time / 3600.0));
+                        sess->setCount(CPAP_Obstructive, r->ai * ratio);
+                        sess->setCph(CPAP_Obstructive, (r->ai * ratio) / (time / 3600.0));
                     }
                     if (r->uai >= 0) {
-                        sess->setCount(CPAP_Apnea, r->uai / ratio);
-                        sess->setCph(CPAP_Apnea, (r->uai / ratio) / (time / 3600.0));
+                        sess->setCount(CPAP_Apnea, r->uai * ratio);
+                        sess->setCph(CPAP_Apnea, (r->uai * ratio) / (time / 3600.0));
                     }
                     if (r->hi >= 0) {
-                        sess->setCount(CPAP_Hypopnea, r->hi / ratio);
-                        sess->setCph(CPAP_Hypopnea, (r->hi / ratio) / (time / 3600.0));
+                        sess->setCount(CPAP_Hypopnea, r->hi * ratio);
+                        sess->setCph(CPAP_Hypopnea, (r->hi * ratio) / (time / 3600.0));
                     }
                     if (r->cai >= 0) {
-                        sess->setCount(CPAP_ClearAirway, r->cai / ratio);
-                        sess->setCph(CPAP_ClearAirway, (r->ai / ratio) / (time / 3600.0));
+                        sess->setCount(CPAP_ClearAirway, r->cai * ratio);
+                        sess->setCph(CPAP_ClearAirway, (r->ai * ratio) / (time / 3600.0));
                     }
 
                 }
diff --git a/sleepyhead/SleepLib/loader_plugins/resmed_loader.h b/sleepyhead/SleepLib/loader_plugins/resmed_loader.h
index 01e0c50b..51e63561 100644
--- a/sleepyhead/SleepLib/loader_plugins/resmed_loader.h
+++ b/sleepyhead/SleepLib/loader_plugins/resmed_loader.h
@@ -23,7 +23,7 @@
 //********************************************************************************************
 // Please INCREMENT the following value when making changes to this loaders implementation.
 //
-const int resmed_data_version = 7;
+const int resmed_data_version = 8;
 //
 //********************************************************************************************
 
diff --git a/sleepyhead/SleepLib/profiles.cpp b/sleepyhead/SleepLib/profiles.cpp
index 836b117f..88caea95 100644
--- a/sleepyhead/SleepLib/profiles.cpp
+++ b/sleepyhead/SleepLib/profiles.cpp
@@ -113,7 +113,7 @@ void Profile::DataFormatError(Machine *m)
     QString msg =
         QObject::tr("Software changes have been made that require the reimporting of the following machines data:\n\n");
     msg = msg + m->properties[STR_PROP_Brand] + " " + m->properties[STR_PROP_Model] + " " +
-          m->properties[STR_PROP_Serial];
+          m->properties[STR_PROP_Serial] + "\n\n";
     msg = msg +
           QObject::tr("I can automatically purge this data for you, or you can cancel now and continue to run in a previous version.\n\n");
     msg = msg +
diff --git a/sleepyhead/SleepLib/session.cpp b/sleepyhead/SleepLib/session.cpp
index 78a80367..f3762cf5 100644
--- a/sleepyhead/SleepLib/session.cpp
+++ b/sleepyhead/SleepLib/session.cpp
@@ -30,7 +30,7 @@ const quint16 filetype_data = 1;
 
 // This is the uber important database version for SleepyHeads internal storage
 // Increment this after stuffing with Session's save & load code.
-const quint16 summary_version = 12;
+const quint16 summary_version = 13;
 const quint16 events_version = 10;
 
 Session::Session(Machine *m, SessionID session)
@@ -1232,7 +1232,7 @@ bool Session::channelExists(ChannelID id)
             return false;
         }
     } else {
-        QHash<ChannelID, int>::iterator q = m_cnt.find(id);
+        QHash<ChannelID, EventDataType>::iterator q = m_cnt.find(id);
 
         if (q == m_cnt.end()) {
             return false;
@@ -1244,7 +1244,7 @@ bool Session::channelExists(ChannelID id)
     return true;
 }
 
-int Session::rangeCount(ChannelID id, qint64 first, qint64 last)
+EventDataType Session::rangeCount(ChannelID id, qint64 first, qint64 last)
 {
     QHash<ChannelID, QVector<EventList *> >::iterator j = eventlist.find(id);
 
@@ -1253,7 +1253,7 @@ int Session::rangeCount(ChannelID id, qint64 first, qint64 last)
     }
 
     QVector<EventList *> &evec = j.value();
-    int sum = 0, cnt;
+    int total = 0, cnt;
 
     qint64 t, start;
 
@@ -1280,7 +1280,7 @@ int Session::rangeCount(ChannelID id, qint64 first, qint64 last)
             }
 
             t = (et - st) / ev.rate();
-            sum += t;
+            total += t;
         } else {
             cnt = ev.count();
             start = ev.first();
@@ -1292,15 +1292,16 @@ int Session::rangeCount(ChannelID id, qint64 first, qint64 last)
 
                 if (t >= first) {
                     if (t <= last) {
-                        sum++;
+                        total++;
                     } else { break; }
                 }
             }
         }
     }
 
-    return sum;
+    return (EventDataType)total;
 }
+
 double Session::rangeSum(ChannelID id, qint64 first, qint64 last)
 {
     QHash<ChannelID, QVector<EventList *> >::iterator j = eventlist.find(id);
@@ -1517,9 +1518,9 @@ EventDataType Session::rangeMax(ChannelID id, qint64 first, qint64 last)
     return max;
 }
 
-int Session::count(ChannelID id)
+EventDataType Session::count(ChannelID id)
 {
-    QHash<ChannelID, int>::iterator i = m_cnt.find(id);
+    QHash<ChannelID, EventDataType>::iterator i = m_cnt.find(id);
 
     if (i != m_cnt.end()) {
         return i.value();
diff --git a/sleepyhead/SleepLib/session.h b/sleepyhead/SleepLib/session.h
index 18e007d7..4f2d8ef5 100644
--- a/sleepyhead/SleepLib/session.h
+++ b/sleepyhead/SleepLib/session.h
@@ -151,7 +151,7 @@ class Session
     }
 
     // Session caches
-    QHash<ChannelID, int> m_cnt;
+    QHash<ChannelID, EventDataType> m_cnt;
     QHash<ChannelID, double> m_sum;
     QHash<ChannelID, EventDataType> m_avg;
     QHash<ChannelID, EventDataType> m_wavg;
@@ -180,7 +180,7 @@ class Session
     void destroyEvent(ChannelID code);
 
     // UpdateSummaries may recalculate all these, but it may be faster setting upfront
-    void setCount(ChannelID id, int val) { m_cnt[id] = val; }
+    void setCount(ChannelID id, EventDataType val) { m_cnt[id] = val; }
     void setSum(ChannelID id, EventDataType val) { m_sum[id] = val; }
     void setMin(ChannelID id, EventDataType val) { m_min[id] = val; }
     void setMax(ChannelID id, EventDataType val) { m_max[id] = val; }
@@ -215,10 +215,10 @@ class Session
     void setFirst(ChannelID id, qint64 val) { m_firstchan[id] = val; }
     void setLast(ChannelID id, qint64 val) { m_lastchan[id] = val; }
 
-    int count(ChannelID id);
+    EventDataType count(ChannelID id);
 
     //! \brief Returns the Count of all events of type id between time range
-    int rangeCount(ChannelID id, qint64 first, qint64 last);
+    EventDataType rangeCount(ChannelID id, qint64 first, qint64 last);
 
     //! \brief Returns the Sum of all events of type id between time range
     double rangeSum(ChannelID id, qint64 first, qint64 last);
diff --git a/sleepyhead/mainwindow.cpp b/sleepyhead/mainwindow.cpp
index 318b0ef0..67706462 100644
--- a/sleepyhead/mainwindow.cpp
+++ b/sleepyhead/mainwindow.cpp
@@ -418,14 +418,8 @@ void MainWindow::Startup()
 
     SnapshotGraph = new gGraphView(this, daily->graphView());
 
-    // the following are platform overides for the UsePixmapCache preference settings
-#ifdef Q_OS_MAC
-    //Mac needs this to be able to offscreen render
-    SnapshotGraph->setUsePixmapCache(true);
-#else
-    //Windows & Linux barfs when offscreen rendering with pixmap cached text
+    // Snapshot graphs mess up with pixmap cache
     SnapshotGraph->setUsePixmapCache(false);
-#endif
 
    // SnapshotGraph->setFormat(daily->graphView()->format());
     //SnapshotGraph->setMaximumSize(1024,512);