]> sigrok.org Git - pulseview.git/commitdiff
Viewport: Implemented on-canvas drag
authorJoel Holdsworth <redacted>
Mon, 29 Dec 2014 10:15:26 +0000 (10:15 +0000)
committerJoel Holdsworth <redacted>
Mon, 29 Dec 2014 12:24:23 +0000 (12:24 +0000)
pv/view/viewport.cpp
pv/view/viewport.hpp
pv/view/viewwidget.cpp
pv/view/viewwidget.hpp

index 0e89f3ad96fc6b983d3ed1d9d8fe2cb495e59f2b..2772c7764ead3a45b2c76733be6b0fe98d252c05 100644 (file)
@@ -21,6 +21,7 @@
 #include <cassert>
 #include <cmath>
 #include <algorithm>
+#include <limits>
 
 #include "signal.hpp"
 #include "view.hpp"
@@ -37,6 +38,7 @@ using std::copy;
 using std::max;
 using std::min;
 using std::none_of;
+using std::numeric_limits;
 using std::shared_ptr;
 using std::stable_sort;
 using std::vector;
@@ -46,7 +48,7 @@ namespace view {
 
 Viewport::Viewport(View &parent) :
        ViewWidget(parent),
-       mouse_down_valid_(false),
+       drag_offset_(numeric_limits<double>::signaling_NaN()),
        pinch_zoom_active_(false)
 {
        setAutoFillBackground(true);
@@ -63,6 +65,25 @@ shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
        return nullptr;
 }
 
+void Viewport::drag()
+{
+       drag_offset_ = view_.offset();
+}
+
+void Viewport::drag_by(const QPoint &delta)
+{
+       if (isnan(drag_offset_))
+               return;
+
+       view_.set_scale_offset(view_.scale(), drag_offset_ -
+               delta.x() * view_.scale());
+}
+
+void Viewport::drag_release()
+{
+       drag_offset_ = numeric_limits<double>::signaling_NaN();
+}
+
 vector< shared_ptr<ViewItem> > Viewport::items()
 {
        vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
@@ -104,13 +125,12 @@ bool Viewport::touch_event(QTouchEvent *event)
 
                if (touchPoint0.state() & Qt::TouchPointReleased) {
                        // Primary touch released
-                       mouse_down_valid_ = false;
+                       drag_release();
                } else {
                        // Update the mouse down fields so that continued
                        // dragging with the primary touch will work correctly
                        mouse_down_point_ = touchPoint0.pos().toPoint();
-                       mouse_down_offset_ = view_.offset();
-                       mouse_down_valid_ = true;
+                       drag();
                }
        }
 
@@ -154,43 +174,6 @@ void Viewport::paintEvent(QPaintEvent*)
        p.end();
 }
 
-void Viewport::mousePressEvent(QMouseEvent *event)
-{
-       assert(event);
-
-       if (event->button() == Qt::LeftButton) {
-               mouse_down_point_ = event->pos();
-               mouse_down_offset_ = view_.offset();
-               mouse_down_valid_ = true;
-       }
-}
-
-void Viewport::mouseReleaseEvent(QMouseEvent *event)
-{
-       assert(event);
-
-       if (event->button() == Qt::LeftButton)
-               mouse_down_valid_ = false;
-}
-
-void Viewport::mouseMoveEvent(QMouseEvent *event)
-{
-       assert(event);
-
-       if (event->buttons() & Qt::LeftButton) {
-               if (!mouse_down_valid_) {
-                       mouse_down_point_ = event->pos();
-                       mouse_down_offset_ = view_.offset();
-                       mouse_down_valid_ = true;
-               }
-
-               view_.set_scale_offset(view_.scale(),
-                       mouse_down_offset_ +
-                       (mouse_down_point_ - event->pos()).x() *
-                       view_.scale());
-       }
-}
-
 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
 {
        assert(event);
index 43905337fefaad1c4b78cc6a71ef432212e15c11..f43177d2d3f611d9ea00abe87b6eadfbe02ce92e 100644 (file)
@@ -52,6 +52,22 @@ private:
        std::shared_ptr<pv::view::ViewItem> get_mouse_over_item(
                const QPoint &pt);
 
+       /**
+        * Sets this item into the dragged state.
+        */
+       void drag();
+
+       /**
+        * Drag the background by the delta offset.
+        * @param delta the drag offset in pixels.
+        */
+       void drag_by(const QPoint &delta);
+
+       /**
+        * Sets this item into the un-dragged state.
+        */
+       void drag_release();
+
        /**
         * Gets the items in the view widget.
         */
@@ -66,15 +82,11 @@ private:
 private:
        void paintEvent(QPaintEvent *event);
 
-       void mousePressEvent(QMouseEvent *event);
-       void mouseReleaseEvent(QMouseEvent *event);
-       void mouseMoveEvent(QMouseEvent *event);
        void mouseDoubleClickEvent(QMouseEvent * event);
        void wheelEvent(QWheelEvent *event);
+
 private:
-       QPoint mouse_down_point_;
-       double mouse_down_offset_;
-       bool mouse_down_valid_;
+       double drag_offset_;
 
        double pinch_offset0_;
        double pinch_offset1_;
index 816fbf513db042fa48dd6edd48fe7c68bd869453..a5174bb37ecd000332a199cea5790a7792e83770 100644 (file)
@@ -85,7 +85,8 @@ bool ViewWidget::accept_drag() const
                return true;
        }
 
-       return false;
+       // A background drag is beginning
+       return true;
 }
 
 bool ViewWidget::mouse_down() const
@@ -96,6 +97,8 @@ bool ViewWidget::mouse_down() const
 
 void ViewWidget::drag_items(const QPoint &delta)
 {
+       bool item_dragged = false;
+
        // Drag the row items
        RowItemOwner *item_owner = nullptr;
        for (std::shared_ptr<RowItem> r : view_)
@@ -108,6 +111,7 @@ void ViewWidget::drag_items(const QPoint &delta)
                }
 
        if (item_owner) {
+               item_dragged = true;
                item_owner->restack_items();
                for (const auto &r : *item_owner)
                        r->animate_to_layout_v_offset();
@@ -116,8 +120,27 @@ void ViewWidget::drag_items(const QPoint &delta)
        // Drag the time items
        const vector< shared_ptr<TimeItem> > items(view_.time_items());
        for (auto &i : items)
-               if (i->dragging())
+               if (i->dragging()) {
                        i->drag_by(delta);
+                       item_dragged = true;
+               }
+
+       // Do the background drag
+       if (!item_dragged)
+               drag_by(delta);
+}
+
+void ViewWidget::drag()
+{
+}
+
+void ViewWidget::drag_by(const QPoint &delta)
+{
+       (void)delta;
+}
+
+void ViewWidget::drag_release()
+{
 }
 
 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
@@ -141,10 +164,17 @@ void ViewWidget::mouse_left_press_event(QMouseEvent *event)
        }
 
        // Save the offsets of any signals which will be dragged
+       bool item_dragged = false;
        const auto items = this->items();
        for (auto &i : items)
-               if (i->selected())
+               if (i->selected()) {
+                       item_dragged = true;
                        i->drag();
+               }
+
+       // Do the background drag
+       if (!item_dragged)
+               drag();
 
        selection_changed();
        update();
index b6b1a5b48ec99a8166ba426c03f8425d43437578..6fd1ecefd1f853a2403fc880cacb36c1c699a108 100644 (file)
@@ -65,6 +65,23 @@ protected:
         */
        void drag_items(const QPoint &delta);
 
+       /**
+        * Sets this item into the dragged state.
+        */
+       virtual void drag();
+
+       /**
+        * Drag the background by the delta offset.
+        * @param delta the drag offset in pixels.
+        * @remarks The default implementation does nothing.
+        */
+       virtual void drag_by(const QPoint &delta);
+
+       /**
+        * Sets this item into the un-dragged state.
+        */
+       virtual void drag_release();
+
        /**
         * Gets the items in the view widget.
         */