Skip to content

Commit 6be8249

Browse files
committed
[options search] fix search not available in option tree
1 parent 71bdda5 commit 6be8249

File tree

2 files changed

+100
-18
lines changed

2 files changed

+100
-18
lines changed

src/gui/qgsoptionsdialogbase.cpp

+92-17
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <QStackedWidget>
3333
#include <QTimer>
3434
#include <QTreeView>
35+
#include <QTreeWidget>
3536
#include <QAbstractItemModel>
3637

3738
#include "qgsfilterlineedit.h"
@@ -406,52 +407,122 @@ QgsSearchHighlightOptionWidget::QgsSearchHighlightOptionWidget( QWidget *widget
406407
}
407408
}
408409

410+
QString styleSheet;
409411
if ( qobject_cast<QLabel *>( widget ) )
410412
{
411-
mStyleSheet = QStringLiteral( "QLabel { background-color: yellow; color: blue;}" );
413+
styleSheet = QStringLiteral( "QLabel { background-color: yellow; color: blue;}" );
412414
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QLabel *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
413415
}
414416
else if ( qobject_cast<QCheckBox *>( widget ) )
415417
{
416-
mStyleSheet = QStringLiteral( "QCheckBox { background-color: yellow; color: blue;}" );
418+
styleSheet = QStringLiteral( "QCheckBox { background-color: yellow; color: blue;}" );
417419
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QCheckBox *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
418420
}
419421
else if ( qobject_cast<QAbstractButton *>( widget ) )
420422
{
421-
mStyleSheet = QStringLiteral( "QAbstractButton { background-color: yellow; color: blue;}" );
423+
styleSheet = QStringLiteral( "QAbstractButton { background-color: yellow; color: blue;}" );
422424
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QAbstractButton *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
423425
}
424426
else if ( qobject_cast<QGroupBox *>( widget ) )
425427
{
426-
mStyleSheet = QStringLiteral( "QGroupBox::title { background-color: yellow; color: blue;}" );
428+
styleSheet = QStringLiteral( "QGroupBox::title { background-color: yellow; color: blue;}" );
427429
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QGroupBox *>( mWidget )->title().contains( searchText, Qt::CaseInsensitive );};
428430
}
431+
if ( !styleSheet.isEmpty() )
432+
{
433+
styleSheet.prepend( "/*!search!*/" ).append( "/*!search!*/" );
434+
435+
mHighlight = [ = ]( QString searchText )
436+
{
437+
Q_UNUSED( searchText );
438+
mWidget->setStyleSheet( mWidget->styleSheet() + styleSheet );
439+
};
440+
441+
mReset = [ = ]()
442+
{
443+
if ( mWidget )
444+
{
445+
QString ss = mWidget->styleSheet();
446+
ss.remove( styleSheet );
447+
mWidget->setStyleSheet( ss );
448+
}
449+
};
450+
}
429451
else if ( qobject_cast<QTreeView *>( widget ) )
430452
{
431-
// TODO - style individual matching items
432453
mTextFound = [ = ]( QString searchText )
433454
{
434-
QTreeView *tree = qobject_cast<QTreeView *>( mWidget );
435-
if ( !tree )
455+
QTreeView *treeView = qobject_cast<QTreeView *>( mWidget );
456+
if ( !treeView )
436457
return false;
437-
QModelIndexList hits = tree->model()->match( tree->model()->index( 0, 0 ), Qt::DisplayRole, searchText, 1, Qt::MatchContains | Qt::MatchRecursive );
458+
QModelIndexList hits = treeView->model()->match( treeView->model()->index( 0, 0 ), Qt::DisplayRole, searchText, 1, Qt::MatchContains | Qt::MatchRecursive );
438459
return !hits.isEmpty();
439460
};
461+
462+
if ( qobject_cast<QTreeWidget *>( widget ) )
463+
{
464+
mHighlight = [ = ]( QString searchText )
465+
{
466+
QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( widget );
467+
if ( treeWidget )
468+
{
469+
QList<QTreeWidgetItem *> items = treeWidget->findItems( searchText, Qt::MatchContains | Qt::MatchRecursive, 0 );
470+
mChangedStyle = items.count() ? true : false;
471+
mTreeInitialBackground.clear();
472+
mTreeInitialExpand.clear();
473+
for ( QTreeWidgetItem *item : items )
474+
{
475+
mTreeInitialBackground.insert( item, item->background( 0 ) );
476+
item->setBackground( 0, QBrush( QColor( Qt::yellow ) ) );
477+
478+
QTreeWidgetItem *parent = item;
479+
while ( ( parent = parent->parent() ) )
480+
{
481+
if ( mTreeInitialExpand.contains( parent ) )
482+
break;
483+
mTreeInitialExpand.insert( parent, parent->isExpanded() );
484+
parent->setExpanded( true );
485+
}
486+
}
487+
488+
}
489+
};
490+
491+
mReset = [ = ]()
492+
{
493+
for ( QTreeWidgetItem *item : mTreeInitialExpand.keys() )
494+
{
495+
if ( item )
496+
{
497+
item->setExpanded( mTreeInitialExpand.value( item ) );
498+
}
499+
}
500+
for ( QTreeWidgetItem *item : mTreeInitialBackground.keys() )
501+
{
502+
if ( item )
503+
{
504+
item->setBackground( 0, mTreeInitialBackground.value( item ) );
505+
}
506+
}
507+
mTreeInitialBackground.clear();
508+
mTreeInitialExpand.clear();
509+
};
510+
}
440511
}
441512
else
442513
{
443514
mValid = false;
444515
}
516+
445517
if ( mValid )
446518
{
447-
mStyleSheet.prepend( "/*!search!*/" ).append( "/*!search!*/" );
448-
QgsDebugMsgLevel( mStyleSheet, 4 );
449519
connect( mWidget, &QWidget::destroyed, this, &QgsSearchHighlightOptionWidget::widgetDestroyed );
450520
}
451521
}
452522

