QGIS API Documentation  3.2.0-Bonn (bc43194)
qgssearchwidgettoolbutton.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssearchwidgettoolbutton.cpp
3  -----------------------------
4  Date : May 2016
5  Copyright : (C) 2016 Nyall Dawson
6  Email : nyall dot dawson at gmail.com
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 
17 #include "qgsapplication.h"
18 #include <QMenu>
19 
21  : QToolButton( parent )
22  , mAvailableFilterFlags( QgsSearchWidgetWrapper::EqualTo | QgsSearchWidgetWrapper::NotEqualTo | QgsSearchWidgetWrapper::CaseInsensitive )
23  , mDefaultFilterFlags( QgsSearchWidgetWrapper::EqualTo )
24  , mFilterFlags( QgsSearchWidgetWrapper::EqualTo )
25 
26 {
27  setFocusPolicy( Qt::StrongFocus );
28  setPopupMode( QToolButton::InstantPopup );
29 
30  mMenu = new QMenu( this );
31  connect( mMenu, &QMenu::aboutToShow, this, &QgsSearchWidgetToolButton::aboutToShowMenu );
32  setMenu( mMenu );
33 
34  // sets initial appearance
35  updateState();
36 }
37 
38 void QgsSearchWidgetToolButton::setAvailableFlags( QgsSearchWidgetWrapper::FilterFlags flags )
39 {
40  mFilterFlags &= flags;
41  mAvailableFilterFlags = flags;
42  mDefaultFilterFlags = mDefaultFilterFlags & flags;
43  updateState();
44 }
45 
46 void QgsSearchWidgetToolButton::setDefaultFlags( QgsSearchWidgetWrapper::FilterFlags flags )
47 {
48  mDefaultFilterFlags = flags & mAvailableFilterFlags;
49 }
50 
51 void QgsSearchWidgetToolButton::setActiveFlags( QgsSearchWidgetWrapper::FilterFlags flags )
52 {
53  // sanitize list
54  QgsSearchWidgetWrapper::FilterFlags newFlags;
55 
56  // only accept a single exclusive flag
58  {
59  if ( !( mAvailableFilterFlags & flag ) )
60  {
61  //unsupported
62  continue;
63  }
64  if ( flags & flag )
65  {
66  newFlags |= flag;
67  break;
68  }
69  }
70 
72  {
73  if ( !( mAvailableFilterFlags & flag ) )
74  {
75  //unsupported
76  continue;
77  }
78 
79  if ( flags & flag )
80  newFlags |= flag;
81  }
82 
83  mFilterFlags = newFlags;
84 
85  updateState();
86 }
87 
89 {
90  if ( !( flag & mAvailableFilterFlags ) )
91  return;
92 
93  if ( QgsSearchWidgetWrapper::nonExclusiveFilterFlags().contains( flag ) )
94  {
95  if ( flag & mFilterFlags )
96  mFilterFlags &= ~flag;
97  else
98  mFilterFlags |= flag;
99  }
100  else
101  {
102  // clear other exclusive flags
104  {
105  mFilterFlags &= ~exclusiveFlag;
106  }
107  // and set new exclusive flag
108  mFilterFlags |= flag;
109  }
110 
111  updateState();
112 }
113 
115 {
117  {
118  if ( mFilterFlags & flag )
119  return true;
120  }
121  return false;
122 }
123 
124 void QgsSearchWidgetToolButton::aboutToShowMenu()
125 {
126  mMenu->clear();
127  bool fieldActive = false;
129  {
130  if ( !( mAvailableFilterFlags & flag ) )
131  {
132  //unsupported
133  continue;
134  }
135 
136  QAction *action = mMenu->addAction( QgsSearchWidgetWrapper::toString( flag ) );
137  connect( action, &QAction::triggered, this, &QgsSearchWidgetToolButton::actionSelected );
138  action->setData( flag );
139  action->setCheckable( true );
140  if ( mFilterFlags & flag )
141  {
142  fieldActive = true;
143  action->setChecked( true );
144  }
145  }
146 
147  QAction *clearAction = mMenu->addAction( tr( "Exclude Field" ) );
148  connect( clearAction, &QAction::triggered, this, &QgsSearchWidgetToolButton::setInactive );
149  clearAction->setCheckable( true );
150  clearAction->setChecked( !fieldActive );
151  if ( mMenu->actions().count() > 0 )
152  {
153  mMenu->insertAction( mMenu->actions().at( 0 ), clearAction );
154  mMenu->insertSeparator( mMenu->actions().at( 1 ) );
155  }
156  else
157  mMenu->addAction( clearAction );
158 
159  mMenu->addSeparator();
160 
162  {
163  if ( !( mAvailableFilterFlags & flag ) )
164  {
165  //unsupported
166  continue;
167  }
168 
169  QAction *action = mMenu->addAction( QgsSearchWidgetWrapper::toString( flag ) );
170  connect( action, &QAction::triggered, this, &QgsSearchWidgetToolButton::actionSelected );
171  action->setData( flag );
172  action->setCheckable( true );
173  if ( mFilterFlags & flag )
174  action->setChecked( true );
175  }
176 }
177 
178 void QgsSearchWidgetToolButton::actionSelected()
179 {
180  QgsSearchWidgetWrapper::FilterFlag flag = static_cast< QgsSearchWidgetWrapper::FilterFlag >( qobject_cast< QAction * >( sender() )->data().toInt() );
181  toggleFlag( flag );
182 }
183 
184 void QgsSearchWidgetToolButton::searchWidgetValueChanged()
185 {
186  setActive();
187 }
188 
190 {
191  if ( !isActive() )
192  return;
193 
194  QgsSearchWidgetWrapper::FilterFlags newFlags;
196  {
197  if ( !( mAvailableFilterFlags & flag ) || !( mFilterFlags & flag ) )
198  continue;
199  newFlags |= flag;
200  }
201  mFilterFlags = newFlags;
202  updateState();
203 }
204 
206 {
207  if ( isActive() )
208  return;
209 
211  {
212  if ( mDefaultFilterFlags & flag )
213  {
214  toggleFlag( flag );
215  return;
216  }
217  }
218 }
219 
220 void QgsSearchWidgetToolButton::updateState()
221 {
222  bool active = false;
223  QStringList toolTips;
225  {
226  if ( mFilterFlags & flag )
227  {
228  toolTips << QgsSearchWidgetWrapper::toString( flag );
229  active = true;
230  }
231  }
233  {
234  if ( mFilterFlags & flag )
235  {
236  toolTips << QgsSearchWidgetWrapper::toString( flag ).toLower();
237  }
238  }
239 
240  if ( active )
241  {
242  QString text = toolTips.join( QStringLiteral( ", " ) );
243  setText( text );
244  setToolTip( text );
245  }
246  else
247  {
248  setText( tr( "Exclude field" ) );
249  setToolTip( QString() );
250  }
251 
252  emit activeFlagsChanged( mFilterFlags );
253 }
Shows a search widget on a filter form.
void setActive()
Sets the search widget as active by selecting the first available search type.
void setDefaultFlags(QgsSearchWidgetWrapper::FilterFlags flags)
Sets the default filter flags to show in the widget.
void setActiveFlags(QgsSearchWidgetWrapper::FilterFlags flags)
Sets the current active filter flags for the widget.
static QList< QgsSearchWidgetWrapper::FilterFlag > exclusiveFilterFlags()
Returns a list of exclusive filter flags, which cannot be combined with other flags (e...
FilterFlag
Flags which indicate what types of filtering and searching is possible using the widget.
static QString toString(QgsSearchWidgetWrapper::FilterFlag flag)
Returns a translated string representing a filter flag.
QgsSearchWidgetToolButton(QWidget *parent=nullptr)
Constructor for QgsSearchWidgetToolButton.
void toggleFlag(QgsSearchWidgetWrapper::FilterFlag flag)
Toggles an individual active filter flag for the widget.
void setInactive()
Sets the search widget as inactive, ie do not search the corresponding field.
void setAvailableFlags(QgsSearchWidgetWrapper::FilterFlags flags)
Sets the available filter flags to show in the widget.
bool isActive() const
Returns true if the widget is set to be included in the search.
static QList< QgsSearchWidgetWrapper::FilterFlag > nonExclusiveFilterFlags()
Returns a list of non-exclusive filter flags, which can be combined with other flags (e...
void activeFlagsChanged(QgsSearchWidgetWrapper::FilterFlags flags)
Emitted when the active flags selected in the widget is changed.