From e26cbe331a352c743635e5ca2b92d9454a6ac770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Kov=C3=A1cs?= Date: Mon, 8 Jul 2019 14:17:07 +0200 Subject: [PATCH] Implement zooming with the mouse wheel --- src/ui/fractalwidget.cpp | 6 ++++++ src/ui/fractalwidget.h | 1 + src/ui/mainwindow.cpp | 18 ++++++++++++++++++ src/ui/mainwindow.h | 3 +++ 4 files changed, 28 insertions(+) diff --git a/src/ui/fractalwidget.cpp b/src/ui/fractalwidget.cpp index 5500a71e..c54e37b2 100644 --- a/src/ui/fractalwidget.cpp +++ b/src/ui/fractalwidget.cpp @@ -87,3 +87,9 @@ void FractalWidget::mouseMoveEvent(QMouseEvent *event) m_mousePosition = event->pos(); event->ignore(); } + +void FractalWidget::wheelEvent(QWheelEvent *event) +{ + m_mousePosition = event->pos(); + event->ignore(); +} diff --git a/src/ui/fractalwidget.h b/src/ui/fractalwidget.h index 2c1a13fc..643566e8 100644 --- a/src/ui/fractalwidget.h +++ b/src/ui/fractalwidget.h @@ -23,6 +23,7 @@ class FractalWidget: public QWidget void mousePressEvent(QMouseEvent * event); void mouseReleaseEvent(QMouseEvent * event); void resizeEvent(QResizeEvent * event); + void wheelEvent(QWheelEvent *event); #ifdef USE_OPENGL void paintGL(); void resizeGL (int w, int h); diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 886e6472..e3a45a94 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -217,6 +217,18 @@ int MainWindow::mouseButtons() if (m_mouseButtons & Qt::RightButton) mouseButtons |= BUTTON3; } + // handle mouse wheel operations + if (m_mouseWheel > 0) + mouseButtons |= BUTTON1; + if (m_mouseWheel < 0) + mouseButtons |= BUTTON3; + if (m_mouseWheel != 0) { + timespec timenow; + clock_gettime(CLOCK_REALTIME, &timenow); + long elapsed = timenow.tv_sec * 1.0e9 + timenow.tv_nsec - wheeltimer.tv_sec * 1.0e9 - wheeltimer.tv_nsec; + if (elapsed > 1.0e9) // timing is hardcoded here + m_mouseWheel = 0; + } return mouseButtons; } @@ -235,6 +247,12 @@ void MainWindow::mouseReleaseEvent(QMouseEvent *event) m_mouseButtons = event->buttons(); } +void MainWindow::wheelEvent(QWheelEvent *event) +{ + m_mouseWheel = event->delta(); + clock_gettime(CLOCK_REALTIME, &wheeltimer); +} + void MainWindow::keyPressEvent(QKeyEvent *event) { m_keyboardModifiers = event->modifiers(); diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index 4ea9ba9d..973cfa63 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -8,6 +8,8 @@ class MainWindow:public QMainWindow { Q_OBJECT private: Qt::MouseButtons m_mouseButtons = 0; + int m_mouseWheel = 0; + timespec wheeltimer; Qt::KeyboardModifiers m_keyboardModifiers = 0; int m_keyCombination = 0; FractalWidget * m_fractalWidget; @@ -18,6 +20,7 @@ class MainWindow:public QMainWindow { void closeEvent(QCloseEvent *); void mousePressEvent(QMouseEvent * event); void mouseReleaseEvent(QMouseEvent * event); + void wheelEvent(QWheelEvent *event); void keyPressEvent(QKeyEvent * event); void keyReleaseEvent(QKeyEvent * event); private slots: