Skip to content

Commit

Permalink
Allow filtering WM(T)S list in source dialog (#36166)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 5, 2020
1 parent 0888d37 commit afbb204
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 146 deletions.
74 changes: 74 additions & 0 deletions src/providers/wms/qgswmssourceselect.cpp
Expand Up @@ -75,6 +75,9 @@ QgsWMSSourceSelect::QgsWMSSourceSelect( QWidget *parent, Qt::WindowFlags fl, Qgs
connect( mLayerDownButton, &QPushButton::clicked, this, &QgsWMSSourceSelect::mLayerDownButton_clicked );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsWMSSourceSelect::showHelp );

connect( mLayersFilterLineEdit, &QgsFilterLineEdit::textChanged, this, &QgsWMSSourceSelect::filterLayers );
connect( mTilesetsFilterLineEdit, &QgsFilterLineEdit::textChanged, this, &QgsWMSSourceSelect::filterTiles );

// Creates and connects standard ok/apply buttons
setupButtons( buttonBox );

Expand Down Expand Up @@ -1120,6 +1123,77 @@ void QgsWMSSourceSelect::cmbConnections_activated( int )
QgsWMSConnection::setSelectedConnection( cmbConnections->currentText() );
}

void QgsWMSSourceSelect::filterLayers( const QString &searchText )
{
std::function< void( QTreeWidgetItem *, bool ) > setChildrenVisible;
setChildrenVisible = [&setChildrenVisible]( QTreeWidgetItem * item, bool visible )
{
for ( int i = 0; i < item->childCount(); ++i )
setChildrenVisible( item->child( i ), visible );
item->setHidden( !visible );
};


if ( searchText.isEmpty() )
{
// show everything and reset tree nesting
setChildrenVisible( lstLayers->invisibleRootItem(), true );
for ( QTreeWidgetItem *item : mTreeInitialExpand.keys() )
if ( item )
item->setExpanded( mTreeInitialExpand.value( item ) );
mTreeInitialExpand.clear();
}
else
{
// hide all
setChildrenVisible( lstLayers->invisibleRootItem(), false );
// find and show matching items in name and title columns
QSet<QTreeWidgetItem *> items = lstLayers->findItems( searchText, Qt::MatchContains | Qt::MatchRecursive, 1 ).toSet();
items.unite( lstLayers->findItems( searchText, Qt::MatchContains | Qt::MatchRecursive, 2 ).toSet() );

// if nothing found, search in abstract too
if ( items.isEmpty() )
{
items = lstLayers->findItems( searchText, Qt::MatchContains | Qt::MatchRecursive, 3 ).toSet();
}

mTreeInitialExpand.clear();
for ( QTreeWidgetItem *item : qgis::as_const( items ) )
{
setChildrenVisible( item, true );

QTreeWidgetItem *parent = item;
while ( parent )
{
if ( mTreeInitialExpand.contains( parent ) )
break;
mTreeInitialExpand.insert( parent, parent->isExpanded() );
parent->setExpanded( true );
parent->setHidden( false );
parent = parent->parent();
}
}
}
}

void QgsWMSSourceSelect::filterTiles( const QString &searchText )
{
QList<int> rowsShown;
if ( !searchText.isEmpty() )
{
const QList<QTableWidgetItem *> items = lstTilesets->findItems( searchText, Qt::MatchContains );
for ( const QTableWidgetItem *item : items )
{
rowsShown << item->row();
}
}
for ( int r = 0; r < lstTilesets->rowCount(); r++ )
{
bool visible = rowsShown.isEmpty() || rowsShown.contains( r );
lstTilesets->setRowHidden( r, !visible );
}
}

QString QgsWMSSourceSelect::descriptionForAuthId( const QString &authId )
{
if ( mCrsNames.contains( authId ) )
Expand Down
9 changes: 9 additions & 0 deletions src/providers/wms/qgswmssourceselect.h
Expand Up @@ -93,6 +93,12 @@ class QgsWMSSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsWM
//! Stores the selected datasource whenerver it is changed
void cmbConnections_activated( int );

//! filter the layers
void filterLayers( const QString &searchText );

//! Filters the tiles sets
void filterTiles( const QString &searchText );

private:
//! Populate the connection list combo box
void populateConnectionList();
Expand Down Expand Up @@ -184,6 +190,9 @@ class QgsWMSSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsWM
//! Stores all the layers properties from the service capabilities.
QVector<QgsWmsLayerProperty> mLayerProperties;

// save the current status of the layer true
QMap<QTreeWidgetItem *, bool> mTreeInitialExpand = QMap<QTreeWidgetItem *, bool>();

private slots:
void lstTilesets_itemClicked( QTableWidgetItem *item );
void mLayerUpButton_clicked();
Expand Down

0 comments on commit afbb204

Please sign in to comment.