QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 ).arg( HIGHLIGHT_BACKGROUND_GREEN ).arg( HIGHLIGHT_BACKGROUND_BLUE ).arg( HIGHLIGHT_TEXT_RED ).arg( HIGHLIGHT_TEXT_GREEN ).arg( HIGHLIGHT_TEXT_BLUE ) )
47{}
48
50{
51 if ( !mLabel )
52 return false;
53
54 const QString labelText = QTextDocumentFragment::fromHtml( mLabel->text() ).toPlainText();
55 return labelText.contains( text, Qt::CaseInsensitive );
56}
57
59{
60 if ( !mWidget )
61 return false;
62 Q_UNUSED( text )
63 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
64 return true;
65}
66
68{
69 if ( !mWidget )
70 return;
71 QString ss = mWidget->styleSheet();
72 ss.remove( mStyleSheet );
73 mWidget->setStyleSheet( ss );
74}
75
76// ****************
77// QCheckBox
80 , mCheckBox( checkBox )
81 , mStyleSheet( QStringLiteral( "/*!search!*/QCheckBox { 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 ) )
82{
83}
84
86{
87 if ( !mCheckBox )
88 return false;
89
90 return mCheckBox->text().contains( text, Qt::CaseInsensitive );
91}
92
94{
95 if ( !mWidget )
96 return false;
97 Q_UNUSED( text )
98 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
99 return true;
100}
101
103{
104 if ( !mWidget )
105 return;
106 QString ss = mWidget->styleSheet();
107 ss.remove( mStyleSheet );
108 mWidget->setStyleSheet( ss );
109}
110
111// ****************
112// QAbstractButton
115 , mButton( button )
116 , mStyleSheet( QStringLiteral( "/*!search!*/QAbstractButton { 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 ) )
117{
118}
119
121{
122 if ( !mButton )
123 return false;
124
125 return mButton->text().contains( text, Qt::CaseInsensitive );
126}
127
129{
130 if ( !mWidget )
131 return false;
132 Q_UNUSED( text )
133 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
134 return true;
135}
136
138{
139 if ( !mWidget )
140 return;
141 QString ss = mWidget->styleSheet();
142 ss.remove( mStyleSheet );
143 mWidget->setStyleSheet( ss );
144}
145
146// ****************
147// QGroupBox
150 , mGroupBox( groupBox )
151 , mStyleSheet( QStringLiteral( "/*!search!*/QGroupBox::title { 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 ) )
152{
153}
154
156{
157 if ( !mGroupBox )
158 return false;
159
160 return mGroupBox->title().contains( text, Qt::CaseInsensitive );
161}
162
164{
165 Q_UNUSED( text )
166 if ( !mWidget )
167 return false;
168
169 mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
170 return true;
171}
172
174{
175 if ( !mWidget )
176 return;
177 QString ss = mWidget->styleSheet();
178 ss.remove( mStyleSheet );
179 mWidget->setStyleSheet( ss );
180}
181
182// ****************
183// QTreeView
186 , mTreeView( treeView )
187{
188}
189
191{
192 if ( !mTreeView || !mTreeView->model() )
193 return false;
194
195 // search headers too!
196 for ( int col = 0; col < mTreeView->model()->columnCount(); ++col )
197 {
198 const QString headerText = mTreeView->model()->headerData( col, Qt::Horizontal ).toString();
199 if ( headerText.contains( text, Qt::CaseInsensitive ) )
200 return true;
201 }
202
203 const QModelIndexList hits = mTreeView->model()->match( mTreeView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
204 return !hits.isEmpty();
205}
206
208{
209 bool success = false;
210 QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
211 if ( treeWidget )
212 {
213 mTreeInitialVisible.clear();
214 // initially hide everything
215 std::function<void( QTreeWidgetItem *, bool )> setChildrenVisible;
216 setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem *item, bool visible ) {
217 for ( int i = 0; i < item->childCount(); ++i )
218 setChildrenVisible( item->child( i ), visible );
219 mTreeInitialVisible.insert( item, !item->isHidden() );
220 item->setHidden( !visible );
221 };
222 setChildrenVisible( treeWidget->invisibleRootItem(), false );
223
224 const QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
225 success = !items.empty();
226 mTreeInitialExpand.clear();
227 for ( QTreeWidgetItem *item : items )
228 {
229 setChildrenVisible( item, true );
230
231 QTreeWidgetItem *parent = item;
232 while ( parent )
233 {
234 if ( mTreeInitialExpand.contains( parent ) )
235 break;
236 mTreeInitialExpand.insert( parent, parent->isExpanded() );
237 parent->setExpanded( true );
238 parent->setHidden( false );
239 parent = parent->parent();
240 }
241 }
242 }
243
244 return success;
245}
246
248{
249 if ( !mTreeView )
250 return;
251
252 QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
253 if ( treeWidget )
254 {
255 // show everything
256 std::function<void( QTreeWidgetItem * )> showChildren;
257 showChildren = [this, &showChildren]( QTreeWidgetItem *item ) {
258 for ( int i = 0; i < item->childCount(); ++i )
259 showChildren( item->child( i ) );
260 item->setHidden( !mTreeInitialVisible.value( item, true ) );
261 };
262 showChildren( treeWidget->invisibleRootItem() );
263 for ( auto it = mTreeInitialExpand.constBegin(); it != mTreeInitialExpand.constEnd(); it++ )
264 {
265 QTreeWidgetItem *item = it.key();
266 if ( item )
267 {
268 item->setExpanded( it.value() );
269 }
270 }
271 mTreeInitialExpand.clear();
272 }
273}
274
275
276// ****************
277// QTableView
280 , mTableView( tableView )
281{
282}
283
285{
286 if ( !mTableView || !mTableView->model() )
287 return false;
288
289 // search headers too!
290 for ( int col = 0; col < mTableView->model()->columnCount(); ++col )
291 {
292 const QString headerText = mTableView->model()->headerData( col, Qt::Horizontal ).toString();
293 if ( headerText.contains( text, Qt::CaseInsensitive ) )
294 return true;
295 }
296
297 const QModelIndexList hits = mTableView->model()->match( mTableView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
298 return !hits.isEmpty();
299}
300
302{
303 return false;
304}
305
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