Quantum GIS API Documentation  master-693a1fe
src/core/composer/qgsaddremoveitemcommand.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           qgsaddremoveitemcommand.cpp
00003                           ---------------------------
00004     begin                : 2010-11-27
00005     copyright            : (C) 2010 by Marco Hugentobler
00006     email                : marco dot hugentobler at sourcepole dot ch
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgsaddremoveitemcommand.h"
00019 #include "qgscomposeritem.h"
00020 #include "qgscomposition.h"
00021 
00022 QgsAddRemoveItemCommand::QgsAddRemoveItemCommand( State s, QgsComposerItem* item, QgsComposition* c, const QString& text, QUndoCommand* parent ):
00023     QUndoCommand( text, parent ), mItem( item ), mComposition( c ), mState( s ), mFirstRun( true )
00024 {
00025 }
00026 
00027 QgsAddRemoveItemCommand::~QgsAddRemoveItemCommand()
00028 {
00029   if ( mState == Removed ) //command class stores the item if removed from the composition
00030   {
00031     delete mItem;
00032   }
00033 }
00034 
00035 void QgsAddRemoveItemCommand::redo()
00036 {
00037   if ( mFirstRun )
00038   {
00039     mFirstRun = false;
00040     return;
00041   }
00042   switchState();
00043 }
00044 
00045 void QgsAddRemoveItemCommand::undo()
00046 {
00047   if ( mFirstRun )
00048   {
00049     mFirstRun = false;
00050     return;
00051   }
00052   switchState();
00053 }
00054 
00055 void QgsAddRemoveItemCommand::switchState()
00056 {
00057   if ( mState == Added )
00058   {
00059     if ( mComposition )
00060     {
00061       mComposition->removeItem( mItem );
00062     }
00063     emit itemRemoved( mItem );
00064     mState = Removed;
00065   }
00066   else //Removed
00067   {
00068     if ( mComposition )
00069     {
00070       mComposition->addItem( mItem );
00071     }
00072     emit itemAdded( mItem );
00073     mState = Added;
00074   }
00075 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines