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