QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsaddremoveitemcommand.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsaddremoveitemcommand.cpp
3  ---------------------------
4  begin : 2010-11-27
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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 "qgscomposition.h"
21 #include "qgsproject.h"
22 #include "qgscomposermodel.h"
23 
25  QUndoCommand( text, parent ), mItem( item ), mComposition( c ), mState( s ), mFirstRun( true )
26 {
27 }
28 
30 {
31  if ( mState == Removed ) //command class stores the item if removed from the composition
32  {
33  delete mItem;
34  }
35 }
36 
38 {
39  QUndoCommand::redo(); // call redo() on all childs
40  if ( mFirstRun )
41  {
42  mFirstRun = false;
43  return;
44  }
45  switchState();
46 }
47 
49 {
50  QUndoCommand::undo(); // call undo() on all childs, in reverse order
51  if ( mFirstRun )
52  {
53  mFirstRun = false;
54  return;
55  }
56  switchState();
57 }
58 
59 void QgsAddRemoveItemCommand::switchState()
60 {
61  if ( mState == Added )
62  {
63  // Remove
64  if ( mComposition )
65  {
66  mComposition->itemsModel()->setItemRemoved( mItem );
67  mComposition->removeItem( mItem );
68  }
69  emit itemRemoved( mItem );
70  mState = Removed;
71  }
72  else //Removed
73  {
74  // Add
75  if ( mComposition )
76  {
77  mComposition->itemsModel()->setItemRestored( mItem );
78  mComposition->addItem( mItem );
79  }
80  emit itemAdded( mItem );
81  mState = Added;
82  }
83  QgsProject::instance()->setDirty( true );
84 }
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.
A item that forms part of a map composition.
void setItemRestored(QgsComposerItem *item)
Restores an item to the composition.
void itemAdded(QgsComposerItem *item)
void removeItem(QGraphicsItem *item)
virtual void redo()
Graphics scene for map printing.
virtual void undo()
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
QgsAddRemoveItemCommand(State s, QgsComposerItem *item, QgsComposition *c, const QString &text, QUndoCommand *parent=nullptr)
void setItemRemoved(QgsComposerItem *item)
Marks an item as removed from the composition.
void addItem(QGraphicsItem *item)
void itemRemoved(QgsComposerItem *item)