From cd87d10736126ea34926d42cc61aec6dd052ed54 Mon Sep 17 00:00:00 2001
From: Mark Watkins <jedimark@users.sourceforge.net>
Date: Mon, 21 Jul 2014 02:22:51 +1000
Subject: [PATCH] Added current pressure settings to alt-key graph hover

---
 sleepyhead/Graphs/gLineChart.cpp   | 22 +++++++++++++++++++---
 sleepyhead/Graphs/gLineOverlay.cpp | 10 ++++++----
 sleepyhead/SleepLib/day.cpp        | 16 ++++++++++++++++
 sleepyhead/SleepLib/day.h          |  6 ++++--
 sleepyhead/SleepLib/session.cpp    | 29 +++++++++++++++--------------
 sleepyhead/SleepLib/session.h      |  4 ++--
 6 files changed, 62 insertions(+), 25 deletions(-)

diff --git a/sleepyhead/Graphs/gLineChart.cpp b/sleepyhead/Graphs/gLineChart.cpp
index 14b54c14..109a170e 100644
--- a/sleepyhead/Graphs/gLineChart.cpp
+++ b/sleepyhead/Graphs/gLineChart.cpp
@@ -165,6 +165,7 @@ bool gLineChart::mouseMoveEvent(QMouseEvent *event, gGraph *graph)
     return true;
 }
 
+
 // Time Domain Line Chart
 void gLineChart::paint(QPainter &painter, gGraph &w, const QRegion &region)
 {
@@ -238,15 +239,30 @@ void gLineChart::paint(QPainter &painter, gGraph &w, const QRegion &region)
         QPoint mouse = w.graphView()->currentMousePos();
         double pos = mouse.x() - left;
         if (pos > 0) {
-            double xval = minx + (pos * (xx / double(width)));
+            qint64 xpos = minx + (pos * (xx / double(width)));
 
+            EventDataType val = 0, val2 = 0, val3 = 0;
+            QString pressure;
+            if (m_day) {
+                CPAPMode mode = (CPAPMode)round(m_day->settings_wavg(CPAP_Mode));
+                if (mode >= MODE_BIPAP) {
+                    val = m_day->lookupValue(CPAP_EPAP, xpos);
+                    val2 = m_day->lookupValue(CPAP_IPAP, xpos);
+                    val3 = val2 - val;
+                    pressure=QString("%1: %2%3 %4:%5%3 %6:%7%3").arg(STR_TR_EPAP).arg(val,0,'f',1).arg(STR_UNIT_CMH2O).arg(STR_TR_IPAP).arg(val2,0,'f',1).arg(STR_TR_PS).arg(val3,0,'f',1);
+
+                } else {
+                    val = m_day->lookupValue(CPAP_Pressure, xpos);
+                    pressure=QString("%1: %2%3").arg(STR_TR_Pressure).arg(val).arg(STR_UNIT_CMH2O);
+                }
+            }
 
             painter.setPen(QPen(QBrush(QColor(Qt::gray)),1));
             painter.drawLine(mouse.x(), top-w.marginTop()-3, mouse.x(), top+height+w.bottom-1);
 
-            QDateTime dt=QDateTime::fromMSecsSinceEpoch(xval);
+            QDateTime dt=QDateTime::fromMSecsSinceEpoch(xpos);
 
-            QString text = dt.toString("MMM dd - HH:mm:ss:zzz");
+            QString text = dt.toString("MMM dd - HH:mm:ss:zzz")+" "+pressure;
 
             int wid, h;
             GetTextExtent(text, wid, h);
diff --git a/sleepyhead/Graphs/gLineOverlay.cpp b/sleepyhead/Graphs/gLineOverlay.cpp
index be54cc3f..81502197 100644
--- a/sleepyhead/Graphs/gLineOverlay.cpp
+++ b/sleepyhead/Graphs/gLineOverlay.cpp
@@ -241,18 +241,19 @@ void gLineOverlayBar::paint(QPainter &painter, gGraph &w, const QRegion &region)
                         painter.drawLine(x1, z, x1, z - 12);
                     }
 
+                    QColor txcol = hover ? Qt::red: Qt::black;
                     if (xx < 300000) {
-                        QString lab = schema::channel[m_code].fullname();
+                        QString lab = QString("%1 (%2)").arg(schema::channel[m_code].fullname()).arg(raw);
                         GetTextExtent(lab, x, y);
-                        w.renderText(lab, x1 - (x / 2)+2, top - y + (3 * w.printScaleY()));
+                        w.renderText(lab, x1 - (x / 2)+2, top - y + (3 * w.printScaleY()),0,txcol);
                     } else if (xx < (3600000)) {
                         if (!hover) {
                             GetTextExtent(m_label, x, y);
-                            w.renderText(m_label, x1 - (x / 2)+2, top - y + (3 * w.printScaleY()));
+                            w.renderText(m_label, x1 - (x / 2)+2, top - y + (3 * w.printScaleY()),0,txcol);
                         } else {
                             QString lab = QString("%1 (%2)").arg(m_label).arg(raw);
                             GetTextExtent(lab, x, y);
-                            w.renderText(lab, x1 - (x / 2)+2, top - y + (3 * w.printScaleY()),0,Qt::red);
+                            w.renderText(lab, x1 - (x / 2)+2, top - y + (3 * w.printScaleY()),0,txcol);
                         }
                     }
                 }
