Basic model/view action works
This commit is contained in:
parent
752f270cf8
commit
2deadc6cfb
15 changed files with 439 additions and 133 deletions
59
tagview.cpp
59
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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue