QGIS API Documentation  2.14.0-Essen
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  if ( mFirstRun )
40  {
41  mFirstRun = false;
42  return;
43  }
44  switchState();
45 }
46 
48 {
49  if ( mFirstRun )
50  {
51  mFirstRun = false;
52  return;
53  }
54  switchState();
55 }
56 
57 void QgsAddRemoveItemCommand::switchState()
58 {
59  if ( mState == Added )
60  {
61  if ( mComposition )
62  {
63  mComposition->itemsModel()->setItemRemoved( mItem );
64  mComposition->removeItem( mItem );
65  }
66  emit itemRemoved( mItem );
67  mState = Removed;
68  }
69  else //Removed
70  {
71  if ( mComposition )
72  {
73  mComposition->itemsModel()->setItemRestored( mItem );
74  mComposition->addItem( mItem );
75  }
76  emit itemAdded( mItem );
77  mState = Added;
78  }
79  QgsProject::instance()->dirty( true );
80 }
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)
Graphics scene for map printing.
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:381
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)
void dirty(bool b)
Definition: qgsproject.cpp:410