diff options
Diffstat (limited to 'tagscene.cpp')
-rw-r--r-- | tagscene.cpp | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/tagscene.cpp b/tagscene.cpp index fb52574..a6efd17 100644 --- a/tagscene.cpp +++ b/tagscene.cpp @@ -2,15 +2,16 @@ #include <QGraphicsSceneMouseEvent> -TagScene::TagScene(SQLiteSaveFile &proj) - : proj(proj) -{ - reloadPicture(); - reloadTags(); +void TagScene::setProject(SQLiteSaveFile *proj) { + if (m_proj) + disconnect(m_proj, nullptr, this, nullptr); + m_proj = proj; + + reloadScene(); - connect(&proj, &SQLiteSaveFile::tagChange, this, &TagScene::tagChanged); - connect(&proj, &SQLiteSaveFile::fileReload, - [=]() { reloadTags(); }); + 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) @@ -40,11 +41,22 @@ void TagScene::tagChanged(TagChange change, const Tag &tag) void TagScene::reloadPicture() { - pix.loadFromData(proj.getImage()); + 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) { @@ -53,16 +65,20 @@ void TagScene::addTag(const Tag tag) { tags[tag.id] = it; } -void TagScene::reloadTags() +void TagScene::reloadScene() { clear(); - for (auto *it : tags.values()) { + for (auto *it : tags.values()) delete it; - } + pix_it = nullptr; - for (const Tag &tag : proj.getAllTags()) { + if (!m_proj) + return; + + for (const Tag &tag : m_proj->getAllTags()) addTag(tag); - } + + reloadPicture(); /* calls invalidate() for us */ } void TagScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) @@ -70,11 +86,18 @@ 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()); |