QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsdefaultsearchwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdefaultsearchwidgettwrapper.cpp
3 --------------------------------------
4 Date : 31.5.2015
5 Copyright : (C) 2015 Karolina Alexiou (carolinux)
6 Email : carolinegr at gmail dot 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_qgsdefaultsearchwidgetwrapper.cpp"
18
19#include "qgsfields.h"
20#include "qgsfieldvalidator.h"
21#include "qgsexpression.h"
23#include "qgssettings.h"
24#include "qgsapplication.h"
25
26#include <QHBoxLayout>
27
29 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
30 , mCaseString( QStringLiteral( "LIKE" ) )
31{
32}
33
35{
36 return mExpression;
37}
38
39void QgsDefaultSearchWidgetWrapper::setCaseString( int caseSensitiveCheckState )
40{
41 if ( caseSensitiveCheckState == Qt::Checked )
42 {
43 mCaseString = QStringLiteral( "LIKE" );
44 }
45 else
46 {
47 mCaseString = QStringLiteral( "ILIKE" );
48 }
49 // need to update also the line edit
50 setExpression( mLineEdit->text() );
51
52 if ( applyDirectly() )
54}
55
56void QgsDefaultSearchWidgetWrapper::setExpression( const QString &expression )
57{
58 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
59 const bool numeric = ( fldType == QMetaType::Type::Int || fldType == QMetaType::Type::Double || fldType == QMetaType::Type::LongLong );
60
61 QString exp = expression;
62 const QString nullValue = QgsApplication::nullRepresentation();
63 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
64 QString str;
65 if ( exp == nullValue )
66 {
67 str = QStringLiteral( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
68 }
69 else
70 {
71 str = QStringLiteral( "%1 %2 '%3'" )
72 .arg( QgsExpression::quotedColumnRef( fieldName ),
73 numeric ? QStringLiteral( "=" ) : mCaseString,
74 numeric ?
75 exp.replace( '\'', QLatin1String( "''" ) )
76 :
77 '%' + exp.replace( '\'', QLatin1String( "''" ) ) + '%' ); // escape quotes
78 }
80}
81
83{
84 return new QWidget( parent );
85}
86
88{
89 return false;
90}
91
93{
95
96 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
97 switch ( fldType )
98 {
99 case QMetaType::Type::Int:
100 case QMetaType::Type::UInt:
101 case QMetaType::Type::Double:
102 case QMetaType::Type::LongLong:
103 case QMetaType::Type::ULongLong:
104 case QMetaType::Type::QDate:
105 case QMetaType::Type::QDateTime:
106 case QMetaType::Type::QTime:
108 break;
109
110 case QMetaType::Type::QString:
112 break;
113
114 default:
115 break;
116 }
117 return flags;
118}
119
121{
122 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
123 switch ( fldType )
124 {
125 //numeric
126 case QMetaType::Type::Int:
127 case QMetaType::Type::UInt:
128 case QMetaType::Type::Double:
129 case QMetaType::Type::LongLong:
130 case QMetaType::Type::ULongLong:
131
132 // date
133 case QMetaType::Type::QDate:
134 case QMetaType::Type::QDateTime:
135 case QMetaType::Type::QTime:
136 return EqualTo;
137
138 case QMetaType::Type::QString:
139 return Contains;
140
141 default:
142 break;
143 }
144 return EqualTo;
145}
146
148{
149 //clear any unsupported flags
150 flags &= supportedFlags();
151
152 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
153 const QString fieldName = createFieldIdentifier();
154
155 if ( flags & IsNull )
156 return fieldName + " IS NULL";
157 if ( flags & IsNotNull )
158 return fieldName + " IS NOT NULL";
159
160 switch ( fldType )
161 {
162 case QMetaType::Type::Int:
163 case QMetaType::Type::UInt:
164 case QMetaType::Type::Double:
165 case QMetaType::Type::LongLong:
166 case QMetaType::Type::ULongLong:
167 {
168 if ( flags & EqualTo )
169 return fieldName + '=' + mLineEdit->text();
170 else if ( flags & NotEqualTo )
171 return fieldName + "<>" + mLineEdit->text();
172 else if ( flags & GreaterThan )
173 return fieldName + '>' + mLineEdit->text();
174 else if ( flags & LessThan )
175 return fieldName + '<' + mLineEdit->text();
176 else if ( flags & GreaterThanOrEqualTo )
177 return fieldName + ">=" + mLineEdit->text();
178 else if ( flags & LessThanOrEqualTo )
179 return fieldName + "<=" + mLineEdit->text();
180 break;
181 }
182
183 case QMetaType::Type::QDate:
184 case QMetaType::Type::QDateTime:
185 case QMetaType::Type::QTime:
186 {
187 if ( flags & EqualTo )
188 return fieldName + "='" + mLineEdit->text() + '\'';
189 else if ( flags & NotEqualTo )
190 return fieldName + "<>'" + mLineEdit->text() + '\'';
191 else if ( flags & GreaterThan )
192 return fieldName + ">'" + mLineEdit->text() + '\'';
193 else if ( flags & LessThan )
194 return fieldName + "<'" + mLineEdit->text() + '\'';
195 else if ( flags & GreaterThanOrEqualTo )
196 return fieldName + ">='" + mLineEdit->text() + '\'';
197 else if ( flags & LessThanOrEqualTo )
198 return fieldName + "<='" + mLineEdit->text() + '\'';
199 break;
200 }
201
202 case QMetaType::Type::QString:
203 {
204 // case insensitive!
205 if ( flags & EqualTo || flags & NotEqualTo )
206 {
207 if ( mCheckbox && mCheckbox->isChecked() )
208 return fieldName + ( ( flags & EqualTo ) ? "=" : "<>" )
209 + QgsExpression::quotedString( mLineEdit->text() );
210 else
211 return QStringLiteral( "lower(%1)" ).arg( fieldName )
212 + ( ( flags & EqualTo ) ? "=" : "<>" ) +
213 QStringLiteral( "lower(%1)" ).arg( QgsExpression::quotedString( mLineEdit->text() ) );
214 }
215 else if ( flags & Contains || flags & DoesNotContain || flags & StartsWith || flags & EndsWith )
216 {
217 QString exp = fieldName + ( mCheckbox && mCheckbox->isChecked() ? " LIKE " : " ILIKE " );
218 QString value = QgsExpression::quotedString( mLineEdit->text() );
219 value.chop( 1 );
220 value = value.remove( 0, 1 );
221 exp += '\'';
222 if ( !flags.testFlag( StartsWith ) )
223 exp += '%';
224 exp += value;
225 if ( !flags.testFlag( EndsWith ) )
226 exp += '%';
227 exp += '\'';
228 if ( flags & DoesNotContain )
229 exp.prepend( "NOT (" ).append( ')' );
230 return exp;
231 }
232
233 break;
234 }
235
236 default:
237 break;
238 }
239
240 return QString();
241}
242
244{
245 mLineEdit->setText( QString() );
246}
247
249{
250 mLineEdit->setEnabled( enabled );
251 if ( mCheckbox )
252 mCheckbox->setEnabled( enabled );
253}
254
256{
257 mContainer = widget;
258 mContainer->setLayout( new QHBoxLayout() );
259 mContainer->layout()->setContentsMargins( 0, 0, 0, 0 );
260 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
261
262 if ( fldType == QMetaType::Type::QString )
263 {
264 mLineEdit = new QgsFieldValuesLineEdit();
265 static_cast< QgsFieldValuesLineEdit * >( mLineEdit )->setLayer( layer() );
266 static_cast< QgsFieldValuesLineEdit * >( mLineEdit )->setAttributeIndex( mFieldIdx );
267 }
268 else
269 {
270 mLineEdit = new QgsFilterLineEdit();
271 }
272 mContainer->layout()->addWidget( mLineEdit );
273 mContainer->setFocusProxy( mLineEdit );
274
275 if ( fldType == QMetaType::Type::QString )
276 {
277 mCheckbox = new QCheckBox( QStringLiteral( "Case sensitive" ) );
278 mContainer->layout()->addWidget( mCheckbox );
279 connect( mCheckbox, &QCheckBox::stateChanged, this, &QgsDefaultSearchWidgetWrapper::setCaseString );
280 mCheckbox->setChecked( Qt::Unchecked );
281 }
282
283 connect( mLineEdit, &QLineEdit::textChanged, this, &QgsDefaultSearchWidgetWrapper::textChanged );
284 connect( mLineEdit, &QLineEdit::returnPressed, this, &QgsDefaultSearchWidgetWrapper::filterChanged );
285 connect( mLineEdit, &QLineEdit::textEdited, this, &QgsSearchWidgetWrapper::valueChanged );
286
287 mCaseString = QStringLiteral( "ILIKE" );
288}
289
291{
292 return true;
293}
294
299
301{
302 return mCheckbox;
303}
304
305void QgsDefaultSearchWidgetWrapper::filterChanged()
306{
308}
309
310void QgsDefaultSearchWidgetWrapper::textChanged( const QString &text )
311{
312 if ( text.isEmpty() )
313 emit valueCleared();
314
315 setExpression( text );
316}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
Creates a filter expression based on the current state of the search widget and the specified filter ...
void setExpression(const QString &exp) override
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QString expression() const override
Will be used to access the widget's value.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
QCheckBox * caseSensitiveCheckBox()
Returns a pointer to the case sensitivity checkbox in the widget.
QgsFilterLineEdit * lineEdit()
Returns a pointer to the line edit part of the widget.
QgsDefaultSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsDefaultSearchWidgetWrapper.
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes.
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
bool valid() const override
Returns true if the widget has been properly initialized.
static QString quotedString(QString text)
Returns a quoted version of a string (in single quotes)
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
A line edit with an autocompleter which takes unique values from a vector layer's fields.
QMetaType::Type type
Definition qgsfield.h:60
QString name
Definition qgsfield.h:62
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
Shows a search widget on a filter form.
@ Contains
Supports value "contains" searching.
@ IsNotBetween
Supports searching for values outside of a set range.
@ StartsWith
Supports searching for strings that start with.
@ LessThan
Supports less than.
@ IsNull
Supports searching for null values.
@ EndsWith
Supports searching for strings that end with.
@ GreaterThan
Supports greater than.
@ IsNotNull
Supports searching for non-null values.
@ DoesNotContain
Supports value does not contain searching.
@ Between
Supports searches between two values.
@ NotEqualTo
Supports not equal to.
void valueChanged()
Emitted when a user changes the value of the search widget.
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state.
void expressionChanged(const QString &exp)
Emitted whenever the expression changes.
QString createFieldIdentifier() const
Gets a field name or expression to use as field comparison.
QFlags< FilterFlag > FilterFlags
Represents a vector layer which manages a vector based data sets.
QWidget * widget()
Access the widget managed by this wrapper.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
#define str(x)
Definition qgis.cpp:39