24 if ( !layout || items.size() < 2 )
29 QRectF itemBBox = boundingRectOfItems( items );
30 if ( !itemBBox.isValid() )
39 refCoord = itemBBox.left();
42 refCoord = itemBBox.center().x();
45 refCoord = itemBBox.right();
48 refCoord = itemBBox.top();
51 refCoord = itemBBox.center().y();
54 refCoord = itemBBox.bottom();
63 QPointF shifted = item->pos();
67 shifted.setX( refCoord );
70 shifted.setX( refCoord - item->rect().width() / 2.0 );
73 shifted.setX( refCoord - item->rect().width() );
76 shifted.setY( refCoord );
79 shifted.setY( refCoord - item->rect().height() / 2.0 );
82 shifted.setY( refCoord - item->rect().height() );
88 item->attemptMove( newPos );
97 if ( items.size() < 2 )
100 auto collectReferenceCoord = [distribution](
QgsLayoutItem * item )->
double 102 QRectF itemBBox = item->sceneBoundingRect();
103 switch ( distribution )
106 return itemBBox.left();
108 return itemBBox.center().x();
110 return itemBBox.right();
112 return itemBBox.top();
114 return itemBBox.center().y();
116 return itemBBox.bottom();
119 return itemBBox.left();
123 double minCoord = DBL_MAX;
124 double maxCoord = -DBL_MAX;
125 QMap< double, QgsLayoutItem * > itemCoords;
128 double refCoord = collectReferenceCoord( item );
129 minCoord = std::min( minCoord, refCoord );
130 maxCoord = std::max( maxCoord, refCoord );
131 itemCoords.insert( refCoord, item );
134 double step = ( maxCoord - minCoord ) / ( items.size() - 1 );
136 auto distributeItemToCoord = [layout, distribution](
QgsLayoutItem * item,
double refCoord )
138 QPointF shifted = item->pos();
139 switch ( distribution )
142 shifted.setX( refCoord );
145 shifted.setX( refCoord - item->rect().width() / 2.0 );
148 shifted.setX( refCoord - item->rect().width() );
151 shifted.setY( refCoord );
154 shifted.setY( refCoord - item->rect().height() / 2.0 );
157 shifted.setY( refCoord - item->rect().height() );
163 item->attemptMove( newPos );
168 double currentVal = minCoord;
169 for (
auto itemIt = itemCoords.constBegin(); itemIt != itemCoords.constEnd(); ++itemIt )
172 distributeItemToCoord( itemIt.value(), currentVal );
182 if ( !( items.size() >= 2 || ( items.size() == 1 && resize ==
ResizeToSquare ) ) )
187 QRectF itemBBox = item->sceneBoundingRect();
193 return itemBBox.width();
196 return itemBBox.height();
199 return itemBBox.width();
202 double newSize = collectSize( items.at( 0 ) );
205 double size = collectSize( item );
210 newSize = std::min( size, newSize );
214 newSize = std::max( size, newSize );
221 auto resizeItemToSize = [layout, resize](
QgsLayoutItem * item,
double size )
223 QSizeF newSize = item->rect().size();
228 newSize.setWidth( size );
232 newSize.setHeight( size );
236 if ( newSize.width() > newSize.height() )
237 newSize.setHeight( newSize.width() );
239 newSize.setWidth( newSize.height() );
246 item->attemptResize( newSizeWithUnits );
253 resizeItemToSize( item, newSize );
259 QRectF QgsLayoutAligner::boundingRectOfItems(
const QList<QgsLayoutItem *> &items )
266 auto it = items.constBegin();
270 double minX = currentItem->pos().x();
271 double minY = currentItem->pos().y();
272 double maxX = minX + currentItem->rect().width();
273 double maxY = minY + currentItem->rect().height();
275 double currentMinX, currentMinY, currentMaxX, currentMaxY;
277 for ( ; it != items.constEnd(); ++it )
280 currentMinX = currentItem->pos().x();
281 currentMinY = currentItem->pos().y();
282 currentMaxX = currentMinX + currentItem->rect().width();
283 currentMaxY = currentMinY + currentItem->rect().height();
285 if ( currentMinX < minX )
287 if ( currentMaxX > maxX )
289 if ( currentMinY < minY )
291 if ( currentMaxY > maxY )
295 return QRectF( QPointF( minX, minY ), QPointF( maxX, maxY ) );
298 QString QgsLayoutAligner::undoText(
Distribution distribution )
300 switch ( distribution )
303 return QObject::tr(
"Distribute Items by Left" );
305 return QObject::tr(
"Distribute Items by Center" );
307 return QObject::tr(
"Distribute Items by Right" );
309 return QObject::tr(
"Distribute Items by Top" );
311 return QObject::tr(
"Distribute Items by Vertical Center" );
313 return QObject::tr(
"Distribute Items by Bottom" );
323 return QObject::tr(
"Resize Items to Narrowest" );
325 return QObject::tr(
"Resize Items to Widest" );
327 return QObject::tr(
"Resize Items to Shortest" );
329 return QObject::tr(
"Resize Items to Tallest" );
331 return QObject::tr(
"Resize Items to Square" );
336 QString QgsLayoutAligner::undoText(
Alignment alignment )
341 return QObject::tr(
"Align Items to Left" );
343 return QObject::tr(
"Align Items to Center" );
345 return QObject::tr(
"Align Items to Right" );
347 return QObject::tr(
"Align Items to Top" );
349 return QObject::tr(
"Align Items to Vertical Center" );
351 return QObject::tr(
"Align Items to Bottom" );
Base class for graphical items within a QgsLayout.
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
Resize height to match shortest height.
static void alignItems(QgsLayout *layout, const QList< QgsLayoutItem * > &items, Alignment alignment)
Aligns a set of items from a layout in place.
Distribute vertical centers.
This class provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
void endCommand()
Saves final state of an object and pushes the active command to the undo history. ...
Distribution
Distribution options.
static void distributeItems(QgsLayout *layout, const QList< QgsLayoutItem * > &items, Distribution distribution)
Distributes a set of items from a layout in place.
Resize height to match tallest height.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
void beginMacro(const QString &commandText)
Starts a macro command, with the given descriptive commandText.
Resize width to match widest width.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
QgsLayoutMeasurement convertFromLayoutUnits(const double length, const QgsUnitTypes::LayoutUnit unit) const
Converts a length measurement from the layout's native units to a specified target unit...
Align horizontal centers.
Alignment
Alignment options.
Distribute horizontal centers.
void endMacro()
Ends a macro command.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
static void resizeItems(QgsLayout *layout, const QList< QgsLayoutItem * > &items, Resize resize)
Resizes a set of items from a layout in place.
Resize width to match narrowest width.