QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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!*/" )
53 .arg( HIGHLIGHT_TEXT_RED )
55 .arg( HIGHLIGHT_TEXT_BLUE ) )
56{}
57
59{
60 if ( !mLabel )
61 return false;
62
63 const QString labelText = QTextDocumentFragment::fromHtml( mLabel->text() ).toPlainText();
64 return labelText.contains( text, Qt::CaseInsensitive );
65}
66
68{
69 if ( !mWidget )
70 return false;
71 Q_UNUSED( text )
72 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
73 return true;
74}
75
77{
78 if ( !mWidget )
79 return;
80 QString ss = mWidget->styleSheet();
81 ss.remove( mStyleSheet );
82 mWidget->setStyleSheet( ss );
83}
84
85// ****************
86// QCheckBox
89 , mCheckBox( checkBox )
90 , mStyleSheet( u"/*!search!*/QCheckBox { background-color: rgb(%1, %2, %3); color: rgb( %4, %5, %6);}/*!search!*/"_s.arg( HIGHLIGHT_BACKGROUND_RED )
93 .arg( HIGHLIGHT_TEXT_RED )
95 .arg( HIGHLIGHT_TEXT_BLUE ) )
96{}
97
99{
100 if ( !mCheckBox )
101 return false;
102
103 return mCheckBox->text().contains( text, Qt::CaseInsensitive );
104}
105
107{
108 if ( !mWidget )
109 return false;
110 Q_UNUSED( text )
111 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
112 return true;
113}
114
116{
117 if ( !mWidget )
118 return;
119 QString ss = mWidget->styleSheet();
120 ss.remove( mStyleSheet );
121 mWidget->setStyleSheet( ss );
122}
123
124// ****************
125// QAbstractButton
128 , mButton( button )
129 , mStyleSheet( u"/*!search!*/QAbstractButton { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/"_s.arg( HIGHLIGHT_BACKGROUND_RED )
132 .arg( HIGHLIGHT_TEXT_RED )
134 .arg( HIGHLIGHT_TEXT_BLUE ) )
135{}
136
138{
139 if ( !mButton )
140 return false;
141
142 return mButton->text().contains( text, Qt::CaseInsensitive );
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( u"/*!search!*/QGroupBox::title { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/"_s.arg( HIGHLIGHT_BACKGROUND_RED )
171 .arg( HIGHLIGHT_TEXT_RED )
173 .arg( HIGHLIGHT_TEXT_BLUE ) )
174{}
175
177{
178 if ( !mGroupBox )
179 return false;
180
181 return mGroupBox->title().contains( text, Qt::CaseInsensitive );
182}
183
185{
186 Q_UNUSED( text )
187 if ( !mWidget )
188 return false;
189
190 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
191 return true;
192}
193
195{
196 if ( !mWidget )
197 return;
198 QString ss = mWidget->styleSheet();
199 ss.remove( mStyleSheet );
200 mWidget->setStyleSheet( ss );
201}
202
203// ****************
204// QTreeView
209
211{
212 if ( !mTreeView || !mTreeView->model() )
213 return false;
214
215 // search headers too!
216 for ( int col = 0; col < mTreeView->model()->columnCount(); ++col )
217 {
218 const QString headerText = mTreeView->model()->headerData( col, Qt::Horizontal ).toString();
219 if ( headerText.contains( text, Qt::CaseInsensitive ) )
220 return true;
221 }
222
223 const QModelIndexList hits = mTreeView->model()->match( mTreeView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
224 return !hits.isEmpty();
225}
226
228{
229 bool success = false;
230 QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
231 if ( treeWidget )
232 {
233 mTreeInitialVisible.clear();
234 // initially hide everything
235 std::function<void( QTreeWidgetItem *, bool )> setChildrenVisible;
236 setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem *item, bool visible ) {
237 for ( int i = 0; i < item->childCount(); ++i )
238 setChildrenVisible( item->child( i ), visible );
239 mTreeInitialVisible.insert( item, !item->isHidden() );
240 item->setHidden( !visible );
241 };
242 setChildrenVisible( treeWidget->invisibleRootItem(), false );
243
244 const QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
245 success = !items.empty();
246 mTreeInitialExpand.clear();
247 for ( QTreeWidgetItem *item : items )
248 {
249 setChildrenVisible( item, true );
250
251 QTreeWidgetItem *parent = item;
252 while ( parent )
253 {
254 if ( mTreeInitialExpand.contains( parent ) )
255 break;
256 mTreeInitialExpand.insert( parent, parent->isExpanded() );
257 parent->setExpanded( true );
258 parent->setHidden( false );
259 parent = parent->parent();
260 }
261 }
262 }
263
264 return success;
265}
266
268{
269 if ( !mTreeView )
270 return;
271
272 QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
273 if ( treeWidget )
274 {
275 // show everything
276 std::function<void( QTreeWidgetItem * )> showChildren;
277 showChildren = [this, &showChildren]( QTreeWidgetItem *item ) {
278 for ( int i = 0; i < item->childCount(); ++i )
279 showChildren( item->child( i ) );
280 item->setHidden( !mTreeInitialVisible.value( item, true ) );
281 };
282 showChildren( treeWidget->invisibleRootItem() );
283 for ( auto it = mTreeInitialExpand.constBegin(); it != mTreeInitialExpand.constEnd(); it++ )
284 {
285 QTreeWidgetItem *item = it.key();
286 if ( item )
287 {
288 item->setExpanded( it.value() );
289 }
290 }
291 mTreeInitialExpand.clear();
292 }
293}
294
295
296// ****************
297// QTableView
300 , mTableView( tableView )
301{}
302
304{
305 if ( !mTableView || !mTableView->model() )
306 return false;
307
308 // search headers too!
309 for ( int col = 0; col < mTableView->model()->columnCount(); ++col )
310 {
311 const QString headerText = mTableView->model()->headerData( col, Qt::Horizontal ).toString();
312 if ( headerText.contains( text, Qt::CaseInsensitive ) )
313 return true;
314 }
315
316 const QModelIndexList hits = mTableView->model()->match( mTableView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
317 return !hits.isEmpty();
318}
319
321{
322 return false;
323}
324
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