From: Marcus Comstedt Date: Sat, 19 Jul 2014 08:04:19 +0000 (+0200) Subject: View: Implement pinch-zoom X-Git-Tag: pulseview-0.3.0~571 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4b4f1c0dc17b481b798e60bbc3fa639c09a9c788;p=pulseview.git View: Implement pinch-zoom The special handling of a moveMoveEvent without a preceeding mousePressEvent is a workaround for an issue where you won't get an emulated mousePressEvent for the primary touch after a multitouch gesture where you release the primary touch before the secondary touch. --- diff --git a/pv/view/view.cpp b/pv/view/view.cpp index 392114c8..1d55e14f 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -469,6 +469,9 @@ bool View::viewportEvent(QEvent *e) case QEvent::MouseButtonDblClick: case QEvent::MouseMove: case QEvent::Wheel: + case QEvent::TouchBegin: + case QEvent::TouchUpdate: + case QEvent::TouchEnd: return false; default: diff --git a/pv/view/viewport.cpp b/pv/view/viewport.cpp index c5789630..9f60c1a8 100644 --- a/pv/view/viewport.cpp +++ b/pv/view/viewport.cpp @@ -38,8 +38,12 @@ namespace view { Viewport::Viewport(View &parent) : QWidget(&parent), - _view(parent) + _view(parent), + _mouse_down_valid(false), + _pinch_zoom_active(false) { + setAttribute(Qt::WA_AcceptTouchEvents, true); + setMouseTracking(true); setAutoFillBackground(true); setBackgroundRole(QPalette::Base); @@ -96,12 +100,43 @@ 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); - _mouse_down_point = event->pos(); - _mouse_down_offset = _view.offset(); + 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) @@ -110,6 +145,12 @@ void Viewport::mouseMoveEvent(QMouseEvent *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() * @@ -142,6 +183,53 @@ 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; +} + void Viewport::on_signals_changed() { const vector< shared_ptr > traces(_view.get_traces()); diff --git a/pv/view/viewport.h b/pv/view/viewport.h index 0474b7ee..f7c80287 100644 --- a/pv/view/viewport.h +++ b/pv/view/viewport.h @@ -23,6 +23,7 @@ #include #include +#include class QPainter; class QPaintEvent; @@ -46,10 +47,13 @@ protected: void paintEvent(QPaintEvent *event); private: + 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 slots: void on_signals_changed(); @@ -60,6 +64,11 @@ private: QPoint _mouse_down_point; double _mouse_down_offset; + bool _mouse_down_valid; + + double _pinch_offset0; + double _pinch_offset1; + bool _pinch_zoom_active; }; } // namespace view