Skip to content

Commit e249bfe

Browse files
committedMay 17, 2015
[FEATURE] Allow changing vector layer data source
1 parent 173ee45 commit e249bfe

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed
 

‎python/core/qgsvectorlayer.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ class QgsVectorLayer : QgsMapLayer
560560
*/
561561
virtual long featureCount() const;
562562

563+
void setDataSource(QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag = true);
564+
563565
/**
564566
* Number of features rendered with specified symbol. Features must be first
565567
* calculated by countSymbolFeatures()

‎src/core/qgsvectorlayer.cpp

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -151,31 +151,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
151151
// if we're given a provider type, try to create and bind one to this layer
152152
if ( ! mProviderKey.isEmpty() )
153153
{
154-
setDataProvider( mProviderKey );
155-
}
156-
if ( mValid )
157-
{
158-
// Always set crs
159-
setCoordinateSystem();
160-
161-
// check if there is a default style / propertysheet defined
162-
// for this layer and if so apply it
163-
bool defaultLoadedFlag = false;
164-
if ( loadDefaultStyleFlag )
165-
{
166-
loadDefaultStyle( defaultLoadedFlag );
167-
}
168-
169-
// if the default style failed to load or was disabled use some very basic defaults
170-
if ( !defaultLoadedFlag && hasGeometryType() )
171-
{
172-
// add single symbol renderer
173-
setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
174-
}
175-
176-
setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );
177-
178-
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( checkJoinLayerRemove( QString ) ) );
154+
setDataSource( vectorLayerPath, baseName, providerKey, loadDefaultStyleFlag );
179155
}
180156

181157
connect( this, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SIGNAL( selectionChanged() ) );
@@ -1380,6 +1356,40 @@ bool QgsVectorLayer::readXml( const QDomNode& layer_node )
13801356
} // void QgsVectorLayer::readXml
13811357

13821358

1359+
void QgsVectorLayer::setDataSource( QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag )
1360+
{
1361+
mDataSource = dataSource;
1362+
mLayerName = capitaliseLayerName( baseName );
1363+
setLayerName( mLayerName );
1364+
setDataProvider( provider );
1365+
1366+
if ( mValid )
1367+
{
1368+
// Always set crs
1369+
setCoordinateSystem();
1370+
1371+
// check if there is a default style / propertysheet defined
1372+
// for this layer and if so apply it
1373+
bool defaultLoadedFlag = false;
1374+
if ( loadDefaultStyleFlag )
1375+
{
1376+
loadDefaultStyle( defaultLoadedFlag );
1377+
}
1378+
1379+
// if the default style failed to load or was disabled use some very basic defaults
1380+
if ( !defaultLoadedFlag && hasGeometryType() )
1381+
{
1382+
// add single symbol renderer
1383+
setRendererV2( QgsFeatureRendererV2::defaultRenderer( geometryType() ) );
1384+
}
1385+
1386+
setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );
1387+
1388+
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( checkJoinLayerRemove( QString ) ) );
1389+
}
1390+
}
1391+
1392+
13831393
bool QgsVectorLayer::setDataProvider( QString const & provider )
13841394
{
13851395
// XXX should I check for and possibly delete any pre-existing providers?

‎src/core/qgsvectorlayer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
930930
*/
931931
long featureCount( QgsSymbolV2* symbol );
932932

933+
/**
934+
* Update the data source of the layer
935+
*/
936+
void setDataSource( QString dataSource, QString baseName, QString provider , bool loadDefaultStyleFlag = false );
937+
933938
/**
934939
* Count features for symbols. Feature counts may be get by featureCount( QgsSymbolV2*).
935940
* @param showProgress show progress dialog
@@ -1721,6 +1726,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
17211726
*/
17221727
void writeCustomSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage ) const;
17231728

1729+
17241730
private slots:
17251731
void onRelationsLoaded();
17261732
void onJoinedFieldsChanged();
@@ -1737,6 +1743,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
17371743
/** vector layers are not copyable */
17381744
QgsVectorLayer & operator=( QgsVectorLayer const & rhs );
17391745

1746+
17401747
/** bind layer to a specific data provider
17411748
@param provider should be "postgres", "ogr", or ??
17421749
@todo XXX should this return bool? Throw exceptions?

0 commit comments

Comments
 (0)
Please sign in to comment.