24 #include "qgsnative.h"
26 #include <QPushButton>
28 #include <QDesktopServices>
29 #include <QDialogButtonBox>
35 bool setFilterByLayerType,
47 mBrowserModel = browserModel;
54 mBrowserTreeView->setHeaderHidden(
true );
56 if ( setFilterByLayerType )
63 mBrowserTreeView->setModel( &mBrowserProxyModel );
67 mBrowserTreeView->setBrowserModel( mBrowserModel );
69 mWidgetFilter->hide();
70 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
73 QMenu *menu =
new QMenu(
this );
74 menu->setSeparatorsCollapsible(
false );
75 mBtnFilterOptions->setMenu( menu );
76 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
77 action->setData(
"case" );
78 action->setCheckable(
true );
79 action->setChecked(
false );
81 menu->addAction( action );
82 QActionGroup *group =
new QActionGroup( menu );
83 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
84 action->setSeparator(
true );
85 menu->addAction( action );
86 action =
new QAction( tr(
"Normal" ), group );
88 action->setCheckable(
true );
89 action->setChecked(
true );
90 menu->addAction( action );
91 action =
new QAction( tr(
"Wildcard(s)" ), group );
93 action->setCheckable(
true );
94 menu->addAction( action );
95 action =
new QAction( tr(
"Regular Expression" ), group );
97 action->setCheckable(
true );
98 menu->addAction( action );
100 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
101 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
102 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
103 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
112 if (
QgsSettings().value( QStringLiteral(
"datasourceSelectFilterVisible" ),
false, QgsSettings::Section::Gui ).toBool() )
114 mActionShowFilter->trigger();
122 QgsPanelWidget::showEvent( e );
123 QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
124 QString(), QgsSettings::Section::Gui ).toString() );
125 if ( ! lastSelectedPath.isEmpty() )
127 QModelIndexList items = mBrowserProxyModel.match(
128 mBrowserProxyModel.index( 0, 0 ),
130 QVariant::fromValue( lastSelectedPath ),
132 Qt::MatchRecursive );
133 if ( items.count( ) > 0 )
135 QModelIndex expandIndex = items.at( 0 );
136 if ( expandIndex.isValid() )
138 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
139 mBrowserTreeView->expand( expandIndex );
147 QgsSettings().
setValue( QStringLiteral(
"datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
148 mWidgetFilter->setVisible( visible );
151 mLeFilter->setText( QString() );
156 mLeFilter->setFocus();
162 if ( !description.isEmpty() )
164 if ( !mDescriptionLabel )
166 mDescriptionLabel =
new QLabel();
167 mDescriptionLabel->setWordWrap(
true );
168 mDescriptionLabel->setMargin( 4 );
169 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
170 connect( mDescriptionLabel, &QLabel::linkActivated,
this, [ = ](
const QString & link )
173 QFileInfo file( url.toLocalFile() );
174 if ( file.exists() && !file.isDir() )
177 QDesktopServices::openUrl( url );
179 verticalLayout->insertWidget( 1, mDescriptionLabel );
181 mDescriptionLabel->setText( description );
185 if ( mDescriptionLabel )
187 verticalLayout->removeWidget( mDescriptionLabel );
188 delete mDescriptionLabel;
189 mDescriptionLabel =
nullptr;
196 QString filter = mLeFilter->text();
201 void QgsDataSourceSelectWidget::refreshModel(
const QModelIndex &index )
216 mBrowserModel->
refresh( index );
219 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
221 QModelIndex idx = mBrowserModel->
index( i, 0, index );
222 QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
227 if ( mBrowserTreeView->isExpanded( proxyIdx ) || mBrowserTreeView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
241 void QgsDataSourceSelectWidget::setValid(
bool valid )
243 const bool prev = mIsValid;
245 if ( prev != mIsValid )
268 mBrowserTreeView->setModel( &mBrowserProxyModel );
277 void QgsDataSourceSelectWidget::onLayerSelected(
const QModelIndex &index )
279 bool isLayerCompatible =
false;
281 if ( index.isValid() )
286 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
290 isLayerCompatible =
true;
297 setValid( isLayerCompatible );
301 void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
303 onLayerSelected( index );
315 setWindowTitle( tr(
"Select a Data Source" ) );
316 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
321 QVBoxLayout *vl =
new QVBoxLayout();
322 vl->addWidget( mWidget, 1 );
323 vl->setContentsMargins( 4, 4, 4, 4 );
324 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
325 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
326 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
327 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
334 vl->addWidget( buttonBox );
350 return mWidget->
uri();
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 setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
QgsMapLayerType layerType() const
Returns the layer type to filter the model by.
void setLayerType(QgsMapLayerType type)
Sets the layer type to filter the model by.
bool filterByLayerType() const
Returns true if the model is filtered by map layer type.
void setBrowserModel(QgsBrowserModel *model)
Sets the underlying browser model.
Base class for all items in the model.
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
@ 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,...
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.
QgsDataSourceSelectDialog(QgsBrowserGuiModel *browserModel=nullptr, bool setFilterByLayerType=false, QgsMapLayerType layerType=QgsMapLayerType::VectorLayer, QWidget *parent=nullptr)
Constructs a QgsDataSourceSelectDialog, optionally filtering by layer type.
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.
void setLayerTypeFilter(QgsMapLayerType layerType)
Sets layer type filter to layerType and activates the filtering.
void cleared()
Emitted when the widget is cleared.
static QgsGui * instance()
Returns a pointer to the singleton instance.
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.
QgsMapLayerType mapLayerType() const
Returns QgsMapLayerType.
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.
QgsMapLayerType
Types of layers that can be added to a map.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
#define QgsDebugMsgLevel(str, level)