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