QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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 : denis.rouzaud@gmail.com
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 "moc_qgsoptionsdialoghighlightwidget.cpp"
28#include "qgsmessagebaritem.h"
29#include "qgsfilterlineedit.h"
30
32
33
34
35
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 ( dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget ) )
56 {
57 return dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget );
58 }
59
60 if ( qobject_cast<QLabel *>( widget ) )
61 {
62 return new QgsOptionsDialogHighlightLabel( qobject_cast<QLabel *>( widget ) );
63 }
64 else if ( qobject_cast<QCheckBox *>( widget ) )
65 {
66 return new QgsOptionsDialogHighlightCheckBox( qobject_cast<QCheckBox *>( widget ) );
67 }
68 else if ( qobject_cast<QAbstractButton *>( widget ) )
69 {
70 return new QgsOptionsDialogHighlightButton( qobject_cast<QAbstractButton *>( widget ) );
71 }
72 else if ( qobject_cast<QGroupBox *>( widget ) )
73 {
74 return new QgsOptionsDialogHighlightGroupBox( qobject_cast<QGroupBox *>( widget ) );
75 }
76 else if ( qobject_cast<QTreeView *>( widget ) )
77 {
78 return new QgsOptionsDialogHighlightTree( qobject_cast<QTreeView *>( widget ) );
79 }
80 else if ( qobject_cast<QTableView *>( widget ) )
81 {
82 return new QgsOptionsDialogHighlightTable( qobject_cast<QTableView *>( widget ) );
83 }
84 else
85 {
86 // return invalid widget
87 return nullptr;
88 }
89}
90
92{
93 mSearchText = text;
94 bool found = false;
95
96 if ( !mWidget )
97 return found;
98
99 if ( mEventFilter )
100 {
101 mWidget->removeEventFilter( mEventFilter );
102 delete mEventFilter;
103 mEventFilter = nullptr;
104 }
105
106 if ( !text.isEmpty() )
107 {
108 found = searchText( mSearchText );
109 }
110 else
111 {
112 reset();
113 mChangedStyle = false;
114 }
115
116 if ( mChangedStyle )
117 {
118 reset();
119 mChangedStyle = false;
120 }
121
122 if ( found )
123 {
124
125 if ( !mWidget->isVisible() )
126 {
127 mEventFilter = new QgsOptionsDialogHighlightWidgetEventFilter( this );
128 mWidget->installEventFilter( mEventFilter );
129 }
130 else
131 {
132 mChangedStyle = highlightText( mSearchText );
133 }
134 }
135
136 return found;
137}
138
139
140
142
143QgsOptionsDialogHighlightWidgetEventFilter::QgsOptionsDialogHighlightWidgetEventFilter( QgsOptionsDialogHighlightWidget *highlightWidget )
144 : QObject( highlightWidget->widget() )
145 , mHighlightWidget( highlightWidget )
146{}
147
148bool QgsOptionsDialogHighlightWidgetEventFilter::eventFilter( QObject *obj, QEvent *event )
149{
150 if ( event->type() == QEvent::Show && obj == mHighlightWidget->widget() )
151 {
152 mHighlightWidget->widget()->removeEventFilter( this );
153 // instead of catching the event and calling show again
154 // it might be better to use a timer to change the style
155 // after the widget is shown
156#if 1
157 mHighlightWidget->widget()->show();
158 mHighlightWidget->mChangedStyle = mHighlightWidget->highlightText( mHighlightWidget->mSearchText );
159 return true;
160#else
161 QTimer::singleShot( 500, this, [ = ]
162 {
163 mChangedStyle = highlightText( mSearchText );
164 } );
165#endif
166 }
167 return QObject::eventFilter( obj, event );
168}
169
171
172
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.