QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgsapplication.h"
19
20#include <QMenu>
21
22#include "moc_qgssearchwidgettoolbutton.cpp"
23
25 : QToolButton( parent )
26 , mAvailableFilterFlags( QgsSearchWidgetWrapper::EqualTo | QgsSearchWidgetWrapper::NotEqualTo | QgsSearchWidgetWrapper::CaseInsensitive )
27 , mDefaultFilterFlags( QgsSearchWidgetWrapper::EqualTo )
28 , mFilterFlags( QgsSearchWidgetWrapper::EqualTo )
29
30{
31 setFocusPolicy( Qt::StrongFocus );
32 setPopupMode( QToolButton::InstantPopup );
33
34 mMenu = new QMenu( this );
35 connect( mMenu, &QMenu::aboutToShow, this, &QgsSearchWidgetToolButton::aboutToShowMenu );
36 setMenu( mMenu );
37
38 // sets initial appearance
39 updateState();
40}
41
43{
44 mFilterFlags &= flags;
45 mAvailableFilterFlags = flags;
46 mDefaultFilterFlags = mDefaultFilterFlags & flags;
47 updateState();
48}
49
51{
52 mDefaultFilterFlags = flags & mAvailableFilterFlags;
53}
54
56{
57 // sanitize list
59
60 // only accept a single exclusive flag
61 const auto exclusiveFilterFlags { QgsSearchWidgetWrapper::exclusiveFilterFlags() };
62 for ( const QgsSearchWidgetWrapper::FilterFlag flag : exclusiveFilterFlags )
63 {
64 if ( !( mAvailableFilterFlags & flag ) )
65 {
66 //unsupported
67 continue;
68 }
69 if ( flags & flag )
70 {
71 newFlags |= flag;
72 break;
73 }
74 }
75 const auto nonExclusiveFilterFlags { QgsSearchWidgetWrapper::nonExclusiveFilterFlags() };
76 for ( const QgsSearchWidgetWrapper::FilterFlag flag : nonExclusiveFilterFlags )
77 {
78 if ( !( mAvailableFilterFlags & flag ) )
79 {
80 //unsupported
81 continue;
82 }
83
84 if ( flags & flag )
85 newFlags |= flag;
86 }
87
88 mFilterFlags = newFlags;
89
90 updateState();
91}
92
94{
95 if ( !( flag & mAvailableFilterFlags ) )
96 return;
97
99 {
100 if ( flag & mFilterFlags )
101 mFilterFlags &= ~flag;
102 else
103 mFilterFlags |= flag;
104 }
105 else
106 {
107 // clear other exclusive flags
108 const auto exclusiveFilterFlags { QgsSearchWidgetWrapper::exclusiveFilterFlags() };
109 for ( const QgsSearchWidgetWrapper::FilterFlag exclusiveFlag : exclusiveFilterFlags )
110 {
111 mFilterFlags &= ~exclusiveFlag;
112 }
113 // and set new exclusive flag
114 mFilterFlags |= flag;
115 }
116
117 updateState();
118}
119
121{
122 const auto exclusiveFilterFlags { QgsSearchWidgetWrapper::exclusiveFilterFlags() };
123 for ( const QgsSearchWidgetWrapper::FilterFlag flag : exclusiveFilterFlags )
124 {
125 if ( mFilterFlags & flag )
126 return true;
127 }
128 return false;
129}
130
131void QgsSearchWidgetToolButton::aboutToShowMenu()
132{
133 mMenu->clear();
134 bool fieldActive = false;
135 const auto exclusiveFilterFlags { QgsSearchWidgetWrapper::exclusiveFilterFlags() };
136 for ( const QgsSearchWidgetWrapper::FilterFlag flag : exclusiveFilterFlags )
137 {
138 if ( !( mAvailableFilterFlags & flag ) )
139 {
140 //unsupported
141 continue;
142 }
143
144 QAction *action = mMenu->addAction( QgsSearchWidgetWrapper::toString( flag ) );
145 connect( action, &QAction::triggered, this, &QgsSearchWidgetToolButton::actionSelected );
146 action->setData( flag );
147 action->setCheckable( true );
148 if ( mFilterFlags & flag )
149 {
150 fieldActive = true;
151 action->setChecked( true );
152 }
153 }
154
155 QAction *clearAction = mMenu->addAction( tr( "Exclude Field" ) );
156 connect( clearAction, &QAction::triggered, this, &QgsSearchWidgetToolButton::setInactive );
157 clearAction->setCheckable( true );
158 clearAction->setChecked( !fieldActive );
159 if ( mMenu->actions().count() > 0 )
160 {
161 mMenu->insertAction( mMenu->actions().at( 0 ), clearAction );
162 mMenu->insertSeparator( mMenu->actions().at( 1 ) );
163 }
164 else
165 mMenu->addAction( clearAction );
166
167 mMenu->addSeparator();
168
169 const auto nonExclusiveFilterFlags { QgsSearchWidgetWrapper::nonExclusiveFilterFlags() };
170 for ( const QgsSearchWidgetWrapper::FilterFlag flag : nonExclusiveFilterFlags )
171 {
172 if ( !( mAvailableFilterFlags & flag ) )
173 {
174 //unsupported
175 continue;
176 }
177
178 QAction *action = mMenu->addAction( QgsSearchWidgetWrapper::toString( flag ) );
179 connect( action, &QAction::triggered, this, &QgsSearchWidgetToolButton::actionSelected );
180 action->setData( flag );
181 action->setCheckable( true );
182 if ( mFilterFlags & flag )
183 action->setChecked( true );
184 }
185}
186
187void QgsSearchWidgetToolButton::actionSelected()
188{
189 const QgsSearchWidgetWrapper::FilterFlag flag = static_cast<QgsSearchWidgetWrapper::FilterFlag>( qobject_cast<QAction *>( sender() )->data().toInt() );
190 toggleFlag( flag );
191}
192
193void QgsSearchWidgetToolButton::searchWidgetValueChanged()
194{
195 setActive();
196}
197
199{
200 if ( !isActive() )
201 return;
202
204 const auto nonExclusiveFilterFlags { QgsSearchWidgetWrapper::nonExclusiveFilterFlags() };
205 for ( const QgsSearchWidgetWrapper::FilterFlag flag : nonExclusiveFilterFlags )
206 {
207 if ( !( mAvailableFilterFlags & flag ) || !( mFilterFlags & flag ) )
208 continue;
209 newFlags |= flag;
210 }
211 mFilterFlags = newFlags;
212 updateState();
213}
214
216{
217 if ( isActive() )
218 return;
219
220 const auto exclusiveFilterFlags { QgsSearchWidgetWrapper::exclusiveFilterFlags() };
221 for ( const QgsSearchWidgetWrapper::FilterFlag flag : exclusiveFilterFlags )
222 {
223 if ( mDefaultFilterFlags & flag )
224 {
225 toggleFlag( flag );
226 return;
227 }
228 }
229}
230
231void QgsSearchWidgetToolButton::updateState()
232{
233 bool active = false;
234 QStringList toolTips;
235 const auto exclusiveFilterFlags { QgsSearchWidgetWrapper::exclusiveFilterFlags() };
236 for ( const QgsSearchWidgetWrapper::FilterFlag flag : exclusiveFilterFlags )
237 {
238 if ( mFilterFlags & flag )
239 {
240 toolTips << QgsSearchWidgetWrapper::toString( flag );
241 active = true;
242 }
243 }
244 const auto nonExclusiveFilterFlags { QgsSearchWidgetWrapper::nonExclusiveFilterFlags() };
245 for ( const QgsSearchWidgetWrapper::FilterFlag flag : nonExclusiveFilterFlags )
246 {
247 if ( mFilterFlags & flag )
248 {
249 toolTips << QgsSearchWidgetWrapper::toString( flag ).toLower();
250 }
251 }
252
253 if ( active )
254 {
255 const QString text = toolTips.join( QLatin1String( ", " ) );
256 setText( text );
257 setToolTip( text );
258 }
259 else
260 {
261 setText( tr( "Exclude Field" ) );
262 setToolTip( QString() );
263 }
264
265 emit activeFlagsChanged( mFilterFlags );
266}
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.