Skip to content

Commit 408e2d3

Browse files
committed
Tell the bridge to update when a bad layer was fixed
This fixes an unreported issue: when a bad layer is fixed it does not appear in the layer order panel. The reason why it does not appear is that a bad layer has no geometry information, and the layer order model does not list geometryless layers. But after a bad layer has been fixed, it has a geometry and the panel should show it. cherry-pick b45ab5d
1 parent bd36edb commit 408e2d3

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/app/qgisapp.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -6934,6 +6934,83 @@ void QgisApp::refreshFeatureActions()
69346934
updateDefaultFeatureAction( mFeatureActionMenu->activeAction() );
69356935
}
69366936

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+
69377014
void QgisApp::measure()
69387015
{
69397016
mMapCanvas->setMapTool( mMapTools.mMeasureDist );

0 commit comments

Comments
 (0)