2016-03-30 23:02:35 +03:00
|
|
|
#include "lrgraphicsviewzoom.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
|
2016-03-30 23:02:35 +03:00
|
|
|
#include <QApplication>
|
2024-09-04 17:31:16 +03:00
|
|
|
#include <QMouseEvent>
|
2016-04-05 23:06:11 +03:00
|
|
|
#include <qmath.h>
|
2016-03-30 23:02:35 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
namespace LimeReport {
|
2016-03-30 23:02:35 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
GraphicsViewZoomer::GraphicsViewZoomer(QGraphicsView* view): QObject(view), m_view(view)
|
2016-03-30 23:02:35 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
m_view->viewport()->installEventFilter(this);
|
|
|
|
m_view->setMouseTracking(true);
|
|
|
|
m_modifiers = Qt::ControlModifier;
|
|
|
|
m_zoomFactorBase = 1.0009;
|
2016-03-30 23:02:35 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void GraphicsViewZoomer::gentleZoom(double factor)
|
|
|
|
{
|
|
|
|
m_view->scale(factor, factor);
|
|
|
|
m_view->centerOn(m_targetScenePos);
|
|
|
|
QPointF delta_viewport_pos = m_targetViewportPos
|
|
|
|
- QPointF(m_view->viewport()->width() / 2.0, m_view->viewport()->height() / 2.0);
|
|
|
|
QPointF viewport_center = m_view->mapFromScene(m_targetScenePos) - delta_viewport_pos;
|
|
|
|
m_view->centerOn(m_view->mapToScene(viewport_center.toPoint()));
|
|
|
|
emit zoomed(factor);
|
2016-03-30 23:02:35 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void GraphicsViewZoomer::setModifiers(Qt::KeyboardModifiers modifiers) { m_modifiers = modifiers; }
|
2016-03-30 23:02:35 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void GraphicsViewZoomer::setZoomFactorBase(double value) { m_zoomFactorBase = value; }
|
2016-06-10 18:05:18 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void GraphicsViewZoomer::setView(QGraphicsView* view)
|
2016-06-10 18:05:18 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_view != view) {
|
2016-06-10 18:05:18 +03:00
|
|
|
m_view->viewport()->removeEventFilter(this);
|
|
|
|
m_view = view;
|
|
|
|
m_view->viewport()->installEventFilter(this);
|
|
|
|
m_view->setMouseTracking(true);
|
|
|
|
}
|
2016-03-30 23:02:35 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool GraphicsViewZoomer::eventFilter(QObject* object, QEvent* event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::MouseMove) {
|
|
|
|
QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
|
|
|
|
QPointF delta = m_targetViewportPos - mouse_event->pos();
|
|
|
|
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) {
|
|
|
|
m_targetViewportPos = mouse_event->pos();
|
|
|
|
m_targetScenePos = m_view->mapToScene(mouse_event->pos());
|
|
|
|
}
|
|
|
|
} else if (event->type() == QEvent::Wheel) {
|
|
|
|
QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
|
|
|
|
if (QApplication::keyboardModifiers() == m_modifiers) {
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2024-09-04 17:31:16 +03:00
|
|
|
if (wheel_event->orientation() == Qt::Vertical) {
|
|
|
|
double angle = wheel_event->delta();
|
|
|
|
double factor = qPow(m_zoomFactorBase, angle);
|
|
|
|
gentleZoom(factor);
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-24 08:37:40 +03:00
|
|
|
#else
|
2024-09-04 17:31:16 +03:00
|
|
|
if (wheel_event->angleDelta().y() != 0) {
|
|
|
|
double angle = wheel_event->angleDelta().y();
|
|
|
|
double factor = qPow(m_zoomFactorBase, angle);
|
|
|
|
gentleZoom(factor);
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-24 08:37:40 +03:00
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
}
|
2016-03-30 23:02:35 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
Q_UNUSED(object)
|
|
|
|
return false;
|
2016-03-30 23:02:35 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
} // namespace LimeReport
|