-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6da276
commit 0939333
Showing
10 changed files
with
393 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/layout/qgslayoutaligner.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
class QgsLayoutAligner | ||
{ | ||
%Docstring | ||
Handles aligning and distributing sets of layout items. | ||
|
||
QgsLayoutAligner contains methods for automatically aligning and distributing | ||
sets of layout items, e.g. aligning a group of items to top or left sides. | ||
|
||
.. versionadded:: 3.0 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgslayoutaligner.h" | ||
%End | ||
public: | ||
|
||
enum Alignment | ||
{ | ||
Left, | ||
HCenter, | ||
Right, | ||
Top, | ||
VCenter, | ||
Bottom, | ||
}; | ||
|
||
static void alignItems( QgsLayout *layout, const QList< QgsLayoutItem * > &items, Alignment alignment ); | ||
%Docstring | ||
Aligns a set of ``items`` from a ``layout`` in place. | ||
|
||
The ``alignment`` argument specifies the method to use when aligning the items. | ||
%End | ||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/layout/qgslayoutaligner.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/*************************************************************************** | ||
qgslayoutaligner.cpp | ||
-------------------- | ||
begin : October 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgslayoutaligner.h" | ||
#include "qgslayoutitem.h" | ||
#include "qgslayout.h" | ||
|
||
void QgsLayoutAligner::alignItems( QgsLayout *layout, const QList<QgsLayoutItem *> &items, QgsLayoutAligner::Alignment alignment ) | ||
{ | ||
if ( !layout || items.size() < 2 ) | ||
{ | ||
return; | ||
} | ||
|
||
QRectF itemBBox = boundingRectOfItems( items ); | ||
if ( !itemBBox.isValid() ) | ||
{ | ||
return; | ||
} | ||
|
||
double refCoord = 0; | ||
switch ( alignment ) | ||
{ | ||
case Left: | ||
refCoord = itemBBox.left(); | ||
break; | ||
case HCenter: | ||
refCoord = itemBBox.center().x(); | ||
break; | ||
case Right: | ||
refCoord = itemBBox.right(); | ||
break; | ||
case Top: | ||
refCoord = itemBBox.top(); | ||
break; | ||
case VCenter: | ||
refCoord = itemBBox.center().y(); | ||
break; | ||
case Bottom: | ||
refCoord = itemBBox.bottom(); | ||
break; | ||
} | ||
|
||
layout->undoStack()->beginMacro( QObject::tr( "Aligned items bottom" ) ); | ||
for ( QgsLayoutItem *item : items ) | ||
{ | ||
layout->undoStack()->beginCommand( item, QString() ); | ||
|
||
QPointF shifted = item->pos(); | ||
switch ( alignment ) | ||
{ | ||
case Left: | ||
shifted.setX( refCoord ); | ||
break; | ||
case HCenter: | ||
shifted.setX( refCoord - item->rect().width() / 2.0 ); | ||
break; | ||
case Right: | ||
shifted.setX( refCoord - item->rect().width() ); | ||
break; | ||
case Top: | ||
shifted.setY( refCoord ); | ||
break; | ||
case VCenter: | ||
shifted.setY( refCoord - item->rect().height() / 2.0 ); | ||
break; | ||
case Bottom: | ||
shifted.setY( refCoord - item->rect().height() ); | ||
break; | ||
} | ||
|
||
// need to keep item units | ||
QgsLayoutPoint newPos = layout->convertFromLayoutUnits( shifted, item->positionWithUnits().units() ); | ||
item->attemptMove( newPos ); | ||
|
||
layout->undoStack()->endCommand(); | ||
} | ||
layout->undoStack()->endMacro(); | ||
} | ||
|
||
QRectF QgsLayoutAligner::boundingRectOfItems( const QList<QgsLayoutItem *> &items ) | ||
{ | ||
if ( items.empty() ) | ||
{ | ||
return QRectF(); | ||
} | ||
|
||
auto it = items.constBegin(); | ||
//set the box to the first item | ||
QgsLayoutItem *currentItem = *it; | ||
it++; | ||
double minX = currentItem->pos().x(); | ||
double minY = currentItem->pos().y(); | ||
double maxX = minX + currentItem->rect().width(); | ||
double maxY = minY + currentItem->rect().height(); | ||
|
||
double currentMinX, currentMinY, currentMaxX, currentMaxY; | ||
|
||
for ( ; it != items.constEnd(); ++it ) | ||
{ | ||
currentItem = *it; | ||
currentMinX = currentItem->pos().x(); | ||
currentMinY = currentItem->pos().y(); | ||
currentMaxX = currentMinX + currentItem->rect().width(); | ||
currentMaxY = currentMinY + currentItem->rect().height(); | ||
|
||
if ( currentMinX < minX ) | ||
minX = currentMinX; | ||
if ( currentMaxX > maxX ) | ||
maxX = currentMaxX; | ||
if ( currentMinY < minY ) | ||
minY = currentMinY; | ||
if ( currentMaxY > maxY ) | ||
maxY = currentMaxY; | ||
} | ||
|
||
return QRectF( QPointF( minX, minY ), QPointF( maxX, maxY ) ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*************************************************************************** | ||
qgslayoutaligner.h | ||
------------------ | ||
begin : October 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#ifndef QGSLAYOUTALIGNER_H | ||
#define QGSLAYOUTALIGNER_H | ||
|
||
#include "qgis_core.h" | ||
#include <QList> | ||
#include <QRectF> | ||
|
||
class QgsLayoutItem; | ||
class QgsLayout; | ||
|
||
/** | ||
* \ingroup core | ||
* \class QgsLayoutAligner | ||
* \brief Handles aligning and distributing sets of layout items. | ||
* | ||
* QgsLayoutAligner contains methods for automatically aligning and distributing | ||
* sets of layout items, e.g. aligning a group of items to top or left sides. | ||
* | ||
* \since QGIS 3.0 | ||
*/ | ||
class CORE_EXPORT QgsLayoutAligner | ||
{ | ||
|
||
public: | ||
|
||
//! Alignment options | ||
enum Alignment | ||
{ | ||
Left, //!< Align left edges | ||
HCenter, //!< Align horizontal centers | ||
Right, //!< Align right edges | ||
Top, //!< Align top edges | ||
VCenter, //!< Align vertical centers | ||
Bottom, //!< Align bottom edges | ||
}; | ||
|
||
/** | ||
* Aligns a set of \a items from a \a layout in place. | ||
* | ||
* The \a alignment argument specifies the method to use when aligning the items. | ||
*/ | ||
static void alignItems( QgsLayout *layout, const QList< QgsLayoutItem * > &items, Alignment alignment ); | ||
|
||
private: | ||
|
||
/** | ||
* Returns the bounding rectangle of the selected items in | ||
* scene coordinates. | ||
*/ | ||
static QRectF boundingRectOfItems( const QList< QgsLayoutItem * > &items ); | ||
|
||
|
||
|
||
}; | ||
|
||
#endif //QGSLAYOUTALIGNER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.