[QGIS Commit] r11903 - branches/symbology-ng-branch/src/core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Tue Nov 3 08:43:04 EST 2009


Author: wonder
Date: 2009-11-03 08:43:02 -0500 (Tue, 03 Nov 2009)
New Revision: 11903

Modified:
   branches/symbology-ng-branch/src/core/qgsvectorlayer.cpp
Log:
Fixed loading and saving of layer styles


Modified: branches/symbology-ng-branch/src/core/qgsvectorlayer.cpp
===================================================================
--- branches/symbology-ng-branch/src/core/qgsvectorlayer.cpp	2009-11-03 07:05:36 UTC (rev 11902)
+++ branches/symbology-ng-branch/src/core/qgsvectorlayer.cpp	2009-11-03 13:43:02 UTC (rev 11903)
@@ -123,33 +123,36 @@
     // Always set crs
     setCoordinateSystem();
 
-    // check if there is a default style / propertysheet defined
-    // for this layer and if so apply it
-    //
     QSettings settings;
     if ( settings.value( "/qgis/use_symbology_ng", false ).toBool() )
     {
       // using symbology-ng!
       setUsingRendererV2(true);
-      setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
     }
-    else if ( loadDefaultStyleFlag )
+
+    // check if there is a default style / propertysheet defined
+    // for this layer and if so apply it
+    bool defaultLoadedFlag = false;
+    if ( loadDefaultStyleFlag )
     {
-      bool defaultLoadedFlag = false;
       loadDefaultStyle( defaultLoadedFlag );
-      if ( !defaultLoadedFlag )
+    }
+
+    // if the default style failed to load or was disabled use some very basic defaults
+    if ( !defaultLoadedFlag )
+    {
+      // add single symbol renderer
+      if (mUsingRendererV2)
       {
-        // add single symbol renderer as default
+        setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
+      }
+      else
+      {
         QgsSingleSymbolRenderer *renderer = new QgsSingleSymbolRenderer( geometryType() );
         setRenderer( renderer );
       }
     }
-    else  // Otherwise use some very basic defaults
-    {
-      // add single symbol renderer as default
-      QgsSingleSymbolRenderer *renderer = new QgsSingleSymbolRenderer( geometryType() );
-      setRenderer( renderer );
-    }
+
     // Get the update threshold from user settings. We
     // do this only on construction to avoid the penality of
     // fetching this each time the layer is drawn. If the user
@@ -2301,29 +2304,12 @@
     }
   }
 
-  QDomElement rendererElement = layer_node.firstChildElement(RENDERER_TAG_NAME);
-  if (!rendererElement.isNull())
+  QString errorMsg;
+  if ( !readSymbology( layer_node, errorMsg ) )
   {
-    // using renderer v2
-    setUsingRendererV2(true);
-
-    QgsFeatureRendererV2* r = QgsFeatureRendererV2::load(rendererElement);
-    if (r == NULL)
-      return false;
-    setRendererV2(r);
+    return false;
   }
-  else
-  {
-    // using renderer v1
-    setUsingRendererV2(false);
 
-    QString errorMsg;
-    if ( !readSymbology( layer_node, errorMsg ) )
-    {
-      return false;
-    }
-  }
-
   return mValid;               // should be true if read successfully
 
 } // void QgsVectorLayer::readXml
@@ -2457,28 +2443,85 @@
   }
 
   // renderer specific settings
-  if (mUsingRendererV2)
+  QString errorMsg;
+  if ( !writeSymbology( layer_node, document, errorMsg ) )
   {
-    QDomElement rendererElement = mRendererV2->save(document);
-    layer_node.appendChild(rendererElement);
+    return false;
   }
+
+  return true;
+} // bool QgsVectorLayer::writeXml
+
+bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage )
+{
+  // try renderer v2 first
+  QDomElement rendererElement = node.firstChildElement(RENDERER_TAG_NAME);
+  if (!rendererElement.isNull())
+  {
+    // using renderer v2
+    setUsingRendererV2(true);
+
+    QgsFeatureRendererV2* r = QgsFeatureRendererV2::load(rendererElement);
+    if (r == NULL)
+      return false;
+
+    setRendererV2(r);
+  }
   else
   {
-    QString errorMsg;
-    if ( !writeSymbology( layer_node, document, errorMsg ) )
+    // using renderer v1
+    setUsingRendererV2(false);
+
+    // create and bind a renderer to this layer
+
+    QDomNode singlenode = node.namedItem( "singlesymbol" );
+    QDomNode graduatednode = node.namedItem( "graduatedsymbol" );
+    QDomNode continuousnode = node.namedItem( "continuoussymbol" );
+    QDomNode uniquevaluenode = node.namedItem( "uniquevalue" );
+
+    QgsRenderer * renderer = 0;
+    int returnCode = 1;
+
+    if ( !singlenode.isNull() )
     {
+      renderer = new QgsSingleSymbolRenderer( geometryType() );
+      returnCode = renderer->readXML( singlenode, *this );
+    }
+    else if ( !graduatednode.isNull() )
+    {
+      renderer = new QgsGraduatedSymbolRenderer( geometryType() );
+      returnCode = renderer->readXML( graduatednode, *this );
+    }
+    else if ( !continuousnode.isNull() )
+    {
+      renderer = new QgsContinuousColorRenderer( geometryType() );
+      returnCode = renderer->readXML( continuousnode, *this );
+    }
+    else if ( !uniquevaluenode.isNull() )
+    {
+      renderer = new QgsUniqueValueRenderer( geometryType() );
+      returnCode = renderer->readXML( uniquevaluenode, *this );
+    }
+
+    if ( !renderer )
+    {
+      errorMessage = tr( "Unknown renderer" );
       return false;
     }
-  }
 
-  return true;
-} // bool QgsVectorLayer::writeXml
+    if ( returnCode == 1 )
+    {
+      errorMessage = tr( "No renderer object" ); delete renderer; return false;
+    }
+    else if ( returnCode == 2 )
+    {
+      errorMessage = tr( "Classification field not found" ); delete renderer; return false;
+    }
 
-bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage )
-{
-  // TODO: load symbology-ng renderers
-  setUsingRendererV2(false);
+    mRenderer = renderer;
 
+  }
+
   // process the attribute actions
   mActions->readXML( node );
 
@@ -2552,54 +2595,6 @@
     }
   }
 
-  // create and bind a renderer to this layer
-
-  QDomNode singlenode = node.namedItem( "singlesymbol" );
-  QDomNode graduatednode = node.namedItem( "graduatedsymbol" );
-  QDomNode continuousnode = node.namedItem( "continuoussymbol" );
-  QDomNode uniquevaluenode = node.namedItem( "uniquevalue" );
-
-  QgsRenderer * renderer = 0;
-  int returnCode = 1;
-
-  if ( !singlenode.isNull() )
-  {
-    renderer = new QgsSingleSymbolRenderer( geometryType() );
-    returnCode = renderer->readXML( singlenode, *this );
-  }
-  else if ( !graduatednode.isNull() )
-  {
-    renderer = new QgsGraduatedSymbolRenderer( geometryType() );
-    returnCode = renderer->readXML( graduatednode, *this );
-  }
-  else if ( !continuousnode.isNull() )
-  {
-    renderer = new QgsContinuousColorRenderer( geometryType() );
-    returnCode = renderer->readXML( continuousnode, *this );
-  }
-  else if ( !uniquevaluenode.isNull() )
-  {
-    renderer = new QgsUniqueValueRenderer( geometryType() );
-    returnCode = renderer->readXML( uniquevaluenode, *this );
-  }
-
-  if ( !renderer )
-  {
-    errorMessage = tr( "Unknown renderer" );
-    return false;
-  }
-
-  if ( returnCode == 1 )
-  {
-    errorMessage = tr( "No renderer object" ); delete renderer; return false;
-  }
-  else if ( returnCode == 2 )
-  {
-    errorMessage = tr( "Classification field not found" ); delete renderer; return false;
-  }
-
-  mRenderer = renderer;
-
   // Test if labeling is on or off
   QDomNode labelnode = node.namedItem( "label" );
   QDomElement element = labelnode.toElement();
@@ -2626,23 +2621,48 @@
 
 bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const
 {
+  if (mUsingRendererV2)
+  {
+    QDomElement rendererElement = mRendererV2->save(doc);
+    node.appendChild(rendererElement);
+  }
+  else
+  {
+    //classification field(s)
+    QgsAttributeList attributes = mRenderer->classificationAttributes();
+    const QgsFieldMap providerFields = mDataProvider->fields();
+    for ( QgsAttributeList::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
+    {
+      QDomElement classificationElement = doc.createElement( "classificationattribute" );
+      QDomText classificationText = doc.createTextNode( providerFields[*it].name() );
+      classificationElement.appendChild( classificationText );
+      node.appendChild( classificationElement );
+    }
+
+    // renderer settings
+    const QgsRenderer * myRenderer = renderer();
+    if ( myRenderer )
+    {
+      if ( !myRenderer->writeXML( node, doc, *this ) )
+      {
+        errorMessage = "renderer failed to save";
+        return false;
+      }
+    }
+    else
+    {
+      QgsDebugMsg( "no renderer" );
+      errorMessage = "no renderer";
+      return false;
+    }
+  }
+
   // use scale dependent visibility flag
   QDomElement mapLayerNode = node.toElement();
   mapLayerNode.setAttribute( "scaleBasedLabelVisibilityFlag", label()->scaleBasedVisibility() ? 1 : 0 );
   mapLayerNode.setAttribute( "minLabelScale", label()->minScale() );
   mapLayerNode.setAttribute( "maxLabelScale", label()->maxScale() );
 
-  //classification field(s)
-  QgsAttributeList attributes = mRenderer->classificationAttributes();
-  const QgsFieldMap providerFields = mDataProvider->fields();
-  for ( QgsAttributeList::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
-  {
-    QDomElement classificationElement = doc.createElement( "classificationattribute" );
-    QDomText classificationText = doc.createTextNode( providerFields[*it].name() );
-    classificationElement.appendChild( classificationText );
-    node.appendChild( classificationElement );
-  }
-
   //edit types
   if ( mEditTypes.size() > 0 )
   {
@@ -2725,22 +2745,6 @@
   // add attribute actions
   mActions->writeXML( node, doc );
 
-  const QgsRenderer * myRenderer = renderer();
-  if ( myRenderer )
-  {
-    if ( !myRenderer->writeXML( node, doc, *this ) )
-    {
-      errorMessage = "renderer failed to save";
-      return false;
-    }
-  }
-  else
-  {
-    QgsDebugMsg( "no renderer" );
-    errorMessage = "no renderer";
-    return false;
-  }
-
   // Now we get to do all that all over again for QgsLabel
 
   // XXX Since this is largely a cut-n-paste from the previous, this



More information about the QGIS-commit mailing list