From 8ee95e63a92b07ba89003243b0e93718762a7d88 Mon Sep 17 00:00:00 2001 From: "Eduardo M. Fleury" Date: Wed, 4 Nov 2009 10:49:05 -0300 Subject: QGAL: Support for out-of-order parallel anchors Now parallel anchors account for the fact they may be composed of anchors with different directions. We arbitrarily set the direction of the parallel anchor to be the same direction as the first child. The methods refreshSizeHints and updateChildren were updated to support this situation. Signed-off-by: Eduardo M. Fleury Reviewed-by: Caio Marcelo de Oliveira Filho --- src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 46 ++++++++++++++++++++---- src/gui/graphicsview/qgraphicsanchorlayout_p.h | 9 ++--- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index 7ad994c..95922ca 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -218,9 +218,20 @@ bool AnchorData::refreshSizeHints(const QLayoutStyleInfo *styleInfo) void ParallelAnchorData::updateChildrenSizes() { - firstEdge->sizeAtMinimum = secondEdge->sizeAtMinimum = sizeAtMinimum; - firstEdge->sizeAtPreferred = secondEdge->sizeAtPreferred = sizeAtPreferred; - firstEdge->sizeAtMaximum = secondEdge->sizeAtMaximum = sizeAtMaximum; + firstEdge->sizeAtMinimum = sizeAtMinimum; + firstEdge->sizeAtPreferred = sizeAtPreferred; + firstEdge->sizeAtMaximum = sizeAtMaximum; + + const bool secondFwd = (secondEdge->from == from); + if (secondFwd) { + secondEdge->sizeAtMinimum = sizeAtMinimum; + secondEdge->sizeAtPreferred = sizeAtPreferred; + secondEdge->sizeAtMaximum = sizeAtMaximum; + } else { + secondEdge->sizeAtMinimum = -sizeAtMinimum; + secondEdge->sizeAtPreferred = -sizeAtPreferred; + secondEdge->sizeAtMaximum = -sizeAtMaximum; + } firstEdge->updateChildrenSizes(); secondEdge->updateChildrenSizes(); @@ -239,8 +250,16 @@ bool ParallelAnchorData::refreshSizeHints_helper(const QLayoutStyleInfo *styleIn return false; } - minSize = qMax(firstEdge->minSize, secondEdge->minSize); - maxSize = qMin(firstEdge->maxSize, secondEdge->maxSize); + // Account for parallel anchors where the second edge is backwards. + // We rely on the fact that a forward anchor of sizes min, pref, max is equivalent + // to a backwards anchor of size (-max, -pref, -min) + const bool secondFwd = (secondEdge->from == from); + const qreal secondMin = secondFwd ? secondEdge->minSize : -secondEdge->maxSize; + const qreal secondPref = secondFwd ? secondEdge->prefSize : -secondEdge->prefSize; + const qreal secondMax = secondFwd ? secondEdge->maxSize : -secondEdge->minSize; + + minSize = qMax(firstEdge->minSize, secondMin); + maxSize = qMin(firstEdge->maxSize, secondMax); // This condition means that the maximum size of one anchor being simplified is smaller than // the minimum size of the other anchor. The consequence is that there won't be a valid size @@ -249,7 +268,22 @@ bool ParallelAnchorData::refreshSizeHints_helper(const QLayoutStyleInfo *styleIn return false; } - prefSize = qMax(firstEdge->prefSize, secondEdge->prefSize); + // The equivalent preferred Size of a parallel anchor is calculated as to + // reduce the deviation from the original preferred sizes _and_ to avoid shrinking + // items below their preferred sizes, unless strictly needed. + + // ### This logic only holds if all anchors in the layout are "well-behaved" in the + // following terms: + // + // - There are no negative-sized anchors + // - All sequential anchors are composed of children in the same direction as the + // sequential anchor itself + // + // With these assumptions we can grow a child knowing that no hidden items will + // have to shrink as the result of that. + // If any of these does not hold, we have a situation where the ParallelAnchor + // does not have enough information to calculate its equivalent prefSize. + prefSize = qMax(firstEdge->prefSize, secondPref); prefSize = qMin(prefSize, maxSize); // See comment in AnchorData::refreshSizeHints() about sizeAt* values diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.h b/src/gui/graphicsview/qgraphicsanchorlayout_p.h index 1e11ee2..8d77b1a 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.h +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.h @@ -256,10 +256,11 @@ struct ParallelAnchorData : public AnchorData type = AnchorData::Parallel; orientation = first->orientation; - // ### Those asserts force that both child anchors have the same direction, - // but can't we simplify a pair of anchors in opposite directions? - Q_ASSERT(first->from == second->from); - Q_ASSERT(first->to == second->to); + // This assert whether the child anchors share their vertices + Q_ASSERT(((first->from == second->from) && (first->to == second->to)) || + ((first->from == second->to) && (first->to == second->from))); + + // We arbitrarily choose the direction of the first child as "our" direction from = first->from; to = first->to; #ifdef QT_DEBUG -- cgit v0.12 From 720e04ac5f572c1ca0a32e1675fbda6e778d3e8c Mon Sep 17 00:00:00 2001 From: "Eduardo M. Fleury" Date: Thu, 5 Nov 2009 19:18:48 -0300 Subject: QGAL: Revamp the edge interpolation code Simplify the code of interpolateSequentialAnchor to use the distance values of the sequential anchor itself as base for the children distances. So the 'base' parameter was removed. This also allowed out-of-order parallel anchors to be interpolated, and the 'base' parameter also to be removed from interpolateParallelAnchor. Signed-off-by: Eduardo M. Fleury Reviewed-by: Caio Marcelo de Oliveira Filho --- src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 49 ++++++++++++------------ src/gui/graphicsview/qgraphicsanchorlayout_p.h | 6 +-- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index 95922ca..318ce20 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -2443,17 +2443,13 @@ void QGraphicsAnchorLayoutPrivate::interpolateEdge(AnchorVertex *base, // Process child anchors if (edge->type == AnchorData::Sequential) - interpolateSequentialEdges(edge->from, - static_cast(edge), - orientation); + interpolateSequentialEdges(static_cast(edge), orientation); else if (edge->type == AnchorData::Parallel) - interpolateParallelEdges(edge->from, - static_cast(edge), - orientation); + interpolateParallelEdges(static_cast(edge), orientation); } void QGraphicsAnchorLayoutPrivate::interpolateParallelEdges( - AnchorVertex *base, ParallelAnchorData *data, Orientation orientation) + ParallelAnchorData *data, Orientation orientation) { // In parallels the boundary vertices are already calculate, we // just need to look for sequential groups inside, because only @@ -2461,40 +2457,43 @@ void QGraphicsAnchorLayoutPrivate::interpolateParallelEdges( // First edge if (data->firstEdge->type == AnchorData::Sequential) - interpolateSequentialEdges(base, - static_cast(data->firstEdge), + interpolateSequentialEdges(static_cast(data->firstEdge), orientation); else if (data->firstEdge->type == AnchorData::Parallel) - interpolateParallelEdges(base, - static_cast(data->firstEdge), + interpolateParallelEdges(static_cast(data->firstEdge), orientation); // Second edge if (data->secondEdge->type == AnchorData::Sequential) - interpolateSequentialEdges(base, - static_cast(data->secondEdge), + interpolateSequentialEdges(static_cast(data->secondEdge), orientation); else if (data->secondEdge->type == AnchorData::Parallel) - interpolateParallelEdges(base, - static_cast(data->secondEdge), + interpolateParallelEdges(static_cast(data->secondEdge), orientation); } void QGraphicsAnchorLayoutPrivate::interpolateSequentialEdges( - AnchorVertex *base, SequentialAnchorData *data, Orientation orientation) + SequentialAnchorData *data, Orientation orientation) { - AnchorVertex *prev = base; + // This method is supposed to handle any sequential anchor, even out-of-order + // ones. However, in the current QGAL implementation we should get only the + // well behaved ones. + Q_ASSERT(data->m_edges.first()->from == data->from); + Q_ASSERT(data->m_edges.last()->to == data->to); - // ### I'm not sure whether this assumption is safe. If not, - // consider that m_edges.last() could be used instead (so - // at(0) would be the one to be treated specially). - Q_ASSERT(base == data->m_edges.at(0)->to || base == data->m_edges.at(0)->from); + // At this point, the two outter vertices already have their distance + // calculated. + // We use the first as the base to calculate the internal ones + + AnchorVertex *prev = data->from; - // Skip the last for (int i = 0; i < data->m_edges.count() - 1; ++i) { - AnchorData *child = data->m_edges.at(i); - interpolateEdge(prev, child, orientation); - prev = child->to; + AnchorData *edge = data->m_edges.at(i); + interpolateEdge(prev, edge, orientation); + + // Use the recently calculated vertex as the base for the next one + const bool edgeIsForward = (edge->from == prev); + prev = edgeIsForward ? edge->to : edge->from; } // Treat the last specially, since we already calculated it's end diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.h b/src/gui/graphicsview/qgraphicsanchorlayout_p.h index 8d77b1a..d8e62b4 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.h +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.h @@ -475,10 +475,8 @@ public: void calculateVertexPositions(Orientation orientation); void setupEdgesInterpolation(Orientation orientation); void interpolateEdge(AnchorVertex *base, AnchorData *edge, Orientation orientation); - void interpolateSequentialEdges(AnchorVertex *base, SequentialAnchorData *edge, - Orientation orientation); - void interpolateParallelEdges(AnchorVertex *base, ParallelAnchorData *edge, - Orientation orientation); + void interpolateSequentialEdges(SequentialAnchorData *edge, Orientation orientation); + void interpolateParallelEdges(ParallelAnchorData *edge, Orientation orientation); // Linear Programming solver methods bool solveMinMax(const QList &constraints, -- cgit v0.12 From f2f7ad1e0a73ef28aee406de358e1308f817bf6d Mon Sep 17 00:00:00 2001 From: "Eduardo M. Fleury" Date: Thu, 5 Nov 2009 14:44:04 -0300 Subject: QGAL (Test): Add test where center anchor is simplified by parallel We were not handling the case where a parallel anchor is created to simplify a central anchor. Unfortunately no tests had shown that case yet. Signed-off-by: Eduardo M. Fleury Reviewed-by: Caio Marcelo de Oliveira Filho --- .../tst_qgraphicsanchorlayout.cpp | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/auto/qgraphicsanchorlayout/tst_qgraphicsanchorlayout.cpp b/tests/auto/qgraphicsanchorlayout/tst_qgraphicsanchorlayout.cpp index cc96edb..8b52674 100644 --- a/tests/auto/qgraphicsanchorlayout/tst_qgraphicsanchorlayout.cpp +++ b/tests/auto/qgraphicsanchorlayout/tst_qgraphicsanchorlayout.cpp @@ -83,6 +83,7 @@ private slots: void infiniteMaxSizes(); void simplifiableUnfeasible(); void simplificationVsOrder(); + void parallelSimplificationOfCenter(); }; class RectWidget : public QGraphicsWidget @@ -1801,5 +1802,30 @@ void tst_QGraphicsAnchorLayout::simplificationVsOrder() } } +void tst_QGraphicsAnchorLayout::parallelSimplificationOfCenter() +{ + QSizeF min(10, 10); + QSizeF pref(20, 10); + QSizeF max(50, 10); + + QGraphicsWidget *a = createItem(min, pref, max, "A"); + QGraphicsWidget *b = createItem(min, pref, max, "B"); + + QGraphicsWidget parent; + QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout(&parent); + l->setContentsMargins(0, 0, 0, 0); + + l->addAnchor(l, Qt::AnchorLeft, a, Qt::AnchorLeft); + l->addAnchor(l, Qt::AnchorRight, a, Qt::AnchorRight); + + l->addAnchor(a, Qt::AnchorHorizontalCenter, b, Qt::AnchorLeft); + l->addAnchor(b, Qt::AnchorRight, a, Qt::AnchorRight); + + parent.resize(l->effectiveSizeHint(Qt::PreferredSize)); + + QCOMPARE(a->geometry(), QRectF(0, 0, 40, 10)); + QCOMPARE(b->geometry(), QRectF(20, 0, 20, 10)); +} + QTEST_MAIN(tst_QGraphicsAnchorLayout) #include "tst_qgraphicsanchorlayout.moc" -- cgit v0.12 From 92b51d119325adc7cef518c1260c71904fc68c40 Mon Sep 17 00:00:00 2001 From: "Eduardo M. Fleury" Date: Thu, 5 Nov 2009 20:11:37 -0300 Subject: QGAL (Test): Enable remaining tests in 'qgraphicsanchorlayout1' Some tests were still disabled because were not supported somewhere in the past. Signed-off-by: Eduardo M. Fleury Reviewed-by: Caio Marcelo de Oliveira Filho --- .../tst_qgraphicsanchorlayout1.cpp | 25 ++++++++-------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/tests/auto/qgraphicsanchorlayout1/tst_qgraphicsanchorlayout1.cpp b/tests/auto/qgraphicsanchorlayout1/tst_qgraphicsanchorlayout1.cpp index 57e5c41..0fbd069 100644 --- a/tests/auto/qgraphicsanchorlayout1/tst_qgraphicsanchorlayout1.cpp +++ b/tests/auto/qgraphicsanchorlayout1/tst_qgraphicsanchorlayout1.cpp @@ -423,7 +423,6 @@ void tst_QGraphicsAnchorLayout1::testAddAndRemoveAnchor() layout->setAnchor(layout, Qt::AnchorLeft, widget5, Qt::AnchorTop, 10); QCOMPARE( layout->count(), 4 ); - // ###: NOT SUPPORTED // anchor two edges of a widget (to define width / height) QTest::ignoreMessage(QtWarningMsg, "QGraphicsAnchorLayout::addAnchor(): Cannot anchor the item to itself"); layout->setAnchor(widget5, Qt::AnchorLeft, widget5, Qt::AnchorRight, 10); @@ -520,8 +519,6 @@ void tst_QGraphicsAnchorLayout1::testIsValid() widget->setLayout(layout); widget->setGeometry(QRectF(0,0,100,100)); - // ###: this shall change once isValid() is ready - // QCOMPARE(layout->isValid(), false); QCOMPARE(layout->isValid(), true); delete widget; } @@ -698,9 +695,8 @@ void tst_QGraphicsAnchorLayout1::testSpecialCases() layout2->setAnchor(widget1, Qt::AnchorRight, layout2, Qt::AnchorRight, 1); layout2->setAnchor(widget1, Qt::AnchorBottom, layout2, Qt::AnchorBottom, 1); - // ###: uncomment when simplification bug is solved - //widget->setGeometry(QRectF(0,0,100,100)); - //QCOMPARE(widget1->geometry(), QRectF(51,2,47,96)); + widget->setGeometry(QRectF(0,0,100,100)); + QCOMPARE(widget1->geometry(), QRectF(51,2,47,96)); delete widget; } @@ -900,9 +896,6 @@ void tst_QGraphicsAnchorLayout1::testBasicLayout_data() << BasicData(-1, Qt::AnchorLeft, 1, Qt::AnchorRight, 20) ; - // ### SIMPLIFICATION BUG FOR ITEM 1 - // ### remove this when bug is solved - theResult << BasicResult(0, QRectF(10, 10, 180, 80) ) << BasicResult(1, QRectF(10, 80, 10, 10) ) @@ -1732,8 +1725,6 @@ void tst_QGraphicsAnchorLayout1::testBasicLayout() QCOMPARE(expected, actual); } - // ###: not supported yet -/* // Test mirrored mode widget->setLayoutDirection(Qt::RightToLeft); layout->activate(); @@ -1745,10 +1736,13 @@ void tst_QGraphicsAnchorLayout1::testBasicLayout() if (mirroredRect.isValid()){ mirroredRect.moveLeft(size.width()-item.rect.width()-item.rect.left()); } - QCOMPARE(widgets[item.index]->geometry(), mirroredRect); + QRectF expected = truncate(mirroredRect); + QRectF actual = truncate(widgets[item.index]->geometry()); + + QCOMPARE(expected, actual); delete widgets[item.index]; } -*/ + delete widget; } @@ -2447,6 +2441,8 @@ void tst_QGraphicsAnchorLayout1::testDoubleSizePolicy_data() QTest::newRow("double size policy: expanding-preferred") << sizePolicy1 << sizePolicy2 << width1 << width2; } + // QGAL handling of ignored flag is different + if (0) { QSizePolicy sizePolicy1( QSizePolicy::Ignored, QSizePolicy::Ignored ); QSizePolicy sizePolicy2( QSizePolicy::Preferred, QSizePolicy::Preferred ); @@ -2514,9 +2510,6 @@ void tst_QGraphicsAnchorLayout1::testDoubleSizePolicy_data() void tst_QGraphicsAnchorLayout1::testDoubleSizePolicy() { - // ### Size policy is not yet supported - return; - QFETCH(QSizePolicy, policy1); QFETCH(QSizePolicy, policy2); QFETCH(qreal, width1); -- cgit v0.12 From 2906522845d3ce9723b978b8b1490dfc9b9008f0 Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Wed, 4 Nov 2009 19:48:54 -0300 Subject: QGAL: fix leak when restoring anchors The restore algorithm had a leak, so we did not delete all the sequences and parallel anchors that were not part of the simplified graph, i.e. that were nested inside other anchors. This commit fix the leak and clean the restore function a little bit, so the recursive function is called even for Normal anchors (which are dealt accordingly). Note that the before/after information is exactly the ->from/->to information available in the anchor being looked on. Also added an assertion to document the fact that at this point (restoring anchor simplification), one of the two anchors inside a parallel anchor must be a sequence. The algorithm depends on that because you can't have two anchors with the same start and end points. Signed-off-by: Caio Marcelo de Oliveira Filho Reviewed-by: Eduardo M. Fleury --- src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 63 ++++++++++-------------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index 318ce20..ff3fe69 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -880,48 +880,40 @@ bool QGraphicsAnchorLayoutPrivate::simplifyGraphIteration(QGraphicsAnchorLayoutP return false; } -static void restoreSimplifiedAnchor(Graph &g, - AnchorData *edge, - AnchorVertex *before, - AnchorVertex *after) +static void restoreSimplifiedAnchor(Graph &g, AnchorData *edge) { - Q_ASSERT(edge->type != AnchorData::Normal); #if 0 static const char *anchortypes[] = {"Normal", "Sequential", "Parallel"}; qDebug("Restoring %s edge.", anchortypes[int(edge->type)]); #endif - if (edge->type == AnchorData::Sequential) { - SequentialAnchorData* seqEdge = static_cast(edge); - // restore the sequential anchor - AnchorVertex *prev = before; - AnchorVertex *last = after; - if (edge->from != prev) - qSwap(last, prev); - - for (int i = 0; i < seqEdge->m_edges.count(); ++i) { - AnchorVertex *v1 = (i < seqEdge->m_children.count()) ? seqEdge->m_children.at(i) : last; - AnchorData *data = seqEdge->m_edges.at(i); - if (data->type != AnchorData::Normal) { - restoreSimplifiedAnchor(g, data, prev, v1); - } else { - g.createEdge(prev, v1, data); - } - prev = v1; + + if (edge->type == AnchorData::Normal) { + g.createEdge(edge->from, edge->to, edge); + + } else if (edge->type == AnchorData::Sequential) { + SequentialAnchorData *sequence = static_cast(edge); + + for (int i = 0; i < sequence->m_edges.count(); ++i) { + AnchorData *data = sequence->m_edges.at(i); + restoreSimplifiedAnchor(g, data); } + + delete sequence; + } else if (edge->type == AnchorData::Parallel) { - ParallelAnchorData* parallelEdge = static_cast(edge); - AnchorData *parallelEdges[2] = {parallelEdge->firstEdge, - parallelEdge->secondEdge}; - for (int i = 0; i < 2; ++i) { - AnchorData *data = parallelEdges[i]; - if (data->type == AnchorData::Normal) { - g.createEdge(before, after, data); - } else { - restoreSimplifiedAnchor(g, data, before, after); - } - } + ParallelAnchorData* parallel = static_cast(edge); + + // ### Because of the way parallel anchors are created in the anchor simplification + // algorithm, we know that one of these will be a sequence, so it'll be safe if the other + // anchor create an edge between the same vertices as the parallel. + Q_ASSERT(parallel->firstEdge->type == AnchorData::Sequential + || parallel->secondEdge->type == AnchorData::Sequential); + restoreSimplifiedAnchor(g, parallel->firstEdge); + restoreSimplifiedAnchor(g, parallel->secondEdge); + + delete parallel; } } @@ -944,9 +936,8 @@ void QGraphicsAnchorLayoutPrivate::restoreSimplifiedGraph(Orientation orientatio AnchorVertex *v2 = connections.at(i).second; AnchorData *edge = g.edgeData(v1, v2); if (edge->type != AnchorData::Normal) { - AnchorData *oldEdge = g.takeEdge(v1, v2); - restoreSimplifiedAnchor(g, edge, v1, v2); - delete oldEdge; + g.takeEdge(v1, v2); + restoreSimplifiedAnchor(g, edge); } } } -- cgit v0.12 From 9d89f0bd155b9eccc89eac6ff30b572795062baa Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Thu, 5 Nov 2009 15:19:23 -0300 Subject: QGAL: allow parallel anchors simplify center anchors This commit make possible to parallelize anchors that are center anchors, i.e. that have extra constraints (stored in itemCenterConstraints). This is trivial to support since the parallel anchor will have the same size as its children, so it can just "take the place" of its children in the constraints. Restore function was added as well. To manipulate internal structures, relevant static functions were promoted to methods. Signed-off-by: Caio Marcelo de Oliveira Filho Reviewed-by: Eduardo M. Fleury --- src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 80 ++++++++++++++++++++---- src/gui/graphicsview/qgraphicsanchorlayout_p.h | 18 ++++-- 2 files changed, 82 insertions(+), 16 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index ff3fe69..ef89612 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -536,34 +536,67 @@ inline static qreal checkAdd(qreal a, qreal b) /*! \internal - Adds \a newAnchor to the graph \a g. + Adds \a newAnchor to the graph. Returns the newAnchor itself if it could be added without further changes to the graph. If a new parallel anchor had to be created, then returns the new parallel anchor. If a parallel anchor had to be created and it results in an unfeasible setup, \a feasible is set to false, otherwise true. + + Note that in the case a new parallel anchor is created, it might also take over some constraints + from its children anchors. */ -static AnchorData *addAnchorMaybeParallel(Graph *g, - AnchorData *newAnchor, bool *feasible) +AnchorData *QGraphicsAnchorLayoutPrivate::addAnchorMaybeParallel(AnchorData *newAnchor, bool *feasible) { + Orientation orientation = Orientation(newAnchor->orientation); + Graph &g = graph[orientation]; *feasible = true; // If already exists one anchor where newAnchor is supposed to be, we create a parallel // anchor. - if (AnchorData *oldAnchor = g->takeEdge(newAnchor->from, newAnchor->to)) { + if (AnchorData *oldAnchor = g.takeEdge(newAnchor->from, newAnchor->to)) { ParallelAnchorData *parallel = new ParallelAnchorData(oldAnchor, newAnchor); + // The parallel anchor will "replace" its children anchors in + // every center constraint that they appear. + + // ### If the dependent (center) anchors had reference(s) to their constraints, we + // could avoid traversing all the itemCenterConstraints. + QList &constraints = itemCenterConstraints[orientation]; + + AnchorData *children[2] = { oldAnchor, newAnchor }; + QList *childrenConstraints[2] = { ¶llel->m_firstConstraints, + ¶llel->m_secondConstraints }; + + for (int i = 0; i < 2; ++i) { + AnchorData *child = children[i]; + QList *childConstraints = childrenConstraints[i]; + + if (!child->isCenterAnchor) + continue; + + parallel->isCenterAnchor = true; + + for (int i = 0; i < constraints.count(); ++i) { + QSimplexConstraint *c = constraints[i]; + if (c->variables.contains(child)) { + childConstraints->append(c); + qreal v = c->variables.take(child); + c->variables.insert(parallel, v); + } + } + } + // At this point we can identify that the parallel anchor is not feasible, e.g. one // anchor minimum size is bigger than the other anchor maximum size. *feasible = parallel->refreshSizeHints_helper(0, false); newAnchor = parallel; } - g->createEdge(newAnchor->from, newAnchor->to, newAnchor); + g.createEdge(newAnchor->from, newAnchor->to, newAnchor); return newAnchor; } - /*! \internal @@ -858,7 +891,7 @@ bool QGraphicsAnchorLayoutPrivate::simplifyGraphIteration(QGraphicsAnchorLayoutP // If 'beforeSequence' and 'afterSequence' already had an anchor between them, we'll // create a parallel anchor between the new sequence and the old anchor. bool newFeasible; - AnchorData *newAnchor = addAnchorMaybeParallel(&g, sequence, &newFeasible); + AnchorData *newAnchor = addAnchorMaybeParallel(sequence, &newFeasible); if (!newFeasible) { *feasible = false; @@ -880,7 +913,7 @@ bool QGraphicsAnchorLayoutPrivate::simplifyGraphIteration(QGraphicsAnchorLayoutP return false; } -static void restoreSimplifiedAnchor(Graph &g, AnchorData *edge) +void QGraphicsAnchorLayoutPrivate::restoreSimplifiedAnchor(AnchorData *edge) { #if 0 static const char *anchortypes[] = {"Normal", @@ -889,6 +922,8 @@ static void restoreSimplifiedAnchor(Graph &g, AnchorDa qDebug("Restoring %s edge.", anchortypes[int(edge->type)]); #endif + Graph &g = graph[edge->orientation]; + if (edge->type == AnchorData::Normal) { g.createEdge(edge->from, edge->to, edge); @@ -897,26 +932,47 @@ static void restoreSimplifiedAnchor(Graph &g, AnchorDa for (int i = 0; i < sequence->m_edges.count(); ++i) { AnchorData *data = sequence->m_edges.at(i); - restoreSimplifiedAnchor(g, data); + restoreSimplifiedAnchor(data); } delete sequence; } else if (edge->type == AnchorData::Parallel) { ParallelAnchorData* parallel = static_cast(edge); + restoreSimplifiedConstraints(parallel); // ### Because of the way parallel anchors are created in the anchor simplification // algorithm, we know that one of these will be a sequence, so it'll be safe if the other // anchor create an edge between the same vertices as the parallel. Q_ASSERT(parallel->firstEdge->type == AnchorData::Sequential || parallel->secondEdge->type == AnchorData::Sequential); - restoreSimplifiedAnchor(g, parallel->firstEdge); - restoreSimplifiedAnchor(g, parallel->secondEdge); + restoreSimplifiedAnchor(parallel->firstEdge); + restoreSimplifiedAnchor(parallel->secondEdge); delete parallel; } } +void QGraphicsAnchorLayoutPrivate::restoreSimplifiedConstraints(ParallelAnchorData *parallel) +{ + if (!parallel->isCenterAnchor) + return; + + for (int i = 0; i < parallel->m_firstConstraints.count(); ++i) { + QSimplexConstraint *c = parallel->m_firstConstraints[i]; + qreal v = c->variables[parallel]; + c->variables.remove(parallel); + c->variables.insert(parallel->firstEdge, v); + } + + for (int i = 0; i < parallel->m_secondConstraints.count(); ++i) { + QSimplexConstraint *c = parallel->m_secondConstraints[i]; + qreal v = c->variables[parallel]; + c->variables.remove(parallel); + c->variables.insert(parallel->secondEdge, v); + } +} + void QGraphicsAnchorLayoutPrivate::restoreSimplifiedGraph(Orientation orientation) { if (!graphSimplified[orientation]) @@ -937,7 +993,7 @@ void QGraphicsAnchorLayoutPrivate::restoreSimplifiedGraph(Orientation orientatio AnchorData *edge = g.edgeData(v1, v2); if (edge->type != AnchorData::Normal) { g.takeEdge(v1, v2); - restoreSimplifiedAnchor(g, edge); + restoreSimplifiedAnchor(edge); } } } diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.h b/src/gui/graphicsview/qgraphicsanchorlayout_p.h index d8e62b4..d7b3cfd 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.h +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.h @@ -275,6 +275,9 @@ struct ParallelAnchorData : public AnchorData AnchorData* firstEdge; AnchorData* secondEdge; + + QList m_firstConstraints; + QList m_secondConstraints; }; /*! @@ -433,20 +436,27 @@ public: QLayoutStyleInfo &styleInfo() const; - // Activation methods - bool simplifyGraph(Orientation orientation); - bool simplifyGraphIteration(Orientation orientation, bool *feasible); - void restoreSimplifiedGraph(Orientation orientation); + AnchorData *addAnchorMaybeParallel(AnchorData *newAnchor, bool *feasible); + // Activation void calculateGraphs(); void calculateGraphs(Orientation orientation); + // Simplification + bool simplifyGraph(Orientation orientation); + bool simplifyGraphIteration(Orientation orientation, bool *feasible); + + void restoreSimplifiedGraph(Orientation orientation); + void restoreSimplifiedAnchor(AnchorData *edge); + void restoreSimplifiedConstraints(ParallelAnchorData *parallel); + bool calculateTrunk(Orientation orientation, const GraphPath &trunkPath, const QList &constraints, const QList &variables); bool calculateNonTrunk(const QList &constraints, const QList &variables); + // Support functions for calculateGraph() bool refreshAllSizeHints(Orientation orientation); void findPaths(Orientation orientation); void constraintsFromPaths(Orientation orientation); -- cgit v0.12 From 5f46db57f3baed15bfca2b4725e400808eabb7e5 Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Thu, 5 Nov 2009 23:49:43 -0300 Subject: QGAL (Test): redundant anchors shouldn't harm simplification Adds a simple test to check whether redundant anchors avoid simplification to happen. In this case, the use of addCornerAnchors generate redundant anchors. Signed-off-by: Caio Marcelo de Oliveira Filho Reviewed-by: Eduardo M. Fleury --- .../tst_qgraphicsanchorlayout.cpp | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/auto/qgraphicsanchorlayout/tst_qgraphicsanchorlayout.cpp b/tests/auto/qgraphicsanchorlayout/tst_qgraphicsanchorlayout.cpp index 8b52674..c7ed309 100644 --- a/tests/auto/qgraphicsanchorlayout/tst_qgraphicsanchorlayout.cpp +++ b/tests/auto/qgraphicsanchorlayout/tst_qgraphicsanchorlayout.cpp @@ -84,6 +84,7 @@ private slots: void simplifiableUnfeasible(); void simplificationVsOrder(); void parallelSimplificationOfCenter(); + void simplificationVsRedundance(); }; class RectWidget : public QGraphicsWidget @@ -1827,5 +1828,43 @@ void tst_QGraphicsAnchorLayout::parallelSimplificationOfCenter() QCOMPARE(b->geometry(), QRectF(20, 0, 20, 10)); } +/* + Test whether redundance of anchors (in this case by using addCornerAnchors), will + prevent simplification to take place when it should. +*/ +void tst_QGraphicsAnchorLayout::simplificationVsRedundance() +{ + QSizeF min(10, 10); + QSizeF pref(20, 10); + QSizeF max(50, 30); + + QGraphicsWidget *a = createItem(min, pref, max, "A"); + QGraphicsWidget *b = createItem(min, pref, max, "B"); + QGraphicsWidget *c = createItem(min, pref, max, "C"); + + QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout; + + l->addCornerAnchors(a, Qt::TopLeftCorner, l, Qt::TopLeftCorner); + l->addCornerAnchors(a, Qt::BottomLeftCorner, l, Qt::BottomLeftCorner); + + l->addCornerAnchors(b, Qt::TopLeftCorner, a, Qt::TopRightCorner); + l->addCornerAnchors(b, Qt::TopRightCorner, l, Qt::TopRightCorner); + + l->addCornerAnchors(c, Qt::TopLeftCorner, b, Qt::BottomLeftCorner); + l->addCornerAnchors(c, Qt::BottomLeftCorner, a, Qt::BottomRightCorner); + l->addCornerAnchors(c, Qt::TopRightCorner, b, Qt::BottomRightCorner); + l->addCornerAnchors(c, Qt::BottomRightCorner, l, Qt::BottomRightCorner); + + l->effectiveSizeHint(Qt::MinimumSize); + + QCOMPARE(layoutHasConflict(l), false); + + if (!hasSimplification) + QEXPECT_FAIL("", "Test depends on simplification.", Abort); + + QCOMPARE(usedSimplex(l, Qt::Horizontal), false); + QCOMPARE(usedSimplex(l, Qt::Vertical), false); +} + QTEST_MAIN(tst_QGraphicsAnchorLayout) #include "tst_qgraphicsanchorlayout.moc" -- cgit v0.12 From 81955cdea96a7d95eec8df32923273be4df573b2 Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Thu, 5 Nov 2009 15:04:45 -0300 Subject: QGAL: vertex simplification Some vertices are connected by anchors with size 0, which means that in practice, they represent the same point when positioning items, i.e. their distance value is the same. In those cases, we can merge the two anchors in one, that's what vertex simplification do. The algorithm is walking in the graph, merging vertices in pairs (this could be enhanced to allow groups with arbitrary number of children vertices) The vertex simplification stage happens before the anchor simplification, so it'll make less passes. Also, in some situations of redudant anchors it will allow more anchors to be simplified. This commit creates a new type of AnchorVertex, add the algorithm for vertex simplification and restoration, make sure that distribution also works with simplified vertices. Consequences: - the assert after creating a sequence might not be true anymore, it was a structural assumption that vertex simplification may destroy; - we assumed that a center anchor could appear only in the beginning or end of the candidates, because the "center vertex" always would have 3 adjacents. A mix of parallel+center and vertex simplification break that assumption. The commit deal with those consequences. We still have one limitation: vertex simplification needs to restore the graph in every invalidation (which might be caused by some child's updateGeometyry()), since a change in the sizeHint might cause a new anchor to have size 0 or a 0-sized anchor to have a different size. In the future we can track the 0 sized anchors and only restore/re-simplify when the set changes. Signed-off-by: Caio Marcelo de Oliveira Filho Reviewed-by: Eduardo M. Fleury --- src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 315 +++++++++++++++++++++-- src/gui/graphicsview/qgraphicsanchorlayout_p.h | 137 ++++++---- 2 files changed, 379 insertions(+), 73 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index ef89612..395c035 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -61,6 +61,8 @@ QGraphicsAnchorPrivate::QGraphicsAnchorPrivate(int version) QGraphicsAnchorPrivate::~QGraphicsAnchorPrivate() { + // ### + layoutPrivate->restoreSimplifiedGraph(QGraphicsAnchorLayoutPrivate::Orientation(data->orientation)); layoutPrivate->removeAnchor(data->from, data->to); } @@ -700,30 +702,185 @@ bool QGraphicsAnchorLayoutPrivate::simplifyGraph(Orientation orientation) if (graphSimplified[orientation]) return true; - graphSimplified[orientation] = true; #if 0 qDebug("Simplifying Graph for %s", orientation == Horizontal ? "Horizontal" : "Vertical"); #endif - if (!layoutFirstVertex[orientation]) - return true; + // Vertex simplification + if (!simplifyVertices(orientation)) { + restoreVertices(orientation); + return false; + } + // Anchor simplification bool dirty; bool feasible = true; do { dirty = simplifyGraphIteration(orientation, &feasible); } while (dirty && feasible); - if (!feasible) - graphSimplified[orientation] = false; + // Note that if we are not feasible, we fallback and make sure that the graph is fully restored + if (!feasible) { + graphSimplified[orientation] = true; + restoreSimplifiedGraph(orientation); + restoreVertices(orientation); + return false; + } + + graphSimplified[orientation] = true; + return true; +} + +static AnchorVertex *replaceVertex_helper(AnchorData *data, AnchorVertex *oldV, AnchorVertex *newV) +{ + AnchorVertex *other; + if (data->from == oldV) { + data->from = newV; + other = data->to; + } else { + data->to = newV; + other = data->from; + } + return other; +} + +bool QGraphicsAnchorLayoutPrivate::replaceVertex(Orientation orientation, AnchorVertex *oldV, + AnchorVertex *newV, const QList &edges) +{ + Graph &g = graph[orientation]; + bool feasible = true; + + for (int i = 0; i < edges.count(); ++i) { + AnchorData *ad = edges[i]; + AnchorVertex *otherV = replaceVertex_helper(ad, oldV, newV); + +#if defined(QT_DEBUG) + ad->name = QString::fromAscii("%1 --to--> %2").arg(ad->from->toString()).arg(ad->to->toString()); +#endif + + bool newFeasible; + AnchorData *newAnchor = addAnchorMaybeParallel(ad, &newFeasible); + feasible &= newFeasible; + + if (newAnchor != ad) { + // A parallel was created, we mark that in the list of anchors created by vertex + // simplification. This is needed because we want to restore them in a separate step + // from the restoration of anchor simplification. + anchorsFromSimplifiedVertices[orientation].append(newAnchor); + } + + g.takeEdge(oldV, otherV); + } return feasible; } /*! \internal +*/ +bool QGraphicsAnchorLayoutPrivate::simplifyVertices(Orientation orientation) +{ + Q_Q(QGraphicsAnchorLayout); + Graph &g = graph[orientation]; + + // We'll walk through vertices + QStack stack; + stack.push(layoutFirstVertex[orientation]); + QSet visited; + + while (!stack.isEmpty()) { + AnchorVertex *v = stack.pop(); + visited.insert(v); + + // Each adjacent of 'v' is a possible vertex to be merged. So we traverse all of + // them. Since once a merge is made, we might add new adjacents, and we don't want to + // pass two times through one adjacent. The 'index' is used to track our position. + QList adjacents = g.adjacentVertices(v); + int index = 0; + + while (index < adjacents.count()) { + AnchorVertex *next = adjacents[index]; + index++; + + AnchorData *data = g.edgeData(v, next); + const bool bothLayoutVertices = v->m_item == q && next->m_item == q; + const bool zeroSized = !data->minSize && !data->maxSize; + + if (!bothLayoutVertices && zeroSized) { + + // Create a new vertex pair, note that we keep a list of those vertices so we can + // easily process them when restoring the graph. + AnchorVertexPair *newV = new AnchorVertexPair(v, next, data); + simplifiedVertices[orientation].append(newV); + + // Collect the anchors of both vertices, the new vertex pair will take their place + // in those anchors + const QList &vAdjacents = g.adjacentVertices(v); + const QList &nextAdjacents = g.adjacentVertices(next); + + for (int i = 0; i < vAdjacents.count(); ++i) { + AnchorVertex *adjacent = vAdjacents[i]; + if (adjacent != next) { + AnchorData *ad = g.edgeData(v, adjacent); + newV->m_firstAnchors.append(ad); + } + } + + for (int i = 0; i < nextAdjacents.count(); ++i) { + AnchorVertex *adjacent = nextAdjacents[i]; + if (adjacent != v) { + AnchorData *ad = g.edgeData(next, adjacent); + newV->m_secondAnchors.append(ad); + + // We'll also add new vertices to the adjacent list of the new 'v', to be + // created as a vertex pair and replace the current one. + if (!adjacents.contains(adjacent)) + adjacents.append(adjacent); + } + } + + // ### merge this loop into the ones that calculated m_firstAnchors/m_secondAnchors? + // Make newV take the place of v and next + bool feasible = replaceVertex(orientation, v, newV, newV->m_firstAnchors); + feasible &= replaceVertex(orientation, next, newV, newV->m_secondAnchors); + + // Update the layout vertex information if one of the vertices is a layout vertex. + AnchorVertex *layoutVertex = 0; + if (v->m_item == q) + layoutVertex = v; + else if (next->m_item == q) + layoutVertex = next; + + if (layoutVertex) { + // Layout vertices always have m_item == q... + newV->m_item = q; + changeLayoutVertex(orientation, layoutVertex, newV); + } + + g.takeEdge(v, next); + + // If a non-feasibility is found, we leave early and cancel the simplification + if (!feasible) + return false; + + v = newV; + visited.insert(newV); + + } else if (!visited.contains(next) && !stack.contains(next)) { + // If the adjacent is not fit for merge and it wasn't visited by the outermost + // loop, we add it to the stack. + stack.push(next); + } + } + } + + return true; +} + +/*! + \internal One iteration of the simplification algorithm. Returns true if another iteration is needed. @@ -763,7 +920,8 @@ bool QGraphicsAnchorLayoutPrivate::simplifyGraphIteration(QGraphicsAnchorLayoutP // (a) it is a layout vertex, we don't simplify away the layout vertices; // (b) it does not have exactly 2 adjacents; // (c) it will change the direction of the sequence; - // (d) its next adjacent is already visited (a cycle in the graph). + // (d) its next adjacent is already visited (a cycle in the graph); + // (e) the next anchor is a center anchor. const QList &adjacents = g.adjacentVertices(v); const bool isLayoutVertex = v->m_item == q; @@ -786,13 +944,14 @@ bool QGraphicsAnchorLayoutPrivate::simplifyGraphIteration(QGraphicsAnchorLayoutP candidatesForward = (beforeSequence == data->from); } - // This is a tricky part. We peek at the next vertex to find out + // This is a tricky part. We peek at the next vertex to find out whether // - // - whether the edge from this vertex to the next vertex has the same direction; - // - whether we already visited the next vertex. + // - the edge from this vertex to the next vertex has the same direction; + // - we already visited the next vertex; + // - the next anchor is a center. // - // Those are needed to identify (c) and (d). Note that unlike (a) and (b), we preempt - // the end of sequence by looking into the next vertex. + // Those are needed to identify the remaining end of sequence cases. Note that unlike + // (a) and (b), we preempt the end of sequence by looking into the next vertex. // Peek at the next vertex AnchorVertex *after; @@ -810,8 +969,8 @@ bool QGraphicsAnchorLayoutPrivate::simplifyGraphIteration(QGraphicsAnchorLayoutP const bool willChangeDirection = (candidatesForward != (v == data->from)); const bool cycleFound = visited.contains(after); - // Now cases (c) and (d)... - endOfSequence = willChangeDirection || cycleFound; + // Now cases (c), (d) and (e)... + endOfSequence = willChangeDirection || cycleFound || data->isCenterAnchor; if (endOfSequence) { if (!willChangeDirection) { @@ -879,13 +1038,6 @@ bool QGraphicsAnchorLayoutPrivate::simplifyGraphIteration(QGraphicsAnchorLayoutP // Add the sequence to the graph. // - // ### At this point we assume that if some parallel anchor will be created because - // of the new sequence, the other anchor will not be a center anchor (since we - // not deal with that case yet). This assumption will break once we start simplifying - // vertices. - AnchorData *possibleParallel = g.edgeData(beforeSequence, afterSequence); - Q_ASSERT(!possibleParallel || !possibleParallel->isCenterAnchor); - AnchorData *sequence = createSequence(&g, beforeSequence, candidates, afterSequence); // If 'beforeSequence' and 'afterSequence' already had an anchor between them, we'll @@ -938,6 +1090,13 @@ void QGraphicsAnchorLayoutPrivate::restoreSimplifiedAnchor(AnchorData *edge) delete sequence; } else if (edge->type == AnchorData::Parallel) { + + // Skip parallel anchors that were created by vertex simplification, they will be processed + // later, when restoring vertex simplification. + // ### we could improve this check bit having a bit inside 'edge' + if (anchorsFromSimplifiedVertices[edge->orientation].contains(edge)) + return; + ParallelAnchorData* parallel = static_cast(edge); restoreSimplifiedConstraints(parallel); @@ -984,18 +1143,93 @@ void QGraphicsAnchorLayoutPrivate::restoreSimplifiedGraph(Orientation orientatio orientation == Horizontal ? "Horizontal" : "Vertical"); #endif + // Restore anchor simplification Graph &g = graph[orientation]; - QList > connections = g.connections(); for (int i = 0; i < connections.count(); ++i) { AnchorVertex *v1 = connections.at(i).first; AnchorVertex *v2 = connections.at(i).second; AnchorData *edge = g.edgeData(v1, v2); - if (edge->type != AnchorData::Normal) { + + // We restore only sequential anchors and parallels that were not created by + // vertex simplification. + if (edge->type == AnchorData::Sequential + || (edge->type == AnchorData::Parallel && + !anchorsFromSimplifiedVertices[orientation].contains(edge))) { + g.takeEdge(v1, v2); restoreSimplifiedAnchor(edge); } } + + restoreVertices(orientation); +} + +void QGraphicsAnchorLayoutPrivate::restoreVertices(Orientation orientation) +{ + Q_Q(QGraphicsAnchorLayout); + + Graph &g = graph[orientation]; + QList &toRestore = simplifiedVertices[orientation]; + + // We will restore the vertices in the inverse order of creation, this way we ensure that + // the vertex being restored was not wrapped by another simplification. + for (int i = toRestore.count() - 1; i >= 0; --i) { + AnchorVertexPair *pair = toRestore[i]; + QList adjacents = g.adjacentVertices(pair); + + // Restore the removed edge, this will also restore both vertices 'first' and 'second' to + // the graph structure. + AnchorVertex *first = pair->m_first; + AnchorVertex *second = pair->m_second; + g.createEdge(first, second, pair->m_removedAnchor); + + // Restore the anchors for the first child vertex + for (int j = 0; j < pair->m_firstAnchors.count(); ++j) { + AnchorData *ad = pair->m_firstAnchors[j]; + Q_ASSERT(ad->from == pair || ad->to == pair); + + replaceVertex_helper(ad, pair, first); + g.createEdge(ad->from, ad->to, ad); + } + + // Restore the anchors for the second child vertex + for (int j = 0; j < pair->m_secondAnchors.count(); ++j) { + AnchorData *ad = pair->m_secondAnchors[j]; + Q_ASSERT(ad->from == pair || ad->to == pair); + + replaceVertex_helper(ad, pair, second); + g.createEdge(ad->from, ad->to, ad); + } + + for (int j = 0; j < adjacents.count(); ++j) { + g.takeEdge(pair, adjacents[j]); + } + + // The pair simplified a layout vertex, so place back the correct vertex in the variable + // that track layout vertices + if (pair->m_item == q) { + AnchorVertex *layoutVertex = first->m_item == q ? first : second; + Q_ASSERT(layoutVertex->m_item == q); + changeLayoutVertex(orientation, pair, layoutVertex); + } + + delete pair; + } + toRestore.clear(); + + // The restoration process for vertex simplification also restored the effect of the + // parallel anchors created during vertex simplification, so we just need to restore + // the constraints in case of parallels that contain center anchors. For the same + // reason as above, order matters here. + QList ¶llelAnchors = anchorsFromSimplifiedVertices[orientation]; + + for (int i = parallelAnchors.count() - 1; i >= 0; --i) { + ParallelAnchorData *parallel = static_cast(parallelAnchors[i]); + restoreSimplifiedConstraints(parallel); + delete parallel; + } + parallelAnchors.clear(); } QGraphicsAnchorLayoutPrivate::Orientation @@ -1149,7 +1383,6 @@ void QGraphicsAnchorLayoutPrivate::createCenterAnchors( if (item == q) { layoutCentralVertex[orientation] = internalVertex(q, centerEdge); } - } void QGraphicsAnchorLayoutPrivate::removeCenterAnchors( @@ -1811,6 +2044,15 @@ void QGraphicsAnchorLayoutPrivate::calculateGraphs( lastCalculationUsedSimplex[orientation] = false; #endif + // ### This is necessary because now we do vertex simplification, we still don't know + // differentiate between invalidate()s that doesn't need resimplification and those which + // need. For example, when size hint of an item changes, this may cause an anchor to reach 0 or to + // leave 0 and get a size. In both cases we need resimplify. + // + // ### one possible solution would be tracking all the 0-sized anchors, if this set change, we need + // resimplify. + restoreSimplifiedGraph(orientation); + // Reset the nominal sizes of each anchor based on the current item sizes. This function // works with both simplified and non-simplified graphs, so it'll work when the // simplification is going to be reused. @@ -2379,6 +2621,21 @@ void QGraphicsAnchorLayoutPrivate::setItemsGeometries(const QRectF &geom) } /*! + \internal + + Fill the distance in the vertex and in the sub-vertices if its a combined vertex. +*/ +static void setVertexDistance(AnchorVertex *v, qreal distance) +{ + v->distance = distance; + if (v->m_type == AnchorVertex::Pair) { + AnchorVertexPair *pair = static_cast(v); + setVertexDistance(pair->m_first, distance); + setVertexDistance(pair->m_second, distance); + } +} + +/*! \internal Calculate the position of each vertex based on the paths to each of @@ -2393,7 +2650,7 @@ void QGraphicsAnchorLayoutPrivate::calculateVertexPositions( // Get root vertex AnchorVertex *root = layoutFirstVertex[orientation]; - root->distance = 0; + setVertexDistance(root, 0); visited.insert(root); // Add initial edges to the queue @@ -2483,10 +2740,12 @@ void QGraphicsAnchorLayoutPrivate::interpolateEdge(AnchorVertex *base, Q_ASSERT(edge->from == base || edge->to == base); - if (edge->from == base) - edge->to->distance = base->distance + edgeDistance; - else - edge->from->distance = base->distance - edgeDistance; + // Calculate the distance for the vertex opposite to the base + if (edge->from == base) { + setVertexDistance(edge->to, base->distance + edgeDistance); + } else { + setVertexDistance(edge->from, base->distance - edgeDistance); + } // Process child anchors if (edge->type == AnchorData::Sequential) diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.h b/src/gui/graphicsview/qgraphicsanchorlayout_p.h index d7b3cfd..3a22db0 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.h +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.h @@ -78,66 +78,30 @@ QT_BEGIN_NAMESPACE Represents a vertex (anchorage point) in the internal graph */ struct AnchorVertex { + enum Type { + Normal = 0, + Pair + }; + AnchorVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge) - : m_item(item), m_edge(edge) {} + : m_item(item), m_edge(edge), m_type(Normal) {} AnchorVertex() - : m_item(0), m_edge(Qt::AnchorPoint(0)) {} + : m_item(0), m_edge(Qt::AnchorPoint(0)), m_type(Normal) {} #ifdef QT_DEBUG inline QString toString() const; #endif + QGraphicsLayoutItem *m_item; Qt::AnchorPoint m_edge; + uint m_type : 1; // Current distance from this vertex to the layout edge (Left or Top) // Value is calculated from the current anchors sizes. qreal distance; }; -#ifdef QT_DEBUG -inline QString AnchorVertex::toString() const -{ - if (!this || !m_item) { - return QLatin1String("NULL"); - } - QString edge; - switch (m_edge) { - case Qt::AnchorLeft: - edge = QLatin1String("Left"); - break; - case Qt::AnchorHorizontalCenter: - edge = QLatin1String("HorizontalCenter"); - break; - case Qt::AnchorRight: - edge = QLatin1String("Right"); - break; - case Qt::AnchorTop: - edge = QLatin1String("Top"); - break; - case Qt::AnchorVerticalCenter: - edge = QLatin1String("VerticalCenter"); - break; - case Qt::AnchorBottom: - edge = QLatin1String("Bottom"); - break; - default: - edge = QLatin1String("None"); - break; - } - QString itemName; - if (m_item->isLayout()) { - itemName = QLatin1String("layout"); - } else { - if (QGraphicsItem *item = m_item->graphicsItem()) { - itemName = item->data(0).toString(); - } - } - edge.insert(0, QLatin1String("%1_")); - return edge.arg(itemName); -} -#endif - /*! \internal @@ -280,6 +244,68 @@ struct ParallelAnchorData : public AnchorData QList m_secondConstraints; }; +struct AnchorVertexPair : public AnchorVertex { + AnchorVertexPair(AnchorVertex *v1, AnchorVertex *v2, AnchorData *data) + : AnchorVertex(), m_first(v1), m_second(v2), m_removedAnchor(data) { + m_type = AnchorVertex::Pair; + } + + AnchorVertex *m_first; + AnchorVertex *m_second; + + AnchorData *m_removedAnchor; + QList m_firstAnchors; + QList m_secondAnchors; +}; + +#ifdef QT_DEBUG +inline QString AnchorVertex::toString() const +{ + if (!this) { + return QLatin1String("NULL"); + } else if (m_type == Pair) { + const AnchorVertexPair *vp = static_cast(this); + return QString::fromAscii("(%1, %2)").arg(vp->m_first->toString()).arg(vp->m_second->toString()); + } else if (!m_item) { + return QString::fromAscii("NULL_%1").arg(int(this)); + } + QString edge; + switch (m_edge) { + case Qt::AnchorLeft: + edge = QLatin1String("Left"); + break; + case Qt::AnchorHorizontalCenter: + edge = QLatin1String("HorizontalCenter"); + break; + case Qt::AnchorRight: + edge = QLatin1String("Right"); + break; + case Qt::AnchorTop: + edge = QLatin1String("Top"); + break; + case Qt::AnchorVerticalCenter: + edge = QLatin1String("VerticalCenter"); + break; + case Qt::AnchorBottom: + edge = QLatin1String("Bottom"); + break; + default: + edge = QLatin1String("None"); + break; + } + QString itemName; + if (m_item->isLayout()) { + itemName = QLatin1String("layout"); + } else { + if (QGraphicsItem *item = m_item->graphicsItem()) { + itemName = item->data(0).toString(); + } + } + edge.insert(0, QLatin1String("%1_")); + return edge.arg(itemName); +} +#endif + /*! \internal @@ -444,11 +470,17 @@ public: // Simplification bool simplifyGraph(Orientation orientation); + bool simplifyVertices(Orientation orientation); bool simplifyGraphIteration(Orientation orientation, bool *feasible); + bool replaceVertex(Orientation orientation, AnchorVertex *oldV, + AnchorVertex *newV, const QList &edges); + + void restoreSimplifiedGraph(Orientation orientation); void restoreSimplifiedAnchor(AnchorData *edge); void restoreSimplifiedConstraints(ParallelAnchorData *parallel); + void restoreVertices(Orientation orientation); bool calculateTrunk(Orientation orientation, const GraphPath &trunkPath, const QList &constraints, @@ -476,6 +508,17 @@ public: return internalVertex(qMakePair(const_cast(item), edge)); } + inline void changeLayoutVertex(Orientation orientation, AnchorVertex *oldV, AnchorVertex *newV) + { + if (layoutFirstVertex[orientation] == oldV) + layoutFirstVertex[orientation] = newV; + else if (layoutCentralVertex[orientation] == oldV) + layoutCentralVertex[orientation] = newV; + else if (layoutLastVertex[orientation] == oldV) + layoutLastVertex[orientation] = newV; + } + + AnchorVertex *addInternalVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge); void removeInternalVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge); @@ -519,6 +562,10 @@ public: AnchorVertex *layoutCentralVertex[2]; AnchorVertex *layoutLastVertex[2]; + // Combined anchors in order of creation + QList simplifiedVertices[2]; + QList anchorsFromSimplifiedVertices[2]; + // Graph paths and constraints, for both orientations QMultiHash graphPaths[2]; QList constraints[2]; -- cgit v0.12 From 20f6d4080af9945b957fe74520373c015fbdacbc Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Fri, 6 Nov 2009 15:26:36 -0300 Subject: QGAL: avoid passing Orientation for interpolation functions Not all of the interpolation functions needed the orientation information, and the one that needs can look at the orientation of the anchor in the parameter. This simplified a little bit the function calls. Signed-off-by: Caio Marcelo de Oliveira Filho --- src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 33 ++++++++++-------------- src/gui/graphicsview/qgraphicsanchorlayout_p.h | 6 ++--- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index 395c035..f850b0d 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -2674,7 +2674,7 @@ void QGraphicsAnchorLayoutPrivate::calculateVertexPositions( continue; visited.insert(pair.second); - interpolateEdge(pair.first, edge, orientation); + interpolateEdge(pair.first, edge); QList adjacents = graph[orientation].adjacentVertices(pair.second); for (int i = 0; i < adjacents.count(); ++i) { @@ -2728,10 +2728,9 @@ void QGraphicsAnchorLayoutPrivate::setupEdgesInterpolation( vertices to be initalized, so it calls specialized functions that will recurse back to interpolateEdge(). */ -void QGraphicsAnchorLayoutPrivate::interpolateEdge(AnchorVertex *base, - AnchorData *edge, - Orientation orientation) +void QGraphicsAnchorLayoutPrivate::interpolateEdge(AnchorVertex *base, AnchorData *edge) { + const Orientation orientation = Orientation(edge->orientation); const QPair factor(interpolationInterval[orientation], interpolationProgress[orientation]); @@ -2749,13 +2748,12 @@ void QGraphicsAnchorLayoutPrivate::interpolateEdge(AnchorVertex *base, // Process child anchors if (edge->type == AnchorData::Sequential) - interpolateSequentialEdges(static_cast(edge), orientation); + interpolateSequentialEdges(static_cast(edge)); else if (edge->type == AnchorData::Parallel) - interpolateParallelEdges(static_cast(edge), orientation); + interpolateParallelEdges(static_cast(edge)); } -void QGraphicsAnchorLayoutPrivate::interpolateParallelEdges( - ParallelAnchorData *data, Orientation orientation) +void QGraphicsAnchorLayoutPrivate::interpolateParallelEdges(ParallelAnchorData *data) { // In parallels the boundary vertices are already calculate, we // just need to look for sequential groups inside, because only @@ -2763,23 +2761,18 @@ void QGraphicsAnchorLayoutPrivate::interpolateParallelEdges( // First edge if (data->firstEdge->type == AnchorData::Sequential) - interpolateSequentialEdges(static_cast(data->firstEdge), - orientation); + interpolateSequentialEdges(static_cast(data->firstEdge)); else if (data->firstEdge->type == AnchorData::Parallel) - interpolateParallelEdges(static_cast(data->firstEdge), - orientation); + interpolateParallelEdges(static_cast(data->firstEdge)); // Second edge if (data->secondEdge->type == AnchorData::Sequential) - interpolateSequentialEdges(static_cast(data->secondEdge), - orientation); + interpolateSequentialEdges(static_cast(data->secondEdge)); else if (data->secondEdge->type == AnchorData::Parallel) - interpolateParallelEdges(static_cast(data->secondEdge), - orientation); + interpolateParallelEdges(static_cast(data->secondEdge)); } -void QGraphicsAnchorLayoutPrivate::interpolateSequentialEdges( - SequentialAnchorData *data, Orientation orientation) +void QGraphicsAnchorLayoutPrivate::interpolateSequentialEdges(SequentialAnchorData *data) { // This method is supposed to handle any sequential anchor, even out-of-order // ones. However, in the current QGAL implementation we should get only the @@ -2795,7 +2788,7 @@ void QGraphicsAnchorLayoutPrivate::interpolateSequentialEdges( for (int i = 0; i < data->m_edges.count() - 1; ++i) { AnchorData *edge = data->m_edges.at(i); - interpolateEdge(prev, edge, orientation); + interpolateEdge(prev, edge); // Use the recently calculated vertex as the base for the next one const bool edgeIsForward = (edge->from == prev); @@ -2805,7 +2798,7 @@ void QGraphicsAnchorLayoutPrivate::interpolateSequentialEdges( // Treat the last specially, since we already calculated it's end // vertex, so it's only interesting if it's a complex one if (data->m_edges.last()->type != AnchorData::Normal) - interpolateEdge(prev, data->m_edges.last(), orientation); + interpolateEdge(prev, data->m_edges.last()); } bool QGraphicsAnchorLayoutPrivate::solveMinMax(const QList &constraints, diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.h b/src/gui/graphicsview/qgraphicsanchorlayout_p.h index 3a22db0..3ef37f9 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.h +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.h @@ -527,9 +527,9 @@ public: void calculateVertexPositions(Orientation orientation); void setupEdgesInterpolation(Orientation orientation); - void interpolateEdge(AnchorVertex *base, AnchorData *edge, Orientation orientation); - void interpolateSequentialEdges(SequentialAnchorData *edge, Orientation orientation); - void interpolateParallelEdges(ParallelAnchorData *edge, Orientation orientation); + void interpolateEdge(AnchorVertex *base, AnchorData *edge); + void interpolateSequentialEdges(SequentialAnchorData *edge); + void interpolateParallelEdges(ParallelAnchorData *edge); // Linear Programming solver methods bool solveMinMax(const QList &constraints, -- cgit v0.12