QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
29 
30 
31 
32 
34  : QObject( widget )
35  , mWidget( widget )
36 {}
37 
39 {
40  QWidget *parent = widget;
41  while ( ( parent = parent->parentWidget() ) )
42  {
43  // do not register message bar content, items disappear and causes QGIS to crash
44  if ( qobject_cast< QgsMessageBarItem * >( parent ) )
45  {
46  // return invalid widget
47  return nullptr;
48  }
49  }
50 
51  if ( qobject_cast<QLabel *>( widget ) )
52  {
53  return new QgsOptionsDialogHighlightLabel( qobject_cast<QLabel *>( widget ) );
54  }
55  else if ( qobject_cast<QCheckBox *>( widget ) )
56  {
57  return new QgsOptionsDialogHighlightCheckBox( qobject_cast<QCheckBox *>( widget ) );
58  }
59  else if ( qobject_cast<QAbstractButton *>( widget ) )
60  {
61  return new QgsOptionsDialogHighlightButton( qobject_cast<QAbstractButton *>( widget ) );
62  }
63  else if ( qobject_cast<QGroupBox *>( widget ) )
64  {
65  return new QgsOptionsDialogHighlightGroupBox( qobject_cast<QGroupBox *>( widget ) );
66  }
67  else if ( qobject_cast<QTreeView *>( widget ) )
68  {
69  return new QgsOptionsDialogHighlightTree( qobject_cast<QTreeView *>( widget ) );
70  }
71  else
72  {
73  // return invalid widget
74  return nullptr;
75  }
76 }
77 
79 {
80  mSearchText = text;
81  bool found = false;
82 
83  if ( !mWidget )
84  return found;
85 
86  if ( mChangedStyle )
87  {
88  reset();
89  mChangedStyle = false;
90  }
91 
92  if ( mInstalledFilter )
93  {
94  mWidget->removeEventFilter( this );
95  mInstalledFilter = false;
96  }
97 
98  if ( !text.isEmpty() )
99  {
100  found = searchText( text );
101  }
102 
103  if ( found )
104  {
105 
106  if ( !mWidget->isVisible() )
107  {
108  mWidget->installEventFilter( this );
109  mInstalledFilter = true;
110  }
111  else
112  {
113  mChangedStyle = highlightText( text );
114  }
115  }
116 
117  return found;
118 }
119 
120 bool QgsOptionsDialogHighlightWidget::eventFilter( QObject *obj, QEvent *event )
121 {
122  if ( mInstalledFilter && event->type() == QEvent::Show && obj == mWidget )
123  {
124  mWidget->removeEventFilter( this );
125  mInstalledFilter = false;
126  // instead of catching the event and calling show again
127  // it might be better to use a timer to change the style
128  // after the widget is shown
129 #if 1
130  mWidget->show();
131  mChangedStyle = highlightText( mSearchText );
132  return true;
133 #else
134  QTimer::singleShot( 500, this, [ = ]
135  {
136  mChangedStyle = highlightText( mSearchText );
137  } );
138 #endif
139  }
140  return QObject::eventFilter( obj, event );
141 }
142 
143 
144 
bool searchHighlight(const QString &text)
search for a text pattern and highlight the widget if the text is found
virtual void reset()=0
reset the style of the widgets to its original state
QPointer< QWidget > mWidget
Pointer to the widget.
bool eventFilter(QObject *obj, QEvent *event) override
static QgsOptionsDialogHighlightWidget * createWidget(QWidget *widget)
create a highlight widget implementation for the proper widget type.
virtual bool searchText(const QString &text)=0
Search for the text in the widget and return true if it was found.
Container for a widget to be used to search text in the option dialog If the widget type is handled...
QgsOptionsDialogHighlightWidget(QWidget *widget=nullptr)
Constructor.
virtual bool highlightText(const QString &text)=0
Highlight the text in the widget.
QWidget * widget()
Returns the widget.