18 #include "ui_qgsdatasourceselectdialog.h" 25 #include "qgsnative.h" 27 #include <QPushButton> 29 #include <QDesktopServices> 33 bool setFilterByLayerType,
45 mBrowserModel = browserModel;
50 setWindowTitle( tr(
"Select a Data Source" ) );
54 mBrowserTreeView->setHeaderHidden(
true );
56 if ( setFilterByLayerType )
63 mBrowserTreeView->setModel( &mBrowserProxyModel );
64 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled(
false );
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 mBrowserTreeView->setExpandsOnDoubleClick(
false );
102 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
103 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectDialog::onLayerSelected );
104 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectDialog::itemDoubleClicked );
105 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
114 if (
QgsSettings().value( QStringLiteral(
"datasourceSelectFilterVisible" ),
false, QgsSettings::Section::Gui ).toBool() )
116 mActionShowFilter->trigger();
124 QDialog::showEvent( e );
125 QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
126 QString(), QgsSettings::Section::Gui ).toString() );
127 if ( ! lastSelectedPath.isEmpty() )
129 QModelIndexList items = mBrowserProxyModel.match(
130 mBrowserProxyModel.index( 0, 0 ),
132 QVariant::fromValue( lastSelectedPath ),
134 Qt::MatchRecursive );
135 if ( items.count( ) > 0 )
137 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 )
175 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 QString filter = mLeFilter->text();
203 void QgsDataSourceSelectDialog::refreshModel(
const QModelIndex &index )
218 mBrowserModel->
refresh( index );
221 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
223 QModelIndex idx = mBrowserModel->
index( i, 0, index );
224 QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
229 if ( mBrowserTreeView->isExpanded( proxyIdx ) || mBrowserTreeView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
248 mBrowserProxyModel.
setFilterSyntax( static_cast< QgsBrowserProxyModel::FilterSyntax >( action->data().toInt() ) );
261 mBrowserTreeView->setModel( &mBrowserProxyModel );
262 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled(
false );
270 void QgsDataSourceSelectDialog::onLayerSelected(
const QModelIndex &index )
272 bool isLayerCompatible =
false;
274 if ( index.isValid() )
283 isLayerCompatible =
true;
290 buttonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isLayerCompatible );
293 void QgsDataSourceSelectDialog::itemDoubleClicked(
const QModelIndex &index )
295 onLayerSelected( index );
296 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.
void setFilterSyntax(QAction *)
Sets filter syntax.
This class is a composition of two QSettings instances:
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
int rowCount(const QModelIndex &parent=QModelIndex()) const override
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.
QgsDataSourceSelectDialog(QgsBrowserGuiModel *browserModel=nullptr, bool setFilterByLayerType=false, QgsMapLayerType layerType=QgsMapLayerType::VectorLayer, QWidget *parent=nullptr)
Constructs a QgsDataSourceSelectDialog, optionally filtering by layer type.
virtual void depopulate()
Remove children recursively and set as not populated. This is used when refreshing collapsed items...
A model for showing available data sources and other items in a structured tree.
static QgsGui * instance()
Returns a pointer to the singleton instance.
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.
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index...
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.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void cleared()
Emitted when the widget is cleared.
void refresh(const QString &path)
Refresh item specified by path.
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.
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.