Skip to content

Commit 436350a

Browse files
committed
Make QgsVectorLayer::setDataSource maintain renderer where possible
1 parent 3ccd2a1 commit 436350a

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

python/core/qgsvectorlayer.sip

+11-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,17 @@ class QgsVectorLayer : QgsMapLayer
577577
*/
578578
virtual long featureCount() const;
579579

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

582592
/**
583593
* Number of features rendered with specified symbol. Features must be first

src/core/qgsvectorlayer.cpp

+20-13
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,8 @@ bool QgsVectorLayer::readXml( const QDomNode& layer_node )
13301330

13311331
void QgsVectorLayer::setDataSource( QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag )
13321332
{
1333+
QGis::GeometryType oldGeomType = geometryType();
1334+
13331335
mDataSource = dataSource;
13341336
mLayerName = capitaliseLayerName( baseName );
13351337
setLayerName( mLayerName );
@@ -1340,24 +1342,29 @@ void QgsVectorLayer::setDataSource( QString dataSource, QString baseName, QStrin
13401342
// Always set crs
13411343
setCoordinateSystem();
13421344

1343-
// check if there is a default style / propertysheet defined
1344-
// for this layer and if so apply it
1345-
bool defaultLoadedFlag = false;
1346-
if ( loadDefaultStyleFlag )
1345+
// reset style if loading default style, style is missing, or geometry type has changed
1346+
if ( !rendererV2() || !legend() || oldGeomType != geometryType() || loadDefaultStyleFlag )
13471347
{
1348-
loadDefaultStyle( defaultLoadedFlag );
1349-
}
1348+
// check if there is a default style / propertysheet defined
1349+
// for this layer and if so apply it
1350+
bool defaultLoadedFlag = false;
1351+
if ( loadDefaultStyleFlag )
1352+
{
1353+
loadDefaultStyle( defaultLoadedFlag );
1354+
}
13501355

1351-
// if the default style failed to load or was disabled use some very basic defaults
1352-
if ( !defaultLoadedFlag && hasGeometryType() )
1353-
{
1354-
// add single symbol renderer
1355-
setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
1356-
}
1356+
// if the default style failed to load or was disabled use some very basic defaults
1357+
if ( !defaultLoadedFlag && hasGeometryType() )
1358+
{
1359+
// add single symbol renderer
1360+
setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
1361+
}
13571362

1358-
setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );
1363+
setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );
1364+
}
13591365

13601366
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( checkJoinLayerRemove( QString ) ) );
1367+
emit repaintRequested();
13611368
}
13621369
}
13631370

src/core/qgsvectorlayer.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,16 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
10781078
long featureCount( QgsSymbolV2* symbol );
10791079

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

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

0 commit comments

Comments
 (0)