From 631b3244059d18dc655adc884a3483b9d308ff75 Mon Sep 17 00:00:00 2001
From: Mark Watkins <jedimark@users.sourceforge.net>
Date: Thu, 25 Aug 2011 19:00:19 +1000
Subject: [PATCH] Return of the FooBar

---
 Graphs/gFooBar.cpp    | 77 ++++++++++++++++++++++++++++---------------
 Graphs/gFooBar.h      | 24 +++++++++-----
 Graphs/gGraphView.cpp | 58 +++++++++++++++++++++++++++-----
 Graphs/gGraphView.h   |  8 +++--
 daily.cpp             |  2 ++
 5 files changed, 123 insertions(+), 46 deletions(-)

diff --git a/Graphs/gFooBar.cpp b/Graphs/gFooBar.cpp
index 97027c6e..4de86fa5 100644
--- a/Graphs/gFooBar.cpp
+++ b/Graphs/gFooBar.cpp
@@ -6,14 +6,58 @@
 
 #include "gFooBar.h"
 
-gFooBar::gFooBar(int offset,QColor handle_color,QColor line_color,bool shadow,QColor shadow_color)
-:gLayer(EmptyChannel),m_offset(offset),m_handle_color(handle_color),m_line_color(line_color),m_shadow(shadow),m_shadow_color(shadow_color)
+gShadowArea::gShadowArea(QColor shadow_color)
+:Layer(EmptyChannel),m_shadow_color(shadow_color)
+{
+}
+gShadowArea::~gShadowArea()
+{
+}
+void gShadowArea::paint(gGraph & w,int left, int top, int width, int height)
+{
+    if (!m_visible) return;
+    double xx=w.max_x-w.min_x;
+
+    if (xx==0)
+        return;
+
+    int start_px=left;
+    int end_px=left+width;
+
+    glDisable(GL_DEPTH_TEST);
+    float h=top;
+
+    double rmx=w.rmax_x-w.rmin_x;
+    double px=((1/rmx)*(w.min_x-w.rmin_x))*width;
+    double py=((1/rmx)*(w.max_x-w.rmin_x))*width;
+
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+
+    glBegin(GL_QUADS);
+    w.qglColor(m_shadow_color);
+
+    glVertex2f(start_px, top);
+    glVertex2f(start_px, top+height);
+    glVertex2f(start_px+px, top+height);
+    glVertex2f(start_px+px, top);
+
+    glVertex2f(start_px+py, top);
+    glVertex2f(start_px+py, top+height);
+    glVertex2f(end_px, top+height);
+    glVertex2f(end_px, top);
+    glEnd();
+    glDisable(GL_BLEND);
+}
+
+gFooBar::gFooBar(int offset,QColor handle_color,QColor line_color)
+:Layer(EmptyChannel),m_offset(offset),m_handle_color(handle_color),m_line_color(line_color)
 {
 }
 gFooBar::~gFooBar()
 {
 }
-void gFooBar::Plot(gGraphWindow & w,float scrx,float scry)
+void gFooBar::paint(gGraph & w,int left, int top, int width, int height)
 {
     if (!m_visible) return;
 
@@ -22,13 +66,11 @@ void gFooBar::Plot(gGraphWindow & w,float scrx,float scry)
     if (xx==0)
         return;
 
-    int start_px=w.GetLeftMargin()-1;
-    int width=scrx - (w.GetLeftMargin() + w.GetRightMargin());
-    int height=scry - (w.GetTopMargin() + w.GetBottomMargin());
-    int end_px=scrx-w.GetRightMargin();
+    int start_px=left;
+    int end_px=left+width;
 
     glDisable(GL_DEPTH_TEST);
-    float h=m_offset;
+    float h=top;
 
     glLineWidth(1);
     glBegin(GL_LINES);
@@ -50,24 +92,5 @@ void gFooBar::Plot(gGraphWindow & w,float scrx,float scry)
 
     glLineWidth(1);
 
-    if ((m_shadow)) {
-        glEnable(GL_BLEND);
-        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
-
-        glBegin(GL_QUADS);
-        w.qglColor(m_shadow_color);
-
-        glVertex2f(start_px, w.GetBottomMargin());
-        glVertex2f(start_px, w.GetBottomMargin()+height);
-        glVertex2f(start_px+px, w.GetBottomMargin()+height);
-        glVertex2f(start_px+px, w.GetBottomMargin());
-
-        glVertex2f(start_px+py, w.GetBottomMargin());
-        glVertex2f(start_px+py, w.GetBottomMargin()+height);
-        glVertex2f(end_px, w.GetBottomMargin()+height);
-        glVertex2f(end_px, w.GetBottomMargin());
-        glEnd();
-        glDisable(GL_BLEND);
-    }
 }
 
