Skip to content

Commit fe31c28

Browse files
nyalldawson3nids
authored andcommitted
[options] Hide non-matching items in tree widgets when searching children
Makes the search more useful for the advanced panel
1 parent 8e9bb6d commit fe31c28

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/gui/qgsoptionsdialoghighlightwidgetsimpl.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include "qgsoptionsdialoghighlightwidgetsimpl.h"
3030

31-
31+
#include <functional>
3232

3333
// ****************
3434
// QLabel
@@ -190,6 +190,18 @@ bool QgsOptionsDialogHighlightTree::highlightText( const QString &text )
190190
QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
191191
if ( treeWidget )
192192
{
193+
mTreeInitialVisible.clear();
194+
// initially hide everything
195+
std::function< void( QTreeWidgetItem *, bool ) > setChildrenVisible;
196+
setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem * item, bool visible )
197+
{
198+
for ( int i = 0; i < item->childCount(); ++i )
199+
setChildrenVisible( item->child( i ), visible );
200+
mTreeInitialVisible.insert( item, !item->isHidden() );
201+
item->setHidden( !visible );
202+
};
203+
setChildrenVisible( treeWidget->invisibleRootItem(), false );
204+
193205
QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
194206
success = items.count() ? true : false;
195207
mTreeInitialStyle.clear();
@@ -199,6 +211,7 @@ bool QgsOptionsDialogHighlightTree::highlightText( const QString &text )
199211
mTreeInitialStyle.insert( item, qMakePair( item->background( 0 ), item->foreground( 0 ) ) );
200212
item->setBackground( 0, QBrush( QColor( Qt::yellow ) ) );
201213
item->setForeground( 0, QBrush( QColor( Qt::blue ) ) );
214+
setChildrenVisible( item, true );
202215

203216
QTreeWidgetItem *parent = item;
204217
while ( ( parent = parent->parent() ) )
@@ -207,6 +220,7 @@ bool QgsOptionsDialogHighlightTree::highlightText( const QString &text )
207220
break;
208221
mTreeInitialExpand.insert( parent, parent->isExpanded() );
209222
parent->setExpanded( true );
223+
parent->setHidden( false );
210224
}
211225
}
212226
}
@@ -222,6 +236,15 @@ void QgsOptionsDialogHighlightTree::reset()
222236
QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
223237
if ( treeWidget )
224238
{
239+
// show everything
240+
std::function< void( QTreeWidgetItem * ) > showChildren;
241+
showChildren = [this, &showChildren]( QTreeWidgetItem * item )
242+
{
243+
for ( int i = 0; i < item->childCount(); ++i )
244+
showChildren( item->child( i ) );
245+
item->setHidden( !mTreeInitialVisible.value( item, true ) );
246+
};
247+
showChildren( treeWidget->invisibleRootItem() );
225248
for ( QTreeWidgetItem *item : mTreeInitialExpand.keys() )
226249
{
227250
if ( item )

src/gui/qgsoptionsdialoghighlightwidgetsimpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,6 @@ class GUI_EXPORT QgsOptionsDialogHighlightTree : public QgsOptionsDialogHighligh
140140
// a map to save the tree state (backouground, font, expanded) before highlighting items
141141
QMap<QTreeWidgetItem *, QPair<QBrush, QBrush>> mTreeInitialStyle = QMap<QTreeWidgetItem *, QPair<QBrush, QBrush>>();
142142
QMap<QTreeWidgetItem *, bool> mTreeInitialExpand = QMap<QTreeWidgetItem *, bool>();
143+
QMap<QTreeWidgetItem *, bool> mTreeInitialVisible = QMap<QTreeWidgetItem *, bool>();
143144
};
144145
#endif // QGSOPTIONSDIALOGHIGHLIGHTWIDGETSIMPL_H

0 commit comments

Comments
 (0)