24 #include "qgsnative.h"
27 #include <QPushButton>
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 );
69 mBrowserTreeView->setBrowserModel( mBrowserModel );
71 mWidgetFilter->hide();
72 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
75 QMenu *menu =
new QMenu(
this );
76 menu->setSeparatorsCollapsible(
false );
77 mBtnFilterOptions->setMenu( menu );
78 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
79 action->setData(
"case" );
80 action->setCheckable(
true );
81 action->setChecked(
false );
83 menu->addAction( action );
84 QActionGroup *group =
new QActionGroup( menu );
85 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
86 action->setSeparator(
true );
87 menu->addAction( action );
88 action =
new QAction( tr(
"Normal" ), group );
90 action->setCheckable(
true );
91 action->setChecked(
true );
92 menu->addAction( action );
93 action =
new QAction( tr(
"Wildcard(s)" ), group );
95 action->setCheckable(
true );
96 menu->addAction( action );
97 action =
new QAction( tr(
"Regular Expression" ), group );
99 action->setCheckable(
true );
100 menu->addAction( action );
102 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
103 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
104 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
105 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
114 if (
QgsSettings().value( QStringLiteral(
"datasourceSelectFilterVisible" ),
false, QgsSettings::Section::Gui ).toBool() )
116 mActionShowFilter->trigger();
124 QgsPanelWidget::showEvent( e );
125 const QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
126 QString(), QgsSettings::Section::Gui ).toString() );
127 if ( ! lastSelectedPath.isEmpty() )
129 const QModelIndexList items = mBrowserProxyModel.match(
130 mBrowserProxyModel.index( 0, 0 ),
132 QVariant::fromValue( lastSelectedPath ),
134 Qt::MatchRecursive );
135 if ( items.count( ) > 0 )
137 const QModelIndex expandIndex = items.at( 0 );
138 if ( expandIndex.isValid() )
140 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
141 mBrowserTreeView->expand( expandIndex );
149 QgsSettings().
setValue( QStringLiteral(
"datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
150 mWidgetFilter->setVisible( visible );
153 mLeFilter->setText( QString() );
158 mLeFilter->setFocus();
164 if ( !description.isEmpty() )
166 if ( !mDescriptionLabel )
168 mDescriptionLabel =
new QLabel();
169 mDescriptionLabel->setWordWrap(
true );
170 mDescriptionLabel->setMargin( 4 );
171 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
172 connect( mDescriptionLabel, &QLabel::linkActivated,
this, [ = ](
const QString & link )
174 const QUrl url( link );
175 const QFileInfo file( url.toLocalFile() );
176 if ( file.exists() && !file.isDir() )
179 QDesktopServices::openUrl( url );
181 verticalLayout->insertWidget( 1, mDescriptionLabel );
183 mDescriptionLabel->setText( description );
187 if ( mDescriptionLabel )
189 verticalLayout->removeWidget( mDescriptionLabel );
190 delete mDescriptionLabel;
191 mDescriptionLabel =
nullptr;
198 const QString filter = mLeFilter->text();
203 void QgsDataSourceSelectWidget::refreshModel(
const QModelIndex &index )
218 mBrowserModel->
refresh( index );
221 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
223 const QModelIndex idx = mBrowserModel->
index( i, 0, index );
224 const QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
243 void QgsDataSourceSelectWidget::setValid(
bool valid )
245 const bool prev = mIsValid;
247 if ( prev != mIsValid )
270 mBrowserTreeView->setModel( &mBrowserProxyModel );
279 void QgsDataSourceSelectWidget::onLayerSelected(
const QModelIndex &index )
281 bool isLayerCompatible =
false;
283 if ( index.isValid() )
288 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
292 isLayerCompatible =
true;
299 setValid( isLayerCompatible );
303 void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
305 onLayerSelected( index );
317 setWindowTitle( tr(
"Select a Data Source" ) );
318 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
323 QVBoxLayout *vl =
new QVBoxLayout();
324 vl->addWidget( mWidget, 1 );
325 vl->setContentsMargins( 4, 4, 4, 4 );
326 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
327 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
328 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
329 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
336 vl->addWidget( buttonBox );
352 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,...
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 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.
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)