29#include <QDesktopServices>
30#include <QDialogButtonBox>
33#include <QActionGroup>
37 bool setFilterByLayerType,
49 mBrowserModel = browserModel;
56 mBrowserTreeView->setHeaderHidden(
true );
58 if ( setFilterByLayerType )
65 mBrowserTreeView->setModel( &mBrowserProxyModel );
66 mBrowserTreeView->setBrowserModel( mBrowserModel );
70 mBrowserTreeView->setBrowserModel( mBrowserModel );
72 mWidgetFilter->hide();
73 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
76 QMenu *menu =
new QMenu(
this );
77 menu->setSeparatorsCollapsible(
false );
78 mBtnFilterOptions->setMenu( menu );
79 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
80 action->setData(
"case" );
81 action->setCheckable(
true );
82 action->setChecked(
false );
84 menu->addAction( action );
85 QActionGroup *group =
new QActionGroup( menu );
86 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
87 action->setSeparator(
true );
88 menu->addAction( action );
89 action =
new QAction( tr(
"Normal" ), group );
91 action->setCheckable(
true );
92 action->setChecked(
true );
93 menu->addAction( action );
94 action =
new QAction( tr(
"Wildcard(s)" ), group );
96 action->setCheckable(
true );
97 menu->addAction( action );
98 action =
new QAction( tr(
"Regular Expression" ), group );
100 action->setCheckable(
true );
101 menu->addAction( action );
103 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
104 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
105 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
106 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
117 mActionShowFilter->trigger();
125 QgsPanelWidget::showEvent( e );
126 const QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
128 if ( ! lastSelectedPath.isEmpty() )
130 const QModelIndexList items = mBrowserProxyModel.match(
131 mBrowserProxyModel.index( 0, 0 ),
133 QVariant::fromValue( lastSelectedPath ),
135 Qt::MatchRecursive );
136 if ( items.count( ) > 0 )
138 const QModelIndex expandIndex = items.at( 0 );
139 if ( expandIndex.isValid() )
141 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
142 mBrowserTreeView->expand( expandIndex );
151 mWidgetFilter->setVisible( visible );
154 mLeFilter->setText( QString() );
159 mLeFilter->setFocus();
165 if ( !description.isEmpty() )
167 if ( !mDescriptionLabel )
169 mDescriptionLabel =
new QLabel();
170 mDescriptionLabel->setWordWrap(
true );
171 mDescriptionLabel->setMargin( 4 );
172 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
173 connect( mDescriptionLabel, &QLabel::linkActivated,
this, [ = ](
const QString & link )
175 const QUrl url( link );
176 const QFileInfo file( url.toLocalFile() );
177 if ( file.exists() && !file.isDir() )
180 QDesktopServices::openUrl( url );
182 verticalLayout->insertWidget( 1, mDescriptionLabel );
184 mDescriptionLabel->setText( description );
188 if ( mDescriptionLabel )
190 verticalLayout->removeWidget( mDescriptionLabel );
191 delete mDescriptionLabel;
192 mDescriptionLabel =
nullptr;
199 mBrowserTreeView->expandPath( path );
204 const QString filter = mLeFilter->text();
209void QgsDataSourceSelectWidget::refreshModel(
const QModelIndex &index )
224 mBrowserModel->
refresh( index );
227 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
229 const QModelIndex idx = mBrowserModel->
index( i, 0, index );
230 const QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
249void QgsDataSourceSelectWidget::setValid(
bool valid )
251 const bool prev = mIsValid;
253 if ( prev != mIsValid )
276 mBrowserTreeView->setModel( &mBrowserProxyModel );
277 mBrowserTreeView->setBrowserModel( mBrowserModel );
286void QgsDataSourceSelectWidget::onLayerSelected(
const QModelIndex &index )
288 bool isLayerCompatible =
false;
290 if ( index.isValid() )
295 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
299 isLayerCompatible =
true;
306 setValid( isLayerCompatible );
310void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
312 onLayerSelected( index );
324 setWindowTitle( tr(
"Select a Data Source" ) );
325 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
330 QVBoxLayout *vl =
new QVBoxLayout();
331 vl->addWidget( mWidget, 1 );
332 vl->setContentsMargins( 4, 4, 4, 4 );
333 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
334 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
335 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
336 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
343 vl->addWidget( buttonBox );
364 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
@ PathRole
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 expandPath(const QString &path)
Expands out a file path in the view.
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 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)