453523
bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText )
454524
{
525+
mSearchText = searchText;
455526
bool found = false;
456527
if ( !mWidget )
457528
return found;
@@ -461,8 +532,13 @@ bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText
461532
found = mTextFound( searchText );
462533
}
463534

464-
if ( found && !mChangedStyle )
535+
if ( found )
465536
{
537+
if ( mChangedStyle )
538+
{
539+
mReset();
540+
mChangedStyle = false;
541+
}
466542
if ( !mWidget->isVisible() )
467543
{
468544
// show the widget to get initial stylesheet in case it's modified
@@ -474,7 +550,7 @@ bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText
474550
}
475551
else
476552
{
477-
mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
553+
mHighlight( searchText );
478554
mChangedStyle = true;
479555
}
480556
}
@@ -493,7 +569,8 @@ bool QgsSearchHighlightOptionWidget::eventFilter( QObject *obj, QEvent *event )
493569
// after the widget is shown
494570
#if 1
495571
mWidget->show();
496-
mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
572+
mHighlight( mSearchText );
573+
mChangedStyle = true;
497574
return true;
498575
#else
499576
QTimer::singleShot( 500, this, [ = ]
@@ -512,12 +589,10 @@ void QgsSearchHighlightOptionWidget::reset()
512589
{
513590
if ( mChangedStyle )
514591
{
515-
QString ss = mWidget->styleSheet();
516-
ss.remove( mStyleSheet );
517-
mWidget->setStyleSheet( ss );
592+
mReset();
518593
mChangedStyle = false;
519594
}
520-
else if ( mInstalledFilter )
595+
if ( mInstalledFilter )
521596
{
522597
mWidget->removeEventFilter( this );
523598
mInstalledFilter = false;

src/gui/qgsoptionsdialogbase.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QDialog>
2727
#include <QPointer>
2828
#include <QStyledItemDelegate>
29+
#include <QMap>
2930

3031

3132
class QDialogButtonBox;
@@ -36,6 +37,7 @@ class QPainter;
3637
class QStackedWidget;
3738
class QStyleOptionViewItem;
3839
class QSplitter;
40+
class QTreeWidgetItem;
3941

4042
class QgsFilterLineEdit;
4143

@@ -87,10 +89,15 @@ class GUI_EXPORT QgsSearchHighlightOptionWidget : public QObject
8789

8890
private:
8991
QPointer< QWidget > mWidget;
90-
QString mStyleSheet;
92+
QString mSearchText = QString();
93+
// a map to save the tree state (backouground, expanded) before highlighting items
94+
QMap<QTreeWidgetItem *, QBrush> mTreeInitialBackground = QMap<QTreeWidgetItem *, QBrush>();
95+
QMap<QTreeWidgetItem *, bool> mTreeInitialExpand = QMap<QTreeWidgetItem *, bool>();
9196
bool mValid = true;
9297
bool mChangedStyle = false;
9398
std::function < bool( QString )> mTextFound = []( QString searchText ) {Q_UNUSED( searchText ); return false;};
99+
std::function < void( QString )> mHighlight = []( QString searchText ) {Q_UNUSED( searchText );};
100+
std::function < void()> mReset = []() {};
94101
bool mInstalledFilter = false;
95102
};
96103

0 commit comments

Comments
 (0)