From 2f8cfc2c608159bfbe6e085c37bd76bbaa5bd607 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 5 May 2020 15:28:46 +0200 Subject: QGraphicsAnchorLayout: port to QHVContainer [1/4]: local QHVContainer This part of the patch changes the definitons of the member variables from 'C arrays of extent 2' to QHVContainer and fixes the code where ints were used to index into the array. To not drown in renames, keep the locally-defined enum 'Orientation', and create a local version of QHVContainer whose index operator is overloaded for both Qt::Orientation and the local 'Orientation'. Follow-up patches will remove these, then, completely. After this patch, NOrientations is no longer used, and consequently removed. Change-Id: I2a241520fce4beeb87fc0e26cd6ab18f324a956a Reviewed-by: Giuseppe D'Angelo --- src/widgets/graphicsview/qgraphicsanchorlayout.cpp | 10 ++-- .../graphicsview/qgraphicsanchorlayout_p.cpp | 25 +++------- src/widgets/graphicsview/qgraphicsanchorlayout_p.h | 53 +++++++++++++++------- 3 files changed, 47 insertions(+), 41 deletions(-) (limited to 'src/widgets/graphicsview') diff --git a/src/widgets/graphicsview/qgraphicsanchorlayout.cpp b/src/widgets/graphicsview/qgraphicsanchorlayout.cpp index 4f1855a606..b7c83e32a0 100644 --- a/src/widgets/graphicsview/qgraphicsanchorlayout.cpp +++ b/src/widgets/graphicsview/qgraphicsanchorlayout.cpp @@ -238,8 +238,8 @@ QGraphicsAnchorLayout::~QGraphicsAnchorLayout() d->removeCenterConstraints(this, QGraphicsAnchorLayoutPrivate::Vertical); d->deleteLayoutEdges(); - Q_ASSERT(d->itemCenterConstraints[0].isEmpty()); - Q_ASSERT(d->itemCenterConstraints[1].isEmpty()); + Q_ASSERT(d->itemCenterConstraints[Qt::Horizontal].isEmpty()); + Q_ASSERT(d->itemCenterConstraints[Qt::Vertical].isEmpty()); Q_ASSERT(d->items.isEmpty()); Q_ASSERT(d->m_vertexList.isEmpty()); } @@ -372,7 +372,7 @@ void QGraphicsAnchorLayout::setHorizontalSpacing(qreal spacing) { Q_D(QGraphicsAnchorLayout); - d->spacings[0] = spacing; + d->spacings[Qt::Horizontal] = spacing; invalidate(); } @@ -385,7 +385,7 @@ void QGraphicsAnchorLayout::setVerticalSpacing(qreal spacing) { Q_D(QGraphicsAnchorLayout); - d->spacings[1] = spacing; + d->spacings[Qt::Vertical] = spacing; invalidate(); } @@ -404,7 +404,7 @@ void QGraphicsAnchorLayout::setSpacing(qreal spacing) { Q_D(QGraphicsAnchorLayout); - d->spacings[0] = d->spacings[1] = spacing; + d->spacings = {spacing, spacing}; invalidate(); } diff --git a/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp b/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp index 78549e4c24..9934255d25 100644 --- a/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp @@ -621,19 +621,6 @@ QString GraphPath::toString() const QGraphicsAnchorLayoutPrivate::QGraphicsAnchorLayoutPrivate() : calculateGraphCacheDirty(true), styleInfoDirty(true) { - for (int i = 0; i < NOrientations; ++i) { - for (int j = 0; j < 3; ++j) { - sizeHints[i][j] = -1; - } - interpolationProgress[i] = -1; - - spacings[i] = -1; - graphHasConflicts[i] = false; - - layoutFirstVertex[i] = nullptr; - layoutCentralVertex[i] = nullptr; - layoutLastVertex[i] = nullptr; - } } Qt::AnchorPoint QGraphicsAnchorLayoutPrivate::oppositeEdge(Qt::AnchorPoint edge) @@ -2032,8 +2019,8 @@ QLayoutStyleInfo &QGraphicsAnchorLayoutPrivate::styleInfo() const QStyle *style = w ? w->style() : QApplication::style(); cachedStyleInfo = QLayoutStyleInfo(style, wid); - cachedStyleInfo.setDefaultSpacing(Qt::Horizontal, spacings[0]); - cachedStyleInfo.setDefaultSpacing(Qt::Vertical, spacings[1]); + cachedStyleInfo.setDefaultSpacing(Qt::Horizontal, spacings[Qt::Horizontal]); + cachedStyleInfo.setDefaultSpacing(Qt::Vertical, spacings[Qt::Vertical]); styleInfoDirty = false; } @@ -2953,9 +2940,9 @@ bool QGraphicsAnchorLayoutPrivate::hasConflicts() const QGraphicsAnchorLayoutPrivate *that = const_cast(this); that->calculateGraphs(); - bool floatConflict = !m_floatItems[0].isEmpty() || !m_floatItems[1].isEmpty(); + bool floatConflict = !m_floatItems[Qt::Horizontal].isEmpty() || !m_floatItems[Qt::Vertical].isEmpty(); - return graphHasConflicts[0] || graphHasConflicts[1] || floatConflict; + return graphHasConflicts[Qt::Horizontal] || graphHasConflicts[Qt::Vertical] || floatConflict; } #ifdef QT_DEBUG @@ -2966,8 +2953,8 @@ void QGraphicsAnchorLayoutPrivate::dumpGraph(const QString &name) 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(); - dotContents += graph[1].serializeToDot(); + QString dotContents = graph[Qt::Horizontal].serializeToDot(); + dotContents += graph[Qt::Vertical].serializeToDot(); file.write(str.arg(dotContents).toLocal8Bit()); file.close(); diff --git a/src/widgets/graphicsview/qgraphicsanchorlayout_p.h b/src/widgets/graphicsview/qgraphicsanchorlayout_p.h index e3f1d139b9..35211c0182 100644 --- a/src/widgets/graphicsview/qgraphicsanchorlayout_p.h +++ b/src/widgets/graphicsview/qgraphicsanchorlayout_p.h @@ -60,6 +60,10 @@ #include "qgraph_p.h" #include "qsimplex_p.h" +#include + +#include + QT_REQUIRE_CONFIG(graphicsview); QT_BEGIN_NAMESPACE @@ -395,7 +399,22 @@ public: enum Orientation { Horizontal = 0, Vertical, - NOrientations + }; + + template + class QHVContainer : public QT_PREPEND_NAMESPACE(QHVContainer) + { + using Base = QT_PREPEND_NAMESPACE(QHVContainer); + static constexpr Qt::Orientation map(Orientation o) noexcept + { return static_cast(int(o) + 1); } + public: + using Base::Base; + using Base::operator[]; + + constexpr const T &operator[](Orientation o) const noexcept + { return this->operator[](map(o)); } + constexpr T &operator[](Orientation o) noexcept + { return this->operator[](map(o)); } }; QGraphicsAnchorLayoutPrivate(); @@ -552,9 +571,9 @@ public: #endif - qreal spacings[NOrientations]; + QHVContainer spacings = {-1, -1}; // Size hints from simplex engine - qreal sizeHints[2][3]; + QHVContainer> sizeHints = {{-1, -1, -1}, {-1, -1, -1}}; // Items QVector items; @@ -565,31 +584,31 @@ public: QHash, QPair > m_vertexList; // Internal graph of anchorage points and anchors, for both orientations - Graph graph[2]; + QHVContainer> graph; - AnchorVertex *layoutFirstVertex[2]; - AnchorVertex *layoutCentralVertex[2]; - AnchorVertex *layoutLastVertex[2]; + QHVContainer layoutFirstVertex = {}; + QHVContainer layoutCentralVertex = {}; + QHVContainer layoutLastVertex = {}; // Combined anchors in order of creation - QList simplifiedVertices[2]; - QList anchorsFromSimplifiedVertices[2]; + QHVContainer> simplifiedVertices; + QHVContainer> anchorsFromSimplifiedVertices; // Graph paths and constraints, for both orientations - QMultiHash graphPaths[2]; - QList constraints[2]; - QList itemCenterConstraints[2]; + QHVContainer> graphPaths; + QHVContainer> constraints; + QHVContainer> itemCenterConstraints; // The interpolation interval and progress based on the current size // as well as the key values (minimum, preferred and maximum) - Interval interpolationInterval[2]; - qreal interpolationProgress[2]; + QHVContainer interpolationInterval; + QHVContainer interpolationProgress = {-1, -1}; - bool graphHasConflicts[2]; - QSet m_floatItems[2]; + QHVContainer graphHasConflicts = {}; + QHVContainer> m_floatItems; #if defined(QT_DEBUG) || defined(QT_BUILD_INTERNAL) - bool lastCalculationUsedSimplex[2]; + QHVContainer lastCalculationUsedSimplex; #endif uint calculateGraphCacheDirty : 1; -- cgit v1.2.3