23 #include "qgssettings.h" 
   24 #include "qgsnative.h" 
   27 #include <QPushButton> 
   29 #include <QDesktopServices> 
   30 #include <QDialogButtonBox> 
   36   bool setFilterByLayerType,
 
   48     mBrowserModel = browserModel;
 
   55   mBrowserTreeView->setHeaderHidden( 
true );
 
   57   if ( setFilterByLayerType )
 
   64     mBrowserTreeView->setModel( &mBrowserProxyModel );
 
   68   mBrowserTreeView->setBrowserModel( mBrowserModel );
 
   70   mWidgetFilter->hide();
 
   71   mLeFilter->setPlaceholderText( tr( 
"Type here to filter visible items…" ) );
 
   74   QMenu *menu = 
new QMenu( 
this );
 
   75   menu->setSeparatorsCollapsible( 
false );
 
   76   mBtnFilterOptions->setMenu( menu );
 
   77   QAction *action = 
new QAction( tr( 
"Case Sensitive" ), menu );
 
   78   action->setData( 
"case" );
 
   79   action->setCheckable( 
true );
 
   80   action->setChecked( 
false );
 
   82   menu->addAction( action );
 
   83   QActionGroup *group = 
new QActionGroup( menu );
 
   84   action = 
new QAction( tr( 
"Filter Pattern Syntax" ), group );
 
   85   action->setSeparator( 
true );
 
   86   menu->addAction( action );
 
   87   action = 
new QAction( tr( 
"Normal" ), group );
 
   89   action->setCheckable( 
true );
 
   90   action->setChecked( 
true );
 
   91   menu->addAction( action );
 
   92   action = 
new QAction( tr( 
"Wildcard(s)" ), group );
 
   94   action->setCheckable( 
true );
 
   95   menu->addAction( action );
 
   96   action = 
