25 if ( !layout || items.size() < 2 )
30 const QRectF itemBBox = boundingRectOfItems( items );
31 if ( !itemBBox.isValid() )
40 refCoord = itemBBox.left();
43 refCoord = itemBBox.center().x();
46 refCoord = itemBBox.right();
49 refCoord = itemBBox.top();
52 refCoord = itemBBox.center().y();
55 refCoord = itemBBox.bottom();
64 QPointF shifted = item->pos();
68 shifted.setX( refCoord );
71 shifted.setX( refCoord - item->rect().width() / 2.0 );
74 shifted.setX( refCoord - item->rect().width() );
77 shifted.setY( refCoord );
80 shifted.setY( refCoord - item->rect().height() / 2.0 );
83 shifted.setY( refCoord - item->rect().height() );
89 item->attemptMove( newPos,
false );
98 if ( items.size() < 2 )
104 distributeEquispacedItems( layout, items, distribution );
108 auto collectReferenceCoord = [distribution](
QgsLayoutItem * item )->
double
110 const QRectF itemBBox = item->sceneBoundingRect();
111 switch ( distribution )
114 return itemBBox.left();
116 return itemBBox.center().x();
118 return itemBBox.right();
120 return itemBBox.top();
122 return itemBBox.center().y();
124 return itemBBox.bottom();
128 return std::numeric_limits<double>::quiet_NaN();
131 return itemBBox.left();
135 double minCoord = std::numeric_limits<double>::max();
136 double maxCoord = std::numeric_limits<double>::lowest();
137 QMap< double, QgsLayoutItem * > itemCoords;
140 const double refCoord = collectReferenceCoord( item );
141 minCoord = std::min( minCoord, refCoord );
142 maxCoord = std::max( maxCoord, refCoord );
143 itemCoords.insert( refCoord, item );
146 const double step = ( maxCoord - minCoord ) / ( items.size() - 1 );
148 auto distributeItemToCoord = [layout, distribution](
QgsLayoutItem * item,
double refCoord )
150 QPointF shifted = item->pos();
151 switch ( distribution )
154 shifted.setX( refCoord );
157 shifted.setX( refCoord - item->rect().width() / 2.0 );
160 shifted.setX( refCoord - item->rect().width() );
163 shifted.setY( refCoord );
166 shifted.setY( refCoord - item->rect().height() / 2.0 );
169 shifted.setY( refCoord - item->rect().height() );
179 item->attemptMove( newPos,
false );
184 double currentVal = minCoord;
185 for (
auto itemIt = itemCoords.constBegin(); itemIt != itemCoords.constEnd(); ++itemIt )
188 distributeItemToCoord( itemIt.value(), currentVal );
198 if ( !( items.size() >= 2 || ( items.size() == 1 && resize ==
ResizeToSquare ) ) )
203 const QRectF itemBBox = item->sceneBoundingRect();
209 return itemBBox.width();
212 return itemBBox.height();
215 return itemBBox.width();
218 double newSize = collectSize( items.at( 0 ) );
221 const double size = collectSize( item );
226 newSize = std::min( size, newSize );
230 newSize = std::max( size, newSize );
237 auto resizeItemToSize = [layout, resize](
QgsLayoutItem * item,
double size )
239 QSizeF newSize = item->rect().size();
244 newSize.setWidth( size );
248 newSize.setHeight( size );
252 if ( newSize.width() > newSize.height() )
253 newSize.setHeight( newSize.width() );
255 newSize.setWidth( newSize.height() );
262 item->attemptResize( newSizeWithUnits );
269 resizeItemToSize( item, newSize );
275QRectF QgsLayoutAligner::boundingRectOfItems(
const QList<QgsLayoutItem *> &items )
282 auto it = items.constBegin();
284 QgsLayoutItem *currentItem = *it;
286 double minX = currentItem->pos().x();
287 double minY = currentItem->pos().y();
288 double maxX = minX + currentItem->rect().width();
289 double maxY = minY + currentItem->rect().height();
291 double currentMinX, currentMinY, currentMaxX, currentMaxY;
293 for ( ; it != items.constEnd(); ++it )
296 currentMinX = currentItem->pos().x();
297 currentMinY = currentItem->pos().y();
298 currentMaxX = currentMinX + currentItem->rect().width();
299 currentMaxY = currentMinY + currentItem->rect().height();
301 if ( currentMinX < minX )
303 if ( currentMaxX > maxX )
305 if ( currentMinY < minY )
307 if ( currentMaxY > maxY )
311 return QRectF( QPointF( minX, minY ), QPointF( maxX, maxY ) );
314QString QgsLayoutAligner::undoText( Distribution distribution )
316 switch ( distribution )
319 return QObject::tr(
"Distribute Items by Left" );
321 return QObject::tr(
"Distribute Items by Horizontal Center" );
323 return QObject::tr(
"Distribute Horizontal Spacing Equally" );
325 return QObject::tr(
"Distribute Items by Right" );
327 return QObject::tr(
"Distribute Items by Top" );
329 return QObject::tr(
"Distribute Items by Vertical Center" );
331 return QObject::tr(
"Distribute Vertical Spacing Equally" );
333 return QObject::tr(
"Distribute Items by Bottom" );
343 return QObject::tr(
"Resize Items to Narrowest" );
345 return QObject::tr(
"Resize Items to Widest" );
347 return QObject::tr(
"Resize Items to Shortest" );
349 return QObject::tr(
"Resize Items to Tallest" );
351 return QObject::tr(
"Resize Items to Square" );
356QString QgsLayoutAligner::undoText( Alignment alignment )
361 return QObject::tr(
"Align Items to Left" );
363 return QObject::tr(
"Align Items to Center" );
365 return QObject::tr(
"Align Items to Right" );
367 return QObject::tr(
"Align Items to Top" );
369 return QObject::tr(
"Align Items to Vertical Center" );
371 return QObject::tr(
"Align Items to Bottom" );
379 double minCoord = std::numeric_limits<double>::max();
380 double maxCoord = std::numeric_limits<double>::lowest();
381 QMap< double, QgsLayoutItem * > itemCoords;
383 for ( QgsLayoutItem *item : items )
385 const QRectF itemBBox = item->sceneBoundingRect();
387 const double item_min = ( distribution ==
DistributeHSpace ? itemBBox.left() :
389 const double item_max = ( distribution ==
DistributeHSpace ? itemBBox.right() :
392 minCoord = std::min( minCoord, item_min );
393 maxCoord = std::max( maxCoord, item_max );
394 length += ( item_max - item_min );
395 itemCoords.insert( item_min, item );
397 const double step = ( maxCoord - minCoord - length ) / ( items.size() - 1 );
399 double currentVal = minCoord;
401 for (
auto itemIt = itemCoords.constBegin(); itemIt != itemCoords.constEnd(); ++itemIt )
403 QgsLayoutItem *item = itemIt.value();
404 QPointF shifted = item->pos();
410 shifted.setX( currentVal );
414 shifted.setY( currentVal );
423 item->rect().height() ) + step;
Alignment
Alignment options.
@ AlignVCenter
Align vertical centers.
@ AlignLeft
Align left edges.
@ AlignBottom
Align bottom edges.
@ AlignRight
Align right edges.
@ AlignTop
Align top edges.
@ AlignHCenter
Align horizontal centers.
@ ResizeNarrowest
Resize width to match narrowest width.
@ ResizeShortest
Resize height to match shortest height.
@ ResizeTallest
Resize height to match tallest height.
@ ResizeToSquare
Resize items to square.
@ ResizeWidest
Resize width to match widest width.
static void alignItems(QgsLayout *layout, const QList< QgsLayoutItem * > &items, Alignment alignment)
Aligns a set of items from a layout in place.
static void distributeItems(QgsLayout *layout, const QList< QgsLayoutItem * > &items, Distribution distribution)
Distributes a set of items from a layout in place.
static void resizeItems(QgsLayout *layout, const QList< QgsLayoutItem * > &items, Resize resize)
Resizes a set of items from a layout in place.
Distribution
Distribution options.
@ DistributeHSpace
Distribute horizontal equispaced.
@ DistributeVCenter
Distribute vertical centers.
@ DistributeBottom
Distribute bottom edges.
@ DistributeLeft
Distribute left edges.
@ DistributeHCenter
Distribute horizontal centers.
@ DistributeRight
Distribute right edges.
@ DistributeVSpace
Distribute vertical equispaced.
@ DistributeTop
Distribute top edges.
Base class for graphical items within a QgsLayout.
QgsLayoutPoint positionWithUnits() const
Returns the item's current position, including units.
virtual void attemptMove(const QgsLayoutPoint &point, bool useReferencePoint=true, bool includesFrame=false, int page=-1)
Attempts to move the item to a specified point.
Provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
Qgis::LayoutUnit units() const
Returns the units for the point.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
void beginMacro(const QString &commandText)
Starts a macro command, with the given descriptive commandText.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
void endMacro()
Ends a macro command.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
QgsLayoutMeasurement convertFromLayoutUnits(double length, Qgis::LayoutUnit unit) const
Converts a length measurement from the layout's native units to a specified target unit.
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...