QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsoptionsdialoghighlightwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsoptionsdialoghighlightwidget.cpp
3  -------------------------------
4  Date : February 2018
5  Copyright : (C) 2018 Denis Rouzaud
6  Email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <QCheckBox>
17 #include <QEvent>
18 #include <QGroupBox>
19 #include <QLabel>
20 #include <QLayout>
21 #include <QTimer>
22 #include <QTreeView>
23 #include <QTreeWidget>
24 
26 #include "qgsmessagebaritem.h"
27 #include "qgsfilterlineedit.h"
28 
30 
31 
32 
33 
35  : QObject( widget )
36  , mWidget( widget )
37 {}
38 
40 {
41  QWidget *parent = widget;
42  while ( ( parent = parent->parentWidget() ) )
43  {
44  // do not register message bar content, items disappear and causes QGIS to crash
45  // do not register QgsFilterLineEdit's child widgets, the clear button might be deleted
46  if ( qobject_cast< QgsMessageBarItem * >( parent ) ||
47  qobject_cast< QgsFilterLineEdit * >( parent ) )
48  {
49  // return invalid widget
50  return nullptr;
51  }
52  }
53 
54  if ( qobject_cast<QLabel *>( widget ) )
55  {
56  return new QgsOptionsDialogHighlightLabel( qobject_cast<QLabel *>( widget ) );
57  }
58  else if ( qobject_cast<QCheckBox *>( widget ) )
59  {
60  return new QgsOptionsDialogHighlightCheckBox( qobject_cast<QCheckBox *>( widget ) );
61  }
62  else if ( qobject_cast<QAbstractButton *>( widget ) )
63  {
64  return new QgsOptionsDialogHighlightButton( qobject_cast<QAbstractButton *>( widget ) );
65  }
66  else if ( qobject_cast<QGroupBox *>( widget ) )
67  {
68  return new QgsOptionsDialogHighlightGroupBox( qobject_cast<QGroupBox *>( widget ) );
69  }
70  else if ( qobject_cast<QTreeView *>( widget ) )
71  {
72  return new QgsOptionsDialogHighlightTree( qobject_cast<QTreeView *>( widget ) );
73  }
74  else
75  {
76  // return invalid widget
77  return nullptr;
78  }
79 }
80 
82 {
83  mSearchText = text;
84  bool found = false;
85 
86  if ( !mWidget )
87  return found;
88 
89  if ( mChangedStyle )
90  {
91  reset();
92  mChangedStyle = false;
93  }
94 
95  if ( mInstalledFilter )
96  {
97  mWidget->removeEventFilter( this );
98  mInstalledFilter = false;
99  }
100 
101  if ( !text.isEmpty() )
102  {
103  found = searchText( text );
104  }
105 
106  if ( found )
107  {
108 
109  if ( !mWidget->isVisible() )
110  {
111  mWidget->installEventFilter( this );
112  mInstalledFilter = true;
113  }
114  else
115  {
116  mChangedStyle = highlightText( text );
117  }
118  }
119 
120  return found;
121 }
122 
123 bool QgsOptionsDialogHighlightWidget::eventFilter( QObject *obj, QEvent *event )
124 {
125  if ( mInstalledFilter && event->type() == QEvent::Show && obj == mWidget )
126  {
127  mWidget->removeEventFilter( this );
128  mInstalledFilter = false;
129  // instead of catching the event and calling show again
130  // it might be better to use a timer to change the style
131  // after the widget is shown
132 #if 1
133  mWidget->show();
134  mChangedStyle = highlightText( mSearchText );
135  return true;
136 #else
137  QTimer::singleShot( 500, this, [ = ]
138  {
139  mChangedStyle = highlightText( mSearchText );
140  } );
141 #endif
142  }
143  return QObject::eventFilter( obj, event );
144 }
145 
146 
147 
Container for a widget to be used to search text in the option dialog If the widget type is handled,...
QPointer< QWidget > mWidget
Pointer to the widget.
virtual void reset()=0
reset the style of the widgets to its original state
bool searchHighlight(const QString &text)
search for a text pattern and highlight the widget if the text is found
virtual bool searchText(const QString &text)=0
Search for the text in the widget and return true if it was found.
virtual bool highlightText(const QString &text)=0
Highlight the text in the widget.
bool eventFilter(QObject *obj, QEvent *event) override
static QgsOptionsDialogHighlightWidget * createWidget(QWidget *widget)
create a highlight widget implementation for the proper widget type.
QgsOptionsDialogHighlightWidget(QWidget *widget=nullptr)
Constructor.