QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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
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
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 // do not register QgsFilterLineEdit's child widgets, the clear button might be deleted
45 if ( qobject_cast<QgsMessageBarItem *>( parent ) || qobject_cast<QgsFilterLineEdit *>( parent ) )
46 {
47 // return invalid widget
48 return nullptr;
49 }
50 }
51
52 if ( dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget ) )
53 {
54 return dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget );
55 }
56
57 if ( qobject_cast<QLabel *>( widget ) )
58 {
59 return new QgsOptionsDialogHighlightLabel( qobject_cast<QLabel *>( widget ) );
60 }
61 else if ( qobject_cast<QCheckBox *>( widget ) )
62 {
63 return new QgsOptionsDialogHighlightCheckBox( qobject_cast<QCheckBox *>( widget ) );
64 }
65 else if ( qobject_cast<QAbstractButton *>( widget ) )
66 {
67 return new QgsOptionsDialogHighlightButton( qobject_cast<QAbstractButton *>( widget ) );
68 }
69 else if ( qobject_cast<QGroupBox *>( widget ) )
70 {
71 return new QgsOptionsDialogHighlightGroupBox( qobject_cast<QGroupBox *>( widget ) );
72 }
73 else if ( qobject_cast<QTreeView *>( widget ) )
74 {
75 return new QgsOptionsDialogHighlightTree( qobject_cast<QTreeView *>( widget ) );
76 }
77 else if ( qobject_cast<QTableView *>( widget ) )
78 {
79 return new QgsOptionsDialogHighlightTable( qobject_cast<QTableView *>( widget ) );
80 }
81 else
82 {
83 // return invalid widget
84 return nullptr;
85 }
86}
87
89{
90 mSearchText = text;
91 bool found = false;
92
93 if ( !mWidget )
94 return found;
95
96 if ( mEventFilter )
97 {
98 mWidget->removeEventFilter( mEventFilter );
99 delete mEventFilter;
100 mEventFilter = nullptr;
101 }
102
103 if ( !text.isEmpty() )
104 {
105 found = searchText( mSearchText );
106 }
107 else
108 {
109 reset();
110 mChangedStyle = false;
111 }
112
113 if ( mChangedStyle )
114 {
115 reset();
116 mChangedStyle = false;
117 }
118
119 if ( found )
120 {
121 if ( !mWidget->isVisible() )
122 {
123 mEventFilter = new QgsOptionsDialogHighlightWidgetEventFilter( this );
124 mWidget->installEventFilter( mEventFilter );
125 }
126 else
127 {
128 mChangedStyle = highlightText( mSearchText );
129 }
130 }
131
132 return found;
133}
134
135
137
138QgsOptionsDialogHighlightWidgetEventFilter::QgsOptionsDialogHighlightWidgetEventFilter( QgsOptionsDialogHighlightWidget *highlightWidget )
139 : QObject( highlightWidget->widget() )
140 , mHighlightWidget( highlightWidget )
141{}
142
143bool QgsOptionsDialogHighlightWidgetEventFilter::eventFilter( QObject *obj, QEvent *event )
144{
145 if ( event->type() == QEvent::Show && obj == mHighlightWidget->widget() )
146 {
147 mHighlightWidget->widget()->removeEventFilter( this );
148 // instead of catching the event and calling show again
149 // it might be better to use a timer to change the style
150 // after the widget is shown
151#if 1
152 mHighlightWidget->widget()->show();
153 mHighlightWidget->mChangedStyle = mHighlightWidget->highlightText( mHighlightWidget->mSearchText );
154 return true;
155#else
156 QTimer::singleShot( 500, this, [=] {
157 mChangedStyle = highlightText( mSearchText );
158 } );
159#endif
160 }
161 return QObject::eventFilter( obj, event );
162}
163
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,...
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
QPointer< QWidget > mWidget
Pointer to the widget.
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.