From: Joel Holdsworth Date: Sun, 28 Dec 2014 19:55:46 +0000 (+0000) Subject: ViewWidget: Moved in event and touch_event X-Git-Tag: pulseview-0.3.0~298 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c9743553584975b338890c16ef22943d7beaacdd;p=pulseview.git ViewWidget: Moved in event and touch_event --- diff --git a/pv/view/viewport.cpp b/pv/view/viewport.cpp index 83df7aa4..971eb755 100644 --- a/pv/view/viewport.cpp +++ b/pv/view/viewport.cpp @@ -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 > Viewport::items() return items; } +bool Viewport::touch_event(QTouchEvent *event) +{ + QList 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 > 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(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 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 diff --git a/pv/view/viewport.hpp b/pv/view/viewport.hpp index be3e7d32..43905337 100644 --- a/pv/view/viewport.hpp +++ b/pv/view/viewport.hpp @@ -57,16 +57,20 @@ private: */ std::vector< std::shared_ptr > 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_; diff --git a/pv/view/viewwidget.cpp b/pv/view/viewwidget.cpp index e2b2d172..816fbf51 100644 --- a/pv/view/viewwidget.cpp +++ b/pv/view/viewwidget.cpp @@ -20,6 +20,7 @@ #include #include +#include #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(event))) + return true; + break; + + default: + break; + } + + return QWidget::event(event); +} + void ViewWidget::mousePressEvent(QMouseEvent *event) { assert(event); diff --git a/pv/view/viewwidget.hpp b/pv/view/viewwidget.hpp index 65434f65..b6b1a5b4 100644 --- a/pv/view/viewwidget.hpp +++ b/pv/view/viewwidget.hpp @@ -25,6 +25,8 @@ #include +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);