Skip to content
Permalink
Browse files

Make QgsVectorLayer::setDataSource maintain renderer where possible

  • Loading branch information
nyalldawson committed Jun 22, 2015
1 parent 3ccd2a1 commit 436350a4e396df873f55f71c948c2fb01a3b0cb2
Showing with 41 additions and 17 deletions.
  1. +11 −1 python/core/qgsvectorlayer.sip
  2. +20 −13 src/core/qgsvectorlayer.cpp
  3. +10 −3 src/core/qgsvectorlayer.h
@@ -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
@@ -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 );
@@ -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();
}
}

@@ -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*).

0 comments on commit 436350a

Please sign in to comment.
You can’t perform that action at this time.