QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsoptionsdialoghighlightwidgetsimpl.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsoptionsdialoghighlightwidgetsimpl.cpp
3  -------------------------------
4  Date : February 2018
5  Copyright : (C) 2018 Denis Rouzaud
6  Email : [email protected]
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 #include "qgsmessagebaritem.h"
30 #include "qgslogger.h"
31 
33 
34 #include <functional>
35 
36 const int HIGHLIGHT_BACKGROUND_RED = 255;
38 const int HIGHLIGHT_BACKGROUND_BLUE = 190;
39 const int HIGHLIGHT_TEXT_RED = 0;
40 const int HIGHLIGHT_TEXT_GREEN = 0;
41 const int HIGHLIGHT_TEXT_BLUE = 0;
42 
43 // ****************
44 // QLabel
47  , mLabel( label )
48  , mStyleSheet( QStringLiteral( "QLabel { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6 );}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
51  .arg( HIGHLIGHT_TEXT_RED )
52  .arg( HIGHLIGHT_TEXT_GREEN )
53  .arg( HIGHLIGHT_TEXT_BLUE ) )
54 {}
55 
56 bool QgsOptionsDialogHighlightLabel::searchText( const QString &text )
57 {
58  if ( !mLabel )
59  return false;
60 
61  const QString labelText = QTextDocumentFragment::fromHtml( mLabel->text() ).toPlainText();
62  return labelText.contains( text, Qt::CaseInsensitive );
63 }
64 
66 {
67  if ( !mWidget )
68  return false;
69  Q_UNUSED( text )
70  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
71  return true;
72 }
73 
75 {
76  if ( !mWidget )
77  return;
78  QString ss = mWidget->styleSheet();
79  ss.remove( mStyleSheet );
80  mWidget->setStyleSheet( ss );
81 }
82 
83 // ****************
84 // QCheckBox
87  , mCheckBox( checkBox )
88  , mStyleSheet( QStringLiteral( "/*!search!*/QCheckBox { background-color: rgb(%1, %2, %3); color: rgb( %4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
91  .arg( HIGHLIGHT_TEXT_RED )
92  .arg( HIGHLIGHT_TEXT_GREEN )
93  .arg( HIGHLIGHT_TEXT_BLUE ) )
94 {
95 }
96 
98 {
99  if ( !mCheckBox )
100  return false;
101 
102  return mCheckBox->text().contains( text, Qt::CaseInsensitive );
103 
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( QStringLiteral( "/*!search!*/QAbstractButton { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
132  .arg( HIGHLIGHT_TEXT_RED )
133  .arg( HIGHLIGHT_TEXT_GREEN )
134  .arg( HIGHLIGHT_TEXT_BLUE ) )
135 {
136 }
137 
139 {
140  if ( !mButton )
141  return false;
142 
143  return mButton->text().contains( text, Qt::CaseInsensitive );
144 
145 }
146 
148 {
149  if ( !mWidget )
150  return false;
151  Q_UNUSED( text )
152  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
153  return true;
154 }
155 
157 {
158  if ( !mWidget )
159  return;
160  QString ss = mWidget->styleSheet();
161  ss.remove( mStyleSheet );
162  mWidget->setStyleSheet( ss );
163 }
164 
165 // ****************
166 // QGroupBox
168  : QgsOptionsDialogHighlightWidget( groupBox )
169  , mGroupBox( groupBox )
170  , mStyleSheet( QStringLiteral( "/*!search!*/QGroupBox::title { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
173  .arg( HIGHLIGHT_TEXT_RED )
174  .arg( HIGHLIGHT_TEXT_GREEN )
175  .arg( HIGHLIGHT_TEXT_BLUE ) )
176 {
177 }
178 
180 {
181  if ( !mGroupBox )
182  return false;
183 
184  return mGroupBox->title().contains( text, Qt::CaseInsensitive );
185 }
186 
188 {
189  Q_UNUSED( text )
190  if ( !mWidget )
191  return false;
192 
193  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
194  return true;
195 }
196 
198 {
199  if ( !mWidget )
200  return;
201  QString ss = mWidget->styleSheet();
202  ss.remove( mStyleSheet );
203  mWidget->setStyleSheet( ss );
204 }
205 
206 // ****************
207 // QTreeView
209  : QgsOptionsDialogHighlightWidget( treeView )
210  , mTreeView( treeView )
211 {
212 }
213 
214 bool QgsOptionsDialogHighlightTree::searchText( const QString &text )
215 {
216  if ( !mTreeView || !mTreeView->model() )
217  return false;
218 
219  // search headers too!
220  for ( int col = 0; col < mTreeView->model()->columnCount(); ++col )
221  {
222  const QString headerText = mTreeView->model()->headerData( col, Qt::Horizontal ).toString();
223  if ( headerText.contains( text, Qt::CaseInsensitive ) )
224  return true;
225  }
226 
227  const QModelIndexList hits = mTreeView->model()->match( mTreeView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
228  return !hits.isEmpty();
229 }
230 
232 {
233  bool success = false;
234  QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
235  if ( treeWidget )
236  {
237  mTreeInitialVisible.clear();
238  // initially hide everything
239  std::function< void( QTreeWidgetItem *, bool ) > setChildrenVisible;
240  setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem * item, bool visible )
241  {
242  for ( int i = 0; i < item->childCount(); ++i )
243  setChildrenVisible( item->child( i ), visible );
244  mTreeInitialVisible.insert( item, !item->isHidden() );
245  item->setHidden( !visible );
246  };
247  setChildrenVisible( treeWidget->invisibleRootItem(), false );
248 
249  const QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
250  success = !items.empty();
251  mTreeInitialExpand.clear();
252  for ( QTreeWidgetItem *item : items )
253  {
254  setChildrenVisible( item, true );
255 
256  QTreeWidgetItem *parent = item;
257  while ( parent )
258  {
259  if ( mTreeInitialExpand.contains( parent ) )
260  break;
261  mTreeInitialExpand.insert( parent, parent->isExpanded() );
262  parent->setExpanded( true );
263  parent->setHidden( false );
264  parent = parent->parent();
265  }
266  }
267  }
268 
269  return success;
270 }
271 
273 {
274  if ( !mTreeView )
275  return;
276 
277  QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
278  if ( treeWidget )
279  {
280  // show everything
281  std::function< void( QTreeWidgetItem * ) > showChildren;
282  showChildren = [this, &showChildren]( QTreeWidgetItem * item )
283  {
284  for ( int i = 0; i < item->childCount(); ++i )
285  showChildren( item->child( i ) );
286  item->setHidden( !mTreeInitialVisible.value( item, true ) );
287  };
288  showChildren( treeWidget->invisibleRootItem() );
289  for ( QTreeWidgetItem *item : mTreeInitialExpand.keys() )
290  {
291  if ( item )
292  {
293  item->setExpanded( mTreeInitialExpand.value( item ) );
294  }
295  }
296  mTreeInitialExpand.clear();
297  }
298 }
299 
300 
301 // ****************
302 // QTableView
304  : QgsOptionsDialogHighlightWidget( tableView )
305  , mTableView( tableView )
306 {
307 }
308 
310 {
311  if ( !mTableView || !mTableView->model() )
312  return false;
313 
314  // search headers too!
315  for ( int col = 0; col < mTableView->model()->columnCount(); ++col )
316  {
317  const QString headerText = mTableView->model()->headerData( col, Qt::Horizontal ).toString();
318  if ( headerText.contains( text, Qt::CaseInsensitive ) )
319  return true;
320  }
321 
322  const QModelIndexList hits = mTableView->model()->match( mTableView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
323  return !hits.isEmpty();
324 }
325 
327 {
328  return false;
329 }
330 
332 {
333 }
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_TEXT_GREEN
const int HIGHLIGHT_BACKGROUND_GREEN
const int HIGHLIGHT_BACKGROUND_RED
const int HIGHLIGHT_BACKGROUND_BLUE