QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
27 #include "qgsmessagebaritem.h"
28 #include "qgslogger.h"
29 
31 
32 #include <functional>
33 
34 const int HIGHLIGHT_BACKGROUND_RED = 255;
36 const int HIGHLIGHT_BACKGROUND_BLUE = 190;
37 const int HIGHLIGHT_TEXT_RED = 0;
38 const int HIGHLIGHT_TEXT_GREEN = 0;
39 const 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 )
50  .arg( HIGHLIGHT_TEXT_GREEN )
51  .arg( HIGHLIGHT_TEXT_BLUE ) )
52 {}
53 
54 bool QgsOptionsDialogHighlightLabel::searchText( const QString &text )
55 {
56  if ( !mLabel )
57  return false;
58 
59  return mLabel->text().contains( text, Qt::CaseInsensitive );
60 }
61 
63 {
64  if ( !mWidget )
65  return false;
66  Q_UNUSED( text )
67  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
68  return true;
69 }
70 
72 {
73  if ( !mWidget )
74  return;
75  QString ss = mWidget->styleSheet();
76  ss.remove( mStyleSheet );
77  mWidget->setStyleSheet( ss );
78 }
79 
80 // ****************
81 // QCheckBox
84  , mCheckBox( checkBox )
85  , mStyleSheet( QStringLiteral( "/*!search!*/QCheckBox { background-color: rgb(%1, %2, %3); color: rgb( %4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
88  .arg( HIGHLIGHT_TEXT_RED )
89  .arg( HIGHLIGHT_TEXT_GREEN )
90  .arg( HIGHLIGHT_TEXT_BLUE ) )
91 {
92 }
93 
95 {
96  if ( !mCheckBox )
97  return false;
98 
99  return mCheckBox->text().contains( text, Qt::CaseInsensitive );
100 
101 }
102 
104 {
105  if ( !mWidget )
106  return false;
107  Q_UNUSED( text )
108  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
109  return true;
110 }
111 
113 {
114  if ( !mWidget )
115  return;
116  QString ss = mWidget->styleSheet();
117  ss.remove( mStyleSheet );
118  mWidget->setStyleSheet( ss );
119 }
120 
121 // ****************
122 // QAbstractButton
125  , mButton( button )
126  , mStyleSheet( QStringLiteral( "/*!search!*/QAbstractButton { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
129  .arg( HIGHLIGHT_TEXT_RED )
130  .arg( HIGHLIGHT_TEXT_GREEN )
131  .arg( HIGHLIGHT_TEXT_BLUE ) )
132 {
133 }
134 
136 {
137  if ( !mButton )
138  return false;
139 
140  return mButton->text().contains( text, Qt::CaseInsensitive );
141 
142 }
143 
145 {
146  if ( !mWidget )
147  return false;
148  Q_UNUSED( text )
149  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
150  return true;
151 }
152 
154 {
155  if ( !mWidget )
156  return;
157  QString ss = mWidget->styleSheet();
158  ss.remove( mStyleSheet );
159  mWidget->setStyleSheet( ss );
160 }
161 
162 // ****************
163 // QGroupBox
165  : QgsOptionsDialogHighlightWidget( groupBox )
166  , mGroupBox( groupBox )
167  , mStyleSheet( QStringLiteral( "/*!search!*/QGroupBox::title { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
170  .arg( HIGHLIGHT_TEXT_RED )
171  .arg( HIGHLIGHT_TEXT_GREEN )
172  .arg( HIGHLIGHT_TEXT_BLUE ) )
173 {
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
206  : QgsOptionsDialogHighlightWidget( treeView )
207  , mTreeView( treeView )
208 {
209 }
210 
211 bool QgsOptionsDialogHighlightTree::searchText( const QString &text )
212 {
213  if ( !mTreeView || !mTreeView->model() )
214  return false;
215  QModelIndexList hits = mTreeView->model()->match( mTreeView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
216  return !hits.isEmpty();
217 }
218 
220 {
221  bool success = false;
222  QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
223  if ( treeWidget )
224  {
225  mTreeInitialVisible.clear();
226  // initially hide everything
227  std::function< void( QTreeWidgetItem *, bool ) > setChildrenVisible;
228  setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem * item, bool visible )
229  {
230  for ( int i = 0; i < item->childCount(); ++i )
231  setChildrenVisible( item->child( i ), visible );
232  mTreeInitialVisible.insert( item, !item->isHidden() );
233  item->setHidden( !visible );
234  };
235  setChildrenVisible( treeWidget->invisibleRootItem(), false );
236 
237  QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
238  success = !items.empty();
239  mTreeInitialExpand.clear();
240  for ( QTreeWidgetItem *item : items )
241  {
242  setChildrenVisible( item, true );
243 
244  QTreeWidgetItem *parent = item;
245  while ( parent )
246  {
247  if ( mTreeInitialExpand.contains( parent ) )
248  break;
249  mTreeInitialExpand.insert( parent, parent->isExpanded() );
250  parent->setExpanded( true );
251  parent->setHidden( false );
252  parent = parent->parent();
253  }
254  }
255  }
256 
257  return success;
258 }
259 
261 {
262  if ( !mTreeView )
263  return;
264 
265  QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
266  if ( treeWidget )
267  {
268  // show everything
269  std::function< void( QTreeWidgetItem * ) > showChildren;
270  showChildren = [this, &showChildren]( QTreeWidgetItem * item )
271  {
272  for ( int i = 0; i < item->childCount(); ++i )
273  showChildren( item->child( i ) );
274  item->setHidden( !mTreeInitialVisible.value( item, true ) );
275  };
276  showChildren( treeWidget->invisibleRootItem() );
277  for ( QTreeWidgetItem *item : mTreeInitialExpand.keys() )
278  {
279  if ( item )
280  {
281  item->setExpanded( mTreeInitialExpand.value( item ) );
282  }
283  }
284  mTreeInitialExpand.clear();
285  }
286 }
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
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