27#include "moc_qgslayoutitemgroup.cpp"
29using namespace Qt::StringLiterals;
50 mLayout->removeLayoutItem( item );
69 if ( !
id().isEmpty() )
73 return tr(
"<Group>" );
88 if ( mItems.contains( item ) )
93 mItems << QPointer< QgsLayoutItem >( item );
106 item->setParentGroup(
nullptr );
113 QList<QgsLayoutItem *> val;
125 if ( !shouldBlockUndoCommands() )
126 mLayout->undoStack()->beginMacro( tr(
"Set Group Visibility" ) );
132 bool prev = item->mBlockUndoCommands;
133 item->mBlockUndoCommands = mBlockUndoCommands;
134 item->setVisibility( visible );
135 item->mBlockUndoCommands = prev;
140 if ( !shouldBlockUndoCommands() )
141 mLayout->undoStack()->endMacro();
146 Q_UNUSED( useReferencePoint )
150 if ( !shouldBlockUndoCommands() )
151 mLayout->undoStack()->beginMacro( tr(
"Move group" ) );
155 scenePoint =
mLayout->convertToLayoutUnits( point );
157 scenePoint =
mLayout->pageCollection()->pagePositionToLayoutPosition(
page, point );
159 double deltaX = scenePoint.x() - pos().x();
160 double deltaY = scenePoint.y() - pos().y();
168 std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
169 if ( !shouldBlockUndoCommands() )
172 command->saveBeforeState();
175 item->attemptMoveBy( deltaX, deltaY );
179 command->saveAfterState();
180 mLayout->undoStack()->push( command.release() );
185 if ( !shouldBlockUndoCommands() )
186 mLayout->undoStack()->endMacro();
187 updateBoundingRect();
195 if ( !shouldBlockUndoCommands() )
196 mLayout->undoStack()->beginMacro( tr(
"Resize Group" ) );
198 QRectF oldRect = rect();
199 QSizeF newSizeLayoutUnits =
mLayout->convertToLayoutUnits( size );
201 newRect.setSize( newSizeLayoutUnits );
209 std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
210 if ( !shouldBlockUndoCommands() )
213 command->saveBeforeState();
216 const QRectF originalItemRect = item->rect();
217 QRectF itemRect = mapRectFromItem( item, originalItemRect );
220 itemRect = itemRect.normalized();
221 QPointF newPos = mapToScene( itemRect.topLeft() );
223 QgsLayoutSize itemSize =
mLayout->convertFromLayoutUnits( itemRect.size(), item->sizeWithUnits().units() );
224 item->attemptResize( itemSize, includesFrame );
227 QgsLayoutPoint itemPos =
mLayout->convertFromLayoutUnits( newPos, item->positionWithUnits().units() );
228 item->attemptMove( itemPos,
false );
232 command->saveAfterState();
233 mLayout->undoStack()->push( command.release() );
237 if ( !shouldBlockUndoCommands() )
238 mLayout->undoStack()->endMacro();
240 updateBoundingRect();
250 QDomElement childItem = document.createElement( u
"ComposerItemGroupElement"_s );
251 childItem.setAttribute( u
"uuid"_s, item->uuid() );
252 element.appendChild( childItem );
261 QDomNodeList elementNodes = itemElement.elementsByTagName( u
"ComposerItemGroupElement"_s );
262 for (
int i = 0; i < elementNodes.count(); ++i )
264 QDomNode elementNode = elementNodes.at( i );
265 if ( !elementNode.isElement() )
268 QString
uuid = elementNode.toElement().attribute( u
"uuid"_s );
276 for (
const QString &
uuid : std::as_const( mItemUuids ) )
285 updateBoundingRect();
303void QgsLayoutItemGroup::updateBoundingRect()
306 if ( mItems.isEmpty() )
313 auto itemIter = mItems.constBegin();
316 double rotation = ( *itemIter )->rotation();
319 for ( ++itemIter; itemIter != mItems.constEnd(); ++itemIter )
328 setScenePos( QPointF( 0, 0 ) );
331 itemIter = mItems.constBegin();
334 QRectF groupRect = mapFromItem( ( *itemIter ), ( *itemIter )->rect() ).boundingRect();
335 QRectF groupRectWithFrame = mapFromItem( ( *itemIter ), ( *itemIter )->rectWithFrame() ).boundingRect();
338 for ( ++itemIter; itemIter != mItems.constEnd(); ++itemIter )
340 groupRect |= mapFromItem( ( *itemIter ), ( *itemIter )->rect() ).boundingRect();
341 groupRectWithFrame |= mapFromItem( ( *itemIter ), ( *itemIter )->rectWithFrame() ).boundingRect();
346 setRect( 0, 0, groupRect.width(), groupRect.height() );
347 setPos( mapToScene( groupRect.topLeft() ) );
349 QPointF bleedShift = groupRectWithFrame.topLeft() - groupRect.topLeft();
350 mRectWithFrame = QRectF( bleedShift, groupRectWithFrame.size() );
355 return mRectWithFrame;
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
QRectF rectWithFrame() const override
Returns the item's rectangular bounds, including any bleed caused by the item's frame.
void removeItems()
Removes all items from the group (but does not delete them).
void addItem(QgsLayoutItem *item)
Adds an item to the group.
QgsLayoutItemGroup(QgsLayout *layout)
Constructor for QgsLayoutItemGroup, belonging to the specified layout.
QList< QgsLayoutItem * > items() const
Returns a list of items contained by the group.
~QgsLayoutItemGroup() override
void setVisibility(bool visible) override
Sets whether the item is visible.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
bool writePropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
int type() const override
ExportLayerBehavior exportLayerBehavior() const override
Returns the behavior of this item during exporting to layered exports (e.g.
void cleanup() override
Called just before a batch of items are deleted, allowing them to run cleanup tasks.
void finalizeRestoreFromXml() override
Called after all pending items have been restored from XML.
void attemptResize(const QgsLayoutSize &size, bool includesFrame=false) override
Attempts to resize the item to a specified target size.
static QgsLayoutItemGroup * create(QgsLayout *layout)
Returns a new group item for the specified layout.
void attemptMove(const QgsLayoutPoint &point, bool useReferencePoint=true, bool includesFrame=false, int page=-1) override
Attempts to move the item to a specified point.
bool readPropertiesFromElement(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
QString displayName() const override
Gets item display name.
@ LayoutGroup
Grouped item.
Contains settings and helpers relating to a render of a QgsLayoutItem.
friend class QgsLayoutItemGroup
QgsAbstractLayoutUndoCommand * createCommand(const QString &text, int id, QUndoCommand *parent=nullptr) override
Creates a new layout undo command with the specified text and parent.
virtual void cleanup()
Called just before a batch of items are deleted, allowing them to run cleanup tasks.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
virtual void setItemRotation(double rotation, bool adjustPosition=true)
Sets the layout item's rotation, in degrees clockwise.
virtual void setVisibility(bool visible)
Sets whether the item is visible.
QgsLayoutPoint positionWithUnits() const
Returns the item's current position, including units.
int page() const
Returns the page the item is currently on, with the first page returning 0.
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
virtual QString uuid() const
Returns the item identification string.
virtual void attemptMove(const QgsLayoutPoint &point, bool useReferencePoint=true, bool includesFrame=false, int page=-1)
Attempts to move the item to a specified point.
QString id() const
Returns the item's ID name.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
@ MustPlaceInOwnLayer
Item must be placed in its own individual layer.
void setParentGroup(QgsLayoutItemGroup *group)
Sets the item's parent group.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QPointer< QgsLayout > mLayout
Provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
static void relativeResizeRect(QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter)
Resizes a QRectF relative to a resized bounding rectangle.
A container for the context for various read/write operations on objects.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).