18#include "moc_qgsdatasourceselectdialog.cpp"
30#include <QDesktopServices>
31#include <QDialogButtonBox>
34#include <QActionGroup>
39 bool setFilterByLayerType,
51 mBrowserModel = browserModel;
58 mBrowserTreeView->setHeaderHidden(
true );
60 if ( setFilterByLayerType )
67 mBrowserTreeView->setModel( &mBrowserProxyModel );
68 mBrowserTreeView->setBrowserModel( mBrowserModel );
72 mBrowserTreeView->setBrowserModel( mBrowserModel );
74 mWidgetFilter->hide();
75 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
78 QMenu *menu =
new QMenu(
this );
79 menu->setSeparatorsCollapsible(
false );
80 mBtnFilterOptions->setMenu( menu );
81 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
82 action->setData(
"case" );
83 action->setCheckable(
true );
84 action->setChecked(
false );
86 menu->addAction( action );
87 QActionGroup *group =
new QActionGroup( menu );
88 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
89 action->setSeparator(
true );
90 menu->addAction( action );
91 action =
new QAction( tr(
"Normal" ), group );
93 action->setCheckable(
true );
94 action->setChecked(
true );
95 menu->addAction( action );
96 action =
new QAction( tr(
"Wildcard(s)" ), group );
98 action->setCheckable(
true );
99 menu->addAction( action );
100 action =
new QAction( tr(
"Regular Expression" ), group );
102 action->setCheckable(
true );
103 menu->addAction( action );
105 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
106 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
107 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
108 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
119 mActionShowFilter->trigger();
122 setAcceptDrops(
true );
129 QgsPanelWidget::showEvent( e );
130 const QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
132 if ( ! lastSelectedPath.isEmpty() )
134 const QModelIndexList items = mBrowserProxyModel.match(
135 mBrowserProxyModel.index( 0, 0 ),
137 QVariant::fromValue( lastSelectedPath ),
139 Qt::MatchRecursive );
140 if ( items.count( ) > 0 )
142 const QModelIndex expandIndex = items.at( 0 );
143 if ( expandIndex.isValid() )
145 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
146 mBrowserTreeView->expand( expandIndex );
152QString QgsDataSourceSelectWidget::acceptableFilePath( QDropEvent *event )
const
154 if ( event->mimeData()->hasUrls() )
156 const QList< QUrl > urls =
event->mimeData()->urls();
157 for (
const QUrl &url : urls )
159 const QString local = url.toLocalFile();
160 if ( local.isEmpty() )
163 if ( QFile::exists( local ) )
174 const QString filePath = acceptableFilePath( event );
175 if ( !filePath.isEmpty() )
177 event->acceptProposedAction();
187 const QString filePath = acceptableFilePath( event );
188 if ( !filePath.isEmpty() )
190 event->acceptProposedAction();
192 const QFileInfo fi( filePath );
207 mWidgetFilter->setVisible( visible );
210 mLeFilter->setText( QString() );
215 mLeFilter->setFocus();
221 if ( !description.isEmpty() )
223 if ( !mDescriptionLabel )
225 mDescriptionLabel =
new QLabel();
226 mDescriptionLabel->setWordWrap(
true );
227 mDescriptionLabel->setMargin( 4 );
228 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
229 connect( mDescriptionLabel, &QLabel::linkActivated,
this, [ = ](
const QString & link )
231 const QUrl url( link );
232 const QFileInfo file( url.toLocalFile() );
233 if ( file.exists() && !file.isDir() )
236 QDesktopServices::openUrl( url );
238 verticalLayout->insertWidget( 1, mDescriptionLabel );
240 mDescriptionLabel->setText( description );
244 if ( mDescriptionLabel )
246 verticalLayout->removeWidget( mDescriptionLabel );
247 delete mDescriptionLabel;
248 mDescriptionLabel =
nullptr;
255 mBrowserTreeView->expandPath( path, selectPath );
260 const QString filter = mLeFilter->text();
264void QgsDataSourceSelectWidget::refreshModel(
const QModelIndex &index )
279 mBrowserModel->
refresh( index );
282 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
284 const QModelIndex idx = mBrowserModel->
index( i, 0, index );
285 const QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
304void QgsDataSourceSelectWidget::setValid(
bool valid )
306 const bool prev = mIsValid;
308 if ( prev != mIsValid )
330 mBrowserTreeView->setModel( &mBrowserProxyModel );
331 mBrowserTreeView->setBrowserModel( mBrowserModel );
340void QgsDataSourceSelectWidget::onLayerSelected(
const QModelIndex &index )
342 bool isLayerCompatible =
false;
344 if ( index.isValid() )
349 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
353 isLayerCompatible =
true;
360 setValid( isLayerCompatible );
364void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
366 onLayerSelected( index );
378 setWindowTitle( tr(
"Select a Data Source" ) );
379 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
384 QVBoxLayout *vl =
new QVBoxLayout();
385 vl->addWidget( mWidget, 1 );
386 vl->setContentsMargins( 4, 4, 4, 4 );
387 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
388 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
389 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
390 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
397 vl->addWidget( buttonBox );
418 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,...
LayerType
Types of layers that can be added to a map.
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
@ Path
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 setLayerType(Qgis::LayerType type)
Sets the layer type to filter the model by.
void setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
bool filterByLayerType() const
Returns true if the model is filtered by map layer type.
Qgis::LayerType layerType() const
Returns the layer type to filter the model by.
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.
void setFilterSyntax(QAction *)
Sets filter syntax.
QgsMimeDataUtils::Uri uri() const
Returns the (possibly invalid) uri of the selected data source.
void expandPath(const QString &path, bool selectPath=false)
Expands out a file path in the view.
void setDescription(const QString &description)
Sets a description label.
QgsDataSourceSelectDialog(QgsBrowserGuiModel *browserModel=nullptr, bool setFilterByLayerType=false, Qgis::LayerType layerType=Qgis::LayerType::Vector, QWidget *parent=nullptr)
Constructs a QgsDataSourceSelectDialog, optionally filtering by layer type.
void setLayerTypeFilter(Qgis::LayerType layerType)
Sets layer type filter to layerType and activates the filtering.
void cleared()
Emitted when the widget is cleared.
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.
Qgis::LayerType mapLayerType() const
Returns the associated map layer type.
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.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
#define QgsDebugMsgLevel(str, level)