27#include <QActionGroup>
28#include <QDesktopServices>
29#include <QDialogButtonBox>
36#include "moc_qgsdatasourceselectdialog.cpp"
40 bool setFilterByLayerType,
49 mBrowserModel->initialize();
53 mBrowserModel = browserModel;
59 mBrowserProxyModel.setBrowserModel( mBrowserModel );
60 mBrowserTreeView->setHeaderHidden(
true );
62 if ( setFilterByLayerType )
69 mBrowserTreeView->setModel( &mBrowserProxyModel );
70 mBrowserTreeView->setBrowserModel( mBrowserModel );
74 mBrowserTreeView->setBrowserModel( mBrowserModel );
76 mWidgetFilter->hide();
77 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
80 QMenu *menu =
new QMenu(
this );
81 menu->setSeparatorsCollapsible(
false );
82 mBtnFilterOptions->setMenu( menu );
83 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
84 action->setData(
"case" );
85 action->setCheckable(
true );
86 action->setChecked(
false );
88 menu->addAction( action );
89 QActionGroup *group =
new QActionGroup( menu );
90 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
91 action->setSeparator(
true );
92 menu->addAction( action );
93 action =
new QAction( tr(
"Normal" ), group );
95 action->setCheckable(
true );
96 action->setChecked(
true );
97 menu->addAction( action );
98 action =
new QAction( tr(
"Wildcard(s)" ), group );
100 action->setCheckable(
true );
101 menu->addAction( action );
102 action =
new QAction( tr(
"Regular Expression" ), group );
104 action->setCheckable(
true );
105 menu->addAction( action );
107 connect( mActionRefresh, &QAction::triggered,
this, [
this] { refreshModel( QModelIndex() ); } );
108 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
109 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
110 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
121 mActionShowFilter->trigger();
124 setAcceptDrops(
true );
131 QgsPanelWidget::showEvent( e );
133 if ( !lastSelectedPath.isEmpty() )
135 const QModelIndexList items = mBrowserProxyModel.match(
136 mBrowserProxyModel.index( 0, 0 ),
138 QVariant::fromValue( lastSelectedPath ),
142 if ( items.count() > 0 )
144 const QModelIndex expandIndex = items.at( 0 );
145 if ( expandIndex.isValid() )
147 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
148 mBrowserTreeView->expand( expandIndex );
154QString QgsDataSourceSelectWidget::acceptableFilePath( QDropEvent *event )
const
156 if ( event->mimeData()->hasUrls() )
158 const QList<QUrl> urls =
event->mimeData()->urls();
159 for (
const QUrl &url : urls )
161 const QString local = url.toLocalFile();
162 if ( local.isEmpty() )
165 if ( QFile::exists( local ) )
176 const QString filePath = acceptableFilePath( event );
177 if ( !filePath.isEmpty() )
179 event->acceptProposedAction();
189 const QString filePath = acceptableFilePath( event );
190 if ( !filePath.isEmpty() )
192 event->acceptProposedAction();
194 const QFileInfo fi( filePath );
209 mWidgetFilter->setVisible( visible );
212 mLeFilter->setText( QString() );
217 mLeFilter->setFocus();
223 if ( !description.isEmpty() )
225 if ( !mDescriptionLabel )
227 mDescriptionLabel =
new QLabel();
228 mDescriptionLabel->setWordWrap(
true );
229 mDescriptionLabel->setMargin( 4 );
230 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
231 connect( mDescriptionLabel, &QLabel::linkActivated,
this, [](
const QString &link ) {
232 const QUrl url( link );
233 const QFileInfo file( url.toLocalFile() );
234 if ( file.exists() && !file.isDir() )
237 QDesktopServices::openUrl( url );
239 verticalLayout->insertWidget( 1, mDescriptionLabel );
241 mDescriptionLabel->setText( description );
245 if ( mDescriptionLabel )
247 verticalLayout->removeWidget( mDescriptionLabel );
248 delete mDescriptionLabel;
249 mDescriptionLabel =
nullptr;
256 mBrowserTreeView->expandPath( path, selectPath );
261 const QString filter = mLeFilter->text();
262 mBrowserProxyModel.setFilterString( filter );
265void QgsDataSourceSelectWidget::refreshModel(
const QModelIndex &index )
279 mBrowserModel->refresh( index );
282 for (
int i = 0; i < mBrowserModel->rowCount( index ); i++ )
284 const QModelIndex idx = mBrowserModel->index( i, 0, index );
285 const QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
286 QgsDataItem *child = mBrowserModel->dataItem( idx );
304void QgsDataSourceSelectWidget::setValid(
bool valid )
306 const bool prev = mIsValid;
308 if ( prev != mIsValid )
321 mBrowserProxyModel.setFilterCaseSensitivity( caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
326 mBrowserProxyModel.setFilterByLayerType(
true );
327 mBrowserProxyModel.setLayerType( layerType );
329 mBrowserTreeView->setModel( &mBrowserProxyModel );
330 mBrowserTreeView->setBrowserModel( mBrowserModel );
339void QgsDataSourceSelectWidget::onLayerSelected(
const QModelIndex &index )
341 bool isLayerCompatible =
false;
343 if ( index.isValid() )
348 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
351 isLayerCompatible =
true;
358 setValid( isLayerCompatible );
362void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
364 onLayerSelected( index );
376 setWindowTitle( tr(
"Select a Data Source" ) );
377 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
382 QVBoxLayout *vl =
new QVBoxLayout();
383 vl->addWidget( mWidget, 1 );
384 vl->setContentsMargins( 4, 4, 4, 4 );
385 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
386 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
387 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
388 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
395 vl->addWidget( buttonBox );
401 mWidget->setLayerTypeFilter( layerType );
406 mWidget->setDescription( description );
411 mWidget->expandPath( path, selectPath );
416 return mWidget->
uri();
421 mWidget->showFilterWidget( visible );
426 mWidget->setFilterSyntax( syntax );
431 mWidget->setCaseSensitive( caseSensitive );
436 mWidget->setFilter();
@ 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.
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 initialize()
Delayed initialization, needed because the provider registry must be already populated.
@ 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.
QgsDataItem * dataItem(const QModelIndex &index) const
Returns the data item at the specified proxy index, or nullptr if no item exists at the index.
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.
Base class for all items in the model.
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.
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...
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
A browser 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 ...
Stores settings for use within QGIS.
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)
QString uri
Identifier of the data source recognized by its providerKey.