18 #include "ui_qgsdatasourceselectdialog.h" 26 #include <QPushButton> 31 bool setFilterByLayerType,
38 mBrowserModel = qgis::make_unique<QgsBrowserModel>();
39 mBrowserModel->initialize();
44 mBrowserModel.reset( browserModel );
49 setWindowTitle( tr(
"Select a Data Source" ) );
53 mBrowserTreeView->setHeaderHidden(
true );
55 if ( setFilterByLayerType )
62 mBrowserTreeView->setModel( &mBrowserProxyModel );
63 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled(
false );
66 mBrowserTreeView->setBrowserModel( mBrowserModel.get() );
68 mWidgetFilter->hide();
69 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
72 QMenu *menu =
new QMenu(
this );
73 menu->setSeparatorsCollapsible(
false );
74 mBtnFilterOptions->setMenu( menu );
75 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
76 action->setData(
"case" );
77 action->setCheckable(
true );
78 action->setChecked(
false );
80 menu->addAction( action );
81 QActionGroup *group =
new QActionGroup( menu );
82 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
83 action->setSeparator(
true );
84 menu->addAction( action );
85 action =
new QAction( tr(
"Normal" ), group );
87 action->setCheckable(
true );
88 action->setChecked(
true );
89 menu->addAction( action );
90 action =
new QAction( tr(
"Wildcard(s)" ), group );
92 action->setCheckable(
true );
93 menu->addAction( action );
94 action =
new QAction( tr(
"Regular Expression" ), group );
96 action->setCheckable(
true );
97 menu->addAction( action );
99 mBrowserTreeView->setExpandsOnDoubleClick(
false );
101 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
102 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectDialog::onLayerSelected );
103 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectDialog::itemDoubleClicked );
104 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
113 if (
QgsSettings().value( QStringLiteral(
"datasourceSelectFilterVisible" ),
false, QgsSettings::Section::Gui ).toBool() )
115 mActionShowFilter->trigger();
122 mBrowserModel.release();
128 QDialog::showEvent( e );
129 QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
130 QString(), QgsSettings::Section::Gui ).toString() );
131 if ( ! lastSelectedPath.isEmpty() )
133 QModelIndexList items = mBrowserProxyModel.match(
134 mBrowserProxyModel.index( 0, 0 ),
136 QVariant::fromValue( lastSelectedPath ),
138 Qt::MatchRecursive );
139 if ( items.count( ) > 0 )
141 QModelIndex expandIndex = items.at( 0 );
142 if ( expandIndex.isValid() )
144 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
145 mBrowserTreeView->expand( expandIndex );
153 QgsSettings().
setValue( QStringLiteral(
"datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
154 mWidgetFilter->setVisible( visible );
157 mLeFilter->setText( QString() );
162 mLeFilter->setFocus();
168 if ( !description.isEmpty() )
170 if ( !mDescriptionLabel )
172 mDescriptionLabel =
new QLabel();
173 mDescriptionLabel->setWordWrap(
true );
174 mDescriptionLabel->setMargin( 4 );
175 verticalLayout->insertWidget( 1, mDescriptionLabel );
177 mDescriptionLabel->setText( description );
181 if ( mDescriptionLabel )
183 verticalLayout->removeWidget( mDescriptionLabel );
184 delete mDescriptionLabel;
185 mDescriptionLabel =
nullptr;
192 QString filter = mLeFilter->text();
197 void QgsDataSourceSelectDialog::refreshModel(
const QModelIndex &index )
200 QgsDataItem *item = mBrowserModel->dataItem( index );
212 mBrowserModel->refresh( index );
215 for (
int i = 0; i < mBrowserModel->rowCount( index ); i++ )
217 QModelIndex idx = mBrowserModel->index( i, 0, index );
218 QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
219 QgsDataItem *child = mBrowserModel->dataItem( idx );
223 if ( mBrowserTreeView->isExpanded( proxyIdx ) || mBrowserTreeView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
242 mBrowserProxyModel.
setFilterSyntax( static_cast< QgsBrowserProxyModel::FilterSyntax >( action->data().toInt() ) );
255 mBrowserTreeView->setModel( &mBrowserProxyModel );
256 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled(
false );
264 void QgsDataSourceSelectDialog::onLayerSelected(
const QModelIndex &index )
266 bool isLayerCompatible =
false;
268 if ( index.isValid() )
277 isLayerCompatible =
true;
284 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isLayerCompatible );
287 void QgsDataSourceSelectDialog::itemDoubleClicked(
const QModelIndex &index )
289 onLayerSelected( index );
290 if ( buttonBox->button( QDialogButtonBox::StandardButton::Ok )->isEnabled() )
void setDescription(const QString &description)
Sets a description label.
QgsMimeDataUtils::Uri uri() const
Returns the (possibly invalid) uri of the selected data source.
void setLayerTypeFilter(QgsMapLayerType layerType)
Sets layer type filter to layerType and activates the filtering.
QgsDataSourceSelectDialog(QgsBrowserModel *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.
This class is a composition of two QSettings instances:
void setLayerType(QgsMapLayerType type)
Sets the layer type to filter the model by.
void setFilter()
Apply filter to the model.
QgsMapLayerType mapLayerType() const
Returns QgsMapLayerType.
void setBrowserModel(QgsBrowserModel *model)
Sets the underlying browser model.
QgsMapLayerType layerType() const
Returns the layer type to filter the model by.
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.
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.
Regular expression filtering.
void setFilterString(const QString &filter)
Sets the filter string to use when filtering items in the model.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
~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.
QgsMapLayerType
Types of layers that can be added to a map.
QgsDataItem * dataItem(const QModelIndex &index) const
Returns the data item at the specified proxy index, or 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.