From 14601e46729084f7f822bc7ef402553fba512452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Arve=20S=C3=A6ther?= Date: Wed, 12 Aug 2009 10:42:27 +0200 Subject: Fix implementation of setAnchorSizeHintsFromItems to be simplification ready. --- src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 113 +++++++++++++---------- src/gui/graphicsview/qgraphicsanchorlayout_p.h | 5 + 2 files changed, 70 insertions(+), 48 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index 6b7a12c..337f887 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -55,6 +55,15 @@ void ParallelAnchorData::updateChildrenSizes() secondEdge->updateChildrenSizes(); } +void ParallelAnchorData::refreshSizeHints() +{ + firstEdge->refreshSizeHints(); + secondEdge->refreshSizeHints(); + minSize = qMax(firstEdge->minSize, secondEdge->minSize); + prefSize = qMin(firstEdge->prefSize, secondEdge->prefSize); + maxSize = qMin(firstEdge->maxSize, secondEdge->maxSize); +} + void SequentialAnchorData::updateChildrenSizes() { qreal minFactor = sizeAtMinimum / minSize; @@ -69,6 +78,20 @@ void SequentialAnchorData::updateChildrenSizes() } } +void SequentialAnchorData::refreshSizeHints() +{ + minSize = 0; + prefSize = 0; + maxSize = 0; + for (int i = 0; i < m_edges.count(); ++i) { + AnchorData *edge = m_edges.at(i); + edge->refreshSizeHints(); + minSize += edge->minSize; + prefSize += edge->prefSize; + maxSize += edge->maxSize; + } +} + QSimplexConstraint *GraphPath::constraint(const GraphPath &path) const { // Calculate @@ -1057,7 +1080,7 @@ void QGraphicsAnchorLayoutPrivate::calculateGraphs( setAnchorSizeHintsFromItems(orientation); // Reset the nominal sizes of each anchor based on the current item sizes - setAnchorSizeHintsFromDefaults(orientation); + //setAnchorSizeHintsFromDefaults(orientation); // Simplify the graph // ### Ideally we would like to do that if, and only if, anchors had @@ -1276,6 +1299,11 @@ void QGraphicsAnchorLayoutPrivate::setAnchorSizeHintsFromDefaults(Orientation or */ void QGraphicsAnchorLayoutPrivate::setAnchorSizeHintsFromItems(Orientation orientation) { + Q_Q(QGraphicsAnchorLayout); + Graph &g = graph[orientation]; + QList > conns = g.connections(); + QGraphicsAnchorLayout::Edge centerEdge = pickEdge(QGraphicsAnchorLayout::HCenter, orientation); + QPair beginningKey; QPair centerKey; QPair endKey; @@ -1290,34 +1318,33 @@ void QGraphicsAnchorLayoutPrivate::setAnchorSizeHintsFromItems(Orientation orien endKey.second = QGraphicsAnchorLayout::Bottom; } - foreach (QGraphicsLayoutItem *item, items) { - AnchorVertex *beginning, *center, *end; - qreal min, pref, max; - - beginningKey.first = item; - centerKey.first = item; - endKey.first = item; + for (int i = 0; i < conns.count(); ++i) { + AnchorVertex *from = conns.at(i).first; + AnchorVertex *to = conns.at(i).second; - beginning = internalVertex(beginningKey); - center = internalVertex(centerKey); - end = internalVertex(endKey); + QGraphicsLayoutItem *item = from->m_item; + qreal min, pref, max; - if (orientation == Horizontal) { - min = item->minimumWidth(); - pref = item->preferredWidth(); - max = item->maximumWidth(); - } else { - min = item->minimumHeight(); - pref = item->preferredHeight(); - max = item->maximumHeight(); - } + AnchorData *data = g.edgeData(from, to); + Q_ASSERT(data); + // Internal item anchor + if (item != q && item == to->m_item) { + // internal item anchor: get size from sizeHint + if (orientation == Horizontal) { + min = item->minimumWidth(); + pref = item->preferredWidth(); + max = item->maximumWidth(); + } else { + min = item->minimumHeight(); + pref = item->preferredHeight(); + max = item->maximumHeight(); + } - // To support items that are represented by a single anchor as well as - // those that have been divided into two halfs, we must do this check. - AnchorData *data; - if (center == 0) { - data = graph[orientation].edgeData(beginning, end); - Q_ASSERT(data); + if (from->m_edge == centerEdge || to->m_edge == centerEdge) { + min /= 2; + pref /= 2; + max /= 2; + } // Set the anchor nominal sizes to those of the corresponding item data->minSize = min; data->prefSize = pref; @@ -1333,29 +1360,19 @@ void QGraphicsAnchorLayoutPrivate::setAnchorSizeHintsFromItems(Orientation orien data->sizeAtMinimum = pref; data->sizeAtPreferred = pref; data->sizeAtMaximum = pref; + } else if (data->type != AnchorData::Normal) { + data->refreshSizeHints(); } else { - min = min / 2; - pref = pref / 2; - max = max / 2; - - // Same as above, for each half - data = graph[orientation].edgeData(beginning, center); - Q_ASSERT(data); - data->minSize = min; - data->prefSize = pref; - data->maxSize = max; - data->sizeAtMinimum = pref; - data->sizeAtPreferred = pref; - data->sizeAtMaximum = pref; - - data = graph[orientation].edgeData(center, end); - Q_ASSERT(data); - data->minSize = min; - data->prefSize = pref; - data->maxSize = max; - data->sizeAtMinimum = pref; - data->sizeAtPreferred = pref; - data->sizeAtMaximum = pref; + // anchors between items, their sizes may depend on the style. + if (!data->hasSize) { + qreal s = effectiveSpacing(orientation); + data->minSize = s; + data->prefSize = s; + data->maxSize = s; + data->sizeAtMinimum = s; + data->sizeAtPreferred = s; + data->sizeAtMaximum = s; + } } } } diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.h b/src/gui/graphicsview/qgraphicsanchorlayout_p.h index 131d7c3..b637f08 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.h +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.h @@ -147,6 +147,8 @@ struct AnchorData : public QSimplexVariable { virtual void updateChildrenSizes() { }; + virtual void refreshSizeHints() { }; + inline QString toString() const; QString name; @@ -195,6 +197,7 @@ struct SequentialAnchorData : public AnchorData } virtual void updateChildrenSizes(); + virtual void refreshSizeHints(); void setVertices(const QVector &vertices) { @@ -218,6 +221,8 @@ struct ParallelAnchorData : public AnchorData } virtual void updateChildrenSizes(); + virtual void refreshSizeHints(); + AnchorData* firstEdge; AnchorData* secondEdge; -- cgit v0.12