QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsgroupungroupitemscommand.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgroupungroupitemscommand.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 "qgscomposeritem.h"
20 #include "qgscomposeritemgroup.h"
21 #include "qgscomposition.h"
22 #include "qgsproject.h"
23 #include "qgscomposermodel.h"
24 #include "qgslogger.h"
25 
27  QUndoCommand( text, parent ), mGroup( item ), mComposition( c ), mState( s ), mFirstRun( true )
28 {
29  mItems = mGroup->items();
30 }
31 
33 {
34  if ( mState == Ungrouped )
35  {
36  //command class stores the item if ungrouped from the composition
37  delete mGroup;
38  }
39 }
40 
42 {
43  if ( mFirstRun )
44  {
45  mFirstRun = false;
46  return;
47  }
48  switchState();
49 }
50 
52 {
53  if ( mFirstRun )
54  {
55  mFirstRun = false;
56  return;
57  }
58  switchState();
59 }
60 
61 void QgsGroupUngroupItemsCommand::switchState()
62 {
63  if ( mState == Grouped )
64  {
65  // ungroup
66  if ( mComposition )
67  {
68  // This is probably redundant
69  mComposition->itemsModel()->setItemRemoved( mGroup );
70  mComposition->removeItem( mGroup );
71  }
72  mGroup->removeItems();
73  emit itemRemoved( mGroup );
74  mState = Ungrouped;
75  }
76  else //Ungrouped
77  {
78  // group
79  if ( mComposition )
80  {
81  //delete mGroup; mGroup = new QgsComposerItemGroup( mCompoiser );
82  QSet<QgsComposerItem*>::iterator itemIter = mItems.begin();
83  for ( ; itemIter != mItems.end(); ++itemIter )
84  {
85  mGroup->addItem( *itemIter );
86  QgsDebugMsg( QString( "itemgroup now has %1" ) .arg( mGroup->items().size() ) );
87  }
88  // Add the group
89  mComposition->itemsModel()->setItemRestored( mGroup );
90  mComposition->addItem( mGroup );
91  }
92  mState = Grouped;
93  emit itemAdded( mGroup );
94  }
95  QgsProject::instance()->setDirty( true );
96 }
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:410
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
int size() const
void removeItems() override
Removes the items but does not delete them.
A container for grouping several QgsComposerItems.
void setItemRestored(QgsComposerItem *item)
Restores an item to the composition.
QSet< QgsComposerItem * > items()
void removeItem(QGraphicsItem *item)
void addItem(QgsComposerItem *item) override
Adds an item to the group.
QgsGroupUngroupItemsCommand(State s, QgsComposerItemGroup *item, QgsComposition *c, const QString &text, QUndoCommand *parent=nullptr)
Create a group or ungroup command.
void itemRemoved(QgsComposerItem *item)
Signals removal of an item (the group)
Graphics scene for map printing.
iterator begin()
iterator end()
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
void setItemRemoved(QgsComposerItem *item)
Marks an item as removed from the composition.
void addItem(QGraphicsItem *item)
void itemAdded(QgsComposerItem *item)
Signals addition of an item (the group)