diff options
Diffstat (limited to 'tagscene.cpp')
-rw-r--r-- | tagscene.cpp | 77 |
1 files changed, 76 insertions, 1 deletions
diff --git a/tagscene.cpp b/tagscene.cpp index 5b028a0..fb52574 100644 --- a/tagscene.cpp +++ b/tagscene.cpp @@ -1,6 +1,81 @@ #include "tagscene.h" -TagScene::TagScene() +#include <QGraphicsSceneMouseEvent> + +TagScene::TagScene(SQLiteSaveFile &proj) + : proj(proj) +{ + reloadPicture(); + reloadTags(); + + connect(&proj, &SQLiteSaveFile::tagChange, this, &TagScene::tagChanged); + connect(&proj, &SQLiteSaveFile::fileReload, + [=]() { reloadTags(); }); +} + +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() +{ + pix.loadFromData(proj.getImage()); + if (pix_it) + removeItem(pix_it); + pix_it = new QGraphicsPixmapItem(pix); + addItem(pix_it); +} + +void TagScene::addTag(const Tag tag) { + TagItem *it = new TagItem(tag); + addItem(it); + tags[tag.id] = it; +} + +void TagScene::reloadTags() { + clear(); + for (auto *it : tags.values()) { + delete it; + } + + for (const Tag &tag : proj.getAllTags()) { + addTag(tag); + } +} + +void TagScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) +{ + QGraphicsItem *it = itemAt(event->scenePos(), QTransform()); + if (!it) { + QGraphicsScene::mouseDoubleClickEvent(event); + } + + TagItem *tagitem = qgraphicsitem_cast<TagItem *>(it); + if (!tagitem) { + QGraphicsScene::mouseDoubleClickEvent(event); + } + tagDoubleClicked(tagitem->tag()); } |