diff --git a/Graphs/gFooBar.h b/Graphs/gFooBar.h
index 1115c991..419b5df2 100644
--- a/Graphs/gFooBar.h
+++ b/Graphs/gFooBar.h
@@ -1,27 +1,35 @@
-/********************************************************************
+/*
  gFooBar Header
  Copyright (c)2011 Mark Watkins <jedimark@users.sourceforge.net>
  License: GPL
-*********************************************************************/
+*/
 
 #ifndef GFOOBAR_H
 #define GFOOBAR_H
 
-#include "graphlayer.h"
+#include "gGraphView.h"
 
-class gFooBar:public gLayer
+class gShadowArea:public Layer
 {
     public:
-        gFooBar(int offset=10,QColor handle_color=QColor("orange"),QColor line_color=QColor("dark grey"),bool shadow=false,QColor shadow_color=QColor(40,40,40,40));
+        gShadowArea(QColor shadow_color=QColor(40,40,40,40));
+        virtual ~gShadowArea();
+        virtual void paint(gGraph & w,int left, int top, int width, int height);
+    protected:
+        QColor m_shadow_color;
+};
+
+class gFooBar:public Layer
+{
+    public:
+        gFooBar(int offset=10,QColor handle_color=QColor("orange"),QColor line_color=QColor("dark grey"));
         virtual ~gFooBar();
-        virtual void Plot(gGraphWindow & w,float scrx,float scry);
+        virtual void paint(gGraph & w,int left, int top, int width, int height);
         static const int Margin=15;
     protected:
         int m_offset;
         QColor m_handle_color;
         QColor m_line_color;
-        bool m_shadow;
-        QColor m_shadow_color;
 };
 
 #endif // GFOOBAR_H
diff --git a/Graphs/gGraphView.cpp b/Graphs/gGraphView.cpp
index b8e560a7..0b1c6d3d 100644
--- a/Graphs/gGraphView.cpp
+++ b/Graphs/gGraphView.cpp
@@ -159,7 +159,7 @@ gGraph::gGraph(gGraphView *graphview,QString title,int height) :
     m_marginbottom=10;
     m_marginleft=5;
     m_marginright=10;
-    m_blockzoom=false;
+    m_selecting_area=m_blockzoom=false;
 }
 gGraph::~gGraph()
 {
@@ -305,6 +305,21 @@ void gGraph::paint(int originX, int originY, int width, int height)
             ll->paint(*this,originX+left,originY+top,width-left-right,height-top-bottom);
         }
     }
+
+    if (m_selection.width()>0 && m_selecting_area) {
+        glEnable(GL_BLEND);
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+        glBegin(GL_QUADS);
+        glColor4ub(128,128,128,128);
+        glVertex2i(originX+m_selection.x(),originY+top);
+        glVertex2i(originX+m_selection.x()+m_selection.width(),originY+top);
+        glColor4ub(128,128,255,128);
+        glVertex2i(originX+m_selection.x()+m_selection.width(),originY+height-top-bottom);
+        glVertex2i(originX+m_selection.x(),originY+height-top-bottom);
+        glEnd();
+        glDisable(GL_BLEND);
+    }
+
 }
 
 void gGraph::AddLayer(Layer * l,LayerPosition position, short width, short height, short order, bool movable, short x, short y)
@@ -319,11 +334,22 @@ void gGraph::mouseMoveEvent(QMouseEvent * event)
    // qDebug() << m_title << "Move" << event->pos() << m_graphview->pointClicked();
     int y=event->pos().y();
     int x=event->pos().x();
-    int x2,y2;
+    int x2=m_graphview->pointClicked().x(),y2=m_graphview->pointClicked().y();
     int w=m_width-(m_graphview->titleWidth+right+m_marginright);
     int h=m_height-(bottom+m_marginbottom);
     double xx=max_x-min_x;
     double xmult=xx/w;
