QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 #include <QTableView>
25 
27 #include "qgsmessagebaritem.h"
28 #include "qgsfilterlineedit.h"
29 
31 
32 
33 
34 
36  : QObject( widget )
37  , mWidget( widget )
38 {}
39 
41 {
42  QWidget *parent = widget;
43  while ( ( parent = parent->parentWidget() ) )
44  {
45  // do not register message bar content, items disappear and causes QGIS to crash
46  // do not register QgsFilterLineEdit's child widgets, the clear button might be deleted
47  if ( qobject_cast< QgsMessageBarItem * >( parent ) ||
48  qobject_cast< QgsFilterLineEdit * >( parent ) )
49  {
50  // return invalid widget
51  return nullptr;
52  }
53  }
54 
55  if ( qobject_cast<QLabel *>( widget ) )
56  {
57  return new QgsOptionsDialogHighlightLabel( qobject_cast<QLabel *>( widget ) );
58  }
59  else if ( qobject_cast<QCheckBox *>( widget ) )
60  {
61  return new QgsOptionsDialogHighlightCheckBox( qobject_cast<QCheckBox *>( widget ) );
62  }
63  else if ( qobject_cast<QAbstractButton *>( widget ) )
64  {
65  return new QgsOptionsDialogHighlightButton( qobject_cast<QAbstractButton *>( widget ) );
66  }
67  else if ( qobject_cast<QGroupBox *>( widget ) )
68  {
69  return new QgsOptionsDialogHighlightGroupBox( qobject_cast<QGroupBox *>( widget ) );
70  }
71  else if ( qobject_cast<QTreeView *>( widget ) )
72  {
73  return new QgsOptionsDialogHighlightTree( qobject_cast<QTreeView *>( widget ) );
74  }
75  else if ( qobject_cast<QTableView *>( widget ) )
76  {
77  return new QgsOptionsDialogHighlightTable( qobject_cast<QTableView *>( widget ) );
78  }
79  else
80  {
81  // return invalid widget
82  return nullptr;
83  }
84 }
85 
87 {
88  mSearchText = text;
89  bool found = false;
90 
91  if ( !mWidget )
92  return found;
93 
94  if ( mChangedStyle )
95  {
96  reset();
97  mChangedStyle = false;
98  }
99 
100  if ( mInstalledFilter )
101  {
102  mWidget->removeEventFilter( this );
103  mInstalledFilter = false;
104  }
105 
106  if ( !text.isEmpty() )
107  {
108  found = searchText( text );
109  }
110 
111  if ( found )
112  {
113 
114  if ( !mWidget->isVisible() )
115  {
116  mWidget->installEventFilter( this );
117  mInstalledFilter = true;
118  }
119  else
120  {
121  mChangedStyle = highlightText( text );
122  }
123  }
124 
125  return found;
126 }
127 
128 bool QgsOptionsDialogHighlightWidget::eventFilter( QObject *obj, QEvent *event )
129 {
130  if ( mInstalledFilter && event->type() == QEvent::Show && obj == mWidget )
131  {
132  mWidget->removeEventFilter( this );
133  mInstalledFilter = false;
134  // instead of catching the event and calling show again
135  // it might be better to use a timer to change the style
136  // after the widget is shown
137 #if 1
138  mWidget->show();
139  mChangedStyle = highlightText( mSearchText );
140  return true;
141 #else
142  QTimer::singleShot( 500, this, [ = ]
143  {
144  mChangedStyle = highlightText( mSearchText );
145  } );
146 #endif
147  }
148  return QObject::eventFilter( obj, event );
149 }
150 
151 
152 
A highlight widget for table widgets.
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.