From 46943de6e3a689c9424a0ce326d173ede4e47d4d Mon Sep 17 00:00:00 2001 From: cneben Date: Sat, 24 Aug 2024 16:26:00 +0200 Subject: [PATCH] Add qan::Navigable::center() method. Polish. Signed-off-by: cneben --- samples/groups/groups.qml | 2 ++ src/qanNavigable.cpp | 23 ++++++++++++++++++----- src/qanNavigable.h | 3 +++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/samples/groups/groups.qml b/samples/groups/groups.qml index 18c76e8a..a91c3f44 100644 --- a/samples/groups/groups.qml +++ b/samples/groups/groups.qml @@ -42,6 +42,8 @@ ApplicationWindow { ToolTip { x: 0; id: toolTip; timeout: 2000 } function notifyUser(message) { toolTip.text = message; toolTip.open() } + Component.onCompleted: graphView.center() + Qan.GraphView { id: graphView anchors.fill: parent diff --git a/src/qanNavigable.cpp b/src/qanNavigable.cpp index 4a30cef2..add34568 100644 --- a/src/qanNavigable.cpp +++ b/src/qanNavigable.cpp @@ -107,7 +107,6 @@ void Navigable::updateVirtualBr(const QRectF& containerChildrenRect) bool growTop = containerChildrenRect.top() < virtualBr.top(); bool growBottom = containerChildrenRect.bottom() > virtualBr.bottom(); auto newVirtualBr = virtualBr.united(containerChildrenRect); - // FIXME #244 use virtualBorder configuraiton here ! newVirtualBr.adjust(growLeft ? -50 : 0., growTop ? -50 : 0., growRight ? 50 : 0., @@ -129,6 +128,19 @@ void Navigable::setViewRect(const QRectF& viewRect) } } +void Navigable::center() { + const QRectF content = _containerItem->childrenRect(); + if (content.isEmpty()) + return; + _zoom = 1.0; + _containerItem->setScale(1.0); + const auto scale = 1.0; + const auto graphCenter = content.center(); + const auto viewCenter = QPointF{width() / 2., height() / 2.0}; + const auto topLeft = -(graphCenter * scale) - (viewCenter * scale); + _containerItem->setPosition(-topLeft); + emit navigated(); +} void Navigable::centerOn(QQuickItem* item) { @@ -177,9 +189,10 @@ void Navigable::centerOnPosition(QPointF position) const QPointF navigableCenter{width() / 2., height() / 2.}; const QPointF navigableCenterContainerCs = mapToItem(_containerItem, navigableCenter); const QPointF translation{navigableCenterContainerCs - position}; - const qreal zoom = _containerItem->scale(); + // FIXME: Affreux const auto containerPosition = _containerItem->position() + (translation * zoom); + if (containerPosition != _containerItem->position()) { // fuzzy compare _containerItem->setPosition(containerPosition); updateGrid(); @@ -202,9 +215,9 @@ void Navigable::moveTo(QPointF position) void Navigable::fitContentInView(qreal forceWidth, qreal forceHeight) { - //qWarning() << "qan::Navigable::fitContentInView(): forceWidth=" << forceWidth << " forceHeight=" << forceHeight; - QRectF content = _containerItem->childrenRect(); - //qWarning() << " content=" << content; + qWarning() << "qan::Navigable::fitContentInView(): forceWidth=" << forceWidth << " forceHeight=" << forceHeight; + const QRectF content = _containerItem->childrenRect(); + qWarning() << " content=" << content; if (!content.isEmpty()) { // Protect against div/0, can't fit if there is no content... const qreal viewWidth = forceWidth > 0. ? forceWidth : width(); const qreal viewHeight = forceHeight > 0. ? forceHeight : height(); diff --git a/src/qanNavigable.h b/src/qanNavigable.h index 62387c53..b6cbe58b 100644 --- a/src/qanNavigable.h +++ b/src/qanNavigable.h @@ -180,6 +180,9 @@ protected slots: void viewRectChanged(); public: + //! Center the view on graph content center and set a 1.0 zoom. + Q_INVOKABLE void center(); + //! Center the view on a given child item (zoom level is not modified). Q_INVOKABLE void centerOn(QQuickItem* item);