Quantum GIS API Documentation  1.7.4
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 
00021 QgsAddRemoveItemCommand::QgsAddRemoveItemCommand( State s, QgsComposerItem* item, QgsComposition* c, const QString& text, QUndoCommand* parent ):
00022     QUndoCommand( text, parent ), mItem( item ), mComposition( c ), mState( s ), mFirstRun( true )
00023 {
00024 }
00025 
00026 QgsAddRemoveItemCommand::~QgsAddRemoveItemCommand()
00027 {
00028   if ( mState == Removed ) //command class stores the item if removed from the composition
00029   {
00030     delete mItem;
00031   }
00032 }
00033 
00034 void QgsAddRemoveItemCommand::redo()
00035 {
00036   if ( mFirstRun )
00037   {
00038     mFirstRun = false;
00039     return;
00040   }
00041   switchState();
00042 }
00043 
00044 void QgsAddRemoveItemCommand::undo()
00045 {
00046   if ( mFirstRun )
00047   {
00048     mFirstRun = false;
00049     return;
00050   }
00051   switchState();
00052 }
00053 
00054 void QgsAddRemoveItemCommand::switchState()
00055 {
00056   if ( mState == Added )
00057   {
00058     if ( mComposition )
00059     {
00060       mComposition->removeItem( mItem );
00061     }
00062     emit itemRemoved( mItem );
00063     mState = Removed;
00064   }
00065   else //Removed
00066   {
00067     if ( mComposition )
00068     {
00069       mComposition->addItem( mItem );
00070     }
00071     emit itemAdded( mItem );
00072     mState = Added;
00073   }
00074 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines