summaryrefslogtreecommitdiff
path: root/src/graphicsview.cpp
blob: b81cff402a07795844eee68ff31e364a10358ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "graphicsview.h"

// qt
#include <QWheelEvent>

GraphicsView::GraphicsView(QWidget* parent)
    : QGraphicsView{parent}
{
    init();
}

GraphicsView::GraphicsView(QGraphicsScene* scene, QWidget* parent)
    : QGraphicsView{scene, parent}
{
    init();
}

void GraphicsView::wheelEvent(QWheelEvent* event)
{
    setTransformationAnchor(GraphicsView::AnchorUnderMouse);

    QPoint pixels = event->pixelDelta();
    QPoint degrees = event->angleDelta();
    const auto oldScenePos = event->scenePosition();
    const auto oldMapped = mapToScene(event->position().toPoint());

    if (pixels.isNull() && degrees.isNull())
        return;

    const double scaleMultiplier{1.1};
    const auto steps = double(pixels.isNull() ? degrees.y() : pixels.y());
    auto actualScale = std::abs(steps) * scaleMultiplier /
                       double(QWheelEvent::DefaultDeltasPerStep);

    if (steps < 0)
        actualScale = 1 / actualScale;

    scale(actualScale, actualScale);

    event->accept();
}

void GraphicsView::init()
{
    setDragMode(QGraphicsView::ScrollHandDrag);
    setRenderHint(QPainter::Antialiasing, true);
}