diff options
Diffstat (limited to 'src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp')
-rw-r--r-- | src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp | 130 |
1 files changed, 60 insertions, 70 deletions
diff --git a/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp b/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp index dac8e61645..95cdaaddc1 100644 --- a/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp @@ -1,36 +1,44 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWidgets module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include "qgraphicsanchorlayout_p.h" + #include <QtWidgets/qwidget.h> #include <QtWidgets/qapplication.h> #include <QtCore/qlinkedlist.h> @@ -40,7 +48,7 @@ #include <QtCore/qfile.h> #endif -#include "qgraphicsanchorlayout_p.h" +#include <numeric> #ifndef QT_NO_GRAPHICSVIEW QT_BEGIN_NAMESPACE @@ -602,10 +610,10 @@ QSimplexConstraint *GraphPath::constraint(const GraphPath &path) const QString GraphPath::toString() const { QString string(QLatin1String("Path: ")); - foreach(AnchorData *edge, positives) + for (AnchorData *edge : positives) string += QString::fromLatin1(" (+++) %1").arg(edge->toString()); - foreach(AnchorData *edge, negatives) + for (AnchorData *edge : negatives) string += QString::fromLatin1(" (---) %1").arg(edge->toString()); return string; @@ -1258,7 +1266,7 @@ void QGraphicsAnchorLayoutPrivate::restoreSimplifiedGraph(Orientation orientatio // Restore anchor simplification Graph<AnchorVertex, AnchorData> &g = graph[orientation]; - QList<QPair<AnchorVertex*, AnchorVertex*> > connections = g.connections(); + QVector<QPair<AnchorVertex*, AnchorVertex*> > connections = g.connections(); for (int i = 0; i < connections.count(); ++i) { AnchorVertex *v1 = connections.at(i).first; AnchorVertex *v2 = connections.at(i).second; @@ -1932,8 +1940,7 @@ void QGraphicsAnchorLayoutPrivate::removeVertex(QGraphicsLayoutItem *item, Qt::A if (AnchorVertex *v = internalVertex(item, edge)) { Graph<AnchorVertex, AnchorData> &g = graph[edgeOrientation(edge)]; const QList<AnchorVertex *> allVertices = graph[edgeOrientation(edge)].adjacentVertices(v); - AnchorVertex *v2; - foreach (v2, allVertices) { + for (auto *v2 : allVertices) { g.removeEdge(v, v2); removeInternalVertex(item, edge); removeInternalVertex(v2->m_item, v2->m_edge); @@ -2057,9 +2064,8 @@ QList<AnchorData *> getVariables(const QList<QSimplexConstraint *> &constraints) QSet<AnchorData *> variableSet; for (int i = 0; i < constraints.count(); ++i) { const QSimplexConstraint *c = constraints.at(i); - foreach (QSimplexVariable *var, c->variables.keys()) { - variableSet += static_cast<AnchorData *>(var); - } + for (auto it = c->variables.cbegin(), end = c->variables.cend(); it != end; ++it) + variableSet.insert(static_cast<AnchorData *>(it.key())); } return variableSet.toList(); } @@ -2180,10 +2186,7 @@ static void shiftConstraints(const QList<QSimplexConstraint *> &constraints, qre { for (int i = 0; i < constraints.count(); ++i) { QSimplexConstraint *c = constraints.at(i); - qreal multiplier = 0; - foreach (qreal v, c->variables) { - multiplier += v; - } + const qreal multiplier = std::accumulate(c->variables.cbegin(), c->variables.cend(), qreal(0)); c->constant += multiplier * amount; } } @@ -2223,12 +2226,10 @@ bool QGraphicsAnchorLayoutPrivate::calculateTrunk(Orientation orientation, const // Calculate and set the preferred size for the layout, // from the edge sizes that were calculated above. qreal pref(0.0); - foreach (const AnchorData *ad, path.positives) { + for (const AnchorData *ad : path.positives) pref += ad->sizeAtPreferred; - } - foreach (const AnchorData *ad, path.negatives) { + for (const AnchorData *ad : path.negatives) pref -= ad->sizeAtPreferred; - } sizeHints[orientation][Qt::MinimumSize] = min; sizeHints[orientation][Qt::PreferredSize] = pref; @@ -2294,7 +2295,7 @@ bool QGraphicsAnchorLayoutPrivate::calculateNonTrunk(const QList<QSimplexConstra void QGraphicsAnchorLayoutPrivate::refreshAllSizeHints(Orientation orientation) { Graph<AnchorVertex, AnchorData> &g = graph[orientation]; - QList<QPair<AnchorVertex *, AnchorVertex *> > vertices = g.connections(); + QVector<QPair<AnchorVertex *, AnchorVertex *> > vertices = g.connections(); QLayoutStyleInfo styleInf = styleInfo(); for (int i = 0; i < vertices.count(); ++i) { @@ -2323,9 +2324,9 @@ void QGraphicsAnchorLayoutPrivate::findPaths(Orientation orientation) graphPaths[orientation].insert(root, GraphPath()); - foreach (AnchorVertex *v, graph[orientation].adjacentVertices(root)) { + const auto adjacentVertices = graph[orientation].adjacentVertices(root); + for (AnchorVertex *v : adjacentVertices) queue.enqueue(qMakePair(root, v)); - } while(!queue.isEmpty()) { QPair<AnchorVertex *, AnchorVertex *> pair = queue.dequeue(); @@ -2344,10 +2345,9 @@ void QGraphicsAnchorLayoutPrivate::findPaths(Orientation orientation) graphPaths[orientation].insert(pair.second, current); - foreach (AnchorVertex *v, - graph[orientation].adjacentVertices(pair.second)) { + const auto adjacentVertices = graph[orientation].adjacentVertices(pair.second); + for (AnchorVertex *v : adjacentVertices) queue.enqueue(qMakePair(pair.second, v)); - } } // We will walk through every reachable items (non-float) store them in a temporary set. @@ -2368,8 +2368,8 @@ void QGraphicsAnchorLayoutPrivate::findPaths(Orientation orientation) */ void QGraphicsAnchorLayoutPrivate::constraintsFromPaths(Orientation orientation) { - foreach (AnchorVertex *vertex, graphPaths[orientation].uniqueKeys()) - { + const auto vertices = graphPaths[orientation].uniqueKeys(); + for (AnchorVertex *vertex : vertices) { int valueCount = graphPaths[orientation].count(vertex); if (valueCount == 1) continue; @@ -2388,7 +2388,7 @@ void QGraphicsAnchorLayoutPrivate::constraintsFromPaths(Orientation orientation) void QGraphicsAnchorLayoutPrivate::updateAnchorSizes(Orientation orientation) { Graph<AnchorVertex, AnchorData> &g = graph[orientation]; - const QList<QPair<AnchorVertex *, AnchorVertex *> > &vertices = g.connections(); + const QVector<QPair<AnchorVertex *, AnchorVertex *> > &vertices = g.connections(); for (int i = 0; i < vertices.count(); ++i) { AnchorData *ad = g.edgeData(vertices.at(i).first, vertices.at(i).second); @@ -2513,13 +2513,7 @@ QGraphicsAnchorLayoutPrivate::getGraphParts(Orientation orientation) edgeL1 = graph[orientation].edgeData(layoutFirstVertex[orientation], layoutLastVertex[orientation]); } - QLinkedList<QSimplexConstraint *> remainingConstraints; - for (int i = 0; i < constraints[orientation].count(); ++i) { - remainingConstraints += constraints[orientation].at(i); - } - for (int i = 0; i < itemCenterConstraints[orientation].count(); ++i) { - remainingConstraints += itemCenterConstraints[orientation].at(i); - } + QList<QSimplexConstraint *> remainingConstraints = constraints[orientation] + itemCenterConstraints[orientation]; QList<QSimplexConstraint *> trunkConstraints; QSet<QSimplexVariable *> trunkVariables; @@ -2529,12 +2523,11 @@ QGraphicsAnchorLayoutPrivate::getGraphParts(Orientation orientation) trunkVariables += edgeL2; bool dirty; + auto end = remainingConstraints.end(); do { dirty = false; - QLinkedList<QSimplexConstraint *>::iterator it = remainingConstraints.begin(); - while (it != remainingConstraints.end()) { - QSimplexConstraint *c = *it; + auto isMatch = [&trunkConstraints, &trunkVariables](QSimplexConstraint *c) -> bool { bool match = false; // Check if this constraint have some overlap with current @@ -2550,9 +2543,9 @@ QGraphicsAnchorLayoutPrivate::getGraphParts(Orientation orientation) // remaining constraints. if (match) { trunkConstraints += c; - trunkVariables += QSet<QSimplexVariable *>::fromList(c->variables.keys()); - it = remainingConstraints.erase(it); - dirty = true; + for (auto jt = c->variables.cbegin(), end = c->variables.cend(); jt != end; ++jt) + trunkVariables.insert(jt.key()); + return true; } else { // Note that we don't erase the constraint if it's not // a match, since in a next iteration of a do-while we @@ -2562,24 +2555,21 @@ QGraphicsAnchorLayoutPrivate::getGraphParts(Orientation orientation) // remainingConstraints[1] and it shares with // remainingConstraints[0], we need a second iteration // of the do-while loop to match both. - ++it; + return false; } - } + }; + const auto newEnd = std::remove_if(remainingConstraints.begin(), end, isMatch); + dirty = newEnd != end; + end = newEnd; } while (dirty); + remainingConstraints.erase(end, remainingConstraints.end()); + QList< QList<QSimplexConstraint *> > result; result += trunkConstraints; - if (!remainingConstraints.isEmpty()) { - QList<QSimplexConstraint *> nonTrunkConstraints; - nonTrunkConstraints.reserve(remainingConstraints.size()); - QLinkedList<QSimplexConstraint *>::iterator it = remainingConstraints.begin(); - while (it != remainingConstraints.end()) { - nonTrunkConstraints += *it; - ++it; - } - result += nonTrunkConstraints; - } + if (!remainingConstraints.isEmpty()) + result += remainingConstraints; // non-trunk constraints return result; } @@ -2593,7 +2583,7 @@ void QGraphicsAnchorLayoutPrivate::identifyFloatItems(const QSet<AnchorData *> & { QSet<QGraphicsLayoutItem *> nonFloating; - foreach (const AnchorData *ad, visited) + for (const AnchorData *ad : visited) identifyNonFloatItems_helper(ad, &nonFloating); QSet<QGraphicsLayoutItem *> allItems; @@ -2707,9 +2697,9 @@ void QGraphicsAnchorLayoutPrivate::calculateVertexPositions( visited.insert(root); // Add initial edges to the queue - foreach (AnchorVertex *v, graph[orientation].adjacentVertices(root)) { + const auto adjacentVertices = graph[orientation].adjacentVertices(root); + for (AnchorVertex *v : adjacentVertices) queue.enqueue(qMakePair(root, v)); - } // Do initial calculation required by "interpolateEdge()" setupEdgesInterpolation(orientation); @@ -2796,7 +2786,7 @@ void QGraphicsAnchorLayoutPrivate::interpolateEdge(AnchorVertex *base, AnchorDat } bool QGraphicsAnchorLayoutPrivate::solveMinMax(const QList<QSimplexConstraint *> &constraints, - GraphPath path, qreal *min, qreal *max) + const GraphPath &path, qreal *min, qreal *max) { QSimplex simplex; bool feasible = simplex.setConstraints(constraints); @@ -2983,7 +2973,7 @@ void QGraphicsAnchorLayoutPrivate::dumpGraph(const QString &name) { QFile file(QString::fromLatin1("anchorlayout.%1.dot").arg(name)); if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) - qWarning("Could not write to %s", file.fileName().toLocal8Bit().constData()); + qWarning("Could not write to %ls", qUtf16Printable(file.fileName())); QString str = QString::fromLatin1("digraph anchorlayout {\nnode [shape=\"rect\"]\n%1}"); QString dotContents = graph[0].serializeToDot(); |