@@ -6934,6 +6934,83 @@ void QgisApp::refreshFeatureActions()
6934
6934
updateDefaultFeatureAction( mFeatureActionMenu->activeAction() );
6935
6935
}
6936
6936
6937
+ void QgisApp::changeDataSource( QgsMapLayer *layer )
6938
+ {
6939
+ // Get provider type
6940
+ QString providerType( layer->providerType() );
6941
+ QgsMapLayer::LayerType layerType( layer->type() );
6942
+
6943
+ QgsDataSourceSelectDialog dlg( mBrowserModel, true, layerType );
6944
+
6945
+ if ( dlg.exec() == QDialog::Accepted )
6946
+ {
6947
+ QgsMimeDataUtils::Uri uri( dlg.uri() );
6948
+ if ( uri.isValid() )
6949
+ {
6950
+ bool layerWasValid( layer->isValid() );
6951
+ // Store subset string form vlayer if we are fixing a bad layer
6952
+ QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
6953
+ QString subsetString;
6954
+ // Get the subset string directly from the data provider because
6955
+ // layer's method will return a null string from invalid layers
6956
+ if ( !layerWasValid && vlayer && vlayer->dataProvider() &&
6957
+ vlayer->dataProvider()->supportsSubsetString() &&
6958
+ !vlayer->dataProvider()->subsetString( ).isEmpty() )
6959
+ {
6960
+ subsetString = vlayer->dataProvider()->subsetString();
6961
+ }
6962
+ layer->setDataSource( uri.uri, layer->name(), uri.providerKey, QgsDataProvider::ProviderOptions() );
6963
+ // Re-apply original style and subset string when fixing bad layers
6964
+ if ( !( layerWasValid || layer->originalXmlProperties().isEmpty() ) )
6965
+ {
6966
+ if ( ! subsetString.isEmpty() )
6967
+ {
6968
+ vlayer->setSubsetString( subsetString );
6969
+ }
6970
+ QgsReadWriteContext context;
6971
+ context.setPathResolver( QgsProject::instance()->pathResolver() );
6972
+ context.setProjectTranslator( QgsProject::instance() );
6973
+ QString errorMsg;
6974
+ QDomDocument doc;
6975
+ if ( doc.setContent( layer->originalXmlProperties() ) )
6976
+ {
6977
+ QDomNode layer_node( doc.firstChild( ) );
6978
+ if ( ! layer->readSymbology( layer_node, errorMsg, context ) )
6979
+ {
6980
+ QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from stored XML for layer %1: %2" )
6981
+ .arg( layer->name( ) )
6982
+ .arg( errorMsg ) );
6983
+ }
6984
+ }
6985
+ else
6986
+ {
6987
+ QgsDebugMsg( QStringLiteral( "Failed to create XML QDomDocument for layer %1: %2" )
6988
+ .arg( layer->name( ) )
6989
+ .arg( errorMsg ) );
6990
+ }
6991
+ }
6992
+
6993
+ // All the following code is necessary to refresh the layer
6994
+ QgsLayerTreeModel *model = qobject_cast<QgsLayerTreeModel *>( mLayerTreeView->model() );
6995
+ if ( model )
6996
+ {
6997
+ QgsLayerTreeLayer *tl( model->rootGroup()->findLayer( layer->id() ) );
6998
+ if ( tl && tl->itemVisibilityChecked() )
6999
+ {
7000
+ tl->setItemVisibilityChecked( false );
7001
+ tl->setItemVisibilityChecked( true );
7002
+ }
7003
+ }
7004
+
7005
+ // Tell the bridge that we have fixed a layer
7006
+ if ( ! layerWasValid && layer->isValid() )
7007
+ {
7008
+ QgsProject::instance()->layerTreeRoot()->customLayerOrderChanged( );
7009
+ }
7010
+ }
7011
+ }
7012
+ }
7013
+
6937
7014
void QgisApp::measure()
6938
7015
{
6939
7016
mMapCanvas->setMapTool( mMapTools.mMeasureDist );
0 commit comments