Skip to content

Commit

Permalink
Implement zooming with the mouse wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
kovzol committed Jul 8, 2019
1 parent aa6320b commit e26cbe3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ui/fractalwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions src/ui/fractalwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/ui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down

0 comments on commit e26cbe3

Please sign in to comment.