QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgslayoutitemgroupundocommand.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemgroupundocommand.cpp
3 ---------------------------
4 begin : 2016-06-09
5 copyright : (C) 2016 by Sandro Santilli
6 email : strk at kbt dot io
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "moc_qgslayoutitemgroupundocommand.cpp"
20#include "qgslayoutitemgroup.h"
21#include "qgslayout.h"
22#include "qgsproject.h"
23
25QgsLayoutItemGroupUndoCommand::QgsLayoutItemGroupUndoCommand( State s, QgsLayoutItemGroup *group, QgsLayout *layout, const QString &text, QUndoCommand *parent )
26 : QUndoCommand( text, parent )
27 , mGroupUuid( group->uuid() )
28 , mLayout( layout )
29 , mState( s )
30{
31 const QList< QgsLayoutItem * > items = group->items();
32 for ( QgsLayoutItem *i : items )
33 {
34 mItemUuids.insert( i->uuid() );
35 }
36}
37
38void QgsLayoutItemGroupUndoCommand::redo()
39{
40 if ( mFirstRun )
41 {
42 mFirstRun = false;
43 return;
44 }
45 switchState();
46}
47
48void QgsLayoutItemGroupUndoCommand::undo()
49{
50 if ( mFirstRun )
51 {
52 mFirstRun = false;
53 return;
54 }
55 switchState();
56}
57
58void QgsLayoutItemGroupUndoCommand::switchState()
59{
60 if ( mState == Grouped )
61 {
62 // ungroup
63 QgsLayoutItemGroup *group = dynamic_cast< QgsLayoutItemGroup * >( mLayout->itemByUuid( mGroupUuid ) );
64 Q_ASSERT_X( group, "QgsLayoutItemGroupUndoCommand::switchState", "Could not find group" );
65 group->removeItems();
66 mLayout->removeLayoutItemPrivate( group );
67 mState = Ungrouped;
68 }
69 else //Ungrouped
70 {
71 // find group by uuid...
72 QgsLayoutItemGroup *group = dynamic_cast< QgsLayoutItemGroup * >( mLayout->itemByUuid( mGroupUuid ) );
73 if ( !group )
74 {
75 group = new QgsLayoutItemGroup( mLayout );
76 mLayout->addLayoutItemPrivate( group );
77 }
78
79 for ( const QString &childUuid : std::as_const( mItemUuids ) )
80 {
81 QgsLayoutItem *childItem = mLayout->itemByUuid( childUuid );
82 group->addItem( childItem );
83 }
84
85 mState = Grouped;
86 }
87 mLayout->project()->setDirty( true );
88}
A container for grouping several QgsLayoutItems.
void removeItems()
Removes all items from the group (but does not delete them).
void addItem(QgsLayoutItem *item)
Adds an item to the group.
QList< QgsLayoutItem * > items() const
Returns a list of items contained by the group.
Base class for graphical items within a QgsLayout.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49