From 0afe9ca6bf53da111e445d2aea7cfc94e7e3c601 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 9 Aug 2020 17:12:14 +0200 Subject: db backend: mostly feature-complete --- tagscene.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'tagscene.cpp') 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 + +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(it); + if (!tagitem) { + QGraphicsScene::mouseDoubleClickEvent(event); + } + tagDoubleClicked(tagitem->tag()); } -- cgit