@@ -1161,8 +1161,16 @@ void QgsVectorLayer::updateFeatureAttributes( QgsFeature &f )
1161
1161
f.changeAttribute ( it.key (), it.value () );
1162
1162
}
1163
1163
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 ) );
1166
1174
}
1167
1175
1168
1176
void QgsVectorLayer::updateFeatureGeometry ( QgsFeature &f )
@@ -1193,21 +1201,16 @@ void QgsVectorLayer::select( QgsAttributeList attributes, QgsRectangle rect, boo
1193
1201
// look in the normal features of the provider
1194
1202
if ( mFetchAttributes .size () > 0 )
1195
1203
{
1196
- mFetchNullAttributes .clear ();
1197
-
1198
1204
if ( mEditable )
1199
1205
{
1200
1206
// fetch only available field from provider
1201
1207
QgsAttributeList provAttributes;
1202
1208
for ( QgsAttributeList::iterator it = mFetchAttributes .begin (); it != mFetchAttributes .end (); it++ )
1203
1209
{
1204
- if ( !mUpdatedFields .contains ( *it ) )
1210
+ if ( !mUpdatedFields .contains ( *it ) || mAddedAttributeIds . contains ( *it ) )
1205
1211
continue ;
1206
1212
1207
- if ( !mDeletedAttributeIds .contains ( *it ) && !mAddedAttributeIds .contains ( *it ) )
1208
- provAttributes << *it;
1209
- else
1210
- mFetchNullAttributes << *it;
1213
+ provAttributes << *it;
1211
1214
}
1212
1215
1213
1216
mDataProvider ->select ( provAttributes, rect, fetchGeometries, useIntersect );
@@ -2437,12 +2440,14 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
2437
2440
{
2438
2441
if ( !myRenderer->writeXML ( node, doc, *this ) )
2439
2442
{
2443
+ errorMessage = " renderer failed to save" ;
2440
2444
return false ;
2441
2445
}
2442
2446
}
2443
2447
else
2444
2448
{
2445
2449
QgsDebugMsg ( " no renderer" );
2450
+ errorMessage = " no renderer" ;
2446
2451
return false ;
2447
2452
}
2448
2453
@@ -2546,6 +2551,7 @@ bool QgsVectorLayer::deleteAttribute( int index )
2546
2551
return false ;
2547
2552
2548
2553
mDeletedAttributeIds .insert ( index );
2554
+ mUpdatedFields .remove ( index );
2549
2555
2550
2556
setModified ( true , false );
2551
2557
@@ -2610,42 +2616,42 @@ bool QgsVectorLayer::commitChanges()
2610
2616
int cap = mDataProvider ->capabilities ();
2611
2617
2612
2618
//
2613
- // add attributes
2619
+ // delete attributes
2614
2620
//
2615
2621
bool attributesChanged = false ;
2616
- if ( mAddedAttributeIds .size () > 0 )
2622
+ if ( mDeletedAttributeIds .size () > 0 )
2617
2623
{
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 ) )
2623
2625
{
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 ();
2626
2628
attributesChanged = true ;
2627
2629
}
2628
2630
else
2629
2631
{
2630
- mCommitErrors << tr ( " ERROR: %1 new attributes not added " ).arg ( mAddedAttributeIds .size () );
2632
+ mCommitErrors << tr ( " ERROR: %1 attributes not deleted. " ).arg ( mDeletedAttributeIds .size () );
2631
2633
success = false ;
2632
2634
}
2633
2635
}
2634
2636
2635
2637
//
2636
- // delete attributes
2638
+ // add attributes
2637
2639
//
2638
- if ( mDeletedAttributeIds .size () > 0 )
2640
+ if ( mAddedAttributeIds .size () > 0 )
2639
2641
{
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 ) )
2641
2647
{
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 ();
2644
2650
attributesChanged = true ;
2645
2651
}
2646
2652
else
2647
2653
{
2648
- mCommitErrors << tr ( " ERROR: %1 attributes not deleted. " ).arg ( mDeletedAttributeIds .size () );
2654
+ mCommitErrors << tr ( " ERROR: %1 new attributes not added " ).arg ( mAddedAttributeIds .size () );
2649
2655
success = false ;
2650
2656
}
2651
2657
}
@@ -2660,7 +2666,7 @@ bool QgsVectorLayer::commitChanges()
2660
2666
QMap<int , QString> src;
2661
2667
for ( QgsFieldMap::const_iterator it = mUpdatedFields .begin (); it != mUpdatedFields .end (); it++ )
2662
2668
{
2663
- src[ it.key ()] = it.value ().name ();
2669
+ src[ it.key () ] = it.value ().name ();
2664
2670
}
2665
2671
2666
2672
int maxAttrIdx = -1 ;
@@ -2670,7 +2676,7 @@ bool QgsVectorLayer::commitChanges()
2670
2676
QMap<QString, int > dst;
2671
2677
for ( QgsFieldMap::const_iterator it = pFields.begin (); it != pFields.end (); it++ )
2672
2678
{
2673
- dst[ it.value ().name ()] = it.key ();
2679
+ dst[ it.value ().name () ] = it.key ();
2674
2680
if ( it.key () > maxAttrIdx )
2675
2681
maxAttrIdx = it.key ();
2676
2682
}
0 commit comments