#include "tagscene.h" #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()); }