zoom: add ctrl for small increments with key, to get same as with wheel

This commit is contained in:
Philippe Teuwen 2020-10-05 12:16:19 +02:00
commit 1f2133fa17
2 changed files with 34 additions and 13 deletions

View file

@ -647,15 +647,18 @@ void Plot::closeEvent(QCloseEvent *event) {
g_useOverlays = false; g_useOverlays = false;
} }
void Plot::Zoom(double factor, int refX) { void Plot::Zoom(double factor, uint32_t refX) {
if (factor >= 1) { // Zoom in if (factor >= 1) { // Zoom in
if (GraphPixelsPerPoint <= 25 * factor) { if (GraphPixelsPerPoint <= 25 * factor) {
GraphPixelsPerPoint *= factor; GraphPixelsPerPoint *= factor;
if (refX > GraphStart) {
GraphStart += (refX - GraphStart) - ((refX - GraphStart) / factor); GraphStart += (refX - GraphStart) - ((refX - GraphStart) / factor);
} }
}
} else { // Zoom out } else { // Zoom out
if (GraphPixelsPerPoint >= 0.01 / factor) { if (GraphPixelsPerPoint >= 0.01 / factor) {
GraphPixelsPerPoint *= factor; GraphPixelsPerPoint *= factor;
if (refX > GraphStart) {
if (GraphStart >= ((refX - GraphStart) / factor) - (refX - GraphStart)) { if (GraphStart >= ((refX - GraphStart) / factor) - (refX - GraphStart)) {
GraphStart -= ((refX - GraphStart) / factor) - (refX - GraphStart); GraphStart -= ((refX - GraphStart) / factor) - (refX - GraphStart);
} else { } else {
@ -664,6 +667,7 @@ void Plot::Zoom(double factor, int refX) {
} }
} }
} }
}
void Plot::Move(int offset) { void Plot::Move(int offset) {
if (offset > 0) { // Move right if (offset > 0) { // Move right
@ -722,9 +726,9 @@ void Plot::wheelEvent(QWheelEvent *event) {
if (event->modifiers() & Qt::ShiftModifier) { if (event->modifiers() & Qt::ShiftModifier) {
// event->position doesn't exist in QT5.12.8, both exist in 5.14.2 and event->x doesn't exist in 5.15.0 // event->position doesn't exist in QT5.12.8, both exist in 5.14.2 and event->x doesn't exist in 5.15.0
#if QT_VERSION >= 0x050d00 #if QT_VERSION >= 0x050d00
int x = event->position().x(); uint32_t x = event->position().x();
#else #else
int x = event->x(); uint32_t x = event->x();
#endif #endif
x -= WIDTH_AXES; x -= WIDTH_AXES;
x = (int)(x / GraphPixelsPerPoint); x = (int)(x / GraphPixelsPerPoint);
@ -765,6 +769,7 @@ void Plot::mouseMoveEvent(QMouseEvent *event) {
void Plot::keyPressEvent(QKeyEvent *event) { void Plot::keyPressEvent(QKeyEvent *event) {
uint32_t offset; // Left/right movement offset (in sample size) uint32_t offset; // Left/right movement offset (in sample size)
const double zoom_offset = 1.148698354997035; // 2**(1/5)
if (event->modifiers() & Qt::ShiftModifier) { if (event->modifiers() & Qt::ShiftModifier) {
if (PlotGridX) if (PlotGridX)
@ -781,18 +786,34 @@ void Plot::keyPressEvent(QKeyEvent *event) {
switch (event->key()) { switch (event->key()) {
case Qt::Key_Down: case Qt::Key_Down:
if (event->modifiers() & Qt::ShiftModifier) { if (event->modifiers() & Qt::ShiftModifier) {
if (event->modifiers() & Qt::ControlModifier) {
Zoom(zoom_offset, CursorBPos);
} else {
Zoom(2, CursorBPos); Zoom(2, CursorBPos);
}
} else {
if (event->modifiers() & Qt::ControlModifier) {
Zoom(zoom_offset, CursorAPos);
} else { } else {
Zoom(2, CursorAPos); Zoom(2, CursorAPos);
} }
}
break; break;
case Qt::Key_Up: case Qt::Key_Up:
if (event->modifiers() & Qt::ShiftModifier) { if (event->modifiers() & Qt::ShiftModifier) {
if (event->modifiers() & Qt::ControlModifier) {
Zoom(1.0/zoom_offset, CursorBPos);
} else {
Zoom(0.5, CursorBPos); Zoom(0.5, CursorBPos);
}
} else {
if (event->modifiers() & Qt::ControlModifier) {
Zoom(1.0/zoom_offset, CursorAPos);
} else { } else {
Zoom(0.5, CursorAPos); Zoom(0.5, CursorAPos);
} }
}
break; break;
case Qt::Key_Right: case Qt::Key_Right:

View file

@ -51,7 +51,7 @@ class Plot: public QWidget {
protected: protected:
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
void Zoom(double factor, int refX); void Zoom(double factor, uint32_t refX);
void Move(int offset); void Move(int offset);
void Trim(void); void Trim(void);
void wheelEvent(QWheelEvent *event); void wheelEvent(QWheelEvent *event);