+
+    if ((event->buttons() & Qt::LeftButton) && (m_graphview->m_selected_graph==this)) {
+        qDebug() << m_title << "Moved" << x << y << left << right << top << bottom << m_width << m_height;
+        int a1=MIN(x,x2);
+        int a2=MAX(x,x2);
+        m_selecting_area=true;
+        m_selection=QRect(a1-m_marginleft,0,a2-a1,m_height);
+        m_graphview->updateGL();
+        //repaint();
+    } else m_selecting_area=false;
+
     if (x>left+m_marginleft && x<m_width-(m_graphview->titleWidth+right+m_marginright) && y>top+m_margintop && y<m_height-(bottom+m_marginbottom)) { // main area
         x-=left+m_marginleft;
         y-=top+m_margintop;
@@ -354,8 +380,8 @@ void gGraph::mouseReleaseEvent(QMouseEvent * event)
     int w=m_width-(m_graphview->titleWidth+m_marginleft+left+right+m_marginright);
     int h=m_height-(bottom+m_marginbottom);
     int x2=m_graphview->pointClicked().x(),y2=m_graphview->pointClicked().y();
-    double xx=max_x-min_x;
-    double xmult=xx/double(w);
+    m_selecting_area=false;
+    m_selection.setWidth(0);
     if (x>left+m_marginleft && x<w+m_marginleft+left && y>top+m_margintop && y<h) { // main area
         if (event->button() & Qt::RightButton) {
             ZoomX(2,x);
@@ -368,16 +394,29 @@ void gGraph::mouseReleaseEvent(QMouseEvent * event)
                 y-=top+m_margintop;
                 x2-=left+m_marginleft;
                 y2-=top+m_margintop;
-                qint64 j1=min_x+xmult*x;
-                qint64 j2=min_x+xmult*x2;
-                qint64 a1=MIN(j1,j2)
-                qint64 a2=MAX(j1,j2)
-                m_graphview->SetXBounds(a1,a2);
+                if (!m_blockzoom) {
+                    double xx=max_x-min_x;
+                    double xmult=xx/double(w);
+                    qint64 j1=min_x+xmult*x;
+                    qint64 j2=min_x+xmult*x2;
+                    qint64 a1=MIN(j1,j2)
+                    qint64 a2=MAX(j1,j2)
+                    m_graphview->SetXBounds(a1,a2);
+                } else {
+                    double xx=rmax_x-rmin_x;
+                    double xmult=xx/double(w);
+                    qint64 j1=rmin_x+xmult*x;
+                    qint64 j2=rmin_x+xmult*x2;
+                    qint64 a1=MIN(j1,j2)
+                    qint64 a2=MAX(j1,j2)
+                    m_graphview->SetXBounds(a1,a2);
+                }
             }
         }
         qDebug() << m_title << "Released" << min_x << max_x << x << y << x2 << y2 << left << right << top << bottom << m_width << m_height;
    // qDebug() << m_title << "Released" << event->pos() << m_graphview->pointClicked() << left << top;
     }
+    //m_graphview->updateGL();
 }
 
 
@@ -929,6 +968,7 @@ void gGraphView::mousePressEvent(QMouseEvent * event)
                 } else { // send event to graph..
                     m_global_point_clicked=QPoint(x,y);
                     m_point_clicked=QPoint (x-titleWidth,y-py);
+                    m_selected_graph=m_graphs[i];
 
                     QMouseEvent e(event->type(),m_point_clicked,event->button(),event->buttons(),event->modifiers());
                     m_graphs[i]->mousePressEvent(&e);
diff --git a/Graphs/gGraphView.h b/Graphs/gGraphView.h
index 8622c66c..ae535357 100644
--- a/Graphs/gGraphView.h
+++ b/Graphs/gGraphView.h
@@ -176,7 +176,9 @@ protected:
     bool m_visible;
     bool m_blockzoom;
     QRect m_lastbounds;
-
+    QRect m_selection;
+    bool m_selecting_area;
+    QPoint m_current;
 };
 
 class gGraphView : public QGLWidget
@@ -205,6 +207,9 @@ public:
     QPoint globalPointClicked() { return m_global_point_clicked; }
 
     QPainter *painter;
+
+    gGraph *m_selected_graph;
+
 protected:
 
     float totalHeight();
@@ -245,7 +250,6 @@ protected:
 
     bool m_graph_dragging;
     int m_graph_index;
-
 signals:
 
 
diff --git a/daily.cpp b/daily.cpp
index 05e4e739..9deaca8a 100644
--- a/daily.cpp
+++ b/daily.cpp
@@ -88,7 +88,9 @@ Daily::Daily(QWidget *parent,QGLWidget * shared, MainWindow *mw)
     //fg->AddLayer(AddCPAP(new gFlagsLine(flags[10],QColor("red"),"VS2"));
     SF->setBlockZoom(true);
     SF->AddLayer(AddCPAP(fg));
+    SF->AddLayer(new gShadowArea());
     SF->AddLayer(new gYSpacer(),LayerLeft,gYAxis::Margin);
+    SF->AddLayer(new gFooBar(),LayerBottom,0,10);
     SF->AddLayer(new gXAxis(),LayerBottom,0,gXAxis::Margin);
 
     PRD->AddLayer(AddCPAP(new gLineChart(CPAP_Pressure,QColor("dark green"),true)));