From 2482a8ec06441b7702af7bd947e38a99319b9603 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 21 Sep 2020 16:05:38 +0200 Subject: [PATCH] data plot: support mouse wheel for moving and zooming (+shift) --- client/src/proxguiqt.cpp | 112 ++++++++++++++++++++++++++------------- client/src/proxguiqt.h | 3 ++ 2 files changed, 77 insertions(+), 38 deletions(-) diff --git a/client/src/proxguiqt.cpp b/client/src/proxguiqt.cpp index b96783844..fa7a70194 100644 --- a/client/src/proxguiqt.cpp +++ b/client/src/proxguiqt.cpp @@ -637,6 +637,66 @@ void Plot::closeEvent(QCloseEvent *event) { g_useOverlays = false; } +void Plot::Zoom(float factor, int refX) { + if (factor >=1) { // Zoom in + if (GraphPixelsPerPoint <= 25 * factor) { + GraphPixelsPerPoint *= factor; + GraphStart += (refX - GraphStart) - ((refX - GraphStart) / factor); + } + } else { // Zoom out + if (GraphPixelsPerPoint >= 0.01 / factor) { + GraphPixelsPerPoint *= factor; + if (GraphStart >= ((refX - GraphStart) / factor) - (refX - GraphStart)) { + GraphStart -= ((refX - GraphStart) / factor) - (refX - GraphStart); + } else { + GraphStart = 0; + } + } + } +} + +void Plot::Move(int offset) { + if (offset > 0) { // Move right + if (GraphPixelsPerPoint < 20) { + GraphStart += offset; + } else { + GraphStart++; + } + } else { // Move left + if (GraphPixelsPerPoint < 20) { + if (GraphStart >= (uint)-offset) { + GraphStart += offset; + } else { + GraphStart = 0; + } + } else { + if (GraphStart > 0) { + GraphStart--; + } + } + } +} + +void Plot::wheelEvent(QWheelEvent *event) { + // event->delta() + // 120 => shift right 5% + // -120 => shift left 5% + const float move_offset = 0.05; + // -120+shift => zoom in 10% + // 120+shift => zoom out 10% + const float zoom_offset = 0.1; + if (event->modifiers() & Qt::ShiftModifier) { + int x = event->x(); + x -= WIDTH_AXES; + x = (int)(x / GraphPixelsPerPoint); + x += GraphStart; + Zoom(1.0-(float)event->delta()/(120/zoom_offset), x); + } else { + Move(PageWidth*(-(float)event->delta()/(120/move_offset))); + } + this->update(); +} + void Plot::mouseMoveEvent(QMouseEvent *event) { int x = event->x(); x -= WIDTH_AXES; @@ -667,55 +727,27 @@ void Plot::keyPressEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_Down: - if (GraphPixelsPerPoint <= 50) { - GraphPixelsPerPoint *= 2; - if (event->modifiers() & Qt::ShiftModifier) { - GraphStart += (CursorBPos - GraphStart) / 2; - } else { - GraphStart += (CursorAPos - GraphStart) / 2; - } + if (event->modifiers() & Qt::ShiftModifier) { + Zoom(2, CursorBPos); + } else { + Zoom(2, CursorAPos); } break; case Qt::Key_Up: - if (GraphPixelsPerPoint >= 0.02) { - GraphPixelsPerPoint /= 2; - if (event->modifiers() & Qt::ShiftModifier) { - if (GraphStart >= CursorBPos - GraphStart) { - GraphStart -= CursorBPos - GraphStart; - } else { - GraphStart = 0; - } - } else { - if (GraphStart >= CursorAPos - GraphStart) { - GraphStart -= CursorAPos - GraphStart; - } else { - GraphStart = 0; - } - } + if (event->modifiers() & Qt::ShiftModifier) { + Zoom(0.5, CursorBPos); + } else { + Zoom(0.5, CursorAPos); } break; case Qt::Key_Right: - if (GraphPixelsPerPoint < 20) { - GraphStart += offset; - } else { - GraphStart++; - } + Move(offset); break; case Qt::Key_Left: - if (GraphPixelsPerPoint < 20) { - if (GraphStart >= offset) { - GraphStart -= offset; - } else { - GraphStart = 0; - } - } else { - if (GraphStart > 0) { - GraphStart--; - } - } + Move(-offset); break; case Qt::Key_G: @@ -740,8 +772,10 @@ void Plot::keyPressEvent(QKeyEvent *event) { puts("-----------------------------------------------------------------------"); puts("\tUP Zoom out around yellow cursor"); puts("\t UP Zoom out around purple cursor"); + puts("\t WHEEL MOUSE UP Zoom out around mouse cursor"); puts("\tDOWN Zoom in around yellow cursor"); puts("\t DOWN Zoom in around purple cursor"); + puts("\t WHEEL MOUSE DOWN Zoom in around mouse cursor"); puts("\tG Toggle grid display"); puts("\tH Show help"); puts("\tL Toggle lock grid relative to samples"); @@ -751,10 +785,12 @@ void Plot::keyPressEvent(QKeyEvent *event) { puts("\tPGUP Page left"); puts("\tPGDOWN Page right"); puts("\tLEFT Move left"); + puts("\tWHEEL MOUSE UP Move right"); puts("\t LEFT Move left 1 sample"); puts("\t LEFT Page left"); puts("\tLEFT MOUSE CLICK Set yellow cursor"); puts("\tRIGHT Move right"); + puts("\tWHEEL MOUSE DOWN Move right"); puts("\t RIGHT Move right 1 sample"); puts("\t RIGHT Page right"); puts("\tRIGHT MOUSE CLICK Set purple cursor"); diff --git a/client/src/proxguiqt.h b/client/src/proxguiqt.h index 7f9a82126..3264a2919 100644 --- a/client/src/proxguiqt.h +++ b/client/src/proxguiqt.h @@ -50,6 +50,9 @@ class Plot: public QWidget { protected: void paintEvent(QPaintEvent *event); void closeEvent(QCloseEvent *event); + void Zoom(float factor, int refX); + void Move(int offset); + void wheelEvent(QWheelEvent *event); void mouseMoveEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event) { mouseMoveEvent(event); } void keyPressEvent(QKeyEvent *event);