]> sigrok.org Git - pulseview.git/commitdiff
Added cursor dragging
authorJoel Holdsworth <redacted>
Thu, 1 Nov 2012 22:00:06 +0000 (22:00 +0000)
committerJoel Holdsworth <redacted>
Thu, 1 Nov 2012 23:19:58 +0000 (23:19 +0000)
pv/view/cursor.cpp
pv/view/cursor.h
pv/view/ruler.cpp
pv/view/ruler.h
pv/view/timemarker.cpp
pv/view/timemarker.h
pv/view/view.cpp
pv/view/view.h

index 3ae5f58a82c9811bc9afef528f1d3025684eb1be..b854f2c4995be4a3f93a205f0b2c43c67bcbc534 100644 (file)
@@ -43,11 +43,16 @@ Cursor::Cursor(const View &view, double time) :
 {
 }
 
-void Cursor::paint_label(QPainter &p, const QRect &rect)
+QRectF Cursor::get_label_rect(const QRect &rect) const
 {
        const float x = (_time - _view.offset()) / _view.scale();
-       const QRectF r(x - Size/2, rect.height() - Size - Offset,
+       return QRectF(x - Size/2, rect.height() - Size - Offset,
                Size, Size);
+}
+
+void Cursor::paint_label(QPainter &p, const QRect &rect)
+{
+       const QRectF r(get_label_rect(rect));
 
        p.setPen(LineColour);
        p.setBrush(QBrush(FillColour));
index ec51b81abdb4cfd495a358200eb543a1b9392069..3ee0200518108a30e52ef7b20b7ced7108d92386 100644 (file)
@@ -47,6 +47,13 @@ public:
        Cursor(const View &view, double time);
 
 public:
+       /**
+        * Gets the marker label rectangle.
+        * @param rect The rectangle of the ruler client area.
+        * @return Returns the label rectangle.
+        */
+       QRectF get_label_rect(const QRect &rect) const;
+
        /**
         * Paints the cursor's label to the ruler.
         * @param p The painter to draw with.
index 83863e61ea203e3a3efb08f772425a8510da029d..bbafdb0f22cdce854fb52322a7a666b82bdac039 100644 (file)
@@ -19,6 +19,8 @@
  */
 
 #include "ruler.h"
+
+#include "cursor.h"
 #include "view.h"
 #include "viewport.h"
 
@@ -47,7 +49,8 @@ const int Ruler::HoverArrowSize = 5;
 
 Ruler::Ruler(View &parent) :
        QWidget(&parent),
-       _view(parent)
+       _view(parent),
+       _grabbed_marker(NULL)
 {
        setMouseTracking(true);
 
@@ -134,6 +137,38 @@ void Ruler::paintEvent(QPaintEvent *event)
        p.end();
 }
 
+void Ruler::mouseMoveEvent(QMouseEvent *e)
+{
+       if(!_grabbed_marker)
+               return;
+
+       _grabbed_marker->set_time(_view.offset() +
+               ((double)e->x() + 0.5) * _view.scale());
+}
+
+void Ruler::mousePressEvent(QMouseEvent *e)
+{
+       if(e->buttons() & Qt::LeftButton) {
+               _grabbed_marker = NULL;
+
+               if(_view.cursors_shown()) {
+                       std::pair<Cursor, Cursor> &cursors =
+                               _view.cursors();
+                       if(cursors.first.get_label_rect(
+                               rect()).contains(e->pos()))
+                               _grabbed_marker = &cursors.first;
+                       else if(cursors.second.get_label_rect(
+                               rect()).contains(e->pos()))
+                               _grabbed_marker = &cursors.second;
+               }
+       }
+}
+
+void Ruler::mouseReleaseEvent(QMouseEvent *)
+{
+       _grabbed_marker = NULL;
+}
+
 void Ruler::draw_cursors(QPainter &p)
 {
        if(!_view.cursors_shown())
@@ -148,7 +183,7 @@ void Ruler::draw_cursors(QPainter &p)
 void Ruler::draw_hover_mark(QPainter &p)
 {
        const int x = _view.hover_point().x();
-       if(x == -1)
+       if(x == -1 || _grabbed_marker)
                return;
 
        p.setPen(QPen(Qt::NoPen));
index 1381502d3de48f7c8c64168110e0f7e34a7d0236..129b70cbe54520d0ccebb2b829a91f3b7211e496 100644 (file)
@@ -26,6 +26,7 @@
 namespace pv {
 namespace view {
 
+class TimeMarker;
 class View;
 
 class Ruler : public QWidget
@@ -47,6 +48,10 @@ public:
 private:
        void paintEvent(QPaintEvent *event);
 
+       void mouseMoveEvent(QMouseEvent *e);
+       void mousePressEvent(QMouseEvent *e);
+       void mouseReleaseEvent(QMouseEvent *);
+
 private:
        void draw_cursors(QPainter &p);
 
@@ -60,6 +65,8 @@ private slots:
 
 private:
        View &_view;
+
+       TimeMarker *_grabbed_marker;
 };
 
 } // namespace view
index 49d90e39d551447f563debf9ea2e04b90d6bec4e..ec0ab9a4fe4f59857e47e0fcc7443747e3039a1c 100644 (file)
@@ -50,6 +50,7 @@ double TimeMarker::time() const
 void TimeMarker::set_time(double time)
 {
        _time = time;
+       time_changed();
 }
 
 void TimeMarker::paint(QPainter &p, const QRect &rect)
index c02dab159dbecb93c03a9f5a2b921012e6d82c40..08ed93890b6e4f8647e1855b08162f133387955f 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <QColor>
 #include <QObject>
+#include <QRectF>
 
 class QPainter;
 class QRect;
@@ -68,6 +69,13 @@ public:
         */
        virtual void paint(QPainter &p, const QRect &rect);
 
+       /**
+        * Gets the marker label rectangle.
+        * @param rect The rectangle of the ruler client area.
+        * @return Returns the label rectangle.
+        */
+       virtual QRectF get_label_rect(const QRect &rect) const = 0;
+
        /**
         * Paints the marker's label to the ruler.
         * @param p The painter to draw with.
@@ -75,6 +83,9 @@ public:
         */
        virtual void paint_label(QPainter &p, const QRect &rect) = 0;
 
+signals:
+       void time_changed();
+
 protected:
        const View &_view;
        const QColor &_colour;
index 7d3dc7f628a73b9bd5e4be03d6c8a6e45e33dfb2..d2f708e5e103d5642d1af1de46f9a30a3ce68073 100644 (file)
@@ -77,6 +77,11 @@ View::View(SigSession &session, QWidget *parent) :
        connect(&_session, SIGNAL(data_updated()),
                this, SLOT(data_updated()));
 
+       connect(&_cursors.first, SIGNAL(time_changed()),
+               this, SLOT(marker_time_changed()));
+       connect(&_cursors.second, SIGNAL(time_changed()),
+               this, SLOT(marker_time_changed()));
+
        setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
        setViewport(_viewport);
 
@@ -286,5 +291,11 @@ void View::data_updated()
        _viewport->update();
 }
 
+void View::marker_time_changed()
+{
+       _ruler->update();
+       _viewport->update();
+}
+
 } // namespace view
 } // namespace pv
index 13dd766ce788d1aa5a536a837654bb7066f081c8..f196cfe07d9c3bea5d97e7bf972d6aef9008baa0 100644 (file)
@@ -122,6 +122,8 @@ private slots:
 
        void data_updated();
 
+       void marker_time_changed();
+
 private:
        SigSession &_session;