Skip to content

Commit

Permalink
[FEATURE] Allow changing vector layer data source
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed May 17, 2015
1 parent 173ee45 commit e249bfe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
2 changes: 2 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -560,6 +560,8 @@ class QgsVectorLayer : QgsMapLayer
*/
virtual long featureCount() const;

void setDataSource(QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag = true);

/**
* Number of features rendered with specified symbol. Features must be first
* calculated by countSymbolFeatures()
Expand Down
60 changes: 35 additions & 25 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -151,31 +151,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
// if we're given a provider type, try to create and bind one to this layer
if ( ! mProviderKey.isEmpty() )
{
setDataProvider( mProviderKey );
}
if ( mValid )
{
// 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 )
{
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() ) );
}

setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );

connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( checkJoinLayerRemove( QString ) ) );
setDataSource( vectorLayerPath, baseName, providerKey, loadDefaultStyleFlag );
}

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


void QgsVectorLayer::setDataSource( QString dataSource, QString baseName, QString provider, bool loadDefaultStyleFlag )
{
mDataSource = dataSource;
mLayerName = capitaliseLayerName( baseName );
setLayerName( mLayerName );
setDataProvider( provider );

if ( mValid )
{
// 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 )
{
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() ) );
}

setLegend( QgsMapLayerLegend::defaultVectorLegend( this ) );

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


bool QgsVectorLayer::setDataProvider( QString const & provider )
{
// XXX should I check for and possibly delete any pre-existing providers?
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -930,6 +930,11 @@ 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 );

/**
* Count features for symbols. Feature counts may be get by featureCount( QgsSymbolV2*).
* @param showProgress show progress dialog
Expand Down Expand Up @@ -1721,6 +1726,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
void writeCustomSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage ) const;


private slots:
void onRelationsLoaded();
void onJoinedFieldsChanged();
Expand All @@ -1737,6 +1743,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** vector layers are not copyable */
QgsVectorLayer & operator=( QgsVectorLayer const & rhs );


/** bind layer to a specific data provider
@param provider should be "postgres", "ogr", or ??
@todo XXX should this return bool? Throw exceptions?
Expand Down

0 comments on commit e249bfe

Please sign in to comment.