Commit 70b166b4 authored by Alexander Semke's avatar Alexander Semke
Browse files

Smooth zoom in the worksheet view with the mouse wheel.

parent bb906020
Pipeline #155183 passed with stage
in 4 minutes and 35 seconds
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
SPDX-FileCopyrightText: 2018-2021 Alexander Semke <alexander.semke@web.de>
SPDX-FileCopyrightText: 2018-2022 Alexander Semke <alexander.semke@web.de>
*/
#include "worksheetview.h"
#include "worksheet.h"
#include <QApplication>
#include <QFocusEvent>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QTimeLine>
#include <QScrollBar>
WorksheetView::WorksheetView(Worksheet* scene, QWidget* parent) : QGraphicsView(scene, parent),
......@@ -241,6 +243,48 @@ void WorksheetView::focusOutEvent(QFocusEvent* event)
m_worksheet->stopAnimations();
}
void WorksheetView::wheelEvent(QWheelEvent* event)
{
if ((QApplication::keyboardModifiers() & Qt::ControlModifier)) {
//https://wiki.qt.io/Smooth_Zoom_In_QGraphicsView
int numDegrees = event->delta() / 8;
int numSteps = numDegrees / 15; // see QWheelEvent documentation
zoom(numSteps);
} else
QGraphicsView::wheelEvent(event);
}
void WorksheetView::zoom(int numSteps)
{
m_numScheduledScalings += numSteps;
if (m_numScheduledScalings * numSteps < 0) // if user moved the wheel in another direction, we reset previously scheduled scalings
m_numScheduledScalings = numSteps;
auto* anim = new QTimeLine(350, this);
anim->setUpdateInterval(20);
connect(anim, &QTimeLine::valueChanged, this, &WorksheetView::scalingTime);
connect(anim, &QTimeLine::finished, this, &WorksheetView::animFinished);
anim->start();
}
void WorksheetView::scalingTime()
{
qreal factor = 1.0 + qreal(m_numScheduledScalings) / 300.0;
m_scale *= factor;
updateSceneSize();
scale(factor, factor);
}
void WorksheetView::animFinished()
{
if (m_numScheduledScalings > 0)
m_numScheduledScalings--;
else
m_numScheduledScalings++;
sender()->~QObject();
emit scaleFactorChanged(m_scale);
}
qreal WorksheetView::scaleFactor() const
{
......
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
SPDX-FileCopyrightText: 2018-2021 Alexander Semke <alexander.semke@web.de>
SPDX-FileCopyrightText: 2018-2022 Alexander Semke <alexander.semke@web.de>
*/
#ifndef WORKSHEETVIEW_H
......@@ -50,8 +50,14 @@ private:
void resizeEvent(QResizeEvent*) override;
void focusInEvent(QFocusEvent*) override;
void focusOutEvent(QFocusEvent*) override;
void wheelEvent(QWheelEvent*) override;
void zoom(int);
void scalingTime();
void animFinished();
qreal m_scale = 1.;
int m_numScheduledScalings{0};
QParallelAnimationGroup* m_animation{nullptr};
QPropertyAnimation* m_hAnimation{nullptr};
QPropertyAnimation* m_vAnimation{nullptr};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment