QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsoptionsdialoghighlightwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsoptionsdialoghighlightwidget.cpp
3 -------------------------------
4 Date : February 2018
5 Copyright : (C) 2018 Denis Rouzaud
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 : 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 ( dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget ) )
55 {
56 return dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget );
57 }
58
59 if ( qobject_cast<QLabel *>( widget ) )
60 {
61 return new QgsOptionsDialogHighlightLabel( qobject_cast<QLabel *>( widget ) );
62 }
63 else if ( qobject_cast<QCheckBox *>( widget ) )
64 {
65 return new QgsOptionsDialogHighlightCheckBox( qobject_cast<QCheckBox *>( widget ) );
66 }
67 else if ( qobject_cast<QAbstractButton *>( widget ) )
68 {
69 return new QgsOptionsDialogHighlightButton( qobject_cast<QAbstractButton *>( widget ) );
70 }
71 else if ( qobject_cast<QGroupBox *>( widget ) )
72 {
73 return new QgsOptionsDialogHighlightGroupBox( qobject_cast<QGroupBox *>( widget ) );
74 }
75 else if ( qobject_cast<QTreeView *>( widget ) )
76 {
77 return new QgsOptionsDialogHighlightTree( qobject_cast<QTreeView *>( widget ) );
78 }
79 else if ( qobject_cast<QTableView *>( widget ) )
80 {
81 return new QgsOptionsDialogHighlightTable( qobject_cast<QTableView *>( widget ) );
82 }
83 else
84 {
85 // return invalid widget
86 return nullptr;
87 }
88}
89
91{
92 mSearchText = text;
93 bool found = false;
94
95 if ( !mWidget )
96 return found;
97
98 if ( mEventFilter )
99 {
100 mWidget->removeEventFilter( mEventFilter );
101 delete mEventFilter;
102 mEventFilter = nullptr;
103 }
104
105 if ( !text.isEmpty() )
106 {
107 found = searchText( mSearchText );
108 }
109 else
110 {
111 reset();
112 mChangedStyle = false;
113 }
114
115 if ( mChangedStyle )
116 {
117 reset();
118 mChangedStyle = false;
119 }
120
121 if ( found )
122 {
123
124 if ( !mWidget->isVisible() )
125 {
126 mEventFilter = new QgsOptionsDialogHighlightWidgetEventFilter( this );
127 mWidget->installEventFilter( mEventFilter );
128 }
129 else
130 {
131 mChangedStyle = highlightText( mSearchText );
132 }
133 }
134
135 return found;
136}
137
138
139
141
142QgsOptionsDialogHighlightWidgetEventFilter::QgsOptionsDialogHighlightWidgetEventFilter( QgsOptionsDialogHighlightWidget *highlightWidget )
143 : QObject( highlightWidget->widget() )
144 , mHighlightWidget( highlightWidget )
145{}
146
147bool QgsOptionsDialogHighlightWidgetEventFilter::eventFilter( QObject *obj, QEvent *event )
148{
149 if ( event->type() == QEvent::Show && obj == mHighlightWidget->widget() )
150 {
151 mHighlightWidget->widget()->removeEventFilter( this );
152 // instead of catching the event and calling show again
153 // it might be better to use a timer to change the style
154 // after the widget is shown
155#if 1
156 mHighlightWidget->widget()->show();
157 mHighlightWidget->mChangedStyle = mHighlightWidget->highlightText( mHighlightWidget->mSearchText );
158 return true;
159#else
160 QTimer::singleShot( 500, this, [ = ]
161 {
162 mChangedStyle = highlightText( mSearchText );
163 } );
164#endif
165 }
166 return QObject::eventFilter( obj, event );
167}
168
170
171
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.
static QgsOptionsDialogHighlightWidget * createWidget(QWidget *widget)
create a highlight widget implementation for the proper widget type.
QgsOptionsDialogHighlightWidget(QWidget *widget=nullptr)
Constructor.