Skip to content

Commit

Permalink
[options] Hide non-matching items in tree widgets when searching chil…
Browse files Browse the repository at this point in the history
…dren

Makes the search more useful for the advanced panel
  • Loading branch information
nyalldawson authored and 3nids committed Feb 5, 2018
1 parent 8e9bb6d commit fe31c28
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/gui/qgsoptionsdialoghighlightwidgetsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include "qgsoptionsdialoghighlightwidgetsimpl.h"


#include <functional>

// ****************
// QLabel
Expand Down Expand Up @@ -190,6 +190,18 @@ bool QgsOptionsDialogHighlightTree::highlightText( const QString &text )
QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
if ( treeWidget )
{
mTreeInitialVisible.clear();
// initially hide everything
std::function< void( QTreeWidgetItem *, bool ) > setChildrenVisible;
setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem * item, bool visible )
{
for ( int i = 0; i < item->childCount(); ++i )
setChildrenVisible( item->child( i ), visible );
mTreeInitialVisible.insert( item, !item->isHidden() );
item->setHidden( !visible );
};
setChildrenVisible( treeWidget->invisibleRootItem(), false );

QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
success = items.count() ? true : false;
mTreeInitialStyle.clear();
Expand All @@ -199,6 +211,7 @@ bool QgsOptionsDialogHighlightTree::highlightText( const QString &text )
mTreeInitialStyle.insert( item, qMakePair( item->background( 0 ), item->foreground( 0 ) ) );
item->setBackground( 0, QBrush( QColor( Qt::yellow ) ) );
item->setForeground( 0, QBrush( QColor( Qt::blue ) ) );
setChildrenVisible( item, true );

QTreeWidgetItem *parent = item;
while ( ( parent = parent->parent() ) )
Expand All @@ -207,6 +220,7 @@ bool QgsOptionsDialogHighlightTree::highlightText( const QString &text )
break;
mTreeInitialExpand.insert( parent, parent->isExpanded() );
parent->setExpanded( true );
parent->setHidden( false );
}
}
}
Expand All @@ -222,6 +236,15 @@ void QgsOptionsDialogHighlightTree::reset()
QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
if ( treeWidget )
{
// show everything
std::function< void( QTreeWidgetItem * ) > showChildren;
showChildren = [this, &showChildren]( QTreeWidgetItem * item )
{
for ( int i = 0; i < item->childCount(); ++i )
showChildren( item->child( i ) );
item->setHidden( !mTreeInitialVisible.value( item, true ) );
};
showChildren( treeWidget->invisibleRootItem() );
for ( QTreeWidgetItem *item : mTreeInitialExpand.keys() )
{
if ( item )
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsoptionsdialoghighlightwidgetsimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,6 @@ class GUI_EXPORT QgsOptionsDialogHighlightTree : public QgsOptionsDialogHighligh
// a map to save the tree state (backouground, font, expanded) before highlighting items
QMap<QTreeWidgetItem *, QPair<QBrush, QBrush>> mTreeInitialStyle = QMap<QTreeWidgetItem *, QPair<QBrush, QBrush>>();
QMap<QTreeWidgetItem *, bool> mTreeInitialExpand = QMap<QTreeWidgetItem *, bool>();
QMap<QTreeWidgetItem *, bool> mTreeInitialVisible = QMap<QTreeWidgetItem *, bool>();
};
#endif // QGSOPTIONSDIALOGHIGHLIGHTWIDGETSIMPL_H

0 comments on commit fe31c28

Please sign in to comment.