QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
20#include "qgslayout.h"
21#include "qgslayoutitemgroup.h"
22#include "qgsproject.h"
23
24#include "moc_qgslayoutitemgroupundocommand.cpp"
25
27QgsLayoutItemGroupUndoCommand::QgsLayoutItemGroupUndoCommand( State s, QgsLayoutItemGroup *group, QgsLayout *layout, const QString &text, QUndoCommand *parent )
28 : QUndoCommand( text, parent )
29 , mGroupUuid( group->uuid() )
30 , mLayout( layout )
31 , mState( s )
32{
33 const QList< QgsLayoutItem * > items = group->items();
34 for ( QgsLayoutItem *i : items )
35 {
36 mItemUuids.insert( i->uuid() );
37 }
38}
39
40void QgsLayoutItemGroupUndoCommand::redo()
41{
42 if ( mFirstRun )
43 {
44 mFirstRun = false;
45 return;
46 }
47 switchState();
48}
49
50void QgsLayoutItemGroupUndoCommand::undo()
51{
52 if ( mFirstRun )
53 {
54 mFirstRun = false;
55 return;
56 }
57 switchState();
58}
59
60void QgsLayoutItemGroupUndoCommand::switchState()
61{
62 if ( mState == Grouped )
63 {
64 // ungroup
65 QgsLayoutItemGroup *group = dynamic_cast< QgsLayoutItemGroup * >( mLayout->itemByUuid( mGroupUuid ) );
66 Q_ASSERT_X( group, "QgsLayoutItemGroupUndoCommand::switchState", "Could not find group" );
67 group->removeItems();
68 mLayout->removeLayoutItemPrivate( group );
69 mState = Ungrouped;
70 }
71 else //Ungrouped
72 {
73 // find group by uuid...
74 QgsLayoutItemGroup *group = dynamic_cast< QgsLayoutItemGroup * >( mLayout->itemByUuid( mGroupUuid ) );
75 if ( !group )
76 {
77 group = new QgsLayoutItemGroup( mLayout );
78 mLayout->addLayoutItemPrivate( group );
79 }
80
81 for ( const QString &childUuid : std::as_const( mItemUuids ) )
82 {
83 QgsLayoutItem *childItem = mLayout->itemByUuid( childUuid );
84 group->addItem( childItem );
85 }
86
87 mState = Grouped;
88 }
89 mLayout->project()->setDirty( true );
90}
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:50