QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
HIGHLIGHT_TEXT_GREEN
const int HIGHLIGHT_TEXT_GREEN
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:38
QgsOptionsDialogHighlightCheckBox::QgsOptionsDialogHighlightCheckBox
QgsOptionsDialogHighlightCheckBox(QCheckBox *checkBox)
constructs a highlight widget for a checkbox
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:82
QgsOptionsDialogHighlightGroupBox::mGroupBox
QPointer< QGroupBox > mGroupBox
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:116
qgsmessagebaritem.h
QgsOptionsDialogHighlightLabel::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:62
QgsOptionsDialogHighlightTree::QgsOptionsDialogHighlightTree
QgsOptionsDialogHighlightTree(QTreeView *treeView)
constructs a highlight widget for a tree view or widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:205
QgsOptionsDialogHighlightTree::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:260
QgsOptionsDialogHighlightWidget
Definition: qgsoptionsdialoghighlightwidget.h:35
QgsOptionsDialogHighlightCheckBox::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:103
QgsOptionsDialogHighlightLabel::searchText
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:54
HIGHLIGHT_BACKGROUND_BLUE
const int HIGHLIGHT_BACKGROUND_BLUE
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:36
QgsOptionsDialogHighlightCheckBox::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:112
QgsOptionsDialogHighlightLabel::mLabel
QPointer< QLabel > mLabel
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:53
QgsOptionsDialogHighlightButton::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:144
QgsOptionsDialogHighlightCheckBox::mCheckBox
QPointer< QCheckBox > mCheckBox
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:74
HIGHLIGHT_BACKGROUND_GREEN
const int HIGHLIGHT_BACKGROUND_GREEN
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:35
QgsOptionsDialogHighlightLabel::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:71
QgsOptionsDialogHighlightButton::mButton
QPointer< QAbstractButton > mButton
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:95
QgsOptionsDialogHighlightCheckBox::searchText
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:94
HIGHLIGHT_TEXT_RED
const int HIGHLIGHT_TEXT_RED
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:37
qgsoptionsdialoghighlightwidgetsimpl.h
HIGHLIGHT_TEXT_BLUE
const int HIGHLIGHT_TEXT_BLUE
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:39
QgsOptionsDialogHighlightGroupBox::mStyleSheet
QString mStyleSheet
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:117
QgsOptionsDialogHighlightGroupBox::searchText
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:176
HIGHLIGHT_BACKGROUND_RED
const int HIGHLIGHT_BACKGROUND_RED
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:34
QgsOptionsDialogHighlightLabel::mStyleSheet
QString mStyleSheet
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:54
QgsOptionsDialogHighlightGroupBox::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:194
QgsOptionsDialogHighlightGroupBox::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:184
QgsOptionsDialogHighlightButton::mStyleSheet
QString mStyleSheet
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:96
QgsOptionsDialogHighlightTree::mTreeInitialExpand
QMap< QTreeWidgetItem *, bool > mTreeInitialExpand
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:141
QgsOptionsDialogHighlightCheckBox::mStyleSheet
QString mStyleSheet
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:75
QgsOptionsDialogHighlightTree::mTreeInitialVisible
QMap< QTreeWidgetItem *, bool > mTreeInitialVisible
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:142
qgsoptionsdialoghighlightwidget.h
QgsOptionsDialogHighlightButton::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:153
QgsOptionsDialogHighlightGroupBox::QgsOptionsDialogHighlightGroupBox
QgsOptionsDialogHighlightGroupBox(QGroupBox *groupBox)
constructs a highlight widget for a group box.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:164
QgsOptionsDialogHighlightTree::searchText
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:211
QgsOptionsDialogHighlightTree::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:219
QgsOptionsDialogHighlightLabel::QgsOptionsDialogHighlightLabel
QgsOptionsDialogHighlightLabel(QLabel *label)
constructs a highlight widget for a label
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:43
QgsOptionsDialogHighlightButton::QgsOptionsDialogHighlightButton
QgsOptionsDialogHighlightButton(QAbstractButton *button)
constructs a highlight widget for a button.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:123
qgslogger.h
QgsOptionsDialogHighlightTree::mTreeView
QPointer< QTreeView > mTreeView
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:139
QgsOptionsDialogHighlightButton::searchText
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:135
QgsOptionsDialogHighlightWidget::mWidget
QPointer< QWidget > mWidget
Pointer to the widget.
Definition: qgsoptionsdialoghighlightwidget.h:94