summaryrefslogtreecommitdiff
path: root/tagscene.cpp
blob: c5777e0fe74af29dede336c6aa1d6e1ecee7e2e6 (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
#include "tagscene.h"

#include <QGraphicsSceneMouseEvent>

void TagScene::setProject(SQLiteSaveFile *proj) {
    if (m_proj)
        disconnect(m_proj, nullptr, this, nullptr);
    m_proj = proj;

    reloadScene();

    connect(m_proj, &SQLiteSaveFile::tagChange, this, &TagScene::tagChanged);
    connect(m_proj, &SQLiteSaveFile::fileReload, this, &TagScene::reloadScene);
    connect(m_proj, &SQLiteSaveFile::imageLoaded, this, &TagScene::reloadPicture);
}

void TagScene::tagChanged(TagChange change, const Tag &tag)
{
    TagItem *it;

    switch(change)
    {
    case TagChange::CREATED:
        addTag(tag);
        break;

    case TagChange::DELETED:
        it = tags.take(tag.id);
        assert(it);
        removeItem(it);
        break;

    case TagChange::CHANGED:
        it = tags[tag.id];
        assert(it);

        it->tagUpdated(tag);
        break;
    }
}

void TagScene::reloadPicture()
{
    if (!m_proj)
        return;

    pix.loadFromData(m_proj->getImage());
    if (pix_it)
        removeItem(pix_it);
    pix_it = new QGraphicsPixmapItem(pix);
    pix_it->setZValue(-1);
    addItem(pix_it);
    QRectF imgBounds = pix_it->boundingRect();
    imgBounds = imgBounds.marginsAdded(QMargins(imgBounds.width() * 0.5, imgBounds.height() * 0.5, imgBounds.width() * 0.5, imgBounds.height() * 0.5));
    setSceneRect(itemsBoundingRect().united(imgBounds));
    qDebug() << "TagScene: reloadPicture()" << pix_it->boundingRect() << pix_it << pix << pix_it->isActive();

    invalidate();
    imageLoaded();
}

void TagScene::addTag(const Tag tag) {
    TagItem *it = new TagItem(tag);
    addItem(it);
    tags[tag.id] = it;
}

void TagScene::reloadScene()
{
    clear();
    for (auto *it : tags.values())
        delete it;
    pix_it = nullptr;

    if (!m_proj)
        return;

    for (const Tag &tag : m_proj->getAllTags())
        addTag(tag);

    reloadPicture(); /* calls invalidate() for us */
}

void TagScene::selectTag(const Tag &tag)
{
    clearSelection();
    TagItem *it = tags[tag.id];
    if (!it)
        return;
    it->setSelected(true);
}

void TagScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem *it = itemAt(event->scenePos(), QTransform());
    if (!it) {
        QGraphicsScene::mouseDoubleClickEvent(event);
        return;
    }

    if (it == pix_it) {
        m_proj->createTagAt(event->scenePos());
        return;
    }

    TagItem *tagitem = qgraphicsitem_cast<TagItem *>(it);
    if (!tagitem) {
        QGraphicsScene::mouseDoubleClickEvent(event);
        return;
    }

    tagDoubleClicked(tagitem->tag());
}