Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgsvectorlayerundocommand.cpp 00003 --------------------- 00004 begin : June 2009 00005 copyright : (C) 2009 by Martin Dobias 00006 email : wonder.sk at gmail.com 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 00016 #include "qgsvectorlayerundocommand.h" 00017 00018 #include "qgsgeometry.h" 00019 #include "qgsvectorlayer.h" 00020 00021 #include "qgslogger.h" 00022 00023 QgsUndoCommand::GeometryChangeEntry::GeometryChangeEntry() 00024 : original( NULL ), target( NULL ) 00025 { 00026 } 00027 00028 void QgsUndoCommand::GeometryChangeEntry::setOriginalGeometry( QgsGeometry& orig ) 00029 { 00030 if ( orig.type() != QGis::UnknownGeometry ) 00031 { 00032 original = new QgsGeometry( orig ); 00033 } 00034 else 00035 { 00036 original = NULL; 00037 } 00038 } 00039 00040 void QgsUndoCommand::GeometryChangeEntry::setTargetGeometry( QgsGeometry& dest ) 00041 { 00042 if ( dest.type() != QGis::UnknownGeometry ) 00043 { 00044 target = new QgsGeometry( dest ); 00045 } 00046 else 00047 { 00048 target = NULL; 00049 } 00050 } 00051 00052 00053 QgsUndoCommand::GeometryChangeEntry::~GeometryChangeEntry() 00054 { 00055 delete original; 00056 delete target; 00057 } 00058 00059 00060 QgsUndoCommand::QgsUndoCommand( QgsVectorLayer* vlayer, QString text ) 00061 : QUndoCommand() 00062 { 00063 setText( text ); 00064 mLayer = vlayer; 00065 mFirstRun = true; 00066 } 00067 00068 void QgsUndoCommand::redo() 00069 { 00070 // when the command is added to the undo stack, the redo() function is called 00071 // but we have already done the changes in the layer, so we ignore the first redo() call 00072 if ( mFirstRun ) 00073 { 00074 mFirstRun = false; 00075 return; 00076 } 00077 00078 mLayer->redoEditCommand( this ); 00079 } 00080 00081 void QgsUndoCommand::undo() 00082 { 00083 mLayer->undoEditCommand( this ); 00084 } 00085 00086 00087 void QgsUndoCommand::storeGeometryChange( QgsFeatureId featureId, QgsGeometry& original, QgsGeometry& target ) 00088 { 00089 if ( mGeometryChange.contains( featureId ) ) 00090 { 00091 // geometry has been modified already: just alter the resulting geometry 00092 mGeometryChange[featureId].setTargetGeometry( target ); 00093 } 00094 else 00095 { 00096 // create new entry about changed geometry 00097 mGeometryChange.insert( featureId, GeometryChangeEntry() ); 00098 mGeometryChange[featureId].setOriginalGeometry( original ); 00099 mGeometryChange[featureId].setTargetGeometry( target ); 00100 } 00101 } 00102 00103 void QgsUndoCommand::storeAttributeChange( QgsFeatureId featureId, int field, QVariant original, QVariant target, bool isFirstChange ) 00104 { 00105 AttributeChangeEntry entry; 00106 entry.isFirstChange = isFirstChange; 00107 entry.original = original; 00108 entry.target = target; 00109 mAttributeChange[featureId].insert( field, entry ); 00110 } 00111 00112 void QgsUndoCommand::storeAttributeAdd( int index, const QgsField & value ) 00113 { 00114 mAddedAttributes.insert( index, value ); 00115 } 00116 00117 void QgsUndoCommand::storeAttributeDelete( int index, const QgsField & orig ) 00118 { 00119 mDeletedAttributes.insert( index, orig ); 00120 } 00121 00122 void QgsUndoCommand::storeFeatureDelete( QgsFeatureId featureId ) 00123 { 00124 mDeletedFeatureIdChange.insert( featureId ); 00125 } 00126 00127 void QgsUndoCommand::storeFeatureAdd( QgsFeature& feature ) 00128 { 00129 mAddedFeatures.append( feature ); 00130 }