diff options
author | jaseg <git-bigdata-wsl-arch@jaseg.de> | 2020-08-16 17:04:32 +0200 |
---|---|---|
committer | jaseg <git-bigdata-wsl-arch@jaseg.de> | 2020-08-16 17:04:32 +0200 |
commit | 2deadc6cfbf71e06908963fb1bae628cfe370f9d (patch) | |
tree | d737d2e1e9cfde305840179680610d015f5dbf0e /tagview.cpp | |
parent | 752f270cf81bc8c88cb4bf428e5aceb51405c06a (diff) | |
download | numberator-2deadc6cfbf71e06908963fb1bae628cfe370f9d.tar.gz numberator-2deadc6cfbf71e06908963fb1bae628cfe370f9d.tar.bz2 numberator-2deadc6cfbf71e06908963fb1bae628cfe370f9d.zip |
Basic model/view action works
Diffstat (limited to 'tagview.cpp')
-rw-r--r-- | tagview.cpp | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/tagview.cpp b/tagview.cpp index b4e83dd..9a9c9a0 100644 --- a/tagview.cpp +++ b/tagview.cpp @@ -4,34 +4,41 @@ #include <QScrollBar> #include <cmath> -TagView::TagView(SQLiteSaveFile &proj) - : scene(proj) - , proj(proj) - , saveCenterTimer() +TagView::TagView(QWidget *parent) + : QGraphicsView(parent) + , saveCenterTimer(this) { setDragMode(QGraphicsView::ScrollHandDrag); - setScene(&scene); - connect(&scene, &TagScene::tagDoubleClicked, this, &TagView::tagDoubleClicked); + setScene(&m_scene); + connect(&m_scene, &TagScene::tagDoubleClicked, this, &TagView::tagDoubleClicked); + connect(&m_scene, &TagScene::imageLoaded, this, &TagView::zoomToFit); saveCenterTimer.setSingleShot(true); saveCenterTimer.setInterval(500); - connect(&saveCenterTimer, &QTimer::timeout, - this, &TagView::saveCenter); + connect(&saveCenterTimer, &QTimer::timeout, this, &TagView::saveCenter); +} + +TagView::~TagView() +{ + were_done = true; } void TagView::zoomToFit() { QTransform tx = QTransform().rotate(-rotation); - QRectF rect = tx.mapRect(scene.itemsBoundingRect()); + QRectF rect = tx.mapRect(m_scene.itemsBoundingRect()); QRectF vp = viewport()->rect(); setZoom(qMin(vp.width()/rect.width(), vp.height()/rect.height())); + centerOn(m_scene.backgroundPixmapItem()); + qDebug() << "TagView::zoomToFit():" << sceneRect() << rect << viewport()->rect(); } void TagView::setZoom(qreal zoom) { this->zoom = zoom; - proj.setMeta("view_zoom", zoom); + if (m_proj) + m_proj->setMeta("view_zoom", zoom); setTransform(QTransform::fromScale(zoom, zoom).rotate(rotation)); } @@ -47,7 +54,15 @@ void TagView::rotate(int angle) if (tmp < 0) tmp += 360; rotation = tmp; - proj.setMeta("view_rotation", rotation); + if (m_proj) + m_proj->setMeta("view_rotation", rotation); +} + +void TagView::setProject(SQLiteSaveFile *proj) +{ + m_proj = proj; + m_scene.setProject(m_proj); + restoreViewport(); } void TagView::wheelEvent(QWheelEvent *evt) @@ -63,22 +78,34 @@ void TagView::wheelEvent(QWheelEvent *evt) } } +void TagView::scrollContentsBy(int dx, int dy) +{ + QGraphicsView::scrollContentsBy(dx, dy); + /* Hackety hack: On object destruction this method is called downstream in the destructor chain. Prevent segfaults from calling an uninitialized timer. */ + if (!were_done) + saveCenterTimer.start(); +} + void TagView::saveCenter() { QPointF p = mapToScene(viewport()->rect().center()); - proj.setMeta("view_center", QJsonDocument(QJsonArray({p.x(), p.y()})).toJson()); + if (m_proj) + m_proj->setMeta("view_center", QJsonDocument(QJsonArray({p.x(), p.y()})).toJson()); } void TagView::restoreViewport() { - QVariant v_rot = proj.getMeta("view_rotation"); + if (!m_proj) + return; + + QVariant v_rot = m_proj->getMeta("view_rotation"); if (v_rot.isValid()) { rotation = v_rot.toInt(); } else { rotation = 0; } - QVariant v_zoom = proj.getMeta("view_zoom"); + QVariant v_zoom = m_proj->getMeta("view_zoom"); if (v_zoom.isValid()) { zoom = v_zoom.toDouble(); setTransform(QTransform::fromScale(zoom, zoom).rotate(rotation)); @@ -86,13 +113,13 @@ void TagView::restoreViewport() zoomToFit(); } - QVariant v_center = proj.getMeta("view_center"); + QVariant v_center = m_proj->getMeta("view_center"); if (v_center.isValid()) { QJsonArray arr = QJsonDocument::fromJson(v_center.toByteArray()).toVariant().toJsonArray(); assert(arr.size() == 2); assert(arr[0].isDouble() && arr[1].isDouble()); centerOn(QPointF(arr[0].toDouble(), arr[1].toDouble())); } else { - centerOn(scene.itemsBoundingRect().center()); + centerOn(m_scene.itemsBoundingRect().center()); } } |