QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsoptionsdialoghighlightwidgetsimpl.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsoptionsdialoghighlightwidgetsimpl.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
17
18#include <functional>
19
21
22#include <QAbstractItemModel>
23#include <QCheckBox>
24#include <QDialog>
25#include <QDialogButtonBox>
26#include <QEvent>
27#include <QGroupBox>
28#include <QLabel>
29#include <QString>
30#include <QTableView>
31#include <QTextDocumentFragment>
32#include <QTreeView>
33#include <QTreeWidget>
34
35using namespace Qt::StringLiterals;
36
40const int HIGHLIGHT_TEXT_RED = 0;
42const int HIGHLIGHT_TEXT_BLUE = 0;
43
44// ****************
45// QLabel
48 , mLabel( label )
49 , mStyleSheet( QStringLiteral( "QLabel { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6 );}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED ).arg( HIGHLIGHT_BACKGROUND_GREEN ).arg( HIGHLIGHT_BACKGROUND_BLUE ).arg( HIGHLIGHT_TEXT_RED ).arg( HIGHLIGHT_TEXT_GREEN ).arg( HIGHLIGHT_TEXT_BLUE ) )
50{}
51
53{
54 if ( !mLabel )
55 return false;
56
57 const QString labelText = QTextDocumentFragment::fromHtml( mLabel->text() ).toPlainText();
58 return labelText.contains( text, Qt::CaseInsensitive );
59}
60
62{
63 if ( !mWidget )
64 return false;
65 Q_UNUSED( text )
66 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
67 return true;
68}
69
71{
72 if ( !mWidget )
73 return;
74 QString ss = mWidget->styleSheet();
75 ss.remove( mStyleSheet );
76 mWidget->setStyleSheet( ss );
77}
78
79// ****************
80// QCheckBox
83 , mCheckBox( checkBox )
84 , mStyleSheet( u"/*!search!*/QCheckBox { background-color: rgb(%1, %2, %3); color: rgb( %4, %5, %6);}/*!search!*/"_s.arg( HIGHLIGHT_BACKGROUND_RED ).arg( HIGHLIGHT_BACKGROUND_GREEN ).arg( HIGHLIGHT_BACKGROUND_BLUE ).arg( HIGHLIGHT_TEXT_RED ).arg( HIGHLIGHT_TEXT_GREEN ).arg( HIGHLIGHT_TEXT_BLUE ) )
85{
86}
87
89{
90 if ( !mCheckBox )
91 return false;
92
93 return mCheckBox->text().contains( text, Qt::CaseInsensitive );
94}
95
97{
98 if ( !mWidget )
99 return false;
100 Q_UNUSED( text )
101 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
102 return true;
103}
104
106{
107 if ( !mWidget )
108 return;
109 QString ss = mWidget->styleSheet();
110 ss.remove( mStyleSheet );
111 mWidget->setStyleSheet( ss );
112}
113
114// ****************
115// QAbstractButton
118 , mButton( button )
119 , mStyleSheet( u"/*!search!*/QAbstractButton { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/"_s.arg( HIGHLIGHT_BACKGROUND_RED ).arg( HIGHLIGHT_BACKGROUND_GREEN ).arg( HIGHLIGHT_BACKGROUND_BLUE ).arg( HIGHLIGHT_TEXT_RED ).arg( HIGHLIGHT_TEXT_GREEN ).arg( HIGHLIGHT_TEXT_BLUE ) )
120{
121}
122
124{
125 if ( !mButton )
126 return false;
127
128 return mButton->text().contains( text, Qt::CaseInsensitive );
129}
130
132{
133 if ( !mWidget )
134 return false;
135 Q_UNUSED( text )
136 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
137 return true;
138}
139
141{
142 if ( !mWidget )
143 return;
144 QString ss = mWidget->styleSheet();
145 ss.remove( mStyleSheet );
146 mWidget->setStyleSheet( ss );
147}
148
149// ****************
150// QGroupBox
153 , mGroupBox( groupBox )
154 , mStyleSheet( u"/*!search!*/QGroupBox::title { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/"_s.arg( HIGHLIGHT_BACKGROUND_RED ).arg( HIGHLIGHT_BACKGROUND_GREEN ).arg( HIGHLIGHT_BACKGROUND_BLUE ).arg( HIGHLIGHT_TEXT_RED ).arg( HIGHLIGHT_TEXT_GREEN ).arg( HIGHLIGHT_TEXT_BLUE ) )
155{
156}
157
159{
160 if ( !mGroupBox )
161 return false;
162
163 return mGroupBox->title().contains( text, Qt::CaseInsensitive );
164}
165
167{
168 Q_UNUSED( text )
169 if ( !mWidget )
170 return false;
171
172 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
173 return true;
174}
175
177{
178 if ( !mWidget )
179 return;
180 QString ss = mWidget->styleSheet();
181 ss.remove( mStyleSheet );
182 mWidget->setStyleSheet( ss );
183}
184
185// ****************
186// QTreeView
189 , mTreeView( treeView )
190{
191}
192
194{
195 if ( !mTreeView || !mTreeView->model() )
196 return false;
197
198 // search headers too!
199 for ( int col = 0; col < mTreeView->model()->columnCount(); ++col )
200 {
201 const QString headerText = mTreeView->model()->headerData( col, Qt::Horizontal ).toString();
202 if ( headerText.contains( text, Qt::CaseInsensitive ) )
203 return true;
204 }
205
206 const QModelIndexList hits = mTreeView->model()->match( mTreeView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
207 return !hits.isEmpty();
208}
209
211{
212 bool success = false;
213 QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
214 if ( treeWidget )
215 {
216 mTreeInitialVisible.clear();
217 // initially hide everything
218 std::function<void( QTreeWidgetItem *, bool )> setChildrenVisible;
219 setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem *item, bool visible ) {
220 for ( int i = 0; i < item->childCount(); ++i )
221 setChildrenVisible( item->child( i ), visible );
222 mTreeInitialVisible.insert( item, !item->isHidden() );
223 item->setHidden( !visible );
224 };
225 setChildrenVisible( treeWidget->invisibleRootItem(), false );
226
227 const QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
228 success = !items.empty();
229 mTreeInitialExpand.clear();
230 for ( QTreeWidgetItem *item : items )
231 {
232 setChildrenVisible( item, true );
233
234 QTreeWidgetItem *parent = item;
235 while ( parent )
236 {
237 if ( mTreeInitialExpand.contains( parent ) )
238 break;
239 mTreeInitialExpand.insert( parent, parent->isExpanded() );
240 parent->setExpanded( true );
241 parent->setHidden( false );
242 parent = parent->parent();
243 }
244 }
245 }
246
247 return success;
248}
249
251{
252 if ( !mTreeView )
253 return;
254
255 QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
256 if ( treeWidget )
257 {
258 // show everything
259 std::function<void( QTreeWidgetItem * )> showChildren;
260 showChildren = [this, &showChildren]( QTreeWidgetItem *item ) {
261 for ( int i = 0; i < item->childCount(); ++i )
262 showChildren( item->child( i ) );
263 item->setHidden( !mTreeInitialVisible.value( item, true ) );
264 };
265 showChildren( treeWidget->invisibleRootItem() );
266 for ( auto it = mTreeInitialExpand.constBegin(); it != mTreeInitialExpand.constEnd(); it++ )
267 {
268 QTreeWidgetItem *item = it.key();
269 if ( item )
270 {
271 item->setExpanded( it.value() );
272 }
273 }
274 mTreeInitialExpand.clear();
275 }
276}
277
278
279// ****************
280// QTableView
283 , mTableView( tableView )
284{
285}
286
288{
289 if ( !mTableView || !mTableView->model() )
290 return false;
291
292 // search headers too!
293 for ( int col = 0; col < mTableView->model()->columnCount(); ++col )
294 {
295 const QString headerText = mTableView->model()->headerData( col, Qt::Horizontal ).toString();
296 if ( headerText.contains( text, Qt::CaseInsensitive ) )
297 return true;
298 }
299
300 const QModelIndexList hits = mTableView->model()->match( mTableView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
301 return !hits.isEmpty();
302}
303
305{
306 return false;
307}
308
QgsOptionsDialogHighlightButton(QAbstractButton *button)
constructs a highlight widget for a button.
void reset() override
reset the style of the widgets to its original state
bool highlightText(const QString &text) override
Highlight the text in the widget.
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
void reset() override
reset the style of the widgets to its original state
bool highlightText(const QString &text) override
Highlight the text in the widget.
QgsOptionsDialogHighlightCheckBox(QCheckBox *checkBox)
constructs a highlight widget for a checkbox
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
QgsOptionsDialogHighlightGroupBox(QGroupBox *groupBox)
constructs a highlight widget for a group box.
void reset() override
reset the style of the widgets to its original state
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
bool highlightText(const QString &text) override
Highlight the text in the widget.
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
bool highlightText(const QString &text) override
Highlight the text in the widget.
QgsOptionsDialogHighlightLabel(QLabel *label)
constructs a highlight widget for a label
void reset() override
reset the style of the widgets to its original state
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
void reset() override
reset the style of the widgets to its original state
bool highlightText(const QString &text) override
Highlight the text in the widget.
QgsOptionsDialogHighlightTable(QTableView *tableView)
constructs a highlight widget for a table view or widget.
void reset() override
reset the style of the widgets to its original state
QgsOptionsDialogHighlightTree(QTreeView *treeView)
constructs a highlight widget for a tree view or widget.
bool highlightText(const QString &text) override
Highlight the text in the widget.
QMap< QTreeWidgetItem *, bool > mTreeInitialVisible
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
QMap< QTreeWidgetItem *, bool > mTreeInitialExpand
QPointer< QWidget > mWidget
Pointer to the widget.
QgsOptionsDialogHighlightWidget(QWidget *widget=nullptr)
Constructor.
const int HIGHLIGHT_BACKGROUND_GREEN
const int HIGHLIGHT_BACKGROUND_RED
const int HIGHLIGHT_BACKGROUND_BLUE