29#include <QDesktopServices>
30#include <QDialogButtonBox>
33#include <QActionGroup>
38 bool setFilterByLayerType,
50 mBrowserModel = browserModel;
57 mBrowserTreeView->setHeaderHidden(
true );
59 if ( setFilterByLayerType )
66 mBrowserTreeView->setModel( &mBrowserProxyModel );
67 mBrowserTreeView->setBrowserModel( mBrowserModel );
71 mBrowserTreeView->setBrowserModel( mBrowserModel );
73 mWidgetFilter->hide();
74 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
77 QMenu *menu =
new QMenu(
this );
78 menu->setSeparatorsCollapsible(
false );
79 mBtnFilterOptions->setMenu( menu );
80 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
81 action->setData(
"case" );
82 action->setCheckable(
true );
83 action->setChecked(
false );
85 menu->addAction( action );
86 QActionGroup *group =
new QActionGroup( menu );
87 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
88 action->setSeparator(
true );
89 menu->addAction( action );
90 action =
new QAction( tr(
"Normal" ), group );
92 action->setCheckable(
true );
93 action->setChecked(
true );
94 menu->addAction( action );
95 action =
new QAction( tr(
"Wildcard(s)" ), group );
97 action->setCheckable(
true );
98 menu->addAction( action );
99 action =
new QAction( tr(
"Regular Expression" ), group );
101 action->setCheckable(
true );
102 menu->addAction( action );
104 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
105 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
106 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
107 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
118 mActionShowFilter->trigger();
121 setAcceptDrops(
true );
128 QgsPanelWidget::showEvent( e );
129 const QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
131 if ( ! lastSelectedPath.isEmpty() )
133 const QModelIndexList items = mBrowserProxyModel.match(
134 mBrowserProxyModel.index( 0, 0 ),
136 QVariant::fromValue( lastSelectedPath ),
138 Qt::MatchRecursive );
139 if ( items.count( ) > 0 )
141 const QModelIndex expandIndex = items.at( 0 );
142 if ( expandIndex.isValid() )
144 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
145 mBrowserTreeView->expand( expandIndex );
151QString QgsDataSourceSelectWidget::acceptableFilePath( QDropEvent *event )
const
153 if ( event->mimeData()->hasUrls() )
155 const QList< QUrl > urls =
event->mimeData()->urls();
156 for (
const QUrl &url : urls )
158 const QString local = url.toLocalFile();
159 if ( local.isEmpty() )
162 if ( QFile::exists( local ) )
173 const QString filePath = acceptableFilePath( event );
174 if ( !filePath.isEmpty() )
176 event->acceptProposedAction();
186 const QString filePath = acceptableFilePath( event );
187 if ( !filePath.isEmpty() )
189 event->acceptProposedAction();
191 const QFileInfo fi( filePath );
206 mWidgetFilter->setVisible( visible );
209 mLeFilter->setText( QString() );
214 mLeFilter->setFocus();
220 if ( !description.isEmpty() )
222 if ( !mDescriptionLabel )
224 mDescriptionLabel =
new QLabel();
225 mDescriptionLabel->setWordWrap(
true );
226 mDescriptionLabel->setMargin( 4 );
227 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
228 connect( mDescriptionLabel, &QLabel::linkActivated,
this, [ = ](
const QString & link )
230 const QUrl url( link );
231 const QFileInfo file( url.toLocalFile() );
232 if ( file.exists() && !file.isDir() )
235 QDesktopServices::openUrl( url );
237 verticalLayout->insertWidget( 1, mDescriptionLabel );
239 mDescriptionLabel->setText( description );
243 if ( mDescriptionLabel )
245 verticalLayout->removeWidget( mDescriptionLabel );
246 delete mDescriptionLabel;
247 mDescriptionLabel =
nullptr;
254 mBrowserTreeView->expandPath( path, selectPath );
259 const QString filter = mLeFilter->text();
263void QgsDataSourceSelectWidget::refreshModel(
const QModelIndex &index )
278 mBrowserModel->
refresh( index );
281 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
283 const QModelIndex idx = mBrowserModel->
index( i, 0, index );
284 const QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
303void QgsDataSourceSelectWidget::setValid(
bool valid )
305 const bool prev = mIsValid;
307 if ( prev != mIsValid )
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 );
352 isLayerCompatible =
true;
359 setValid( isLayerCompatible );
363void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
365 onLayerSelected( index );
377 setWindowTitle( tr(
"Select a Data Source" ) );
378 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
383 QVBoxLayout *vl =
new QVBoxLayout();
384 vl->addWidget( mWidget, 1 );
385 vl->setContentsMargins( 4, 4, 4, 4 );
386 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
387 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
388 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
389 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
396 vl->addWidget( buttonBox );
417 return mWidget->
uri();
@ 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 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.
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...
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 ...
This class is a composition of two QSettings instances:
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)