new QAction( tr( 
"Regular Expression" ), group );
 
   98   action->setCheckable( 
true );
 
   99   menu->addAction( action );
 
  101   connect( mActionRefresh, &QAction::triggered, 
this, [ = ] { refreshModel( QModelIndex() ); } );
 
  102   connect( mBrowserTreeView, &QgsBrowserTreeView::clicked, 
this, &QgsDataSourceSelectWidget::onLayerSelected );
 
  103   connect( mBrowserTreeView, &QgsBrowserTreeView::doubleClicked, 
this, &QgsDataSourceSelectWidget::itemDoubleClicked );
 
  104   connect( mActionCollapse, &QAction::triggered, mBrowserTreeView, &QgsBrowserTreeView::collapseAll );
 
  113   if ( QgsSettings().value( QStringLiteral( 
"datasourceSelectFilterVisible" ), 
false, QgsSettings::Section::Gui ).toBool() )
 
  115     mActionShowFilter->trigger();
 
  123   QgsPanelWidget::showEvent( e );
 
  124   QString lastSelectedPath( QgsSettings().value( QStringLiteral( 
"datasourceSelectLastSelectedItem" ),
 
  125                             QString(), QgsSettings::Section::Gui ).toString() );
 
  126   if ( ! lastSelectedPath.isEmpty() )
 
  128     QModelIndexList items = mBrowserProxyModel.match(
 
  129                               mBrowserProxyModel.index( 0, 0 ),
 
  131                               QVariant::fromValue( lastSelectedPath ),
 
  133                               Qt::MatchRecursive );
 
  134     if ( items.count( ) > 0 )
 
  136       QModelIndex expandIndex = items.at( 0 );
 
  137       if ( expandIndex.isValid() )
 
  139         mBrowserTreeView->scrollTo( expandIndex, QgsBrowserTreeView::ScrollHint::PositionAtTop );
 
  140         mBrowserTreeView->expand( expandIndex );
 
  148   QgsSettings().setValue( QStringLiteral( 
"datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
 
  149   mWidgetFilter->setVisible( visible );
 
  152     mLeFilter->setText( QString() );
 
  157     mLeFilter->setFocus();
 
  163   if ( !description.isEmpty() )
 
  165     if ( !mDescriptionLabel )
 
  167       mDescriptionLabel = 
new QLabel();
 
  168       mDescriptionLabel->setWordWrap( 
true );
 
  169       mDescriptionLabel->setMargin( 4 );
 
  170       mDescriptionLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
 
  171       connect( mDescriptionLabel, &QLabel::linkActivated, 
this, [ = ]( 
const QString & link )
 
  174         QFileInfo file( url.toLocalFile() );
 
  175         if ( file.exists() && !file.isDir() )
 
  178           QDesktopServices::openUrl( url );
 
  180       verticalLayout->insertWidget( 1, mDescriptionLabel );
 
  182     mDescriptionLabel->setText( description );
 
  186     if ( mDescriptionLabel )
 
  188       verticalLayout->removeWidget( mDescriptionLabel );
 
  189       delete mDescriptionLabel;
 
  190       mDescriptionLabel = 
nullptr;
 
  197   QString filter = mLeFilter->text();
 
  202 void QgsDataSourceSelectWidget::refreshModel( 
const QModelIndex &index )
 
  217     mBrowserModel->
refresh( index );
 
  220   for ( 
int i = 0; i < mBrowserModel->
rowCount( index ); i++ )
 
  222     QModelIndex idx = mBrowserModel->
index( i, 0, index );
 
  223     QModelIndex proxyIdx = mBrowserProxyModel.mapFromSource( idx );
 
  242 void QgsDataSourceSelectWidget::setValid( 
bool valid )
 
  244   const bool prev = mIsValid;
 
  246   if ( prev != mIsValid )
 
  269   mBrowserTreeView->setModel( &mBrowserProxyModel );
 
  278 void QgsDataSourceSelectWidget::onLayerSelected( 
const QModelIndex &index )
 
  280   bool isLayerCompatible = 
false;
 
  282   if ( index.isValid() )
 
  287       const QgsLayerItem *layerItem = qobject_cast<const QgsLayerItem *>( dataItem );
 
  291         isLayerCompatible = 
true;
 
  294         QgsSettings().setValue( QStringLiteral( 
"datasourceSelectLastSelectedItem" ),  mBrowserProxyModel.data( index, 
QgsBrowserGuiModel::PathRole ).toString(), QgsSettings::Section::Gui );
 
  298   setValid( isLayerCompatible );
 
  302 void QgsDataSourceSelectWidget::itemDoubleClicked( 
const QModelIndex &index )
 
  304   onLayerSelected( index );
 
  316   setWindowTitle( tr( 
"Select a Data Source" ) );
 
  317   setObjectName( QStringLiteral( 
"QgsDataSourceSelectDialog" ) );
 
  322   QVBoxLayout *vl = 
new QVBoxLayout();
 
  323   vl->addWidget( mWidget, 1 );
 
  324   vl->setContentsMargins( 4, 4, 4, 4 );
 
  325   QDialogButtonBox *buttonBox = 
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
 
  326   connect( buttonBox, &QDialogButtonBox::accepted, 
this, &QDialog::accept );
 
  327   connect( buttonBox, &QDialogButtonBox::rejected, 
this, &QDialog::reject );
 
  328   buttonBox->button( QDialogButtonBox::Ok )->setEnabled( 
false );
 
  335   vl->addWidget( buttonBox );
 
  351   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,...
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
@ PathRole
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 setFilterCaseSensitivity(Qt::CaseSensitivity sensitivity)
Sets whether item filtering should be case sensitive.
void setFilterSyntax(FilterSyntax syntax)
Sets the filter syntax.
QgsMapLayerType layerType() const
Returns the layer type to filter the model by.
void setLayerType(QgsMapLayerType type)
Sets the layer type to filter the model by.
bool filterByLayerType() const
Returns true if the model is filtered by map layer type.
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.
QgsDataSourceSelectDialog(QgsBrowserGuiModel *browserModel=nullptr, bool setFilterByLayerType=false, QgsMapLayerType layerType=QgsMapLayerType::VectorLayer, QWidget *parent=nullptr)
Constructs a QgsDataSourceSelectDialog, optionally filtering by layer type.
void setFilterSyntax(QAction *)
Sets filter syntax.
QgsMimeDataUtils::Uri uri() const
Returns the (possibly invalid) uri of the selected data source.
void setDescription(const QString &description)
Sets a description label.
void setLayerTypeFilter(QgsMapLayerType layerType)
Sets layer type filter to layerType and activates the filtering.
void cleared()
Emitted when the widget is cleared.
static QgsGui * instance()
Returns a pointer to the singleton instance.
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.
QgsMapLayerType mapLayerType() const
Returns QgsMapLayerType.
QgsMimeDataUtils::UriList mimeUris() const override
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
QgsMapLayerType
Types of layers that can be added to a map.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
#define QgsDebugMsgLevel(str, level)