summaryrefslogtreecommitdiff
path: root/tagview.cpp
blob: abaefcb60d38dfbcca213b104a789a0f16d5ec2b (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "tagview.h"

#include <QWheelEvent>
#include <QScrollBar>
#include <cmath>

TagView::TagView(SQLiteSaveFile &proj)
    : proj(proj)
    , saveCenterTimer()
{
    setDragMode(QGraphicsView::ScrollHandDrag);
    setScene(&scene);

    saveCenterTimer.setSingleShot(true);
    saveCenterTimer.setInterval(500);
    connect(&saveCenterTimer, &QTimer::timeout,
            this, &TagView::saveCenter);
}

void TagView::zoomToFit()
{
    QTransform tx = QTransform().rotate(-rotation);
    QRectF rect = tx.mapRect(scene.itemsBoundingRect());
    QRectF vp = viewport()->rect();

    setZoom(qMin(vp.width()/rect.width(), vp.height()/rect.height()));
}

void TagView::setZoom(qreal zoom)
{
    this->zoom = zoom;
    proj.setMeta("view_zoom", zoom);
    setTransform(QTransform::fromScale(zoom, zoom).rotate(rotation));
}

void TagView::zoomIn(qreal delta)
{
    setZoom(qMax(1.0/16, qMin(4.0, zoom * qPow(1.2, delta/120))));
}

void TagView::rotate(int angle)
{
    QGraphicsView::rotate(angle);
    int tmp = (rotation + angle) % 360;
    if (tmp < 0)
        tmp += 360;
    rotation = tmp;
    proj.setMeta("view_rotation", rotation);
}

void TagView::wheelEvent(QWheelEvent *evt)
{
    if (evt->modifiers() == Qt::ControlModifier) {
        zoomIn(evt->angleDelta().y());
    } else {
        if (qAbs(evt->angleDelta().x()) > qAbs(evt->angleDelta().y())) {
            QCoreApplication::sendEvent(horizontalScrollBar(), evt);
        } else {
            QCoreApplication::sendEvent(verticalScrollBar(), evt);
        }
    }
}

void TagView::saveCenter()
{
    QPointF p = mapToScene(viewport()->rect().center());
    proj.setMeta("view_center", QJsonDocument(QJsonArray({p.x(), p.y()})).toJson());
}

void TagView::restoreViewport()
{
    QVariant v_rot = proj.getMeta("view_rotation");
    if (v_rot.isValid()) {
        rotation = v_rot.toInt();
    } else {
        rotation = 0;
    }

    QVariant v_zoom = proj.getMeta("view_zoom");
    if (v_zoom.isValid()) {
        zoom = v_zoom.toDouble();
        setTransform(QTransform::fromScale(zoom, zoom).rotate(rotation));
    } else {
        zoomToFit();
    }

    QVariant v_center = proj.getMeta("view_center");
    if (v_center.isValid()) {
        QJsonArray arr = QJsonDocument::fromJson(v_center.toByteArray()).toVariant().toJsonArray();
        assert(arr.size() == 2);
        assert(arr[0].isDouble() && arr[1].isDouble());
        centerOn(QPointF(arr[0].toDouble(), arr[1].toDouble()));
    } else {
        centerOn(scene.itemsBoundingRect().center());
    }
}