Added cursors
authorJoel Holdsworth <joel@airwebreathe.org.uk>
Sun, 28 Oct 2012 18:00:50 +0000 (18:00 +0000)
committerJoel Holdsworth <joel@airwebreathe.org.uk>
Thu, 1 Nov 2012 23:19:58 +0000 (23:19 +0000)
pv/view/ruler.cpp
pv/view/ruler.h
pv/view/view.cpp
pv/view/view.h
pv/view/viewport.cpp
pv/view/viewport.h

index c5f194f2f76f4011009107659c2ad70790605e6f..83863e61ea203e3a3efb08f772425a8510da029d 100644 (file)
@@ -31,6 +31,8 @@
 #include <QPainter>
 #include <QTextStream>
 
+using namespace std;
+
 namespace pv {
 namespace view {
 
@@ -123,12 +125,26 @@ void Ruler::paintEvent(QPaintEvent *event)
                division++;
        }
 
+       // Draw the cursors
+       draw_cursors(p);
+
        // Draw the hover mark
        draw_hover_mark(p);
 
        p.end();
 }
 
+void Ruler::draw_cursors(QPainter &p)
+{
+       if(!_view.cursors_shown())
+               return;
+
+       const QRect r = rect();
+       pair<Cursor, Cursor> &cursors = _view.cursors();
+       cursors.first.paint_label(p, r);
+       cursors.second.paint_label(p, r);
+}
+
 void Ruler::draw_hover_mark(QPainter &p)
 {
        const int x = _view.hover_point().x();
index 1bda06427d253787389d1c87ca59dda26ad9fe71..1381502d3de48f7c8c64168110e0f7e34a7d0236 100644 (file)
@@ -48,6 +48,8 @@ private:
        void paintEvent(QPaintEvent *event);
 
 private:
+       void draw_cursors(QPainter &p);
+
        /**
         * Draw a hover arrow under the cursor position.
         */
index 54871b5ebc3d4c211898f5961fc9eb3978571782..7d3dc7f628a73b9bd5e4be03d6c8a6e45e33dfb2 100644 (file)
@@ -53,6 +53,8 @@ const int View::MaxScrollValue = INT_MAX / 2;
 
 const int View::SignalHeight = 50;
 
+const QColor View::CursorAreaColour(220, 231, 243);
+
 View::View(SigSession &session, QWidget *parent) :
        QAbstractScrollArea(parent),
        _session(session),
@@ -63,6 +65,9 @@ View::View(SigSession &session, QWidget *parent) :
        _scale(1e-6),
        _offset(0),
        _v_offset(0),
+       _show_cursors(false),
+       _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
+               Cursor(*this, 1.0))),
        _hover_point(-1, -1)
 {
        connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
@@ -128,6 +133,23 @@ void View::set_scale_offset(double scale, double offset)
        _viewport->update();
 }
 
+bool View::cursors_shown() const
+{
+       return _show_cursors;
+}
+
+void View::show_cursors(bool show)
+{
+       _show_cursors = show;
+       _ruler->update();
+       _viewport->update();
+}
+
+std::pair<Cursor, Cursor>& View::cursors()
+{
+       return _cursors;
+}
+
 const QPoint& View::hover_point() const
 {
        return _hover_point;
index ac2835a49cb4b6157075741d436c6708bd165727..13dd766ce788d1aa5a536a837654bb7066f081c8 100644 (file)
 
 #include <stdint.h>
 
+#include <utility>
+
 #include <QAbstractScrollArea>
 
+#include "cursor.h"
+
 namespace pv {
 
 class SigSession;
@@ -50,6 +54,8 @@ private:
 public:
        static const int SignalHeight;
 
+       static const QColor CursorAreaColour;
+
 public:
        explicit View(SigSession &session, QWidget *parent = 0);
 
@@ -77,6 +83,21 @@ public:
         */
        void set_scale_offset(double scale, double offset);
 
+       /**
+        * Returns true if cursors are displayed. false otherwise.
+        */
+       bool cursors_shown() const;
+
+       /**
+        * Shows or hides the cursors.
+        */
+       void show_cursors(bool show = true);
+
+       /**
+        * Returns a reference to the pair of cursors.
+        */
+       std::pair<Cursor, Cursor>& cursors();
+
        const QPoint& hover_point() const;
 
 signals:
@@ -118,6 +139,9 @@ private:
 
        int _v_offset;
 
+       bool _show_cursors;
+       std::pair<Cursor, Cursor> _cursors;
+
        QPoint _hover_point;
 };
 
index 99e08b277d151160bbf3b0842d68f0119c60582a..2dcf9dc78f83ac740b3d3051d639ee8caa88c288 100644 (file)
@@ -63,6 +63,8 @@ void Viewport::paintEvent(QPaintEvent *event)
        QPainter p(this);
        p.setRenderHint(QPainter::Antialiasing);
 
+       draw_cursors_background(p);
+
        // Plot the signal
        int offset = -_view.v_offset();
        BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
@@ -77,6 +79,8 @@ void Viewport::paintEvent(QPaintEvent *event)
                offset += View::SignalHeight;
        }
 
+       draw_cursors_foreground(p);
+
        p.end();
 }
 
@@ -112,5 +116,33 @@ void Viewport::wheelEvent(QWheelEvent *event)
        _view.zoom(event->delta() / 120, event->x());
 }
 
+void Viewport::draw_cursors_background(QPainter &p)
+{
+       if(!_view.cursors_shown())
+               return;
+
+       p.setPen(Qt::NoPen);
+       p.setBrush(QBrush(View::CursorAreaColour));
+
+       const pair<Cursor, Cursor> &c = _view.cursors();
+       const float x1 = (c.first.time() - _view.offset()) / _view.scale();
+       const float x2 = (c.second.time() - _view.offset()) / _view.scale();
+       const int l = (int)max(min(x1, x2), 0.0f);
+       const int r = (int)min(max(x1, x2), (float)width());
+
+       p.drawRect(l, 0, r - l, height());
+}
+
+void Viewport::draw_cursors_foreground(QPainter &p)
+{
+       if(!_view.cursors_shown())
+               return;
+
+       const QRect r = rect();
+       pair<Cursor, Cursor> &cursors = _view.cursors();
+       cursors.first.paint(p, r);
+       cursors.second.paint(p, r);
+}
+
 } // namespace view
 } // namespace pv
index a33d4045178628b4de8c2173e549efd21e3fc2df..49b97e3b0d71de953db0b76e70dc9b68e0b7f55a 100644 (file)
@@ -51,6 +51,11 @@ private:
        void mouseReleaseEvent(QMouseEvent *event);
        void wheelEvent(QWheelEvent *event);
 
+private:
+       void draw_cursors_background(QPainter &p);
+
+       void draw_cursors_foreground(QPainter &p);
+
 private:
        View &_view;