Skip to content
Permalink
Browse files

[FEATURE] Allow changing vector layer data source

  • Loading branch information
NathanW2 committed May 17, 2015
1 parent 173ee45 commit e249bfee94220a822e5b602ff972cf57d2e26080
Showing with 44 additions and 25 deletions.
  1. +2 −0 python/core/qgsvectorlayer.sip
  2. +35 −25 src/core/qgsvectorlayer.cpp
  3. +7 −0 src/core/qgsvectorlayer.h
@@ -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()
@@ -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() ) );
@@ -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?
@@ -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
@@ -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();
@@ -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?

0 comments on commit e249bfe

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