18 #include "ui_qgsdatasourceselectdialog.h" 24 #include <QPushButton> 29 bool setFilterByLayerType,
36 mBrowserModel = qgis::make_unique<QgsBrowserModel>();
37 mBrowserModel->initialize();
42 mBrowserModel.reset( browserModel );
47 setWindowTitle( tr(
"Select a Data Source" ) );
51 mBrowserTreeView->setHeaderHidden(
true );
53 if ( setFilterByLayerType )
60 mBrowserTreeView->setModel( &mBrowserProxyModel );
61 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled(
false );
64 mBrowserTreeView->setBrowserModel( mBrowserModel.get() );
66 mWidgetFilter->hide();
67 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
70 QMenu *menu =
new QMenu(
this );
71 menu->setSeparatorsCollapsible(
false );
72 mBtnFilterOptions->setMenu( menu );
73 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
74 action->setData(
"case" );
75 action->setCheckable(
true );
76 action->setChecked(
false );
78 menu->addAction( action );
79 QActionGroup *group =
new QActionGroup( menu );
80 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
81 action->setSeparator(
true );
82 menu->addAction( action );
83 action =
new QAction( tr(
"Normal" ), group );
85 action->setCheckable(
true );
86 action->setChecked(
true );
87 menu->addAction( action );
88 action =
new QAction( tr(
"Wildcard(s)" ), group );
90 action->setCheckable(
true );
91 menu->addAction( action );
92 action =
new QAction( tr(
"Regular Expression" ), group );
94 action->setCheckable(
true );
95 menu->addAction( action );
97 mBrowserTreeView->setExpandsOnDoubleClick(
false );
99 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
100 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectDialog::onLayerSelected );
101 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
108 if (
QgsSettings().value( QStringLiteral(
"datasourceSelectFilterVisible" ),
false, QgsSettings::Section::Gui ).toBool() )
110 mActionShowFilter->trigger();
117 mBrowserModel.release();
123 QDialog::showEvent( e );
124 QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
125 QString(), QgsSettings::Section::Gui ).toString() );
126 if ( ! lastSelectedPath.isEmpty() )
128 QModelIndexList items = mBrowserProxyModel.match(
129 mBrowserProxyModel.index( 0, 0 ),
131 QVariant::fromValue( lastSelectedPath ),
133 Qt::MatchRecursive );
134 if ( items.count( ) > 0 )
136 QModelIndex expandIndex = items.at( 0 );
137 if ( expandIndex.isValid() )
139 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
140 mBrowserTreeView->expand( expandIndex );
148 QgsSettings().
setValue( QStringLiteral(
"datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
149 mWidgetFilter->setVisible( visible );
152 mLeFilter->setText( QString() );
157 mLeFilter->setFocus();
163 QString filter = mLeFilter->text();
168 void QgsDataSourceSelectDialog::refreshModel(
const QModelIndex &index )
171 QgsDataItem *item = mBrowserModel->dataItem( index );
183 mBrowserModel->refresh( index );
186 for (
int i = 0; i < mBrowserModel->rowCount( index ); i++ )
188 QModelIndex idx = mBrowserModel->index( i, 0, index );
189 QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
190 QgsDataItem *child = mBrowserModel->dataItem( idx );
194 if ( mBrowserTreeView->isExpanded( proxyIdx ) || mBrowserTreeView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
213 mBrowserProxyModel.
setFilterSyntax( static_cast< QgsBrowserProxyModel::FilterSyntax >( action->data().toInt() ) );
226 mBrowserTreeView->setModel( &mBrowserProxyModel );
227 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled(
false );
235 void QgsDataSourceSelectDialog::onLayerSelected(
const QModelIndex &index )
237 bool isLayerCompatible =
false;
239 if ( index.isValid() )
248 isLayerCompatible =
true;
255 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isLayerCompatible );
QgsMimeDataUtils::Uri uri() const
Returns the (possibly invalid) uri of the selected data source.
void setFilterSyntax(QAction *)
Sets filter syntax.
void setLayerType(QgsMapLayer::LayerType type)
Sets the layer type to filter the model by.
This class is a composition of two QSettings instances:
QgsMapLayer::LayerType mapLayerType() const
Returns QgsMapLayer::LayerType.
void setFilter()
Apply filter to the model.
void setLayerTypeFilter(QgsMapLayer::LayerType layerType)
Sets layer type filter to layerType and activates the filtering.
void setBrowserModel(QgsBrowserModel *model)
Sets the underlying browser model.
virtual void depopulate()
Remove children recursively and set as not populated. This is used when refreshing collapsed items...
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
QgsDataSourceSelectDialog(QgsBrowserModel *browserModel=nullptr, bool setFilterByLayerType=false, const QgsMapLayer::LayerType &layerType=QgsMapLayer::LayerType::VectorLayer, QWidget *parent=nullptr)
Constructs a QgsDataSourceSelectDialog, optionally filtering by layer type.
Item path used to access path in the tree, see QgsDataItem::mPath.
void setCaseSensitive(bool caseSensitive)
Sets filter case sensitivity.
void showEvent(QShowEvent *e) override
Scroll to last selected index and expand it's children.
LayerType
Types of layers that can be added to a map.
Regular expression filtering.
void setFilterString(const QString &filter)
Sets the filter string to use when filtering items in the model.
QgsMapLayer::LayerType layerType() const
Returns the layer type to filter the model by.
~QgsDataSourceSelectDialog() override
void setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
bool filterByLayerType() const
Returns true if the model is filtered by map layer type.
Base class for all items in the model.
Can create children. Even items without this capability may have children, but cannot create them...
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsMimeDataUtils::Uri mimeUri() const override
Returns mime URI for the data item.
void cleared()
Emitted when the widget is cleared.
Standard string filtering.
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...
void showFilterWidget(bool visible)
Show/hide filter widget.
A model for showing available data sources and other items in a structured tree.
Item that represents a layer that can be opened with one of the providers.
void setFilterByLayerType(bool enabled)
Sets whether the model is filtered by map layer type.
QgsDataItem * dataItem(const QModelIndex &index) const
Returns the data item at the specified proxy index, or a nullptr if no item exists at the index...
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms...
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.