data plot: support mouse wheel for moving and zooming (+shift)

This commit is contained in:
Philippe Teuwen 2020-09-21 16:05:38 +02:00
parent 804fef2ab8
commit 2482a8ec06
2 changed files with 77 additions and 38 deletions

View file

@ -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<SHIFT> UP Zoom out around purple cursor");
puts("\t<SHIFT> WHEEL MOUSE UP Zoom out around mouse cursor");
puts("\tDOWN Zoom in around yellow cursor");
puts("\t<SHIFT> DOWN Zoom in around purple cursor");
puts("\t<SHIFT> 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<CTLR> LEFT Move left 1 sample");
puts("\t<SHIFT> LEFT Page left");
puts("\tLEFT MOUSE CLICK Set yellow cursor");
puts("\tRIGHT Move right");
puts("\tWHEEL MOUSE DOWN Move right");
puts("\t<CTLR> RIGHT Move right 1 sample");
puts("\t<SHIFT> RIGHT Page right");
puts("\tRIGHT MOUSE CLICK Set purple cursor");

View file

@ -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);