Skip to content

Commit

Permalink
Fixed loading and saving of layer styles
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11903 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Nov 3, 2009
1 parent 72c842e commit 5438248
Showing 1 changed file with 125 additions and 121 deletions.
246 changes: 125 additions & 121 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,33 +123,36 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
// 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)
{
setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
}
else
{
// add single symbol renderer as default
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
Expand Down Expand Up @@ -2301,27 +2304,10 @@ bool QgsVectorLayer::readXml( QDomNode & layer_node )
}
}

QDomElement rendererElement = layer_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 ( !readSymbology( layer_node, errorMsg ) )
{
// using renderer v1
setUsingRendererV2(false);

QString errorMsg;
if ( !readSymbology( layer_node, errorMsg ) )
{
return false;
}
return false;
}

return mValid; // should be true if read successfully
Expand Down Expand Up @@ -2457,27 +2443,84 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
}

// 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 );
Expand Down Expand Up @@ -2552,54 +2595,6 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
}
}

// 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();
Expand All @@ -2626,23 +2621,48 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage

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 )
{
Expand Down Expand Up @@ -2725,22 +2745,6 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
// 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
Expand Down

0 comments on commit 5438248

Please sign in to comment.