|
QGIS API Documentation
master-59fd5e0
|
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 #include "qgsproject.h" 00022 00023 QgsAddRemoveItemCommand::QgsAddRemoveItemCommand( State s, QgsComposerItem* item, QgsComposition* c, const QString& text, QUndoCommand* parent ): 00024 QUndoCommand( text, parent ), mItem( item ), mComposition( c ), mState( s ), mFirstRun( true ) 00025 { 00026 } 00027 00028 QgsAddRemoveItemCommand::~QgsAddRemoveItemCommand() 00029 { 00030 if ( mState == Removed ) //command class stores the item if removed from the composition 00031 { 00032 delete mItem; 00033 } 00034 } 00035 00036 void QgsAddRemoveItemCommand::redo() 00037 { 00038 if ( mFirstRun ) 00039 { 00040 mFirstRun = false; 00041 return; 00042 } 00043 switchState(); 00044 } 00045 00046 void QgsAddRemoveItemCommand::undo() 00047 { 00048 if ( mFirstRun ) 00049 { 00050 mFirstRun = false; 00051 return; 00052 } 00053 switchState(); 00054 } 00055 00056 void QgsAddRemoveItemCommand::switchState() 00057 { 00058 if ( mState == Added ) 00059 { 00060 if ( mComposition ) 00061 { 00062 mComposition->removeItem( mItem ); 00063 } 00064 emit itemRemoved( mItem ); 00065 mState = Removed; 00066 } 00067 else //Removed 00068 { 00069 if ( mComposition ) 00070 { 00071 mComposition->addItem( mItem ); 00072 } 00073 emit itemAdded( mItem ); 00074 mState = Added; 00075 } 00076 QgsProject::instance()->dirty( true ); 00077 }