Skip to content

Commit

Permalink
Make QgsVectorLayer::setDataSource maintain renderer where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 22, 2015
1 parent 3ccd2a1 commit 436350a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
12 changes: 11 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -577,7 +577,17 @@ class QgsVectorLayer : QgsMapLayer
*/
virtual long featureCount() const;

void setDataSource(QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag = true);
/**
* Update the data source of the layer. The layer's renderer and legend will be preserved only
* if the geometry type of the new data source matches the current geometry type of the layer.
* @param dataSource new layer data source
* @param baseName base name of the layer
* @param provider provider string
* @param loadDefaultStyleFlag set to true to reset the layer's style to the default for the
* data source
* @note added in QGIS 2.10
*/
void setDataSource( QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag = false );

/**
* Number of features rendered with specified symbol. Features must be first
Expand Down
33 changes: 20 additions & 13 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1330,6 +1330,8 @@ bool QgsVectorLayer::readXml( const QDomNode& layer_node )

void QgsVectorLayer::setDataSource( QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag )
{
QGis::GeometryType oldGeomType = geometryType();

mDataSource = dataSource;
mLayerName = capitaliseLayerName( baseName );
setLayerName( mLayerName );
Expand All @@ -1340,24 +1342,29 @@ void QgsVectorLayer::setDataSource( QString dataSource, QString baseName, QStrin
// Always set crs
setCoordinateSystem();

// check if there is a default style / propertysheet defined
// for this layer and if so apply it
bool defaultLoadedFlag = false;
if ( loadDefaultStyleFlag )
// reset style if loading default style, style is missing, or geometry type has changed
if ( !rendererV2() || !legend() || oldGeomType != geometryType() || loadDefaultStyleFlag )
{
loadDefaultStyle( defaultLoadedFlag );
}
// check if there is a default style / propertysheet defined
// for this layer and if so apply it
bool defaultLoadedFlag = false;
if ( loadDefaultStyleFlag )
{
loadDefaultStyle( defaultLoadedFlag );
}

// if the default style failed to load or was disabled use some very basic defaults
if ( !defaultLoadedFlag && hasGeometryType() )
{
// add single symbol renderer
setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
}
// if the default style failed to load or was disabled use some very basic defaults
if ( !defaultLoadedFlag && hasGeometryType() )
{
// add single symbol renderer
setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
}

setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );
setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );
}

connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( checkJoinLayerRemove( QString ) ) );
emit repaintRequested();
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1078,9 +1078,16 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
long featureCount( QgsSymbolV2* symbol );

/**
* Update the data source of the layer
*/
void setDataSource( QString dataSource, QString baseName, QString provider , bool loadDefaultStyleFlag = false );
* Update the data source of the layer. The layer's renderer and legend will be preserved only
* if the geometry type of the new data source matches the current geometry type of the layer.
* @param dataSource new layer data source
* @param baseName base name of the layer
* @param provider provider string
* @param loadDefaultStyleFlag set to true to reset the layer's style to the default for the
* data source
* @note added in QGIS 2.10
*/
void setDataSource( QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag = false );

/**
* Count features for symbols. Feature counts may be get by featureCount( QgsSymbolV2*).
Expand Down

0 comments on commit 436350a

Please sign in to comment.