18#include "moc_qgsdatasourceselectdialog.cpp"
30#include <QDesktopServices>
31#include <QDialogButtonBox>
34#include <QActionGroup>
39 bool setFilterByLayerType,
52 mBrowserModel = browserModel;
59 mBrowserTreeView->setHeaderHidden(
true );
61 if ( setFilterByLayerType )
68 mBrowserTreeView->setModel( &mBrowserProxyModel );
69 mBrowserTreeView->setBrowserModel( mBrowserModel );
73 mBrowserTreeView->setBrowserModel( mBrowserModel );
75 mWidgetFilter->hide();
76 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
79 QMenu *menu =
new QMenu(
this );
80 menu->setSeparatorsCollapsible(
false );
81 mBtnFilterOptions->setMenu( menu );
82 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
83 action->setData(
"case" );
84 action->setCheckable(
true );
85 action->setChecked(
false );
87 menu->addAction( action );
88 QActionGroup *group =
new QActionGroup( menu );
89 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
90 action->setSeparator(
true );
91 menu->addAction( action );
92 action =
new QAction( tr(
"Normal" ), group );
94 action->setCheckable(
true );
95 action->setChecked(
true );
96 menu->addAction( action );
97 action =
new QAction( tr(
"Wildcard(s)" ), group );
99 action->setCheckable(
true );
100 menu->addAction( action );
101 action =
new QAction( tr(
"Regular Expression" ), group );
103 action->setCheckable(
true );
104 menu->addAction( action );
106 connect( mActionRefresh, &QAction::triggered,
this, [=] { refreshModel( QModelIndex() ); } );
107 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
108 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
109 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
120 mActionShowFilter->trigger();
123 setAcceptDrops(
true );
130 QgsPanelWidget::showEvent( e );
132 if ( !lastSelectedPath.isEmpty() )
134 const QModelIndexList items = mBrowserProxyModel.match(
135 mBrowserProxyModel.index( 0, 0 ),
137 QVariant::fromValue( lastSelectedPath ),
141 if ( items.count() > 0 )
143 const QModelIndex expandIndex = items.at( 0 );
144 if ( expandIndex.isValid() )
146 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
147 mBrowserTreeView->expand( expandIndex );
153QString QgsDataSourceSelectWidget::acceptableFilePath( QDropEvent *event )
const
155 if ( event->mimeData()->hasUrls() )
157 const QList<QUrl> urls =
event->mimeData()->urls();
158 for (
const QUrl &url : urls )
160 const QString local = url.toLocalFile();
161 if ( local.isEmpty() )
164 if ( QFile::exists( local ) )
175 const QString filePath = acceptableFilePath( event );
176 if ( !filePath.isEmpty() )
178 event->acceptProposedAction();
188 const QString filePath = acceptableFilePath( event );
189 if ( !filePath.isEmpty() )
191 event->acceptProposedAction();
193 const QFileInfo fi( filePath );
208 mWidgetFilter->setVisible( visible );
211 mLeFilter->setText( QString() );
216 mLeFilter->setFocus();
222 if ( !description.isEmpty() )
224 if ( !mDescriptionLabel )
226 mDescriptionLabel =
new QLabel();
227 mDescriptionLabel->setWordWrap(
true );
228 mDescriptionLabel->setMargin( 4 );
229 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
230 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 )
278 mBrowserModel->
refresh( index );
281 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
283 const QModelIndex idx = mBrowserModel->
index( i, 0, index );
284 const QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
303void QgsDataSourceSelectWidget::setValid(
bool valid )
305 const bool prev = mIsValid;
307 if ( prev != mIsValid )
328 mBrowserTreeView->setModel( &mBrowserProxyModel );
329 mBrowserTreeView->setBrowserModel( mBrowserModel );
338void QgsDataSourceSelectWidget::onLayerSelected(
const QModelIndex &index )
340 bool isLayerCompatible =
false;
342 if ( index.isValid() )
347 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
350 isLayerCompatible =
true;
357 setValid( isLayerCompatible );
361void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
363 onLayerSelected( index );
375 setWindowTitle( tr(
"Select a Data Source" ) );
376 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
381 QVBoxLayout *vl =
new QVBoxLayout();
382 vl->addWidget( mWidget, 1 );
383 vl->setContentsMargins( 4, 4, 4, 4 );
384 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
385 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
386 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
387 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
394 vl->addWidget( buttonBox );
415 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)