Skip to content

Commit

Permalink
Restore original layer style (tested with vectors only)
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 5, 2018
1 parent 3fe7848 commit 3e9d5c7
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/app/qgslayertreeviewbadlayerindicator.cpp
Expand Up @@ -94,11 +94,48 @@ void QgsLayerTreeViewBadLayerIndicatorProvider::onIndicatorClicked( const QModel
dlg.setLayout( &lay );
if ( dlg.exec() == QDialog::Accepted )
{
QModelIndex index = browserWidget->currentIndex();
if ( isItemCompatible( index ) )
QModelIndex idx = browserWidget->currentIndex();
if ( isItemCompatible( idx ) )
{
const QgsDataItem *item( browserModel.dataItem( index ) );
const QgsDataItem *item( browserModel.dataItem( idx ) );
layer->setDataSource( item->mimeUri().uri, layer->name(), item->mimeUri().providerKey, QgsDataProvider::ProviderOptions() );
// Re-apply style
if ( ! layer->originalXmlProperties().isEmpty() )
{
QgsReadWriteContext context;
context.setPathResolver( QgsProject::instance()->pathResolver() );
context.setProjectTranslator( QgsProject::instance() );
QString errorMsg;
QDomDocument doc;
if ( doc.setContent( layer->originalXmlProperties() ) )
{
QDomNode layer_node( doc.firstChild( ) );
if ( ! layer->readSymbology( layer_node, errorMsg, context ) )
{
QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from stored XML for layer %1: %2" )
.arg( layer->name( ) )
.arg( errorMsg ) );
}
}
else
{
QgsDebugMsg( QStringLiteral( "Failed to create XML QDomDocument for layer %1: %2" )
.arg( layer->name( ) )
.arg( errorMsg ) );
}
}

// All the following code is necessary to refresh the layer
QgsLayerTreeModel *model = qobject_cast<QgsLayerTreeModel *>( mLayerTreeView->model() );
if ( model )
{
QgsLayerTreeLayer *tl( model->rootGroup()->findLayer( layer->id() ) );
if ( tl && tl->itemVisibilityChecked() )
{
tl->setItemVisibilityChecked( false );
tl->setItemVisibilityChecked( true );
}
}
}
}
QgsSettings().setValue( QStringLiteral( "/Windows/selectDataSourceDialog/geometry" ), dlg.saveGeometry(), QgsSettings::Section::App );
Expand Down

0 comments on commit 3e9d5c7

Please sign in to comment.