Skip to content

Commit 083208c

Browse files
committedNov 1, 2018
QgsDataSourceSelectDialog: a simple browser-based reusable data source select dialog
1 parent e83aa5a commit 083208c

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed
 

‎python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
%Include auto_generated/qgsuserinputwidget.sip
1818
%Include auto_generated/qgsbrowserdockwidget.sip
1919
%Include auto_generated/qgsvertexmarker.sip
20+
%Include auto_generated/qgsdatasourceselectdialog.sip
2021
%Include auto_generated/qgsabstractdatasourcewidget.sip
2122
%Include auto_generated/qgssourceselectprovider.sip
2223
%Include auto_generated/qgssourceselectproviderregistry.sip

‎src/gui/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ SET(QGIS_GUI_SRCS
244244
qgscustomdrophandler.cpp
245245
qgscurveeditorwidget.cpp
246246
qgsdatumtransformdialog.cpp
247+
qgsdatasourceselectdialog.cpp
247248
qgsdetaileditemdata.cpp
248249
qgsdetaileditemdelegate.cpp
249250
qgsdetaileditemwidget.cpp
@@ -422,6 +423,7 @@ SET(QGIS_GUI_MOC_HDRS
422423
qgscurveeditorwidget.h
423424
qgscustomdrophandler.h
424425
qgsdatumtransformdialog.h
426+
qgsdatasourceselectdialog.h
425427
qgsdetaileditemdelegate.h
426428
qgsdetaileditemwidget.h
427429
qgsdial.h
@@ -787,6 +789,7 @@ SET(QGIS_GUI_HDRS
787789
qgsbrowserdockwidget_p.h
788790
qgsvertexmarker.h
789791
qgsdatasourcemanagerdialog.h
792+
qgsdatasourceselectdialog.h
790793
qgsabstractdatasourcewidget.h
791794
qgswidgetstatehelper_p.h
792795
qgssourceselectprovider.h

‎src/gui/qgsdatasourceselectdialog.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/***************************************************************************
2+
qgsdatasourceselectdialog.cpp - QgsDataSourceSelectDialog
3+
4+
---------------------
5+
begin : 1.11.2018
6+
copyright : (C) 2018 by ale
7+
email : [your-email-here]
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#include "qgsdatasourceselectdialog.h"
18+
#include "ui_qgsdatasourceselectdialog.h"
19+
#include "qgssettings.h"
20+
#include "qgis.h"
21+
22+
#include <QPushButton>
23+
24+
QgsDataSourceSelectDialog::QgsDataSourceSelectDialog( bool setFilterByLayerType,
25+
const QgsMapLayer::LayerType &layerType,
26+
QWidget *parent )
27+
: QDialog( parent )
28+
{
29+
setupUi( this );
30+
setWindowTitle( tr( "Select a data source" ) );
31+
QByteArray dlgGeom( QgsSettings().value( QStringLiteral( "/Windows/selectDataSourceDialog/geometry" ), QVariant(), QgsSettings::Section::Gui ).toByteArray() );
32+
restoreGeometry( dlgGeom );
33+
34+
mBrowserModel.initialize();
35+
mBrowserProxyModel.setBrowserModel( &mBrowserModel );
36+
mBrowserTreeView->setHeaderHidden( true );
37+
38+
if ( setFilterByLayerType )
39+
{
40+
setLayerTypeFilter( layerType );
41+
}
42+
else
43+
{
44+
mBrowserTreeView->setModel( &mBrowserProxyModel );
45+
buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( false );
46+
}
47+
connect( mBrowserTreeView, &QgsBrowserTreeView::clicked, this, &QgsDataSourceSelectDialog::onLayerSelected );
48+
}
49+
50+
51+
QgsDataSourceSelectDialog::~QgsDataSourceSelectDialog()
52+
{
53+
QgsSettings().setValue( QStringLiteral( "/Windows/selectDataSourceDialog/geometry" ), saveGeometry(), QgsSettings::Section::Gui );
54+
}
55+
56+
void QgsDataSourceSelectDialog::setLayerTypeFilter( const QgsMapLayer::LayerType &layerType )
57+
{
58+
mBrowserProxyModel.setFilterByLayerType( true );
59+
mBrowserProxyModel.setLayerType( layerType );
60+
// reset model and button
61+
mBrowserTreeView->setModel( &mBrowserProxyModel );
62+
buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( false );
63+
}
64+
65+
QgsMimeDataUtils::Uri QgsDataSourceSelectDialog::uri() const
66+
{
67+
return mUri;
68+
}
69+
70+
void QgsDataSourceSelectDialog::onLayerSelected( const QModelIndex &index )
71+
{
72+
bool isLayerCompatible = false;
73+
mUri = QgsMimeDataUtils::Uri();
74+
if ( index.isValid() )
75+
{
76+
const QgsDataItem *dataItem( mBrowserProxyModel.dataItem( index ) );
77+
if ( dataItem )
78+
{
79+
const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
80+
if ( layerItem && ( ! mBrowserProxyModel.filterByLayerType() ||
81+
( layerItem->mapLayerType() == mBrowserProxyModel.layerType() ) ) )
82+
{
83+
isLayerCompatible = true;
84+
mUri = layerItem->mimeUri();
85+
}
86+
}
87+
}
88+
buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isLayerCompatible );
89+
}
90+

‎src/gui/qgsdatasourceselectdialog.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/***************************************************************************
2+
qgsdatasourceselectdialog.h - QgsDataSourceSelectDialog
3+
4+
---------------------
5+
begin : 1.11.2018
6+
copyright : (C) 2018 by ale
7+
email : [your-email-here]
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSDATASOURCESELECTDIALOG_H
17+
#define QGSDATASOURCESELECTDIALOG_H
18+
19+
#include <QObject>
20+
#include "ui_qgsdatasourceselectdialog.h"
21+
22+
#include "qgis_gui.h"
23+
#include "qgsmaplayer.h"
24+
#include "qgsmimedatautils.h"
25+
#include "qgsbrowsermodel.h"
26+
#include "qgsbrowserproxymodel.h"
27+
28+
29+
/**
30+
* \ingroup gui
31+
* The QgsDataSourceSelectDialog class embeds the browser view to
32+
* select an existing data source.
33+
*
34+
* By default it allows to select all layer types, the allowed layer
35+
* type can be restricted by setting a layer type filter with
36+
* setLayerTypeFilter(layerType) or by activating the filter
37+
* directly from the constructor.
38+
*
39+
* To retrieve the selected data source, uri() can be called and it
40+
* will return a (possibly invalid) QgsMimeDataUtils::Uri.
41+
*
42+
* \since QGIS 3.6
43+
*/
44+
class GUI_EXPORT QgsDataSourceSelectDialog: public QDialog, private Ui::QgsDataSourceSelectDialog
45+
{
46+
Q_OBJECT
47+
48+
public:
49+
50+
/**
51+
* Constructs a QgsDataSourceSelectDialog, optionally filtering by layer type
52+
*
53+
* \param setFilterByLayerType activates filtering by layer type
54+
* \param layerType sets the layer type filter, this is in effect only if filtering by layer type is also active
55+
* \param parent the object
56+
*/
57+
QgsDataSourceSelectDialog( bool setFilterByLayerType = false,
58+
const QgsMapLayer::LayerType &layerType = QgsMapLayer::LayerType::VectorLayer,
59+
QWidget *parent = nullptr );
60+
61+
~QgsDataSourceSelectDialog() override;
62+
63+
/**
64+
* Sets layer type filter to \a layerType and activates the filtering
65+
*/
66+
void setLayerTypeFilter( const QgsMapLayer::LayerType &layerType );
67+
68+
QgsMimeDataUtils::Uri uri() const;
69+
70+
private slots:
71+
72+
//! Triggered when a layer is selected in the browser
73+
void onLayerSelected( const QModelIndex &index );
74+
75+
private:
76+
77+
QgsBrowserModel mBrowserModel;
78+
QgsBrowserProxyModel mBrowserProxyModel;
79+
QgsMimeDataUtils::Uri mUri;
80+
81+
};
82+
83+
#endif // QGSDATASOURCESELECTDIALOG_H

‎src/ui/qgsdatasourceselectdialog.ui

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>QgsDataSourceSelectDialog</class>
4+
<widget class="QDialog" name="QgsDataSourceSelectDialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>400</width>
10+
<height>300</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Dialog</string>
15+
</property>
16+
<layout class="QVBoxLayout" name="verticalLayout">
17+
<item>
18+
<widget class="QgsBrowserTreeView" name="mBrowserTreeView"/>
19+
</item>
20+
<item>
21+
<widget class="QDialogButtonBox" name="buttonBox">
22+
<property name="orientation">
23+
<enum>Qt::Horizontal</enum>
24+
</property>
25+
<property name="standardButtons">
26+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
27+
</property>
28+
</widget>
29+
</item>
30+
</layout>
31+
</widget>
32+
<customwidgets>
33+
<customwidget>
34+
<class>QgsBrowserTreeView</class>
35+
<extends>QTreeView</extends>
36+
<header>qgsbrowsertreeview.h</header>
37+
</customwidget>
38+
</customwidgets>
39+
<resources/>
40+
<connections>
41+
<connection>
42+
<sender>buttonBox</sender>
43+
<signal>accepted()</signal>
44+
<receiver>QgsDataSourceSelectDialog</receiver>
45+
<slot>accept()</slot>
46+
<hints>
47+
<hint type="sourcelabel">
48+
<x>248</x>
49+
<y>254</y>
50+
</hint>
51+
<hint type="destinationlabel">
52+
<x>157</x>
53+
<y>274</y>
54+
</hint>
55+
</hints>
56+
</connection>
57+
<connection>
58+
<sender>buttonBox</sender>
59+
<signal>rejected()</signal>
60+
<receiver>QgsDataSourceSelectDialog</receiver>
61+
<slot>reject()</slot>
62+
<hints>
63+
<hint type="sourcelabel">
64+
<x>316</x>
65+
<y>260</y>
66+
</hint>
67+
<hint type="destinationlabel">
68+
<x>286</x>
69+
<y>274</y>
70+
</hint>
71+
</hints>
72+
</connection>
73+
</connections>
74+
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.