Skip to content

Commit

Permalink
Add qan::Navigable::center() method. Polish.
Browse files Browse the repository at this point in the history
Signed-off-by: cneben <[email protected]>
  • Loading branch information
cneben committed Aug 24, 2024
1 parent 009613b commit 46943de
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions samples/groups/groups.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 18 additions & 5 deletions src/qanNavigable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.,
Expand All @@ -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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/qanNavigable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 46943de

Please sign in to comment.