[QGIS Commit] r9780 - trunk/qgis/src/core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Dec 13 05:12:03 EST 2008


Author: jef
Date: 2008-12-13 05:12:02 -0500 (Sat, 13 Dec 2008)
New Revision: 9780

Modified:
   trunk/qgis/src/core/qgsvectorlayer.cpp
   trunk/qgis/src/core/qgsvectorlayer.h
Log:
fix handling of pending vector layer changes

Modified: trunk/qgis/src/core/qgsvectorlayer.cpp
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.cpp	2008-12-13 08:34:45 UTC (rev 9779)
+++ trunk/qgis/src/core/qgsvectorlayer.cpp	2008-12-13 10:12:02 UTC (rev 9780)
@@ -1161,8 +1161,16 @@
       f.changeAttribute( it.key(), it.value() );
   }
 
-  for ( QgsAttributeList::const_iterator it = mFetchNullAttributes.begin(); it != mFetchNullAttributes.end(); it++ )
-    f.changeAttribute( *it, QVariant( QString::null ) );
+  // remove all attributes that will disappear
+  const QgsAttributeMap &map = f.attributeMap();
+  for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); it++ )
+    if( !mUpdatedFields.contains( it.key() ) )
+      f.deleteAttribute( it.key() );
+
+  // null/add all attributes that were added, but don't exist in the feature yet
+  for ( QgsFieldMap::const_iterator it = mUpdatedFields.begin(); it != mUpdatedFields.end(); it++ )
+    if ( !map.contains( it.key() ) )
+      f.changeAttribute( it.key(), QVariant( QString::null ) );
 }
 
 void QgsVectorLayer::updateFeatureGeometry( QgsFeature &f )
@@ -1193,21 +1201,16 @@
   //look in the normal features of the provider
   if ( mFetchAttributes.size() > 0 )
   {
-    mFetchNullAttributes.clear();
-
     if ( mEditable )
     {
       // fetch only available field from provider
       QgsAttributeList provAttributes;
       for ( QgsAttributeList::iterator it = mFetchAttributes.begin(); it != mFetchAttributes.end(); it++ )
       {
-        if ( !mUpdatedFields.contains( *it ) )
+        if ( !mUpdatedFields.contains( *it ) || mAddedAttributeIds.contains( *it ) )
           continue;
 
-        if ( !mDeletedAttributeIds.contains( *it ) && !mAddedAttributeIds.contains( *it ) )
-          provAttributes << *it;
-        else
-          mFetchNullAttributes << *it;
+        provAttributes << *it;
       }
 
       mDataProvider->select( provAttributes, rect, fetchGeometries, useIntersect );
@@ -2437,12 +2440,14 @@
   {
     if ( !myRenderer->writeXML( node, doc, *this ) )
     {
+      errorMessage = "renderer failed to save";
       return false;
     }
   }
   else
   {
     QgsDebugMsg( "no renderer" );
+    errorMessage = "no renderer";
     return false;
   }
 
@@ -2546,6 +2551,7 @@
     return false;
 
   mDeletedAttributeIds.insert( index );
+  mUpdatedFields.remove( index );
 
   setModified( true, false );
 
@@ -2610,42 +2616,42 @@
   int cap = mDataProvider->capabilities();
 
   //
-  // add attributes
+  // delete attributes
   //
   bool attributesChanged = false;
-  if ( mAddedAttributeIds.size() > 0 )
+  if ( mDeletedAttributeIds.size() > 0 )
   {
-    QgsNewAttributesMap addedAttributes;
-    for ( QgsAttributeIds::const_iterator it = mAddedAttributeIds.begin(); it != mAddedAttributeIds.end(); it++ )
-      addedAttributes[ mUpdatedFields[ *it ].name()] = mUpdatedFields[ *it ].typeName();
-
-    if (( cap & QgsVectorDataProvider::AddAttributes ) && mDataProvider->addAttributes( addedAttributes ) )
+    if (( cap & QgsVectorDataProvider::DeleteAttributes ) && mDataProvider->deleteAttributes( mDeletedAttributeIds ) )
     {
-      mCommitErrors << tr( "SUCCESS: %1 attributes added." ).arg( mAddedAttributeIds.size() );
-      mAddedAttributeIds.clear();
+      mCommitErrors << tr( "SUCCESS: %1 attributes deleted." ).arg( mDeletedAttributeIds.size() );
+      mDeletedAttributeIds.clear();
       attributesChanged = true;
     }
     else
     {
-      mCommitErrors << tr( "ERROR: %1 new attributes not added" ).arg( mAddedAttributeIds.size() );
+      mCommitErrors << tr( "ERROR: %1 attributes not deleted." ).arg( mDeletedAttributeIds.size() );
       success = false;
     }
   }
 
   //
-  // delete attributes
+  // add attributes
   //
-  if ( mDeletedAttributeIds.size() > 0 )
+  if ( mAddedAttributeIds.size() > 0 )
   {
-    if (( cap & QgsVectorDataProvider::DeleteAttributes ) && mDataProvider->deleteAttributes( mDeletedAttributeIds ) )
+    QgsNewAttributesMap addedAttributes;
+    for ( QgsAttributeIds::const_iterator it = mAddedAttributeIds.begin(); it != mAddedAttributeIds.end(); it++ )
+      addedAttributes[ mUpdatedFields[ *it ].name() ] = mUpdatedFields[ *it ].typeName();
+
+    if (( cap & QgsVectorDataProvider::AddAttributes ) && mDataProvider->addAttributes( addedAttributes ) )
     {
-      mCommitErrors << tr( "SUCCESS: %1 attributes deleted." ).arg( mDeletedAttributeIds.size() );
-      mDeletedAttributeIds.clear();
+      mCommitErrors << tr( "SUCCESS: %1 attributes added." ).arg( mAddedAttributeIds.size() );
+      mAddedAttributeIds.clear();
       attributesChanged = true;
     }
     else
     {
-      mCommitErrors << tr( "ERROR: %1 attributes not deleted." ).arg( mDeletedAttributeIds.size() );
+      mCommitErrors << tr( "ERROR: %1 new attributes not added" ).arg( mAddedAttributeIds.size() );
       success = false;
     }
   }
@@ -2660,7 +2666,7 @@
     QMap<int, QString> src;
     for ( QgsFieldMap::const_iterator it = mUpdatedFields.begin(); it != mUpdatedFields.end(); it++ )
     {
-      src[ it.key()] = it.value().name();
+      src[ it.key() ] = it.value().name();
     }
 
     int maxAttrIdx = -1;
@@ -2670,7 +2676,7 @@
     QMap<QString, int> dst;
     for ( QgsFieldMap::const_iterator it = pFields.begin(); it != pFields.end(); it++ )
     {
-      dst[ it.value().name()] = it.key();
+      dst[ it.value().name() ] = it.key();
       if ( it.key() > maxAttrIdx )
         maxAttrIdx = it.key();
     }

Modified: trunk/qgis/src/core/qgsvectorlayer.h
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.h	2008-12-13 08:34:45 UTC (rev 9779)
+++ trunk/qgis/src/core/qgsvectorlayer.h	2008-12-13 10:12:02 UTC (rev 9780)
@@ -633,7 +633,6 @@
     bool mFetching;
     QgsRectangle mFetchRect;
     QgsAttributeList mFetchAttributes;
-    QgsAttributeList mFetchNullAttributes;
     bool mFetchGeometry;
 
     QSet<int> mFetchConsidered;



More information about the QGIS-commit mailing list