QGIS API Documentation  3.0.2-Girona (307d082)
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 
30 
31 #include <functional>
32 
33 const int HIGHLIGHT_BACKGROUND_RED = 255;
35 const int HIGHLIGHT_BACKGROUND_BLUE = 190;
36 const int HIGHLIGHT_TEXT_RED = 0;
37 const int HIGHLIGHT_TEXT_GREEN = 0;
38 const int HIGHLIGHT_TEXT_BLUE = 0;
39 
40 // ****************
41 // QLabel
44  , mLabel( label )
45  , mStyleSheet( QStringLiteral( "QLabel { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6 );}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
48  .arg( HIGHLIGHT_TEXT_RED )
49  .arg( HIGHLIGHT_TEXT_GREEN )
50  .arg( HIGHLIGHT_TEXT_BLUE ) )
51 {}
52 
53 bool QgsOptionsDialogHighlightLabel::searchText( const QString &text )
54 {
55  if ( !mLabel )
56  return false;
57 
58  return mLabel->text().contains( text, Qt::CaseInsensitive );
59 }
60 
62 {
63  if ( !mWidget )
64  return false;
65  Q_UNUSED( text );
66  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
67  return true;
68 }
69 
71 {
72  if ( !mWidget )
73  return;
74  QString ss = mWidget->styleSheet();
75  ss.remove( mStyleSheet );
76  mWidget->setStyleSheet( ss );
77 }
78 
79 // ****************
80 // QCheckBox
83  , mCheckBox( checkBox )
84  , mStyleSheet( QStringLiteral( "/*!search!*/QCheckBox { background-color: rgb(%1, %2, %3); color: rgb( %4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
87  .arg( HIGHLIGHT_TEXT_RED )
88  .arg( HIGHLIGHT_TEXT_GREEN )
89  .arg( HIGHLIGHT_TEXT_BLUE ) )
90 {
91 }
92 
94 {
95  if ( !mCheckBox )
96  return false;
97 
98  return mCheckBox->text().contains( text, Qt::CaseInsensitive );
99 
100 }
101 
103 {
104  if ( !mWidget )
105  return false;
106  Q_UNUSED( text );
107  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
108  return true;
109 }
110 
112 {
113  if ( !mWidget )
114  return;
115  QString ss = mWidget->styleSheet();
116  ss.remove( mStyleSheet );
117  mWidget->setStyleSheet( ss );
118 }
119 
120 // ****************
121 // QAbstractButton
124  , mButton( button )
125  , mStyleSheet( QStringLiteral( "/*!search!*/QAbstractButton { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
128  .arg( HIGHLIGHT_TEXT_RED )
129  .arg( HIGHLIGHT_TEXT_GREEN )
130  .arg( HIGHLIGHT_TEXT_BLUE ) )
131 {
132 }
133 
135 {
136  if ( !mButton )
137  return false;
138 
139  return mButton->text().contains( text, Qt::CaseInsensitive );
140 
141 }
142 
144 {
145  if ( !mWidget )
146  return false;
147  Q_UNUSED( text );
148  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
149  return true;
150 }
151 
153 {
154  if ( !mWidget )
155  return;
156  QString ss = mWidget->styleSheet();
157  ss.remove( mStyleSheet );
158  mWidget->setStyleSheet( ss );
159 }
160 
161 // ****************
162 // QGroupBox
164  : QgsOptionsDialogHighlightWidget( groupBox )
165  , mGroupBox( groupBox )
166  , mStyleSheet( QStringLiteral( "/*!search!*/QGroupBox::title { background-color: rgb(%1, %2, %3); color: rgb(%4, %5, %6);}/*!search!*/" ).arg( HIGHLIGHT_BACKGROUND_RED )
169  .arg( HIGHLIGHT_TEXT_RED )
170  .arg( HIGHLIGHT_TEXT_GREEN )
171  .arg( HIGHLIGHT_TEXT_BLUE ) )
172 {
173 }
174 
176 {
177  if ( !mGroupBox )
178  return false;
179 
180  return mGroupBox->title().contains( text, Qt::CaseInsensitive );
181 }
182 
184 {
185  Q_UNUSED( text );
186  if ( !mWidget )
187  return false;
188 
189  mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
190  return true;
191 }
192 
194 {
195  if ( !mWidget )
196  return;
197  QString ss = mWidget->styleSheet();
198  ss.remove( mStyleSheet );
199  mWidget->setStyleSheet( ss );
200 }
201 
202 // ****************
203 // QTreeView
205  : QgsOptionsDialogHighlightWidget( treeView )
206  , mTreeView( treeView )
207 {
208 }
209 
210 bool QgsOptionsDialogHighlightTree::searchText( const QString &text )
211 {
212  if ( !mTreeView )
213  return false;
214  QModelIndexList hits = mTreeView->model()->match( mTreeView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
215  return !hits.isEmpty();
216 }
217 
219 {
220  bool success = false;
221  QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
222  if ( treeWidget )
223  {
224  mTreeInitialVisible.clear();
225  // initially hide everything
226  std::function< void( QTreeWidgetItem *, bool ) > setChildrenVisible;
227  setChildrenVisible = [this, &setChildrenVisible]( QTreeWidgetItem * item, bool visible )
228  {
229  for ( int i = 0; i < item->childCount(); ++i )
230  setChildrenVisible( item->child( i ), visible );
231  mTreeInitialVisible.insert( item, !item->isHidden() );
232  item->setHidden( !visible );
233  };
234  setChildrenVisible( treeWidget->invisibleRootItem(), false );
235 
236  QList<QTreeWidgetItem *> items = treeWidget->findItems( text, Qt::MatchContains | Qt::MatchRecursive, 0 );
237  success = items.count() ? true : false;
238  mTreeInitialStyle.clear();
239  mTreeInitialExpand.clear();
240  for ( QTreeWidgetItem *item : items )
241  {
242  mTreeInitialStyle.insert( item, qMakePair( item->background( 0 ), item->foreground( 0 ) ) );
243  item->setBackground( 0, QBrush( QColor( HIGHLIGHT_BACKGROUND_RED, HIGHLIGHT_BACKGROUND_GREEN, HIGHLIGHT_BACKGROUND_BLUE ) ) );
244  item->setForeground( 0, QBrush( QColor( HIGHLIGHT_TEXT_RED, HIGHLIGHT_TEXT_GREEN, HIGHLIGHT_TEXT_BLUE ) ) );
245  setChildrenVisible( item, true );
246 
247  QTreeWidgetItem *parent = item;
248  while ( parent )
249  {
250  if ( mTreeInitialExpand.contains( parent ) )
251  break;
252  mTreeInitialExpand.insert( parent, parent->isExpanded() );
253  parent->setExpanded( true );
254  parent->setHidden( false );
255  parent = parent->parent();
256  }
257  }
258  }
259 
260  return success;
261 }
262 
264 {
265  if ( !mTreeView )
266  return;
267 
268  QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( mTreeView );
269  if ( treeWidget )
270  {
271  // show everything
272  std::function< void( QTreeWidgetItem * ) > showChildren;
273  showChildren = [this, &showChildren]( QTreeWidgetItem * item )
274  {
275  for ( int i = 0; i < item->childCount(); ++i )
276  showChildren( item->child( i ) );
277  item->setHidden( !mTreeInitialVisible.value( item, true ) );
278  };
279  showChildren( treeWidget->invisibleRootItem() );
280  for ( QTreeWidgetItem *item : mTreeInitialExpand.keys() )
281  {
282  if ( item )
283  {
284  item->setExpanded( mTreeInitialExpand.value( item ) );
285  }
286  }
287  for ( QTreeWidgetItem *item : mTreeInitialStyle.keys() )
288  {
289  if ( item )
290  {
291  item->setBackground( 0, mTreeInitialStyle.value( item ).first );
292  item->setForeground( 0, mTreeInitialStyle.value( item ).second );
293  }
294  }
295  mTreeInitialStyle.clear();
296  mTreeInitialExpand.clear();
297  }
298 }
virtual bool highlightText(const QString &text) override
Highlight the text in the widget.
QgsOptionsDialogHighlightTree(QTreeView *treeView)
constructs a highlight widget for a tree view or widget.
virtual bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
QgsOptionsDialogHighlightButton(QAbstractButton *button)
constructs a highlight widget for a button.
virtual void reset() override
reset the style of the widgets to its original state
QMap< QTreeWidgetItem *, QPair< QBrush, QBrush > > mTreeInitialStyle
virtual bool highlightText(const QString &text) override
Highlight the text in the widget.
QgsOptionsDialogHighlightCheckBox(QCheckBox *checkBox)
constructs a highlight widget for a checkbox
virtual void reset() override
reset the style of the widgets to its original state
QPointer< QWidget > mWidget
Pointer to the widget.
QMap< QTreeWidgetItem *, bool > mTreeInitialExpand
QMap< QTreeWidgetItem *, bool > mTreeInitialVisible
const int HIGHLIGHT_TEXT_GREEN
QgsOptionsDialogHighlightLabel(QLabel *label)
constructs a highlight widget for a label
virtual bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
virtual 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.
virtual bool highlightText(const QString &text) override
Highlight the text in the widget.
Container for a widget to be used to search text in the option dialog If the widget type is handled...
const int HIGHLIGHT_BACKGROUND_GREEN
virtual bool highlightText(const QString &text) override
Highlight the text in the widget.
virtual bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
virtual void reset() override
reset the style of the widgets to its original state
virtual bool highlightText(const QString &text) override
Highlight the text in the widget.
const int HIGHLIGHT_BACKGROUND_BLUE
const int HIGHLIGHT_BACKGROUND_RED
virtual bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
virtual void reset() override
reset the style of the widgets to its original state
virtual void reset() override
reset the style of the widgets to its original state