32
32
#include < QStackedWidget>
33
33
#include < QTimer>
34
34
#include < QTreeView>
35
+ #include < QTreeWidget>
35
36
#include < QAbstractItemModel>
36
37
37
38
#include " qgsfilterlineedit.h"
@@ -406,52 +407,122 @@ QgsSearchHighlightOptionWidget::QgsSearchHighlightOptionWidget( QWidget *widget
406
407
}
407
408
}
408
409
410
+ QString styleSheet;
409
411
if ( qobject_cast<QLabel *>( widget ) )
410
412
{
411
- mStyleSheet = QStringLiteral ( " QLabel { background-color: yellow; color: blue;}" );
413
+ styleSheet = QStringLiteral ( " QLabel { background-color: yellow; color: blue;}" );
412
414
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QLabel *>( mWidget )->text ().contains ( searchText, Qt::CaseInsensitive );};
413
415
}
414
416
else if ( qobject_cast<QCheckBox *>( widget ) )
415
417
{
416
- mStyleSheet = QStringLiteral ( " QCheckBox { background-color: yellow; color: blue;}" );
418
+ styleSheet = QStringLiteral ( " QCheckBox { background-color: yellow; color: blue;}" );
417
419
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QCheckBox *>( mWidget )->text ().contains ( searchText, Qt::CaseInsensitive );};
418
420
}
419
421
else if ( qobject_cast<QAbstractButton *>( widget ) )
420
422
{
421
- mStyleSheet = QStringLiteral ( " QAbstractButton { background-color: yellow; color: blue;}" );
423
+ styleSheet = QStringLiteral ( " QAbstractButton { background-color: yellow; color: blue;}" );
422
424
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QAbstractButton *>( mWidget )->text ().contains ( searchText, Qt::CaseInsensitive );};
423
425
}
424
426
else if ( qobject_cast<QGroupBox *>( widget ) )
425
427
{
426
- mStyleSheet = QStringLiteral ( " QGroupBox::title { background-color: yellow; color: blue;}" );
428
+ styleSheet = QStringLiteral ( " QGroupBox::title { background-color: yellow; color: blue;}" );
427
429
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QGroupBox *>( mWidget )->title ().contains ( searchText, Qt::CaseInsensitive );};
428
430
}
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
+ }
429
451
else if ( qobject_cast<QTreeView *>( widget ) )
430
452
{
431
- // TODO - style individual matching items
432
453
mTextFound = [ = ]( QString searchText )
433
454
{
434
- QTreeView *tree = qobject_cast<QTreeView *>( mWidget );
435
- if ( !tree )
455
+ QTreeView *treeView = qobject_cast<QTreeView *>( mWidget );
456
+ if ( !treeView )
436
457
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 );
438
459
return !hits.isEmpty ();
439
460
};
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
+ }
440
511
}
441
512
else
442
513
{
443
514
mValid = false ;
444
515
}
516
+
445
517
if ( mValid )
446
518
{
447
- mStyleSheet .prepend ( " /*!search!*/" ).append ( " /*!search!*/" );
448
- QgsDebugMsgLevel ( mStyleSheet , 4 );
449
519
connect ( mWidget , &QWidget::destroyed, this , &QgsSearchHighlightOptionWidget::widgetDestroyed );
450
520
}
451
521
}
452
522
453
523
bool QgsSearchHighlightOptionWidget::searchHighlight ( const QString &searchText )
454
524
{
525
+ mSearchText = searchText;
455
526
bool found = false ;
456
527
if ( !mWidget )
457
528
return found;
@@ -461,8 +532,13 @@ bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText
461
532
found = mTextFound ( searchText );
462
533
}
463
534
464
- if ( found && ! mChangedStyle )
535
+ if ( found )
465
536
{
537
+ if ( mChangedStyle )
538
+ {
539
+ mReset ();
540
+ mChangedStyle = false ;
541
+ }
466
542
if ( !mWidget ->isVisible () )
467
543
{
468
544
// show the widget to get initial stylesheet in case it's modified
@@ -474,7 +550,7 @@ bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText
474
550
}
475
551
else
476
552
{
477
- mWidget -> setStyleSheet ( mWidget -> styleSheet () + mStyleSheet );
553
+ mHighlight ( searchText );
478
554
mChangedStyle = true ;
479
555
}
480
556
}
@@ -493,7 +569,8 @@ bool QgsSearchHighlightOptionWidget::eventFilter( QObject *obj, QEvent *event )
493
569
// after the widget is shown
494
570
#if 1
495
571
mWidget ->show ();
496
- mWidget ->setStyleSheet ( mWidget ->styleSheet () + mStyleSheet );
572
+ mHighlight ( mSearchText );
573
+ mChangedStyle = true ;
497
574
return true ;
498
575
#else
499
576
QTimer::singleShot( 500, this, [ = ]
@@ -512,12 +589,10 @@ void QgsSearchHighlightOptionWidget::reset()
512
589
{
513
590
if ( mChangedStyle )
514
591
{
515
- QString ss = mWidget ->styleSheet ();
516
- ss.remove ( mStyleSheet );
517
- mWidget ->setStyleSheet ( ss );
592
+ mReset ();
518
593
mChangedStyle = false ;
519
594
}
520
- else if ( mInstalledFilter )
595
+ if ( mInstalledFilter )
521
596
{
522
597
mWidget ->removeEventFilter ( this );
523
598
mInstalledFilter = false ;
0 commit comments