Skip to content

Commit fd7db2e

Browse files
author
jef
committed
fix handling of pending vector layer changes
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9780 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 2074c83 commit fd7db2e

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

src/core/qgsvectorlayer.cpp

+33-27
Original file line numberDiff line numberDiff line change
@@ -1161,8 +1161,16 @@ void QgsVectorLayer::updateFeatureAttributes( QgsFeature &f )
11611161
f.changeAttribute( it.key(), it.value() );
11621162
}
11631163

1164-
for ( QgsAttributeList::const_iterator it = mFetchNullAttributes.begin(); it != mFetchNullAttributes.end(); it++ )
1165-
f.changeAttribute( *it, QVariant( QString::null ) );
1164+
// remove all attributes that will disappear
1165+
const QgsAttributeMap &map = f.attributeMap();
1166+
for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); it++ )
1167+
if( !mUpdatedFields.contains( it.key() ) )
1168+
f.deleteAttribute( it.key() );
1169+
1170+
// null/add all attributes that were added, but don't exist in the feature yet
1171+
for ( QgsFieldMap::const_iterator it = mUpdatedFields.begin(); it != mUpdatedFields.end(); it++ )
1172+
if ( !map.contains( it.key() ) )
1173+
f.changeAttribute( it.key(), QVariant( QString::null ) );
11661174
}
11671175

