From 245e5bafe62ff744ac7090f0ec79522359efbe1e Mon Sep 17 00:00:00 2001 From: Matteo Nastasi Date: Mon, 11 Mar 2019 09:58:44 +0100 Subject: [PATCH] create a private method to compute equispaced distributions --- src/core/layout/qgslayoutaligner.cpp | 107 ++++++++++++++------------- src/core/layout/qgslayoutaligner.h | 7 ++ 2 files changed, 61 insertions(+), 53 deletions(-) diff --git a/src/core/layout/qgslayoutaligner.cpp b/src/core/layout/qgslayoutaligner.cpp index 737f43acfb5b..d8114fb1e998 100644 --- a/src/core/layout/qgslayoutaligner.cpp +++ b/src/core/layout/qgslayoutaligner.cpp @@ -99,62 +99,10 @@ void QgsLayoutAligner::distributeItems( QgsLayout *layout, const QList::max(); - double maxCoord = std::numeric_limits::lowest(); - QMap< double, QgsLayoutItem * > itemCoords; - - double step; - // equispaced distribution doesn't follow the same approach of the other distribution types if ( distribution == DistributeHSpace || distribution == DistributeVSpace ) { - double length = 0.0; - - for ( QgsLayoutItem *item : items ) - { - QRectF itemBBox = item->sceneBoundingRect(); - double item_min, item_max; - - item_min = ( distribution == DistributeHSpace ? itemBBox.left() : - itemBBox.top() ); - item_max = ( distribution == DistributeHSpace ? itemBBox.right() : - itemBBox.bottom() ); - - minCoord = std::min( minCoord, item_min ); - maxCoord = std::max( maxCoord, item_max ); - length += ( item_max - item_min ); - itemCoords.insert( item_min, item ); - } - step = ( maxCoord - minCoord - length ) / ( items.size() - 1 ); - - double currentVal = minCoord; - layout->undoStack()->beginMacro( undoText( distribution ) ); - for ( auto itemIt = itemCoords.constBegin(); itemIt != itemCoords.constEnd(); ++itemIt ) - { - QgsLayoutItem *item = itemIt.value(); - QPointF shifted = item->pos(); - - layout->undoStack()->beginCommand( itemIt.value(), QString() ); - - if ( distribution == DistributeHSpace ) - { - shifted.setX( currentVal ); - } - else - { - shifted.setY( currentVal ); - } - - QgsLayoutPoint newPos = layout->convertFromLayoutUnits( shifted, item->positionWithUnits().units() ); - item->attemptMove( newPos ); - - layout->undoStack()->endCommand(); - - currentVal += ( distribution == DistributeHSpace ? item->rect().width() : - item->rect().height() ) + step; - } - layout->undoStack()->endMacro(); - + distributeEquispacedItems( layout, items, distribution ); return; } @@ -422,3 +370,56 @@ QString QgsLayoutAligner::undoText( Alignment alignment ) } return QString(); //no warnings } + +void QgsLayoutAligner::distributeEquispacedItems( QgsLayout *layout, const QList &items, QgsLayoutAligner::Distribution distribution ) +{ + double length = 0.0; + double minCoord = std::numeric_limits::max(); + double maxCoord = std::numeric_limits::lowest(); + QMap< double, QgsLayoutItem * > itemCoords; + + for ( QgsLayoutItem *item : items ) + { + QRectF itemBBox = item->sceneBoundingRect(); + + double item_min = ( distribution == DistributeHSpace ? itemBBox.left() : + itemBBox.top() ); + double item_max = ( distribution == DistributeHSpace ? itemBBox.right() : + itemBBox.bottom() ); + + minCoord = std::min( minCoord, item_min ); + maxCoord = std::max( maxCoord, item_max ); + length += ( item_max - item_min ); + itemCoords.insert( item_min, item ); + } + const double step = ( maxCoord - minCoord - length ) / ( items.size() - 1 ); + + double currentVal = minCoord; + layout->undoStack()->beginMacro( undoText( distribution ) ); + for ( auto itemIt = itemCoords.constBegin(); itemIt != itemCoords.constEnd(); ++itemIt ) + { + QgsLayoutItem *item = itemIt.value(); + QPointF shifted = item->pos(); + + layout->undoStack()->beginCommand( itemIt.value(), QString() ); + + if ( distribution == DistributeHSpace ) + { + shifted.setX( currentVal ); + } + else + { + shifted.setY( currentVal ); + } + + QgsLayoutPoint newPos = layout->convertFromLayoutUnits( shifted, item->positionWithUnits().units() ); + item->attemptMove( newPos ); + + layout->undoStack()->endCommand(); + + currentVal += ( distribution == DistributeHSpace ? item->rect().width() : + item->rect().height() ) + step; + } + layout->undoStack()->endMacro(); + return; +} diff --git a/src/core/layout/qgslayoutaligner.h b/src/core/layout/qgslayoutaligner.h index 27aba6e518de..7da626624f16 100644 --- a/src/core/layout/qgslayoutaligner.h +++ b/src/core/layout/qgslayoutaligner.h @@ -105,6 +105,13 @@ class CORE_EXPORT QgsLayoutAligner static QString undoText( Distribution distribution ); static QString undoText( Resize resize ); + /** + * Distributes a set of \a items from a \a layout in place for \a DistributeHSpace + * and \a DistributeVSpace distribution type special cases. + * + * The \a distribution argument specifies the method to use when distributing the items. + */ + static void distributeEquispacedItems( QgsLayout *layout, const QList &items, QgsLayoutAligner::Distribution distribution ); };