QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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
16#include <QCheckBox>
17#include <QDialog>
18#include <QDialogButtonBox>
19#include <QEvent>
20#include <QGroupBox>
21#include <QLabel>
22#include <QTreeView>
23#include <QTreeWidget>
24#include <QAbstractItemModel>
25#include <QTableView>
26#include <QTextDocumentFragment>
27
29
31
32#include <functional>
33
37const int HIGHLIGHT_TEXT_RED = 0;
39const int HIGHLIGHT_TEXT_BLUE = 0;
40
41// ****************
42// QLabel
45 , mLabel( label )
46 , mStyleSheet( QStringLiteral( "QLabel { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6 );}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
49 .arg( HIGHLIGHT_TEXT_RED )
51 .arg( HIGHLIGHT_TEXT_BLUE ) )
52{}
53
55{
56 if ( !mLabel )
57 return false;
58
59 const QString labelText = QTextDocumentFragment::fromHtml( mLabel->text() ).toPlainText();
60 return labelText.contains( text, Qt::CaseInsensitive );
61}
62
64{
65 if ( !mWidget )
66 return false;
67 Q_UNUSED( text )
68 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
69 return true;
70}
71
73{
74 if ( !mWidget )
75 return;
76 QString ss = mWidget->styleSheet();
77 ss.remove( mStyleSheet );
78 mWidget->setStyleSheet( ss );
79}
80
81// ****************
82// QCheckBox
85 , mCheckBox( checkBox )
86 , mStyleSheet( QStringLiteral( "/*!search!*/QCheckBox { background-color: rgb(%1, %2, %3); color: rgb( %4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
89 .arg( HIGHLIGHT_TEXT_RED )
91 .arg( HIGHLIGHT_TEXT_BLUE ) )
92{
93}
94
96{
97 if ( !mCheckBox )
98 return false;
99
100 return mCheckBox->text().contains( text, Qt::CaseInsensitive );
101
102}
103
105{
106 if ( !mWidget )
107 return false;
108 Q_UNUSED( text )
109 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
110 return true;
111}
112
114{
115 if ( !mWidget )
116 return;
117 QString ss = mWidget->styleSheet();
118 ss.remove( mStyleSheet );
119 mWidget->setStyleSheet( ss );
120}
121
122// ****************
123// QAbstractButton
126 , mButton( button )
127 , mStyleSheet( QStringLiteral( "/*!search!*/QAbstractButton { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
130 .arg( HIGHLIGHT_TEXT_RED )
132 .arg( HIGHLIGHT_TEXT_BLUE ) )
133{
134}
135
137{
138 if ( !mButton )
139 return false;
140
141 return mButton->text().contains( text, Qt::CaseInsensitive );
142
143}
144
146{
147 if ( !mWidget )
148 return false;
149 Q_UNUSED( text )
150 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
151 return true;
152}
153
155{
156 if ( !mWidget )
157 return;
158 QString ss = mWidget->styleSheet();
159 ss.remove( mStyleSheet );
160 mWidget->setStyleSheet( ss );
161}
162
163// ****************
164// QGroupBox
167 , mGroupBox( groupBox )
168 , mStyleSheet( QStringLiteral( "/*!search!*/QGroupBox::title { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
171 .arg( HIGHLIGHT_TEXT_RED )
173 .arg( HIGHLIGHT_TEXT_BLUE ) )
174{
175}
176
178{
179 if ( !mGroupBox )
180 return false;
181
182 return mGroupBox->title().contains( text, Qt::CaseInsensitive );
183}
184
186{
187 Q_UNUSED( text )
188 if ( !mWidget )
189 return false;
190
191 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
192 return true;
193}
194
196{
197 if ( !mWidget )
198 return;
199 QString ss = mWidget->styleSheet();
200 ss.remove( mStyleSheet );
201 mWidget->setStyleSheet( ss );
202}
203
204// ****************
205// QTreeView
208 , mTreeView( treeView )
209{
210}
211
213{
214 if ( !mTreeView || !mTreeView->model() )
215 return false;
216
217 // search headers too!
218 for ( int col = 0; col < mTreeView->model()->columnCount(); ++col )
219 {
220 const QString headerText = mTreeView->model()->headerData( col, Qt::Horizontal ).toString();
221 if ( headerText.contains( text, Qt::CaseInsensitive ) )
222 return true;
223 }
224
225 const QModelIndexList hits = mTreeView->model()->match( mTreeView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
226 return !hits.isEmpty();
227}
228
230{
231 bool success = false;
232 QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
233 if ( treeWidget )
234 {
235 mTreeInitialVisible.clear();
236 // initially hide everything
237 std::function< void( QTreeWidgetItem *, bool ) > setChildrenVisible;
238 setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem * item, bool visible )
239 {
240 for ( int i = 0; i < item->childCount(); ++i )
241 setChildrenVisible( item->child( i ), visible );
242 mTreeInitialVisible.insert( item, !item->isHidden() );
243 item->setHidden( !visible );
244 };
245 setChildrenVisible( treeWidget->invisibleRootItem(), false );
246
247 const QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
248 success = !items.empty();
249 mTreeInitialExpand.clear();
250 for ( QTreeWidgetItem *item : items )
251 {
252 setChildrenVisible( item, true );
253
254 QTreeWidgetItem *parent = item;
255 while ( parent )
256 {
257 if ( mTreeInitialExpand.contains( parent ) )
258 break;
259 mTreeInitialExpand.insert( parent, parent->isExpanded() );
260 parent->setExpanded( true );
261 parent->setHidden( false );
262 parent = parent->parent();
263 }
264 }
265 }
266
267 return success;
268}
269
271{
272 if ( !mTreeView )
273 return;
274
275 QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
276 if ( treeWidget )
277 {
278 // show everything
279 std::function< void( QTreeWidgetItem * ) > showChildren;
280 showChildren = [this, &showChildren]( QTreeWidgetItem * item )
281 {
282 for ( int i = 0; i < item->childCount(); ++i )
283 showChildren( item->child( i ) );
284 item->setHidden( !mTreeInitialVisible.value( item, true ) );
285 };
286 showChildren( treeWidget->invisibleRootItem() );
287 for ( auto it = mTreeInitialExpand.constBegin(); it != mTreeInitialExpand.constEnd(); it++ )
288 {
289 QTreeWidgetItem *item = it.key();
290 if ( item )
291 {
292 item->setExpanded( it.value() );
293 }
294 }
295 mTreeInitialExpand.clear();
296 }
297}
298
299
300// ****************
301// QTableView
304 , mTableView( tableView )
305{
306}
307
309{
310 if ( !mTableView || !mTableView->model() )
311 return false;
312
313 // search headers too!
314 for ( int col = 0; col < mTableView->model()->columnCount(); ++col )
315 {
316 const QString headerText = mTableView->model()->headerData( col, Qt::Horizontal ).toString();
317 if ( headerText.contains( text, Qt::CaseInsensitive ) )
318 return true;
319 }
320
321 const QModelIndexList hits = mTableView->model()->match( mTableView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
322 return !hits.isEmpty();
323}
324
326{
327 return false;
328}
329
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
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.
const int HIGHLIGHT_BACKGROUND_GREEN
const int HIGHLIGHT_BACKGROUND_RED
const int HIGHLIGHT_BACKGROUND_BLUE