11681176
void QgsVectorLayer::updateFeatureGeometry( QgsFeature &f )
@@ -1193,21 +1201,16 @@ void QgsVectorLayer::select( QgsAttributeList attributes, QgsRectangle rect, boo
11931201
//look in the normal features of the provider
11941202
if ( mFetchAttributes.size() > 0 )
11951203
{
1196-
mFetchNullAttributes.clear();
1197-
11981204
if ( mEditable )
11991205
{
12001206
// fetch only available field from provider
12011207
QgsAttributeList provAttributes;
12021208
for ( QgsAttributeList::iterator it = mFetchAttributes.begin(); it != mFetchAttributes.end(); it++ )
12031209
{
1204-
if ( !mUpdatedFields.contains( *it ) )
1210+
if ( !mUpdatedFields.contains( *it ) || mAddedAttributeIds.contains( *it ) )
12051211
continue;
12061212

1207-
if ( !mDeletedAttributeIds.contains( *it ) && !mAddedAttributeIds.contains( *it ) )
1208-
provAttributes << *it;
1209-
else
1210-
mFetchNullAttributes << *it;
1213+
provAttributes << *it;
12111214
}
12121215

12131216
mDataProvider->select( provAttributes, rect, fetchGeometries, useIntersect );
@@ -2437,12 +2440,14 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
24372440
{
24382441
if ( !myRenderer->writeXML( node, doc, *this ) )
24392442
{
2443+
errorMessage = "renderer failed to save";
24402444
return false;
24412445
}
24422446
}
24432447
else
24442448
{
24452449
QgsDebugMsg( "no renderer" );
2450+
errorMessage = "no renderer";
24462451
return false;
24472452
}
24482453

@@ -2546,6 +2551,7 @@ bool QgsVectorLayer::deleteAttribute( int index )
25462551
return false;
25472552

25482553
mDeletedAttributeIds.insert( index );
2554+
mUpdatedFields.remove( index );
25492555

25502556
setModified( true, false );
25512557

@@ -2610,42 +2616,42 @@ bool QgsVectorLayer::commitChanges()
26102616
int cap = mDataProvider->capabilities();
26112617

26122618
//
2613-
// add attributes
2619+
// delete attributes
26142620
//
26152621
bool attributesChanged = false;
2616-
if ( mAddedAttributeIds.size() > 0 )
2622+
if ( mDeletedAttributeIds.size() > 0 )
26172623
{
2618-
QgsNewAttributesMap addedAttributes;
2619-
for ( QgsAttributeIds::const_iterator it = mAddedAttributeIds.begin(); it != mAddedAttributeIds.end(); it++ )
2620-
addedAttributes[ mUpdatedFields[ *it ].name()] = mUpdatedFields[ *it ].typeName();
2621-
2622-
if (( cap & QgsVectorDataProvider::AddAttributes ) && mDataProvider->addAttributes( addedAttributes ) )
2624+
if (( cap & QgsVectorDataProvider::DeleteAttributes ) && mDataProvider->deleteAttributes( mDeletedAttributeIds ) )
26232625
{
2624-
mCommitErrors << tr( "SUCCESS: %1 attributes added." ).arg( mAddedAttributeIds.size() );
2625-
mAddedAttributeIds.clear();
2626+
mCommitErrors << tr( "SUCCESS: %1 attributes deleted." ).arg( mDeletedAttributeIds.size() );
2627+
mDeletedAttributeIds.clear();
26262628
attributesChanged = true;
26272629
}
26282630
else
26292631
{
2630-
mCommitErrors << tr( "ERROR: %1 new attributes not added" ).arg( mAddedAttributeIds.size() );
2632+
mCommitErrors << tr( "ERROR: %1 attributes not deleted." ).arg( mDeletedAttributeIds.size() );
26312633
success = false;
26322634
}
26332635
}
26342636

26352637
//
2636-
// delete attributes
2638+
// add attributes
26372639
//
2638-
if ( mDeletedAttributeIds.size() > 0 )
2640+
if ( mAddedAttributeIds.size() > 0 )
26392641
{
2640-
if (( cap & QgsVectorDataProvider::DeleteAttributes ) && mDataProvider->deleteAttributes( mDeletedAttributeIds ) )
2642+
QgsNewAttributesMap addedAttributes;
2643+
for ( QgsAttributeIds::const_iterator it = mAddedAttributeIds.begin(); it != mAddedAttributeIds.end(); it++ )
2644+
addedAttributes[ mUpdatedFields[ *it ].name() ] = mUpdatedFields[ *it ].typeName();
2645+
2646+
if (( cap & QgsVectorDataProvider::AddAttributes ) && mDataProvider->addAttributes( addedAttributes ) )
26412647
{
2642-
mCommitErrors << tr( "SUCCESS: %1 attributes deleted." ).arg( mDeletedAttributeIds.size() );
2643-
mDeletedAttributeIds.clear();
2648+
mCommitErrors << tr( "SUCCESS: %1 attributes added." ).arg( mAddedAttributeIds.size() );
2649+
mAddedAttributeIds.clear();
26442650
attributesChanged = true;
26452651
}
26462652
else
26472653
{
2648-
mCommitErrors << tr( "ERROR: %1 attributes not deleted." ).arg( mDeletedAttributeIds.size() );
2654+
mCommitErrors << tr( "ERROR: %1 new attributes not added" ).arg( mAddedAttributeIds.size() );
26492655
success = false;
26502656
}
26512657
}
@@ -2660,7 +2666,7 @@ bool QgsVectorLayer::commitChanges()
26602666
QMap<int, QString> src;
26612667
for ( QgsFieldMap::const_iterator it = mUpdatedFields.begin(); it != mUpdatedFields.end(); it++ )
26622668
{
2663-
src[ it.key()] = it.value().name();
2669+
src[ it.key() ] = it.value().name();
26642670
}
26652671

26662672
int maxAttrIdx = -1;
@@ -2670,7 +2676,7 @@ bool QgsVectorLayer::commitChanges()
26702676
QMap<QString, int> dst;
26712677
for ( QgsFieldMap::const_iterator it = pFields.begin(); it != pFields.end(); it++ )
26722678
{
2673-
dst[ it.value().name()] = it.key();
2679+
dst[ it.value().name() ] = it.key();
26742680
if ( it.key() > maxAttrIdx )
26752681
maxAttrIdx = it.key();
26762682
}

src/core/qgsvectorlayer.h

-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
633633
bool mFetching;
634634
QgsRectangle mFetchRect;
635635
QgsAttributeList mFetchAttributes;
636-
QgsAttributeList mFetchNullAttributes;
637636
bool mFetchGeometry;
638637

639638
QSet<int> mFetchConsidered;

0 commit comments

Comments
 (0)