summaryrefslogtreecommitdiff
path: root/tagview.cpp
blob: 6bc3934d4e0c6232de9e8d4f9c14124cebb36704 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "tagview.h"

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

TagView::TagView(QWidget *parent)
    : QGraphicsView(parent)
    , saveCenterTimer(this)
{
    setDragMode(QGraphicsView::RubberBandDrag);
    setScene(&m_scene);
    connect(&m_scene, &TagScene::tagDoubleClicked, this, &TagView::tagDoubleClicked);
    connect(&m_scene, &TagScene::imageLoaded, this, &TagView::zoomToFit);

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

TagView::~TagView()
{
    were_done = true;
}

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

    setZoom(qMin(vp.width()/rect.width(), vp.height()/rect.height()));
    centerOn(m_scene.backgroundPixmapItem());
    qDebug() << "TagView::zoomToFit():" << sceneRect() << rect << viewport()->rect();
}

void TagView::setZoom(qreal zoom)
{
    this->zoom = zoom;
    if (m_proj)
        m_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;
    if (m_proj)
        m_proj->setMeta("view_rotation", rotation);
}

void TagView::setProject(SQLiteSaveFile *proj)
{
    m_proj = proj;
    m_scene.setProject(m_proj);
    restoreViewport();
}

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::scrollContentsBy(int dx, int dy)
{
    QGraphicsView::scrollContentsBy(dx, dy);
    /* Hackety hack: On object destruction this method is called downstream in the destructor chain. Prevent segfaults from calling an uninitialized timer. */
    if (!were_done)
        saveCenterTimer.start();
}

void TagView::keyPressEvent(QKeyEvent *event)
{
    if (event->modifiers() & Qt::ControlModifier)
        setDragMode(QGraphicsView::ScrollHandDrag);
    else
        setDragMode(QGraphicsView::RubberBandDrag);
}

void TagView::keyReleaseEvent(QKeyEvent *event)
{
    if (event->modifiers() & Qt::ControlModifier)
        setDragMode(QGraphicsView::ScrollHandDrag);
    else
        setDragMode(QGraphicsView::RubberBandDrag);
}

void TagView::focusInEvent(QFocusEvent *event)
{
    Q_UNUSED(event);
    if (QApplication::keyboardModifiers() & Qt::ControlModifier)
        setDragMode(QGraphicsView::ScrollHandDrag);
    else
        setDragMode(QGraphicsView::RubberBandDrag);
}

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

void TagView::restoreViewport()
{
    if (!m_proj)
        return;

    QVariant v_rot = m_proj->getMeta("view_rotation");
    if (v_rot.isValid()) {
        rotation = v_rot.toInt();
    } else {
        rotation = 0;
    }

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

    QVariant v_center = m_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(m_scene.itemsBoundingRect().center());
    }
}