24 #include "qgsnative.h"
26 #include <QPushButton>
28 #include <QDesktopServices>
29 #include <QDialogButtonBox>
33 bool setFilterByLayerType,
45 mBrowserModel = browserModel;
52 mBrowserTreeView->setHeaderHidden(
true );
54 if ( setFilterByLayerType )
61 mBrowserTreeView->setModel( &mBrowserProxyModel );
65 mBrowserTreeView->setBrowserModel( mBrowserModel );
67 mWidgetFilter->hide();
68 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
71 QMenu *menu =
new QMenu(
this );
72 menu->setSeparatorsCollapsible(
false );
73 mBtnFilterOptions->setMenu( menu );
74 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
75 action->setData(
"case" );
76 action->setCheckable(
true );
77 action->setChecked(
false );
79 menu->addAction( action );
80 QActionGroup *group =
new QActionGroup( menu );
81 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
82 action->setSeparator(
true );
83 menu->addAction( action );
84 action =
new QAction( tr(
"Normal" ), group );
86 action->setCheckable(
true );
87 action->setChecked(
true );
88 menu->addAction( action );
89 action =
new QAction( tr(
"Wildcard(s)" ), group );
91 action->setCheckable(
true );
92 menu->addAction( action );
93 action =
new QAction( tr(
"Regular Expression" ), group );
95 action->setCheckable(
true );
96 menu->addAction( action );
98 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
99 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
100 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
101 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
110 if (
QgsSettings().value( QStringLiteral(
"datasourceSelectFilterVisible" ),
false, QgsSettings::Section::Gui ).toBool() )
112 mActionShowFilter->trigger();
120 QgsPanelWidget::showEvent( e );
121 QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
122 QString(), QgsSettings::Section::Gui ).toString() );
123 if ( ! lastSelectedPath.isEmpty() )
125 QModelIndexList items = mBrowserProxyModel.match(
126 mBrowserProxyModel.index( 0, 0 ),
128 QVariant::fromValue( lastSelectedPath ),
130 Qt::MatchRecursive );
131 if ( items.count( ) > 0 )
133 QModelIndex expandIndex = items.at( 0 );
134 if ( expandIndex.isValid() )
136 mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
137 mBrowserTreeView->expand( expandIndex );
145 QgsSettings().
setValue( QStringLiteral(
"datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
146 mWidgetFilter->setVisible( visible );
149 mLeFilter->setText( QString() );
154 mLeFilter->setFocus();
160 if ( !description.isEmpty() )
162 if ( !mDescriptionLabel )
164 mDescriptionLabel =
new QLabel();
165 mDescriptionLabel->setWordWrap(
true );
166 mDescriptionLabel->setMargin( 4 );
167 mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
168 connect( mDescriptionLabel, &QLabel::linkActivated,
this, [ = ](
const QString & link )
171 QFileInfo file( url.toLocalFile() );
172 if ( file.exists() && !file.isDir() )
175 QDesktopServices::openUrl( url );
177 verticalLayout->insertWidget( 1, mDescriptionLabel );
179 mDescriptionLabel->setText( description );
183 if ( mDescriptionLabel )
185 verticalLayout->removeWidget( mDescriptionLabel );
186 delete mDescriptionLabel;
187 mDescriptionLabel =
nullptr;
194 QString filter = mLeFilter->text();
199 void QgsDataSourceSelectWidget::refreshModel(
const QModelIndex &index )
214 mBrowserModel->
refresh( index );
217 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
219 QModelIndex idx = mBrowserModel->
index( i, 0, index );
220 QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
225 if ( mBrowserTreeView->isExpanded( proxyIdx ) || mBrowserTreeView->hasExpandedDescendant( proxyIdx ) || ( child && child->
capabilities2() &
QgsDataItem::Fast ) )
239 void QgsDataSourceSelectWidget::setValid(
bool valid )
241 const bool prev = mIsValid;
243 if ( prev != mIsValid )
266 mBrowserTreeView->setModel( &mBrowserProxyModel );
275 void QgsDataSourceSelectWidget::onLayerSelected(
const QModelIndex &index )
277 bool isLayerCompatible =
false;
279 if ( index.isValid() )
284 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
288 isLayerCompatible =
true;
295 setValid( isLayerCompatible );
299 void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
301 onLayerSelected( index );
313 setWindowTitle( tr(
"Select a Data Source" ) );
314 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
319 QVBoxLayout *vl =
new QVBoxLayout();
320 vl->addWidget( mWidget, 1 );
321 vl->setContentsMargins( 4, 4, 4, 4 );
322 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
323 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
324 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
325 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
328 vl->addWidget( buttonBox );
334 mWidget->setLayerTypeFilter( layerType );
339 mWidget->setDescription( description );
344 return mWidget->
uri();
349 mWidget->showFilterWidget( visible );
354 mWidget->setFilterSyntax( syntax );
359 mWidget->setCaseSensitive( caseSensitive );
364 mWidget->setFilter();