From 358d1969e00025b80fb4dcccf8f8bedca0578471 Mon Sep 17 00:00:00 2001
From: Mark Watkins <jedimark@users.sourceforge.net>
Date: Tue, 30 Sep 2014 20:16:33 +1000
Subject: [PATCH] By Pressure pixmap caching cleanup

---
 sleepyhead/Graphs/MinutesAtPressure.cpp | 24 +++++++++++++++---------
 sleepyhead/Graphs/gGraphView.cpp        |  1 +
 sleepyhead/SleepLib/session.cpp         |  4 +++-
 sleepyhead/main.cpp                     | 10 +++++++++-
 sleepyhead/reports.cpp                  |  8 ++++++--
 5 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/sleepyhead/Graphs/MinutesAtPressure.cpp b/sleepyhead/Graphs/MinutesAtPressure.cpp
index 1f08ff87..907126fb 100644
--- a/sleepyhead/Graphs/MinutesAtPressure.cpp
+++ b/sleepyhead/Graphs/MinutesAtPressure.cpp
@@ -154,9 +154,6 @@ void MinutesAtPressure::paint(QPainter &painter, gGraph &graph, const QRegion &r
     m_lastmaxx = m_maxx;
 
     QMap<EventStoreType, int>::iterator it;
-    painter.setFont(*defaultfont);
-    painter.setPen(Qt::black);
-
     if (graph.printing()) {
         // lock the other mutex...
 //        while (recalculating()) {};
@@ -164,11 +161,13 @@ void MinutesAtPressure::paint(QPainter &painter, gGraph &graph, const QRegion &r
         while (recalculating()) {};
 
     }
-    if (!painter.isActive()) return;
 
+    if (!painter.isActive()) return;
 
     // Lock the stuff we need to draw
     timelock.lock();
+    painter.setFont(*defaultfont);
+    painter.setPen(Qt::black);
 
     QMap<EventStoreType, int>::iterator times_end = times.end();
     QPoint mouse = graph.graphView()->currentMousePos();
@@ -205,11 +204,15 @@ void MinutesAtPressure::paint(QPainter &painter, gGraph &graph, const QRegion &r
         QString value = QString("%1").arg(float(it.value()) / 60.0, 5, 'f', 1);
         QRectF rec(xpos, top, pix-1, hix);
 
-        painter.fillRect(rec, QColor("orange"));
-        graph.renderText(text, rec, Qt::AlignCenter);
+        GetTextExtent(text, w,h);
 
-        rec.moveTop(top + hix);
-        graph.renderText(value, rec, Qt::AlignCenter);
+        painter.fillRect(rec, QColor("orange"));
+        graph.renderText(text, xpos + pix/2 - w/2, top + hix /2 + h/2);
+
+        GetTextExtent(value, w,h);
+
+//        rec.moveTop(top + hix);
+        graph.renderText(value, xpos + pix/2 - w/2, top + hix+ hix /2+ h/2);
 
         xpos += pix;
     }
@@ -270,8 +273,11 @@ void MinutesAtPressure::paint(QPainter &painter, gGraph &graph, const QRegion &r
                 painter.fillRect(rec, QColor(245,245,255,240));
             }
 
+            text = QString(fmt).arg(value,5,'f',2);
 
-            graph.renderText(QString(fmt).arg(value,5,'f',2), rec, Qt::AlignCenter);
+            GetTextExtent(text, w,h);
+
+            graph.renderText(text, xpos + pix/2 - w/2, ypos + hix /2 + h/2);
           //  painter.drawText(rec, Qt::AlignCenter, QString(fmt).arg(value,5,'f',2));
             xpos += pix;
 
diff --git a/sleepyhead/Graphs/gGraphView.cpp b/sleepyhead/Graphs/gGraphView.cpp
index e71dee9b..db403253 100644
--- a/sleepyhead/Graphs/gGraphView.cpp
+++ b/sleepyhead/Graphs/gGraphView.cpp
@@ -767,6 +767,7 @@ void gGraphView::DrawTextQue(QPainter &painter)
             }
         } else {
             // Just draw the fonts..
+
             painter.setPen(QColor(q.color));
             painter.setFont(*q.font);
 
diff --git a/sleepyhead/SleepLib/session.cpp b/sleepyhead/SleepLib/session.cpp
index 0242a893..54857ad8 100644
--- a/sleepyhead/SleepLib/session.cpp
+++ b/sleepyhead/SleepLib/session.cpp
@@ -414,7 +414,9 @@ bool Session::LoadSummary()
     qint32 ts32;
     in >> ts32;      // MachineID (dont need this result)
 
+    bool upgrade = false;
     if (ts32 != s_machine->id()) {
+        upgrade = true;
         qWarning() << "Machine ID does not match in" << filename <<
                    " I will try to load anyway in case you know what your doing.";
     }
@@ -607,7 +609,7 @@ bool Session::LoadSummary()
     }
 
     // not really a good idea to do this... should flag and do a reindex
-    if (version < summary_version) {
+    if (upgrade || (version < summary_version)) {
 
         qDebug() << "Upgrading Summary file to version" << summary_version;
         if (!s_summaryOnly) {
diff --git a/sleepyhead/main.cpp b/sleepyhead/main.cpp
index c41841c1..ead45592 100644
--- a/sleepyhead/main.cpp
+++ b/sleepyhead/main.cpp
@@ -333,10 +333,18 @@ retry_directory:
     } else {
         if (PREF.contains(STR_PREF_VersionString)) {
 
-            if (compareVersion(PREF[STR_PREF_VersionString].toString()) < 0) {
+            int vc = compareVersion(PREF[STR_PREF_VersionString].toString());
+            if (vc < 0) {
                 release_notes();
 
                 check_updates = false;
+            } else if (vc > 0) {
+                if (QMessageBox::warning(nullptr, STR_MessageBox_Error, QObject::tr("The version of SleepyHead you just ran is OLDER than the one used to create this data (%1).").arg(PREF[STR_PREF_VersionString].toString()) +"\n\n"+
+                                         QObject::tr("It is likely that doing this will cause data corruption, are you sure you want to do this?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No) {
+
+                    return 0;
+                }
+
             }
         }
 
diff --git a/sleepyhead/reports.cpp b/sleepyhead/reports.cpp
index 79338d35..e6f7841b 100644
--- a/sleepyhead/reports.cpp
+++ b/sleepyhead/reports.cpp
@@ -438,7 +438,10 @@ void Report::PrintReport(gGraphView *gv, QString name, QDate date)
 
                 if (!g->visible()) { continue; }
 
-                if (cpap) {
+                if (cpap && oxi) {
+                    st = qMin(day->first(MT_CPAP), day->first(MT_OXIMETER));
+                    et = qMax(day->last(MT_CPAP), day->last(MT_OXIMETER));
+                } else if (cpap) {
                     st = day->first(MT_CPAP);
                     et = day->last(MT_CPAP);
                 } else if (oxi) {
@@ -447,7 +450,8 @@ void Report::PrintReport(gGraphView *gv, QString name, QDate date)
                 }
 
                 if (!g->isSnapshot() && (g->name() == schema::channel[CPAP_FlowRate].code())) {
-                    if (!((qAbs(g->min_x - st) < 2000) && (qAbs(g->max_x - et) < 2000))) {
+                    if (!((qAbs(g->min_x - st) < 5000) && (qAbs(g->max_x - et) < 60000))) {
+                        qDebug() << "Current Selection difference" << (g->min_x - st) << " bleh " << (g->max_x - et);
                         start.push_back(st);
                         end.push_back(et);
                         graphs.push_back(g);