@@ -265,6 +266,7 @@ void gLineOverlayBar::paint(QPainter &painter, gGraph &w, const QRegion &region)
 bool gLineOverlayBar::mouseMoveEvent(QMouseEvent *event, gGraph *graph)
 {
     Q_UNUSED(event)
+    Q_UNUSED(graph)
     return true;
 }
 
diff --git a/sleepyhead/SleepLib/day.cpp b/sleepyhead/SleepLib/day.cpp
index 928d1990..82d7b2f8 100644
--- a/sleepyhead/SleepLib/day.cpp
+++ b/sleepyhead/SleepLib/day.cpp
@@ -55,6 +55,22 @@ void Day::AddSession(Session *s)
     sessions.push_back(s);
 }
 
+EventDataType Day::lookupValue(ChannelID code, qint64 time)
+{
+    QList<Session *>::iterator end = sessions.end();
+    for (QList<Session *>::iterator it = sessions.begin(); it != end; ++it) {
+        Session &sess = *(*it);
+
+        if (sess.enabled()) {
+            if ((time > sess.first()) && (time < sess.last())) {
+                return sess.SearchValue(code,time);
+            }
+        }
+    }
+    return 0;
+}
+
+
 EventDataType Day::timeAboveThreshold(ChannelID code, EventDataType threshold)
 {
     EventDataType val = 0;
diff --git a/sleepyhead/SleepLib/day.h b/sleepyhead/SleepLib/day.h
index 487ff5a1..b6a159d9 100644
--- a/sleepyhead/SleepLib/day.h
+++ b/sleepyhead/SleepLib/day.h
@@ -98,11 +98,13 @@ class Day
     EventDataType settings_max(ChannelID code);
 
     //! \brief Returns the amount of time (in decimal minutes) the Channel spent above the threshold
-    EventDataType timeAboveThreshold(ChannelID id, EventDataType threshold);
+    EventDataType timeAboveThreshold(ChannelID code, EventDataType threshold);
 
     //! \brief Returns the amount of time (in decimal minutes) the Channel spent below the threshold
-    EventDataType timeBelowThreshold(ChannelID id, EventDataType threshold);
+    EventDataType timeBelowThreshold(ChannelID code, EventDataType threshold);
 
+    //! \brief Returns the value for Channel code at a given time
+    EventDataType lookupValue(ChannelID code, qint64 time);
 
     //! \brief Returns the first session time of this day
     qint64 first();
diff --git a/sleepyhead/SleepLib/session.cpp b/sleepyhead/SleepLib/session.cpp
index fbed7490..852d048a 100644
--- a/sleepyhead/SleepLib/session.cpp
+++ b/sleepyhead/SleepLib/session.cpp
@@ -937,8 +937,10 @@ void Session::UpdateSummaries()
             last(id);
             first(id);
 
-            if (((id == CPAP_FlowRate) || (id == CPAP_MaskPressureHi) || (id == CPAP_RespEvent)
-                    || (id == CPAP_MaskPressure))) {
+            if (((id == CPAP_FlowRate)
+                 || (id == CPAP_MaskPressureHi)
+                 || (id == CPAP_RespEvent)
+                 || (id == CPAP_MaskPressure))) {
                 continue;
             }
 
@@ -950,42 +952,41 @@ void Session::UpdateSummaries()
     }
 }
 
-bool Session::SearchEvent(ChannelID code, qint64 time, qint64 dist)
+EventDataType Session::SearchValue(ChannelID code, qint64 time)
 {
-    qint64 t, start;
+    qint64 tt, start;
     QHash<ChannelID, QVector<EventList *> >::iterator it;
     it = eventlist.find(code);
     quint32 *tptr;
     int cnt;
 
-    //qint64 rate;
     if (it != eventlist.end()) {
         int el_size=it.value().size();
         for (int i = 0; i < el_size; i++)  {
             EventList *el = it.value()[i];
-            //            rate=el->rate();
+            if ((time < el->first()) || (time > el->last())) continue;
+
             cnt = el->count();
 
-            // why would this be necessary???
             if (el->type() == EVL_Waveform) {
                 qDebug() << "Called SearchEvent on a waveform object!";
-                return false;
+                return 0;
             } else {
                 start = el->first();
                 tptr = el->rawTime();
 
-                for (int j = 0; j < cnt; j++) {
-                    t = start + *tptr++;
-
-                    if (qAbs(time - t) < dist) {
-                        return true;
+                for (int j = 0; j < cnt-1; j++) {
+                    tptr++;
+                    tt = start + *tptr;
+                    if (tt > time) {
+                        return el->data(j);
                     }
                 }
             }
         }
     }
 
-    return false;
+    return 0;
 }
 
 bool Session::enabled()
diff --git a/sleepyhead/SleepLib/session.h b/sleepyhead/SleepLib/session.h
index 083dd6eb..5950cab5 100644
--- a/sleepyhead/SleepLib/session.h
+++ b/sleepyhead/SleepLib/session.h
@@ -64,8 +64,8 @@ class Session
     //! \brief Returns true if session contains an empty duration
     inline bool isEmpty() { return (s_first == s_last); }
 
-    //! \brief Search for Event code happening within dist milliseconds of supplied time (ms since epoch)
-    bool SearchEvent(ChannelID code, qint64 time, qint64 dist = 15000);
+    //! \brief Search for Event code happening at supplied time (ms since epoch)
+    EventDataType SearchValue(ChannelID code, qint64 time);
 
     //! \brief Return the sessionID
     inline const SessionID &session() {