QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgslayoutaligner.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutaligner.cpp
3 --------------------
4 begin : October 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgslayoutaligner.h"
18
19#include "qgslayout.h"
20#include "qgslayoutitem.h"
21#include "qgslayoutundostack.h"
22
23void QgsLayoutAligner::alignItems( QgsLayout *layout, const QList<QgsLayoutItem *> &items, QgsLayoutAligner::Alignment alignment )
24{
25 if ( !layout || items.size() < 2 )
26 {
27 return;
28 }
29
30 const QRectF itemBBox = boundingRectOfItems( items );
31 if ( !itemBBox.isValid() )
32 {
33 return;
34 }
35
36 double refCoord = 0;
37 switch ( alignment )
38 {
39 case AlignLeft:
40 refCoord = itemBBox.left();
41 break;
42 case AlignHCenter:
43 refCoord = itemBBox.center().x();
44 break;
45 case AlignRight:
46 refCoord = itemBBox.right();
47 break;
48 case AlignTop:
49 refCoord = itemBBox.top();
50 break;
51 case AlignVCenter:
52 refCoord = itemBBox.center().y();
53 break;
54 case AlignBottom:
55 refCoord = itemBBox.bottom();
56 break;
57 }
58
59 layout->undoStack()->beginMacro( undoText( alignment ) );
60 for ( QgsLayoutItem *item : items )
61 {
62 layout->undoStack()->beginCommand( item, QString() );
63
64 QPointF shifted = item->pos();
65 switch ( alignment )
66 {
67 case AlignLeft:
68 shifted.setX( refCoord );
69 break;
70 case AlignHCenter:
71 shifted.setX( refCoord - item->rect().width() / 2.0 );
72 break;
73 case AlignRight:
74 shifted.setX( refCoord - item->rect().width() );
75 break;
76 case AlignTop:
77 shifted.setY( refCoord );
78 break;
79 case AlignVCenter:
80 shifted.setY( refCoord - item->rect().height() / 2.0 );
81 break;
82 case AlignBottom:
83 shifted.setY( refCoord - item->rect().height() );
84 break;
85 }
86
87 // need to keep item units
88 const QgsLayoutPoint newPos = layout->convertFromLayoutUnits( shifted, item->positionWithUnits().units() );
89 item->attemptMove( newPos, false );
90
91 layout->undoStack()->endCommand();
92 }
93 layout->undoStack()->endMacro();
94}
95
96void QgsLayoutAligner::distributeItems( QgsLayout *layout, const QList<QgsLayoutItem *> &items, QgsLayoutAligner::Distribution distribution )
97{
98 if ( items.size() < 2 )
99 return;
100
101 // equispaced distribution doesn't follow the same approach of the other distribution types
102 if ( distribution == DistributeHSpace || distribution == DistributeVSpace )
103 {
104 distributeEquispacedItems( layout, items, distribution );
105 return;
106 }
107
108 auto collectReferenceCoord = [distribution]( QgsLayoutItem *item ) -> double {
109 const QRectF itemBBox = item->sceneBoundingRect();
110 switch ( distribution )
111 {
112 case DistributeLeft:
113 return itemBBox.left();
115 return itemBBox.center().x();
116 case DistributeRight:
117 return itemBBox.right();
118 case DistributeTop:
119 return itemBBox.top();
121 return itemBBox.center().y();
122 case DistributeBottom:
123 return itemBBox.bottom();
124 case DistributeHSpace:
125 case DistributeVSpace:
126 // not reachable branch, just to avoid compilation warning
127 return std::numeric_limits<double>::quiet_NaN();
128 }
129 // no warnings
130 return itemBBox.left();
131 };
132
133
134 double minCoord = std::numeric_limits<double>::max();
135 double maxCoord = std::numeric_limits<double>::lowest();
136 QMap< double, QgsLayoutItem * > itemCoords;
137 for ( QgsLayoutItem *item : items )
138 {
139 const double refCoord = collectReferenceCoord( item );
140 minCoord = std::min( minCoord, refCoord );
141 maxCoord = std::max( maxCoord, refCoord );
142 itemCoords.insert( refCoord, item );
143 }
144
145 const double step = ( maxCoord - minCoord ) / ( items.size() - 1 );
146
147 auto distributeItemToCoord = [layout, distribution]( QgsLayoutItem *item, double refCoord ) {
148 QPointF shifted = item->pos();
149 switch ( distribution )
150 {
151 case DistributeLeft:
152 shifted.setX( refCoord );
153 break;
155 shifted.setX( refCoord - item->rect().width() / 2.0 );
156 break;
157 case DistributeRight:
158 shifted.setX( refCoord - item->rect().width() );
159 break;
160 case DistributeTop:
161 shifted.setY( refCoord );
162 break;
164 shifted.setY( refCoord - item->rect().height() / 2.0 );
165 break;
166 case DistributeBottom:
167 shifted.setY( refCoord - item->rect().height() );
168 break;
169 case DistributeHSpace:
170 case DistributeVSpace:
171 // not reachable branch, just to avoid compilation warning
172 break;
173 }
174
175 // need to keep item units
176 const QgsLayoutPoint newPos = layout->convertFromLayoutUnits( shifted, item->positionWithUnits().units() );
177 item->attemptMove( newPos, false );
178 };
179
180
181 layout->undoStack()->beginMacro( undoText( distribution ) );
182 double currentVal = minCoord;
183 for ( auto itemIt = itemCoords.constBegin(); itemIt != itemCoords.constEnd(); ++itemIt )
184 {
185 layout->undoStack()->beginCommand( itemIt.value(), QString() );
186 distributeItemToCoord( itemIt.value(), currentVal );
187 layout->undoStack()->endCommand();
188
189 currentVal += step;
190 }
191 layout->undoStack()->endMacro();
192}
193
194void QgsLayoutAligner::resizeItems( QgsLayout *layout, const QList<QgsLayoutItem *> &items, QgsLayoutAligner::Resize resize )
195{
196 if ( !( items.size() >= 2 || ( items.size() == 1 && resize == ResizeToSquare ) ) )
197 return;
198
199 auto collectSize = [resize]( QgsLayoutItem *item ) -> double {
200 const QRectF itemBBox = item->sceneBoundingRect();
201 switch ( resize )
202 {
203 case ResizeNarrowest:
204 case ResizeWidest:
205 case ResizeToSquare:
206 return itemBBox.width();
207 case ResizeShortest:
208 case ResizeTallest:
209 return itemBBox.height();
210 }
211 // no warnings
212 return itemBBox.width();
213 };
214
215 double newSize = collectSize( items.at( 0 ) );
216 for ( QgsLayoutItem *item : items )
217 {
218 const double size = collectSize( item );
219 switch ( resize )
220 {
221 case ResizeNarrowest:
222 case ResizeShortest:
223 newSize = std::min( size, newSize );
224 break;
225 case ResizeTallest:
226 case ResizeWidest:
227 newSize = std::max( size, newSize );
228 break;
229 case ResizeToSquare:
230 break;
231 }
232 }
233
234 auto resizeItemToSize = [layout, resize]( QgsLayoutItem *item, double size ) {
235 QSizeF newSize = item->rect().size();
236 switch ( resize )
237 {
238 case ResizeNarrowest:
239 case ResizeWidest:
240 newSize.setWidth( size );
241 break;
242 case ResizeTallest:
243 case ResizeShortest:
244 newSize.setHeight( size );
245 break;
246 case ResizeToSquare:
247 {
248 if ( newSize.width() > newSize.height() )
249 newSize.setHeight( newSize.width() );
250 else
251 newSize.setWidth( newSize.height() );
252 break;
253 }
254 }
255
256 // need to keep item units
257 const QgsLayoutSize newSizeWithUnits = layout->convertFromLayoutUnits( newSize, item->sizeWithUnits().units() );
258 item->attemptResize( newSizeWithUnits );
259 };
260
261 layout->undoStack()->beginMacro( undoText( resize ) );
262 for ( QgsLayoutItem *item : items )
263 {
264 layout->undoStack()->beginCommand( item, QString() );
265 resizeItemToSize( item, newSize );
266 layout->undoStack()->endCommand();
267 }
268 layout->undoStack()->endMacro();
269}
270
271QRectF QgsLayoutAligner::boundingRectOfItems( const QList<QgsLayoutItem *> &items )
272{
273 if ( items.empty() )
274 {
275 return QRectF();
276 }
277
278 auto it = items.constBegin();
279 //set the box to the first item
280 QgsLayoutItem *currentItem = *it;
281 it++;
282 double minX = currentItem->pos().x();
283 double minY = currentItem->pos().y();
284 double maxX = minX + currentItem->rect().width();
285 double maxY = minY + currentItem->rect().height();
286
287 double currentMinX, currentMinY, currentMaxX, currentMaxY;
288
289 for ( ; it != items.constEnd(); ++it )
290 {
291 currentItem = *it;
292 currentMinX = currentItem->pos().x();
293 currentMinY = currentItem->pos().y();
294 currentMaxX = currentMinX + currentItem->rect().width();
295 currentMaxY = currentMinY + currentItem->rect().height();
296
297 if ( currentMinX < minX )
298 minX = currentMinX;
299 if ( currentMaxX > maxX )
300 maxX = currentMaxX;
301 if ( currentMinY < minY )
302 minY = currentMinY;
303 if ( currentMaxY > maxY )
304 maxY = currentMaxY;
305 }
306
307 return QRectF( QPointF( minX, minY ), QPointF( maxX, maxY ) );
308}
309
310QString QgsLayoutAligner::undoText( Distribution distribution )
311{
312 switch ( distribution )
313 {
314 case DistributeLeft:
315 return QObject::tr( "Distribute Items by Left" );
317 return QObject::tr( "Distribute Items by Horizontal Center" );
318 case DistributeHSpace:
319 return QObject::tr( "Distribute Horizontal Spacing Equally" );
320 case DistributeRight:
321 return QObject::tr( "Distribute Items by Right" );
322 case DistributeTop:
323 return QObject::tr( "Distribute Items by Top" );
325 return QObject::tr( "Distribute Items by Vertical Center" );
326 case DistributeVSpace:
327 return QObject::tr( "Distribute Vertical Spacing Equally" );
328 case DistributeBottom:
329 return QObject::tr( "Distribute Items by Bottom" );
330 }
331 return QString(); //no warnings
332}
333
334QString QgsLayoutAligner::undoText( QgsLayoutAligner::Resize resize )
335{
336 switch ( resize )
337 {
338 case ResizeNarrowest:
339 return QObject::tr( "Resize Items to Narrowest" );
340 case ResizeWidest:
341 return QObject::tr( "Resize Items to Widest" );
342 case ResizeShortest:
343 return QObject::tr( "Resize Items to Shortest" );
344 case ResizeTallest:
345 return QObject::tr( "Resize Items to Tallest" );
346 case ResizeToSquare:
347 return QObject::tr( "Resize Items to Square" );
348 }
349 return QString(); //no warnings
350}
351
352QString QgsLayoutAligner::undoText( Alignment alignment )
353{
354 switch ( alignment )
355 {
356 case AlignLeft:
357 return QObject::tr( "Align Items to Left" );
358 case AlignHCenter:
359 return QObject::tr( "Align Items to Center" );
360 case AlignRight:
361 return QObject::tr( "Align Items to Right" );
362 case AlignTop:
363 return QObject::tr( "Align Items to Top" );
364 case AlignVCenter:
365 return QObject::tr( "Align Items to Vertical Center" );
366 case AlignBottom:
367 return QObject::tr( "Align Items to Bottom" );
368 }
369 return QString(); //no warnings
370}
371
372void QgsLayoutAligner::distributeEquispacedItems( QgsLayout *layout, const QList<QgsLayoutItem *> &items, QgsLayoutAligner::Distribution distribution )
373{
374 double length = 0.0;
375 double minCoord = std::numeric_limits<double>::max();
376 double maxCoord = std::numeric_limits<double>::lowest();
377 QMap< double, QgsLayoutItem * > itemCoords;
378
379 for ( QgsLayoutItem *item : items )
380 {
381 const QRectF itemBBox = item->sceneBoundingRect();
382
383 const double item_min = ( distribution == DistributeHSpace ? itemBBox.left() : itemBBox.top() );
384 const double item_max = ( distribution == DistributeHSpace ? itemBBox.right() : itemBBox.bottom() );
385
386 minCoord = std::min( minCoord, item_min );
387 maxCoord = std::max( maxCoord, item_max );
388 length += ( item_max - item_min );
389 itemCoords.insert( item_min, item );
390 }
391 const double step = ( maxCoord - minCoord - length ) / ( items.size() - 1 );
392
393 double currentVal = minCoord;
394 layout->undoStack()->beginMacro( undoText( distribution ) );
395 for ( auto itemIt = itemCoords.constBegin(); itemIt != itemCoords.constEnd(); ++itemIt )
396 {
397 QgsLayoutItem *item = itemIt.value();
398 QPointF shifted = item->pos();
399
400 layout->undoStack()->beginCommand( itemIt.value(), QString() );
401
402 if ( distribution == DistributeHSpace )
403 {
404 shifted.setX( currentVal );
405 }
406 else
407 {
408 shifted.setY( currentVal );
409 }
410
411 const QgsLayoutPoint newPos = layout->convertFromLayoutUnits( shifted, item->positionWithUnits().units() );
412 item->attemptMove( newPos, false );
413
414 layout->undoStack()->endCommand();
415
416 currentVal += ( distribution == DistributeHSpace ? item->rect().width() : item->rect().height() ) + step;
417 }
418 layout->undoStack()->endMacro();
419 return;
420}
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.
Resize
Resize options.
@ 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.
Definition qgslayout.h:50
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 ...