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