QGIS API Documentation 3.41.0-Master (57ec4277f5e)
Loading...
Searching...
No Matches
qgsdatasourceselectdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatasourceselectdialog.cpp - QgsDataSourceSelectDialog
3
4 ---------------------
5 begin : 1.11.2018
6 copyright : (C) 2018 by Alessandro Pasotti
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
18#include "moc_qgsdatasourceselectdialog.cpp"
19
20#include "qgis.h"
21#include "qgsbrowsermodel.h"
22#include "qgsgui.h"
23#include "qgsguiutils.h"
24#include "qgssettings.h"
25#include "qgsnative.h"
26#include "qgslayeritem.h"
27
28#include <QPushButton>
29#include <QMenu>
30#include <QDesktopServices>
31#include <QDialogButtonBox>
32#include <QFileInfo>
33#include <QUrl>
34#include <QActionGroup>
35#include <QDir>
36
38 QgsBrowserGuiModel *browserModel,
39 bool setFilterByLayerType,
40 Qgis::LayerType layerType,
41 QWidget *parent
42)
43 : QgsPanelWidget( parent )
44{
45 if ( !browserModel )
46 {
47 mBrowserModel = new QgsBrowserGuiModel( this );
48 mBrowserModel->initialize();
49 }
50 else
51 {
52 mBrowserModel = browserModel;
53 mBrowserModel->initialize();
54 }
55
56 setupUi( this );
57
58 mBrowserProxyModel.setBrowserModel( mBrowserModel );
59 mBrowserTreeView->setHeaderHidden( true );
60
61 if ( setFilterByLayerType )
62 {
63 // This will also set the (proxy) model
64 setLayerTypeFilter( layerType );
65 }
66 else
67 {
68 mBrowserTreeView->setModel( &mBrowserProxyModel );
69 mBrowserTreeView->setBrowserModel( mBrowserModel );
70 setValid( false );
71 }
72
73 mBrowserTreeView->setBrowserModel( mBrowserModel );
74
75 mWidgetFilter->hide();
76 mLeFilter->setPlaceholderText( tr( "Type here to filter visible items…" ) );
77 // icons from http://www.fatcow.com/free-icons License: CC Attribution 3.0
78
79 QMenu *menu = new QMenu( this );
80 menu->setSeparatorsCollapsible( false );
81 mBtnFilterOptions->setMenu( menu );
82 QAction *action = new QAction( tr( "Case Sensitive" ), menu );
83 action->setData( "case" );
84 action->setCheckable( true );
85 action->setChecked( false );
86 connect( action, &QAction::toggled, this, &QgsDataSourceSelectWidget::setCaseSensitive );
87 menu->addAction( action );
88 QActionGroup *group = new QActionGroup( menu );
89 action = new QAction( tr( "Filter Pattern Syntax" ), group );
90 action->setSeparator( true );
91 menu->addAction( action );
92 action = new QAction( tr( "Normal" ), group );
93 action->setData( QgsBrowserProxyModel::Normal );
94 action->setCheckable( true );
95 action->setChecked( true );
96 menu->addAction( action );
97 action = new QAction( tr( "Wildcard(s)" ), group );
98 action->setData( QgsBrowserProxyModel::Wildcards );
99 action->setCheckable( true );
100 menu->addAction( action );
101 action = new QAction( tr( "Regular Expression" ), group );
103 action->setCheckable( true );
104 menu->addAction( action );
105
106 connect( mActionRefresh, &QAction::triggered, this, [=] { refreshModel( QModelIndex() ); } );
107 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked, this, &QgsDataSourceSelectWidget::onLayerSelected );
108 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked, this, &QgsDataSourceSelectWidget::itemDoubleClicked );
109 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
110 connect( mActionShowFilter, &QAction::triggered, this, &QgsDataSourceSelectWidget::showFilterWidget );
111 connect( mLeFilter, &QgsFilterLineEdit::returnPressed, this, &QgsDataSourceSelectWidget::setFilter );
113 connect( mLeFilter, &QgsFilterLineEdit::textChanged, this, &QgsDataSourceSelectWidget::setFilter );
114 connect( group, &QActionGroup::triggered, this, &QgsDataSourceSelectWidget::setFilterSyntax );
115
116 mBrowserToolbar->setIconSize( QgsGuiUtils::iconSize( true ) );
117
118 if ( QgsSettings().value( QStringLiteral( "datasourceSelectFilterVisible" ), false, QgsSettings::Section::Gui ).toBool() )
119 {
120 mActionShowFilter->trigger();
121 }
122
123 setAcceptDrops( true );
124}
125
127
129{
130 QgsPanelWidget::showEvent( e );
131 const QString lastSelectedPath( QgsSettings().value( QStringLiteral( "datasourceSelectLastSelectedItem" ), QString(), QgsSettings::Section::Gui ).toString() );
132 if ( !lastSelectedPath.isEmpty() )
133 {
134 const QModelIndexList items = mBrowserProxyModel.match(
135 mBrowserProxyModel.index( 0, 0 ),
136 static_cast<int>( QgsBrowserModel::CustomRole::Path ),
137 QVariant::fromValue( lastSelectedPath ),
138 1,
139 Qt::MatchRecursive
140 );
141 if ( items.count() > 0 )
142 {
143 const QModelIndex expandIndex = items.at( 0 );
144 if ( expandIndex.isValid() )
145 {
146 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
147 mBrowserTreeView->expand( expandIndex );
148 }
149 }
150 }
151}
152
153QString QgsDataSourceSelectWidget::acceptableFilePath( QDropEvent *event ) const
154{
155 if ( event->mimeData()->hasUrls() )
156 {
157 const QList<QUrl> urls = event->mimeData()->urls();
158 for ( const QUrl &url : urls )
159 {
160 const QString local = url.toLocalFile();
161 if ( local.isEmpty() )
162 continue;
163
164 if ( QFile::exists( local ) )
165 {
166 return local;
167 }
168 }
169 }
170 return QString();
171}
172
173void QgsDataSourceSelectWidget::dragEnterEvent( QDragEnterEvent *event )
174{
175 const QString filePath = acceptableFilePath( event );
176 if ( !filePath.isEmpty() )
177 {
178 event->acceptProposedAction();
179 }
180 else
181 {
182 event->ignore();
183 }
184}
185
187{
188 const QString filePath = acceptableFilePath( event );
189 if ( !filePath.isEmpty() )
190 {
191 event->acceptProposedAction();
192
193 const QFileInfo fi( filePath );
194 if ( fi.isDir() )
195 {
196 expandPath( filePath, true );
197 }
198 else
199 {
200 expandPath( fi.dir().path(), true );
201 }
202 }
203}
204
206{
207 QgsSettings().setValue( QStringLiteral( "datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
208 mWidgetFilter->setVisible( visible );
209 if ( !visible )
210 {
211 mLeFilter->setText( QString() );
212 setFilter();
213 }
214 else
215 {
216 mLeFilter->setFocus();
217 }
218}
219
220void QgsDataSourceSelectWidget::setDescription( const QString &description )
221{
222 if ( !description.isEmpty() )
223 {
224 if ( !mDescriptionLabel )
225 {
226 mDescriptionLabel = new QLabel();
227 mDescriptionLabel->setWordWrap( true );
228 mDescriptionLabel->setMargin( 4 );
229 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
230 connect( mDescriptionLabel, &QLabel::linkActivated, this, [=]( const QString &link ) {
231 const QUrl url( link );
232 const QFileInfo file( url.toLocalFile() );
233 if ( file.exists() && !file.isDir() )
234 QgsGui::nativePlatformInterface()->openFileExplorerAndSelectFile( url.toLocalFile() );
235 else
236 QDesktopServices::openUrl( url );
237 } );
238 verticalLayout->insertWidget( 1, mDescriptionLabel );
239 }
240 mDescriptionLabel->setText( description );
241 }
242 else
243 {
244 if ( mDescriptionLabel )
245 {
246 verticalLayout->removeWidget( mDescriptionLabel );
247 delete mDescriptionLabel;
248 mDescriptionLabel = nullptr;
249 }
250 }
251}
252
253void QgsDataSourceSelectWidget::expandPath( const QString &path, bool selectPath )
254{
255 mBrowserTreeView->expandPath( path, selectPath );
256}
257
259{
260 const QString filter = mLeFilter->text();
261 mBrowserProxyModel.setFilterString( filter );
262}
263
264void QgsDataSourceSelectWidget::refreshModel( const QModelIndex &index )
265{
266 QgsDataItem *item = mBrowserModel->dataItem( index );
267 if ( item )
268 {
269 QgsDebugMsgLevel( "path = " + item->path(), 2 );
270 }
271 else
272 {
273 QgsDebugMsgLevel( QStringLiteral( "invalid item" ), 2 );
274 }
275
276 if ( item && ( item->capabilities2() & Qgis::BrowserItemCapability::Fertile ) )
277 {
278 mBrowserModel->refresh( index );
279 }
280
281 for ( int i = 0; i < mBrowserModel->rowCount( index ); i++ )
282 {
283 const QModelIndex idx = mBrowserModel->index( i, 0, index );
284 const QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
285 QgsDataItem *child = mBrowserModel->dataItem( idx );
286
287 // Check also expanded descendants so that the whole expanded path does not get collapsed if one item is collapsed.
288 // Fast items (usually root items) are refreshed so that when collapsed, it is obvious they are if empty (no expand symbol).
289 if ( mBrowserTreeView->isExpanded( proxyIdx ) || mBrowserTreeView->hasExpandedDescendant( proxyIdx ) || ( child && child->capabilities2() & Qgis::BrowserItemCapability::Fast ) )
290 {
291 refreshModel( idx );
292 }
293 else
294 {
295 if ( child && ( child->capabilities2() & Qgis::BrowserItemCapability::Fertile ) )
296 {
297 child->depopulate();
298 }
299 }
300 }
301}
302
303void QgsDataSourceSelectWidget::setValid( bool valid )
304{
305 const bool prev = mIsValid;
306 mIsValid = valid;
307 if ( prev != mIsValid )
308 emit validationChanged( mIsValid );
309}
310
312{
313 if ( !action )
314 return;
315 mBrowserProxyModel.setFilterSyntax( static_cast<QgsBrowserProxyModel::FilterSyntax>( action->data().toInt() ) );
316}
317
319{
320 mBrowserProxyModel.setFilterCaseSensitivity( caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
321}
322
324{
325 mBrowserProxyModel.setFilterByLayerType( true );
326 mBrowserProxyModel.setLayerType( layerType );
327 // reset model and button
328 mBrowserTreeView->setModel( &mBrowserProxyModel );
329 mBrowserTreeView->setBrowserModel( mBrowserModel );
330 setValid( false );
331}
332
334{
335 return mUri;
336}
337
338void QgsDataSourceSelectWidget::onLayerSelected( const QModelIndex &index )
339{
340 bool isLayerCompatible = false;
341 mUri = QgsMimeDataUtils::Uri();
342 if ( index.isValid() )
343 {
344 const QgsDataItem *dataItem( mBrowserProxyModel.dataItem( index ) );
345 if ( dataItem )
346 {
347 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
348 if ( layerItem && ( !mBrowserProxyModel.filterByLayerType() || ( layerItem->mapLayerType() == mBrowserProxyModel.layerType() ) ) )
349 {
350 isLayerCompatible = true;
351 mUri = layerItem->mimeUris().isEmpty() ? QgsMimeDataUtils::Uri() : layerItem->mimeUris().first();
352 // Store last viewed item
353 QgsSettings().setValue( QStringLiteral( "datasourceSelectLastSelectedItem" ), mBrowserProxyModel.data( index, static_cast<int>( QgsBrowserModel::CustomRole::Path ) ).toString(), QgsSettings::Section::Gui );
354 }
355 }
356 }
357 setValid( isLayerCompatible );
358 emit selectionChanged();
359}
360
361void QgsDataSourceSelectWidget::itemDoubleClicked( const QModelIndex &index )
362{
363 onLayerSelected( index );
364 if ( mIsValid )
365 emit itemTriggered( uri() );
366}
367
368//
369// QgsDataSourceSelectDialog
370//
371
372QgsDataSourceSelectDialog::QgsDataSourceSelectDialog( QgsBrowserGuiModel *browserModel, bool setFilterByLayerType, Qgis::LayerType layerType, QWidget *parent )
373 : QDialog( parent )
374{
375 setWindowTitle( tr( "Select a Data Source" ) );
376 setObjectName( QStringLiteral( "QgsDataSourceSelectDialog" ) );
378
379 mWidget = new QgsDataSourceSelectWidget( browserModel, setFilterByLayerType, layerType );
380
381 QVBoxLayout *vl = new QVBoxLayout();
382 vl->addWidget( mWidget, 1 );
383 vl->setContentsMargins( 4, 4, 4, 4 );
384 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
385 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
386 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
387 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
388 connect( mWidget, &QgsDataSourceSelectWidget::validationChanged, buttonBox->button( QDialogButtonBox::Ok ), &QWidget::setEnabled );
389 connect( mWidget, &QgsDataSourceSelectWidget::itemTriggered, this, &QDialog::accept );
390
391 // pressing escape should reject the dialog
392 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
393
394 vl->addWidget( buttonBox );
395 setLayout( vl );
396}
397
399{
400 mWidget->setLayerTypeFilter( layerType );
401}
402
403void QgsDataSourceSelectDialog::setDescription( const QString &description )
404{
405 mWidget->setDescription( description );
406}
407
408void QgsDataSourceSelectDialog::expandPath( const QString &path, bool selectPath )
409{
410 mWidget->expandPath( path, selectPath );
411}
412
414{
415 return mWidget->uri();
416}
417
419{
420 mWidget->showFilterWidget( visible );
421}
422
424{
425 mWidget->setFilterSyntax( syntax );
426}
427
429{
430 mWidget->setCaseSensitive( caseSensitive );
431}
432
434{
435 mWidget->setFilter();
436}
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
LayerType
Types of layers that can be added to a map.
Definition qgis.h:169
A model for showing available data sources and other items in a structured tree.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
void refresh(const QString &path)
Refresh item specified by path.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
@ Path
Item path used to access path in the tree, see QgsDataItem::mPath.
FilterSyntax
Filter syntax options.
@ RegularExpression
Regular expression filtering.
@ Wildcards
Wildcard filtering.
@ Normal
Standard string filtering.
void setFilterByLayerType(bool enabled)
Sets whether the model is filtered by map layer type.
void setFilterString(const QString &filter)
Sets the filter string to use when filtering items in the model.
QgsDataItem * dataItem(const QModelIndex &index) const
Returns the data item at the specified proxy index, or nullptr if no item exists at the index.
void setLayerType(Qgis::LayerType type)
Sets the layer type to filter the model by.
void setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
bool filterByLayerType() const
Returns true if the model is filtered by map layer type.
Qgis::LayerType layerType() const
Returns the layer type to filter the model by.
void setBrowserModel(QgsBrowserModel *model)
Sets the underlying browser model.
Base class for all items in the model.
Definition qgsdataitem.h:46
QString path() const
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
virtual void depopulate()
Remove children recursively and set as not populated. This is used when refreshing collapsed items.
void setFilter()
Apply filter to the model.
void showFilterWidget(bool visible)
Show/hide filter widget.
void setCaseSensitive(bool caseSensitive)
Sets filter case sensitivity.
void setFilterSyntax(QAction *)
Sets filter syntax.
QgsMimeDataUtils::Uri uri() const
Returns the (possibly invalid) uri of the selected data source.
void expandPath(const QString &path, bool selectPath=false)
Expands out a file path in the view.
void setDescription(const QString &description)
Sets a description label.
QgsDataSourceSelectDialog(QgsBrowserGuiModel *browserModel=nullptr, bool setFilterByLayerType=false, Qgis::LayerType layerType=Qgis::LayerType::Vector, QWidget *parent=nullptr)
Constructs a QgsDataSourceSelectDialog, optionally filtering by layer type.
void setLayerTypeFilter(Qgis::LayerType layerType)
Sets layer type filter to layerType and activates the filtering.
The QgsDataSourceSelectWidget class embeds the browser view to select an existing data source.
void expandPath(const QString &path, bool selectPath=false)
Expands out a file path in the view.
void selectionChanged()
Emitted when the current selection changes in the widget.
void itemTriggered(const QgsMimeDataUtils::Uri &uri)
Emitted when an item is triggered, e.g.
void setFilter()
Apply filter to the model.
QgsDataSourceSelectWidget(QgsBrowserGuiModel *browserModel=nullptr, bool setFilterByLayerType=false, Qgis::LayerType layerType=Qgis::LayerType::Vector, QWidget *parent=nullptr)
Constructs a QgsDataSourceSelectWidget, optionally filtering by layer type.
void dropEvent(QDropEvent *event) override
void showFilterWidget(bool visible)
Show/hide filter widget.
void setDescription(const QString &description)
Sets a description label.
void validationChanged(bool isValid)
This signal is emitted whenever the validation status of the widget changes.
void showEvent(QShowEvent *e) override
Scroll to last selected index and expand it's children.
~QgsDataSourceSelectWidget() override
void setCaseSensitive(bool caseSensitive)
Sets filter case sensitivity.
void dragEnterEvent(QDragEnterEvent *event) override
void setFilterSyntax(QAction *)
Sets filter syntax.
QgsMimeDataUtils::Uri uri() const
Returns the (possibly invalid) uri of the selected data source.
void setLayerTypeFilter(Qgis::LayerType layerType)
Sets layer type filter to layerType and activates the filtering.
void cleared()
Emitted when the widget is cleared.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:210
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
Definition qgsgui.cpp:85
Item that represents a layer that can be opened with one of the providers.
Qgis::LayerType mapLayerType() const
Returns the associated map layer type.
QgsMimeDataUtils::UriList mimeUris() const override
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
Base class for any widget that can be shown as a inline panel.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39