]> sigrok.org Git - pulseview.git/commitdiff
ViewWidget: Moved in event and touch_event
authorJoel Holdsworth <redacted>
Sun, 28 Dec 2014 19:55:46 +0000 (19:55 +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 83df7aa4d1eaaa9543add3896e2b6363457ba200..971eb755228f978a1355677adbfb9d6f5668615d 100644 (file)
@@ -49,8 +49,6 @@ Viewport::Viewport(View &parent) :
        mouse_down_valid_(false),
        pinch_zoom_active_(false)
 {
-       setAttribute(Qt::WA_AcceptTouchEvents, true);
-
        setAutoFillBackground(true);
        setBackgroundRole(QPalette::Base);
 }
@@ -73,6 +71,53 @@ vector< shared_ptr<ViewItem> > Viewport::items()
        return items;
 }
 
+bool Viewport::touch_event(QTouchEvent *event)
+{
+       QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
+
+       if (touchPoints.count() != 2) {
+               pinch_zoom_active_ = false;
+               return false;
+       }
+
+       const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
+       const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
+
+       if (!pinch_zoom_active_ ||
+           (event->touchPointStates() & Qt::TouchPointPressed)) {
+               pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
+               pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
+               pinch_zoom_active_ = true;
+       }
+
+       double w = touchPoint1.pos().x() - touchPoint0.pos().x();
+       if (abs(w) >= 1.0) {
+               double scale = (pinch_offset1_ - pinch_offset0_) / w;
+               if (scale < 0)
+                       scale = -scale;
+               double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
+               if (scale > 0)
+                       view_.set_scale_offset(scale, offset);
+       }
+
+       if (event->touchPointStates() & Qt::TouchPointReleased) {
+               pinch_zoom_active_ = false;
+
+               if (touchPoint0.state() & Qt::TouchPointReleased) {
+                       // Primary touch released
+                       mouse_down_valid_ = false;
+               } 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;
+               }
+       }
+
+       return true;
+}
+
 void Viewport::paintEvent(QPaintEvent*)
 {
        vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
@@ -110,23 +155,6 @@ void Viewport::paintEvent(QPaintEvent*)
        p.end();
 }
 
-bool Viewport::event(QEvent *event)
-{
-       switch (event->type()) {
-       case QEvent::TouchBegin:
-       case QEvent::TouchUpdate:
-       case QEvent::TouchEnd:
-               if (touchEvent(static_cast<QTouchEvent *>(event)))
-                       return true;
-               break;
-
-       default:
-               break;
-       }
-
-       return QWidget::event(event);
-}
-
 void Viewport::mousePressEvent(QMouseEvent *event)
 {
        assert(event);
@@ -189,52 +217,5 @@ void Viewport::wheelEvent(QWheelEvent *event)
        }
 }
 
-bool Viewport::touchEvent(QTouchEvent *event)
-{
-       QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
-
-       if (touchPoints.count() != 2) {
-               pinch_zoom_active_ = false;
-               return false;
-       }
-
-       const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
-       const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
-
-       if (!pinch_zoom_active_ ||
-           (event->touchPointStates() & Qt::TouchPointPressed)) {
-               pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
-               pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
-               pinch_zoom_active_ = true;
-       }
-
-       double w = touchPoint1.pos().x() - touchPoint0.pos().x();
-       if (abs(w) >= 1.0) {
-               double scale = (pinch_offset1_ - pinch_offset0_) / w;
-               if (scale < 0)
-                       scale = -scale;
-               double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
-               if (scale > 0)
-                       view_.set_scale_offset(scale, offset);
-       }
-
-       if (event->touchPointStates() & Qt::TouchPointReleased) {
-               pinch_zoom_active_ = false;
-
-               if (touchPoint0.state() & Qt::TouchPointReleased) {
-                       // Primary touch released
-                       mouse_down_valid_ = false;
-               } 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;
-               }
-       }
-
-       return true;
-}
-
 } // namespace view
 } // namespace pv
index be3e7d32b719a7d80ca87df69216fbde735b963f..43905337fefaad1c4b78cc6a71ef432212e15c11 100644 (file)
@@ -57,16 +57,20 @@ private:
         */
        std::vector< std::shared_ptr<pv::view::ViewItem> > items();
 
+       /**
+        * Handles touch begin update and end events.
+        * @param e the event that triggered this handler.
+        */
+       bool touch_event(QTouchEvent *e);
+
+private:
        void paintEvent(QPaintEvent *event);
 
-       bool event(QEvent *event);
        void mousePressEvent(QMouseEvent *event);
        void mouseReleaseEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        void mouseDoubleClickEvent(QMouseEvent * event);
        void wheelEvent(QWheelEvent *event);
-       bool touchEvent(QTouchEvent *e);
-
 private:
        QPoint mouse_down_point_;
        double mouse_down_offset_;
index e2b2d17295f3295bc429f550548d95545c3ca1f3..816fbf513db042fa48dd6edd48fe7c68bd869453 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <QApplication>
 #include <QMouseEvent>
+#include <QTouchEvent>
 
 #include "rowitem.hpp"
 #include "view.hpp"
@@ -38,6 +39,7 @@ ViewWidget::ViewWidget(View &parent) :
        item_dragging_(false)
 {
        setFocusPolicy(Qt::ClickFocus);
+       setAttribute(Qt::WA_AcceptTouchEvents, true);
        setMouseTracking(true);
 }
 
@@ -180,6 +182,29 @@ void ViewWidget::mouse_left_release_event(QMouseEvent *event)
        item_dragging_ = false;
 }
 
+bool ViewWidget::touch_event(QTouchEvent *e)
+{
+       (void)e;
+       return false;
+}
+
+bool ViewWidget::event(QEvent *event)
+{
+       switch (event->type()) {
+       case QEvent::TouchBegin:
+       case QEvent::TouchUpdate:
+       case QEvent::TouchEnd:
+               if (touch_event(static_cast<QTouchEvent *>(event)))
+                       return true;
+               break;
+
+       default:
+               break;
+       }
+
+       return QWidget::event(event);
+}
+
 void ViewWidget::mousePressEvent(QMouseEvent *event)
 {
        assert(event);
index 65434f65e1720913ab2589f3cfd932fe1c873b46..b6b1a5b48ec99a8166ba426c03f8425d43437578 100644 (file)
@@ -25,6 +25,8 @@
 
 #include <QWidget>
 
+class QTouchEvent;
+
 namespace pv {
 namespace view {
 
@@ -89,7 +91,15 @@ protected:
         */
        void mouse_left_release_event(QMouseEvent *event);
 
+       /**
+        * Handles touch begin update and end events.
+        * @param e the event that triggered this handler.
+        */
+       virtual bool touch_event(QTouchEvent *e);
+
 protected:
+       bool event(QEvent *event);
+
        void mousePressEvent(QMouseEvent * event);
        void mouseReleaseEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);