24 #include "qgsnative.h"
27 #include <QPushButton>
29 #include <QDesktopServices>
30 #include <QDialogButtonBox>
33 #include <QActionGroup>
37 bool setFilterByLayerType,
49 mBrowserModel = browserModel;
56 mBrowserTreeView->setHeaderHidden(
true );
58 if ( setFilterByLayerType )
65 mBrowserTreeView->setModel( &mBrowserProxyModel );
69 mBrowserTreeView->setBrowserModel( mBrowserModel );
71 mWidgetFilter->hide();
72 mLeFilter->setPlaceholderText( tr(
"Type here to filter visible items…" ) );
75 QMenu *menu =
new QMenu(
this );
76 menu->setSeparatorsCollapsible(
false );
77 mBtnFilterOptions->setMenu( menu );
78 QAction *action =
new QAction( tr(
"Case Sensitive" ), menu );
79 action->setData(
"case" );
80 action->setCheckable(
true );
81 action->setChecked(
false );
83 menu->addAction( action );
84 QActionGroup *group =
new QActionGroup( menu );
85 action =
new QAction( tr(
"Filter Pattern Syntax" ), group );
86 action->setSeparator(
true );
87 menu->addAction( action );
88 action =
new QAction( tr(
"Normal" ), group );
90 action->setCheckable(
true );
91 action->setChecked(
true );
92 menu->addAction( action );
93 action =
new QAction( tr(
"Wildcard(s)" ), group );
95 action->setCheckable(
true );
96 menu->addAction( action );
97 action =
new QAction( tr(
"Regular Expression" ), group );
99 action->setCheckable(
true );
100 menu->addAction( action );
102 connect( mActionRefresh, &QAction::triggered,
this, [ = ] { refreshModel( QModelIndex() ); } );
103 connect( mBrowserTreeView, &QgsBrowserTreeView::clicked,
this, &QgsDataSourceSelectWidget::onLayerSelected );
104 connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked,
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
105 connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
114 if (
QgsSettings().value( QStringLiteral(
"datasourceSelectFilterVisible" ),
false, QgsSettings::Section::Gui ).toBool() )
116 mActionShowFilter->trigger();
124 QgsPanelWidget::showEvent( e );
125 const QString lastSelectedPath(
QgsSettings().value( QStringLiteral(
"datasourceSelectLastSelectedItem" ),
126 QString(), QgsSettings::Section::Gui ).toString() );
127 if ( ! lastSelectedPath.isEmpty() )
129 const QModelIndexList items = mBrowserProxyModel.match(
130 mBrowserProxyModel.index( 0, 0 ),
132 QVariant::fromValue( lastSelectedPath ),
134 Qt::MatchRecursive );
135 if ( items.count( ) > 0 )
137 const 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 )
174 const QUrl url( link );
175 const 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 const QString filter = mLeFilter->text();
203 void QgsDataSourceSelectWidget::refreshModel(
const QModelIndex &index )
218 mBrowserModel->
refresh( index );
221 for (
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
223 const QModelIndex idx = mBrowserModel->
index( i, 0, index );
224 const QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
243 void QgsDataSourceSelectWidget::setValid(
bool valid )
245 const bool prev = mIsValid;
247 if ( prev != mIsValid )
270 mBrowserTreeView->setModel( &mBrowserProxyModel );
279 void QgsDataSourceSelectWidget::onLayerSelected(
const QModelIndex &index )
281 bool isLayerCompatible =
false;
283 if ( index.isValid() )
288 const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
292 isLayerCompatible =
true;
299 setValid( isLayerCompatible );
303 void QgsDataSourceSelectWidget::itemDoubleClicked(
const QModelIndex &index )
305 onLayerSelected( index );
317 setWindowTitle( tr(
"Select a Data Source" ) );
318 setObjectName( QStringLiteral(
"QgsDataSourceSelectDialog" ) );
323 QVBoxLayout *vl =
new QVBoxLayout();
324 vl->addWidget( mWidget, 1 );
325 vl->setContentsMargins( 4, 4, 4, 4 );
326 QDialogButtonBox *buttonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
327 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
328 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
329 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
336 vl->addWidget( buttonBox );
352 return mWidget->
uri();