Skip to content
Permalink
Browse files

Bad layers: open a browser widget dialog to select a data source

when indicator is clicked
  • Loading branch information
elpaso committed Oct 29, 2018
1 parent f2654f2 commit 147608ea98613621069e6496daf6103ce1faeeb3
Showing with 58 additions and 1 deletion.
  1. +58 −1 src/app/qgslayertreeviewbadlayerindicator.cpp
@@ -21,6 +21,13 @@
#include "qgslayertreemodel.h"
#include "qgsvectorlayer.h"
#include "qgisapp.h"
#include "qgsbrowsermodel.h"
#include "qgsbrowsertreeview.h"

#include <functional>
#include <QDialog>
#include <QVBoxLayout>
#include <QDialogButtonBox>

QgsLayerTreeViewBadLayerIndicatorProvider::QgsLayerTreeViewBadLayerIndicatorProvider( QgsLayerTreeView *view )
: QgsLayerTreeViewIndicatorProvider( view )
@@ -37,7 +44,57 @@ void QgsLayerTreeViewBadLayerIndicatorProvider::onIndicatorClicked( const QModel
if ( !vlayer )
return;

// TODO: open source select dialog
// TODO: raster layers

// Get provider type
QString providerType( vlayer->providerType() );
QgsBrowserModel browserModel;
browserModel.initialize();
QDialog dlg;
dlg.setWindowTitle( tr( "Select the new data source" ) );
QByteArray dlgGeom( QgsSettings().value( QStringLiteral( "/Windows/selectDataSourceDialog/geometry" ), QVariant(), QgsSettings::Section::App ).toByteArray() );
dlg.restoreGeometry( dlgGeom );
QVBoxLayout lay( &dlg );
QgsBrowserTreeView *browserWidget( new QgsBrowserTreeView( ) );
browserWidget->setModel( &browserModel );
browserWidget->setHeaderHidden( true );
lay.addWidget( browserWidget );
QDialogButtonBox *buttonBox( new QDialogButtonBox( QDialogButtonBox::StandardButton::Ok | QDialogButtonBox::StandardButton::Cancel ) );
lay.addWidget( buttonBox );
connect( buttonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
connect( buttonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( false );

std::function<bool( const QModelIndex & )> isItemCompatible = [ & ]( const QModelIndex & index )
{
if ( index.isValid() )
{
const QgsDataItem *item( browserModel.dataItem( index ) );
if ( item->mimeUri().layerType == QStringLiteral( "vector" ) && item->mimeUri().providerKey == providerType )
{
return true;
}
}
return false;
};

connect( browserWidget, &QgsBrowserTreeView::clicked, [ & ]( const QModelIndex & index )
{
buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isItemCompatible( index ) );
} );

dlg.setLayout( &lay );
if ( dlg.exec() == QDialog::Accepted )
{
// Get selected item(s)
QModelIndex index = browserWidget->currentIndex();
if ( isItemCompatible( index ) )
{
const QgsDataItem *item( browserModel.dataItem( index ) );
vlayer->setDataSource( item->mimeUri().uri, vlayer->name(), item->mimeUri().providerKey, QgsDataProvider::ProviderOptions() );
}
}
QgsSettings().setValue( QStringLiteral( "/Windows/selectDataSourceDialog/geometry" ), dlg.saveGeometry(), QgsSettings::Section::App );
}

QString QgsLayerTreeViewBadLayerIndicatorProvider::iconName( QgsMapLayer *layer )

0 comments on commit 147608e

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