Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bad layers: open a browser widget dialog to select a data source
when indicator is clicked
  • Loading branch information
elpaso committed Nov 5, 2018
1 parent f2654f2 commit 147608e
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/app/qgslayertreeviewbadlayerindicator.cpp
Expand Up @@ -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 )
Expand All @@ -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 )
Expand Down

0 comments on commit 147608e

Please sign in to comment.