111 changes: 34 additions & 77 deletions src/core/qgsvectorlayerundocommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,9 @@ QgsVectorLayerUndoCommandAddFeature::QgsVectorLayerUndoCommandAddFeature( QgsVec

void QgsVectorLayerUndoCommandAddFeature::undo()
{
for (int i = 0; mBuffer->mAddedFeatures.count(); ++i)
{
if (mBuffer->mAddedFeatures[i].id() == mFeature.id())
{
mBuffer->mAddedFeatures.removeAt(i);
break;
}
}
QgsFeatureMap::const_iterator it = mBuffer->mAddedFeatures.find(mFeature.id());
Q_ASSERT( it != mBuffer->mAddedFeatures.end() );
mBuffer->mAddedFeatures.remove(mFeature.id());

if ( mFeature.geometry() )
cache()->removeGeometry(mFeature.id());
Expand All @@ -64,7 +59,7 @@ void QgsVectorLayerUndoCommandAddFeature::undo()

void QgsVectorLayerUndoCommandAddFeature::redo()
{
mBuffer->mAddedFeatures.append( mFeature );
mBuffer->mAddedFeatures.insert( mFeature.id(), mFeature );

if ( mFeature.geometry() )
cache()->cacheGeometry( mFeature.id(), *mFeature.geometry() );
Expand All @@ -81,22 +76,17 @@ QgsVectorLayerUndoCommandDeleteFeature::QgsVectorLayerUndoCommandDeleteFeature(

if ( FID_IS_NEW( mFid ) )
{
for ( int i = 0; i < mBuffer->mAddedFeatures.count(); ++i )
{
if ( mBuffer->mAddedFeatures[i].id() == fid )
{
mOldAddedFeature = mBuffer->mAddedFeatures[i];
break;
}
}
QgsFeatureMap::const_iterator it = mBuffer->mAddedFeatures.find( mFid );
Q_ASSERT( it != mBuffer->mAddedFeatures.end() );
mOldAddedFeature = it.value();
}
}

void QgsVectorLayerUndoCommandDeleteFeature::undo()
{
if ( FID_IS_NEW( mFid ) )
{
mBuffer->mAddedFeatures.append( mOldAddedFeature );
mBuffer->mAddedFeatures.insert( mOldAddedFeature.id(), mOldAddedFeature );
}
else
{
Expand All @@ -110,14 +100,7 @@ void QgsVectorLayerUndoCommandDeleteFeature::redo()
{
if ( FID_IS_NEW( mFid ) )
{
for ( int i = 0; i < mBuffer->mAddedFeatures.count(); ++i )
{
if ( mBuffer->mAddedFeatures[i].id() == mFid )
{
mBuffer->mAddedFeatures.removeAt(i);
break;
}
}
mBuffer->mAddedFeatures.remove( mFid );
}
else
{
Expand All @@ -136,14 +119,9 @@ QgsVectorLayerUndoCommandChangeGeometry::QgsVectorLayerUndoCommandChangeGeometry

if ( FID_IS_NEW( mFid ) )
{
for ( int i = 0; i < mBuffer->mAddedFeatures.count(); ++i )
{
if ( mBuffer->mAddedFeatures[i].id() == mFid )
{
mOldGeom = new QgsGeometry( *mBuffer->mAddedFeatures[i].geometry() );
break;
}
}
QgsFeatureMap::const_iterator it = mBuffer->mAddedFeatures.find( mFid );
Q_ASSERT( it != mBuffer->mAddedFeatures.end() );
mOldGeom = new QgsGeometry( *it.value().geometry() );
}
else
{
Expand All @@ -167,14 +145,10 @@ void QgsVectorLayerUndoCommandChangeGeometry::undo()
if ( FID_IS_NEW( mFid ) )
{
// modify added features
for ( int i = 0; i < mBuffer->mAddedFeatures.count(); ++i )
{
if ( mBuffer->mAddedFeatures[i].id() == mFid )
{
mBuffer->mAddedFeatures[i].setGeometry( *mOldGeom );
break;
}
}
QgsFeatureMap::iterator it = mBuffer->mAddedFeatures.find( mFid );
Q_ASSERT( it != mBuffer->mAddedFeatures.end() );
it.value().setGeometry( *mOldGeom );

cache()->cacheGeometry( mFid, *mOldGeom );
emit mBuffer->geometryChanged( mFid, *mOldGeom );
}
Expand Down Expand Up @@ -208,14 +182,9 @@ void QgsVectorLayerUndoCommandChangeGeometry::redo()
if ( FID_IS_NEW( mFid ) )
{
// modify added features
for ( int i = 0; i < mBuffer->mAddedFeatures.count(); ++i )
{
if ( mBuffer->mAddedFeatures[i].id() == mFid )
{
mBuffer->mAddedFeatures[i].setGeometry( *mNewGeom );
break;
}
}
QgsFeatureMap::iterator it = mBuffer->mAddedFeatures.find( mFid );
Q_ASSERT( it != mBuffer->mAddedFeatures.end() );
it.value().setGeometry( *mNewGeom );
}
else
{
Expand All @@ -240,14 +209,12 @@ QgsVectorLayerUndoCommandChangeAttribute::QgsVectorLayerUndoCommandChangeAttribu
if ( FID_IS_NEW( mFid ) )
{
// work with added feature
for ( int i = 0; i < mBuffer->mAddedFeatures.size(); i++ )
QgsFeatureMap::const_iterator it = mBuffer->mAddedFeatures.find( mFid );
Q_ASSERT( it != mBuffer->mAddedFeatures.end() );
if ( it.value().attribute( mFieldIndex ).isValid() )
{
if ( mBuffer->mAddedFeatures[i].id() == mFid && mBuffer->mAddedFeatures[i].attribute( mFieldIndex ).isValid() )
{
mOldValue = mBuffer->mAddedFeatures[i].attribute( mFieldIndex );
mFirstChange = false;
break;
}
mOldValue = it.value().attribute( mFieldIndex );
mFirstChange = false;
}
}
else
Expand All @@ -268,14 +235,9 @@ void QgsVectorLayerUndoCommandChangeAttribute::undo()
if ( FID_IS_NEW( mFid ) )
{
// added feature
for ( int i = 0; i < mBuffer->mAddedFeatures.size(); i++ )
{
if ( mBuffer->mAddedFeatures[i].id() == mFid )
{
mBuffer->mAddedFeatures[i].setAttribute( mFieldIndex, mOldValue );
break;
}
}
QgsFeatureMap::iterator it = mBuffer->mAddedFeatures.find( mFid );
Q_ASSERT( it != mBuffer->mAddedFeatures.end() );
it.value().setAttribute( mFieldIndex, mOldValue );
}
else
{
Expand Down Expand Up @@ -310,14 +272,9 @@ void QgsVectorLayerUndoCommandChangeAttribute::redo()
if ( FID_IS_NEW( mFid ) )
{
// updated added feature
for ( int i = 0; i < mBuffer->mAddedFeatures.size(); i++ )
{
if ( mBuffer->mAddedFeatures[i].id() == mFid )
{
mBuffer->mAddedFeatures[i].setAttribute( mFieldIndex, mNewValue );
break;
}
}
QgsFeatureMap::iterator it = mBuffer->mAddedFeatures.find( mFid );
Q_ASSERT( it != mBuffer->mAddedFeatures.end() );
it.value().setAttribute( mFieldIndex, mNewValue );
}
else
{
Expand Down Expand Up @@ -385,9 +342,9 @@ QgsVectorLayerUndoCommandDeleteAttribute::QgsVectorLayerUndoCommandDeleteAttribu
}

// save values of new features
for (int i = 0; i < mBuffer->mAddedFeatures.count(); ++i)
for (QgsFeatureMap::const_iterator it = mBuffer->mAddedFeatures.begin(); it != mBuffer->mAddedFeatures.end(); ++it)
{
const QgsFeature& f = mBuffer->mAddedFeatures[i];
const QgsFeature& f = it.value();
mDeletedValues.insert(f.id(), f.attribute(mFieldIndex));
}

Expand Down Expand Up @@ -416,9 +373,9 @@ void QgsVectorLayerUndoCommandDeleteAttribute::undo()
mBuffer->handleAttributeAdded( mFieldIndex ); // update changed attributes + new features

// set previously used attributes of new features
for (int i = 0; i < mBuffer->mAddedFeatures.count(); ++i)
for (QgsFeatureMap::iterator it = mBuffer->mAddedFeatures.begin(); it != mBuffer->mAddedFeatures.end(); ++it)
{
QgsFeature& f = mBuffer->mAddedFeatures[i];
QgsFeature& f = it.value();
f.setAttribute( mFieldIndex, mDeletedValues.value(f.id()) );
}
// set previously used changed attributes
Expand Down
6 changes: 2 additions & 4 deletions tests/src/python/test_qgsvectorlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def dumpEditBuffer(layer):
print "NO EDITING!"
return
print "ADDED:"
for f in editBuffer.addedFeatures(): print "%d: %s | %s" % (f.id(), formatAttributes(f.attributes()), f.geometry().exportToWkt())
for fid,f in editBuffer.addedFeatures().iteritems(): print "%d: %s | %s" % (f.id(), formatAttributes(f.attributes()), f.geometry().exportToWkt())
print "CHANGED GEOM:"
for fid,geom in editBuffer.changedGeometries().iteritems(): print "%d | %s" % (f.id(), f.geometry().exportToWkt())

Expand Down Expand Up @@ -136,7 +136,7 @@ def checkBefore():
checkBefore()
layer.undoStack().redo()
checkAfter()

assert layer.commitChanges()

checkAfter()
Expand Down Expand Up @@ -232,7 +232,6 @@ def checkAfter2():
fid = feat.id()
assert layer.deleteFeature(fid)
checkAfter2()
dumpEditBuffer(layer)

# now try undo/redo
layer.undoStack().undo()
Expand All @@ -244,7 +243,6 @@ def checkAfter2():
layer.undoStack().redo()
checkAfter2()

dumpEditBuffer(layer)
assert layer.commitChanges()
checkAfter2()

Expand Down