#include "tagscene.h" #include 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::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(it); if (!tagitem) { QGraphicsScene::mouseDoubleClickEvent(event); return; } tagDoubleClicked(tagitem->tag()); }