diff --git a/python/gui/gui_auto.sip b/python/gui/gui_auto.sip index bff290273b5..fed01fb6f7c 100644 --- a/python/gui/gui_auto.sip +++ b/python/gui/gui_auto.sip @@ -17,6 +17,7 @@ %Include auto_generated/qgsuserinputwidget.sip %Include auto_generated/qgsbrowserdockwidget.sip %Include auto_generated/qgsvertexmarker.sip +%Include auto_generated/qgsdatasourceselectdialog.sip %Include auto_generated/qgsabstractdatasourcewidget.sip %Include auto_generated/qgssourceselectprovider.sip %Include auto_generated/qgssourceselectproviderregistry.sip diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 3640bd1580c..7d006bc59c0 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -244,6 +244,7 @@ SET(QGIS_GUI_SRCS qgscustomdrophandler.cpp qgscurveeditorwidget.cpp qgsdatumtransformdialog.cpp + qgsdatasourceselectdialog.cpp qgsdetaileditemdata.cpp qgsdetaileditemdelegate.cpp qgsdetaileditemwidget.cpp @@ -422,6 +423,7 @@ SET(QGIS_GUI_MOC_HDRS qgscurveeditorwidget.h qgscustomdrophandler.h qgsdatumtransformdialog.h + qgsdatasourceselectdialog.h qgsdetaileditemdelegate.h qgsdetaileditemwidget.h qgsdial.h @@ -787,6 +789,7 @@ SET(QGIS_GUI_HDRS qgsbrowserdockwidget_p.h qgsvertexmarker.h qgsdatasourcemanagerdialog.h + qgsdatasourceselectdialog.h qgsabstractdatasourcewidget.h qgswidgetstatehelper_p.h qgssourceselectprovider.h diff --git a/src/gui/qgsdatasourceselectdialog.cpp b/src/gui/qgsdatasourceselectdialog.cpp new file mode 100644 index 00000000000..2f70d81c014 --- /dev/null +++ b/src/gui/qgsdatasourceselectdialog.cpp @@ -0,0 +1,90 @@ +/*************************************************************************** + qgsdatasourceselectdialog.cpp - QgsDataSourceSelectDialog + + --------------------- + begin : 1.11.2018 + copyright : (C) 2018 by ale + email : [your-email-here] + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "qgsdatasourceselectdialog.h" +#include "ui_qgsdatasourceselectdialog.h" +#include "qgssettings.h" +#include "qgis.h" + +#include + +QgsDataSourceSelectDialog::QgsDataSourceSelectDialog( bool setFilterByLayerType, + const QgsMapLayer::LayerType &layerType, + QWidget *parent ) + : QDialog( parent ) +{ + setupUi( this ); + setWindowTitle( tr( "Select a data source" ) ); + QByteArray dlgGeom( QgsSettings().value( QStringLiteral( "/Windows/selectDataSourceDialog/geometry" ), QVariant(), QgsSettings::Section::Gui ).toByteArray() ); + restoreGeometry( dlgGeom ); + + mBrowserModel.initialize(); + mBrowserProxyModel.setBrowserModel( &mBrowserModel ); + mBrowserTreeView->setHeaderHidden( true ); + + if ( setFilterByLayerType ) + { + setLayerTypeFilter( layerType ); + } + else + { + mBrowserTreeView->setModel( &mBrowserProxyModel ); + buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( false ); + } + connect( mBrowserTreeView, &QgsBrowserTreeView::clicked, this, &QgsDataSourceSelectDialog::onLayerSelected ); +} + + +QgsDataSourceSelectDialog::~QgsDataSourceSelectDialog() +{ + QgsSettings().setValue( QStringLiteral( "/Windows/selectDataSourceDialog/geometry" ), saveGeometry(), QgsSettings::Section::Gui ); +} + +void QgsDataSourceSelectDialog::setLayerTypeFilter( const QgsMapLayer::LayerType &layerType ) +{ + mBrowserProxyModel.setFilterByLayerType( true ); + mBrowserProxyModel.setLayerType( layerType ); + // reset model and button + mBrowserTreeView->setModel( &mBrowserProxyModel ); + buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( false ); +} + +QgsMimeDataUtils::Uri QgsDataSourceSelectDialog::uri() const +{ + return mUri; +} + +void QgsDataSourceSelectDialog::onLayerSelected( const QModelIndex &index ) +{ + bool isLayerCompatible = false; + mUri = QgsMimeDataUtils::Uri(); + if ( index.isValid() ) + { + const QgsDataItem *dataItem( mBrowserProxyModel.dataItem( index ) ); + if ( dataItem ) + { + const QgsLayerItem *layerItem = qobject_cast( dataItem ); + if ( layerItem && ( ! mBrowserProxyModel.filterByLayerType() || + ( layerItem->mapLayerType() == mBrowserProxyModel.layerType() ) ) ) + { + isLayerCompatible = true; + mUri = layerItem->mimeUri(); + } + } + } + buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isLayerCompatible ); +} + diff --git a/src/gui/qgsdatasourceselectdialog.h b/src/gui/qgsdatasourceselectdialog.h new file mode 100644 index 00000000000..c9f7e8f37ef --- /dev/null +++ b/src/gui/qgsdatasourceselectdialog.h @@ -0,0 +1,83 @@ +/*************************************************************************** + qgsdatasourceselectdialog.h - QgsDataSourceSelectDialog + + --------------------- + begin : 1.11.2018 + copyright : (C) 2018 by ale + email : [your-email-here] + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +#ifndef QGSDATASOURCESELECTDIALOG_H +#define QGSDATASOURCESELECTDIALOG_H + +#include +#include "ui_qgsdatasourceselectdialog.h" + +#include "qgis_gui.h" +#include "qgsmaplayer.h" +#include "qgsmimedatautils.h" +#include "qgsbrowsermodel.h" +#include "qgsbrowserproxymodel.h" + + +/** + * \ingroup gui + * The QgsDataSourceSelectDialog class embeds the browser view to + * select an existing data source. + * + * By default it allows to select all layer types, the allowed layer + * type can be restricted by setting a layer type filter with + * setLayerTypeFilter(layerType) or by activating the filter + * directly from the constructor. + * + * To retrieve the selected data source, uri() can be called and it + * will return a (possibly invalid) QgsMimeDataUtils::Uri. + * + * \since QGIS 3.6 + */ +class GUI_EXPORT QgsDataSourceSelectDialog: public QDialog, private Ui::QgsDataSourceSelectDialog +{ + Q_OBJECT + + public: + + /** + * Constructs a QgsDataSourceSelectDialog, optionally filtering by layer type + * + * \param setFilterByLayerType activates filtering by layer type + * \param layerType sets the layer type filter, this is in effect only if filtering by layer type is also active + * \param parent the object + */ + QgsDataSourceSelectDialog( bool setFilterByLayerType = false, + const QgsMapLayer::LayerType &layerType = QgsMapLayer::LayerType::VectorLayer, + QWidget *parent = nullptr ); + + ~QgsDataSourceSelectDialog() override; + + /** + * Sets layer type filter to \a layerType and activates the filtering + */ + void setLayerTypeFilter( const QgsMapLayer::LayerType &layerType ); + + QgsMimeDataUtils::Uri uri() const; + + private slots: + + //! Triggered when a layer is selected in the browser + void onLayerSelected( const QModelIndex &index ); + + private: + + QgsBrowserModel mBrowserModel; + QgsBrowserProxyModel mBrowserProxyModel; + QgsMimeDataUtils::Uri mUri; + +}; + +#endif // QGSDATASOURCESELECTDIALOG_H diff --git a/src/ui/qgsdatasourceselectdialog.ui b/src/ui/qgsdatasourceselectdialog.ui new file mode 100644 index 00000000000..795498eb7a8 --- /dev/null +++ b/src/ui/qgsdatasourceselectdialog.ui @@ -0,0 +1,74 @@ + + + QgsDataSourceSelectDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + QgsBrowserTreeView + QTreeView +
qgsbrowsertreeview.h
+
+
+ + + + buttonBox + accepted() + QgsDataSourceSelectDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + QgsDataSourceSelectDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +