QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
HIGHLIGHT_TEXT_GREEN
const int HIGHLIGHT_TEXT_GREEN
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:40
QgsOptionsDialogHighlightTable::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:326
QgsOptionsDialogHighlightCheckBox::QgsOptionsDialogHighlightCheckBox
QgsOptionsDialogHighlightCheckBox(QCheckBox *checkBox)
constructs a highlight widget for a checkbox
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:85
QgsOptionsDialogHighlightGroupBox::mGroupBox
QPointer< QGroupBox > mGroupBox
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:117
qgsmessagebaritem.h
QgsOptionsDialogHighlightLabel::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:65
QgsOptionsDialogHighlightTree::QgsOptionsDialogHighlightTree
QgsOptionsDialogHighlightTree(QTreeView *treeView)
constructs a highlight widget for a tree view or widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:208
QgsOptionsDialogHighlightTree::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:272
QgsOptionsDialogHighlightTable::QgsOptionsDialogHighlightTable
QgsOptionsDialogHighlightTable(QTableView *tableView)
constructs a highlight widget for a table view or widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:303
QgsOptionsDialogHighlightWidget
Container for a widget to be used to search text in the option dialog If the widget type is handled,...
Definition: qgsoptionsdialoghighlightwidget.h:35
QgsOptionsDialogHighlightCheckBox::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:106
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:56
HIGHLIGHT_BACKGROUND_BLUE
const int HIGHLIGHT_BACKGROUND_BLUE
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:38
QgsOptionsDialogHighlightCheckBox::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:115
QgsOptionsDialogHighlightLabel::mLabel
QPointer< QLabel > mLabel
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:54
QgsOptionsDialogHighlightButton::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:147
QgsOptionsDialogHighlightCheckBox::mCheckBox
QPointer< QCheckBox > mCheckBox
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:75
HIGHLIGHT_BACKGROUND_GREEN
const int HIGHLIGHT_BACKGROUND_GREEN
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:37
QgsOptionsDialogHighlightLabel::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:74
QgsOptionsDialogHighlightButton::mButton
QPointer< QAbstractButton > mButton
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:96
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:97
HIGHLIGHT_TEXT_RED
const int HIGHLIGHT_TEXT_RED
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:39
qgsoptionsdialoghighlightwidgetsimpl.h
HIGHLIGHT_TEXT_BLUE
const int HIGHLIGHT_TEXT_BLUE
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:41
QgsOptionsDialogHighlightGroupBox::mStyleSheet
QString mStyleSheet
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:118
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:179
HIGHLIGHT_BACKGROUND_RED
const int HIGHLIGHT_BACKGROUND_RED
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:36
QgsOptionsDialogHighlightLabel::mStyleSheet
QString mStyleSheet
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:55
QgsOptionsDialogHighlightGroupBox::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:197
QgsOptionsDialogHighlightGroupBox::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:187
QgsOptionsDialogHighlightButton::mStyleSheet
QString mStyleSheet
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:97
QgsOptionsDialogHighlightTree::mTreeInitialExpand
QMap< QTreeWidgetItem *, bool > mTreeInitialExpand
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:142
QgsOptionsDialogHighlightCheckBox::mStyleSheet
QString mStyleSheet
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:76
QgsOptionsDialogHighlightTree::mTreeInitialVisible
QMap< QTreeWidgetItem *, bool > mTreeInitialVisible
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:143
QgsOptionsDialogHighlightTable::searchText
bool searchText(const QString &text) override
Search for the text in the widget and return true if it was found.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:309
qgsoptionsdialoghighlightwidget.h
QgsOptionsDialogHighlightButton::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:156
QgsOptionsDialogHighlightTable::reset
void reset() override
reset the style of the widgets to its original state
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:331
QgsOptionsDialogHighlightGroupBox::QgsOptionsDialogHighlightGroupBox
QgsOptionsDialogHighlightGroupBox(QGroupBox *groupBox)
constructs a highlight widget for a group box.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:167
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:214
QgsOptionsDialogHighlightTable::mTableView
QPointer< QTableView > mTableView
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:163
QgsOptionsDialogHighlightTree::highlightText
bool highlightText(const QString &text) override
Highlight the text in the widget.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:231
QgsOptionsDialogHighlightLabel::QgsOptionsDialogHighlightLabel
QgsOptionsDialogHighlightLabel(QLabel *label)
constructs a highlight widget for a label
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:45
QgsOptionsDialogHighlightButton::QgsOptionsDialogHighlightButton
QgsOptionsDialogHighlightButton(QAbstractButton *button)
constructs a highlight widget for a button.
Definition: qgsoptionsdialoghighlightwidgetsimpl.cpp:126
qgslogger.h
QgsOptionsDialogHighlightTree::mTreeView
QPointer< QTreeView > mTreeView
Definition: qgsoptionsdialoghighlightwidgetsimpl.h:140
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:138
QgsOptionsDialogHighlightWidget::mWidget
QPointer< QWidget > mWidget
Pointer to the widget.
Definition: qgsoptionsdialoghighlightwidget.h:94