25#include "moc_qgslayoutitemgroup.cpp"
46 mLayout->removeLayoutItem( item );
65 if ( !
id().isEmpty() )
69 return tr(
"<Group>" );
84 if ( mItems.contains( item ) )
89 mItems << QPointer< QgsLayoutItem >( item );
102 item->setParentGroup(
nullptr );
109 QList<QgsLayoutItem *> val;
121 if ( !shouldBlockUndoCommands() )
122 mLayout->undoStack()->beginMacro( tr(
"Set Group Visibility" ) );
128 bool prev = item->mBlockUndoCommands;
129 item->mBlockUndoCommands = mBlockUndoCommands;
130 item->setVisibility( visible );
131 item->mBlockUndoCommands = prev;
136 if ( !shouldBlockUndoCommands() )
137 mLayout->undoStack()->endMacro();
142 Q_UNUSED( useReferencePoint )
146 if ( !shouldBlockUndoCommands() )
147 mLayout->undoStack()->beginMacro( tr(
"Move group" ) );
151 scenePoint =
mLayout->convertToLayoutUnits( point );
153 scenePoint =
mLayout->pageCollection()->pagePositionToLayoutPosition(
page, point );
155 double deltaX = scenePoint.x() - pos().x();
156 double deltaY = scenePoint.y() - pos().y();
164 std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
165 if ( !shouldBlockUndoCommands() )
168 command->saveBeforeState();
171 item->attemptMoveBy( deltaX, deltaY );
175 command->saveAfterState();
176 mLayout->undoStack()->push( command.release() );
181 if ( !shouldBlockUndoCommands() )
182 mLayout->undoStack()->endMacro();
183 updateBoundingRect();
191 if ( !shouldBlockUndoCommands() )
192 mLayout->undoStack()->beginMacro( tr(
"Resize Group" ) );
194 QRectF oldRect = rect();
195 QSizeF newSizeLayoutUnits =
mLayout->convertToLayoutUnits( size );
197 newRect.setSize( newSizeLayoutUnits );
205 std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
206 if ( !shouldBlockUndoCommands() )
209 command->saveBeforeState();
212 const QRectF originalItemRect = item->rect();
213 QRectF itemRect = mapRectFromItem( item, originalItemRect );
216 itemRect = itemRect.normalized();
217 QPointF newPos = mapToScene( itemRect.topLeft() );
219 QgsLayoutSize itemSize =
mLayout->convertFromLayoutUnits( itemRect.size(), item->sizeWithUnits().units() );
220 item->attemptResize( itemSize, includesFrame );
223 QgsLayoutPoint itemPos =
mLayout->convertFromLayoutUnits( newPos, item->positionWithUnits().units() );
224 item->attemptMove( itemPos,
false );
228 command->saveAfterState();
229 mLayout->undoStack()->push( command.release() );
233 if ( !shouldBlockUndoCommands() )
234 mLayout->undoStack()->endMacro();
236 updateBoundingRect();
246 QDomElement childItem = document.createElement( QStringLiteral(
"ComposerItemGroupElement" ) );
247 childItem.setAttribute( QStringLiteral(
"uuid" ), item->uuid() );
248 element.appendChild( childItem );
257 QDomNodeList elementNodes = itemElement.elementsByTagName( QStringLiteral(
"ComposerItemGroupElement" ) );
258 for (
int i = 0; i < elementNodes.count(); ++i )
260 QDomNode elementNode = elementNodes.at( i );
261 if ( !elementNode.isElement() )
264 QString
uuid = elementNode.toElement().attribute( QStringLiteral(
"uuid" ) );
272 for (
const QString &
uuid : std::as_const( mItemUuids ) )
281 updateBoundingRect();
299void QgsLayoutItemGroup::updateBoundingRect()
302 if ( mItems.isEmpty() )
309 auto itemIter = mItems.constBegin();
312 double rotation = ( *itemIter )->rotation();
315 for ( ++itemIter; itemIter != mItems.constEnd(); ++itemIter )
324 setScenePos( QPointF( 0, 0 ) );
327 itemIter = mItems.constBegin();
330 QRectF groupRect = mapFromItem( ( *itemIter ), ( *itemIter )->rect() ).boundingRect();
331 QRectF groupRectWithFrame = mapFromItem( ( *itemIter ), ( *itemIter )->rectWithFrame() ).boundingRect();
334 for ( ++itemIter; itemIter != mItems.constEnd(); ++itemIter )
336 groupRect |= mapFromItem( ( *itemIter ), ( *itemIter )->rect() ).boundingRect();
337 groupRectWithFrame |= mapFromItem( ( *itemIter ), ( *itemIter )->rectWithFrame() ).boundingRect();
342 setRect( 0, 0, groupRect.width(), groupRect.height() );
343 setPos( mapToScene( groupRect.topLeft() ) );
345 QPointF bleedShift = groupRectWithFrame.topLeft() - groupRect.topLeft();
346 mRectWithFrame = QRectF( bleedShift, groupRectWithFrame.size() );
351 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).