QGIS API Documentation 3.29.0-Master (19d7edcfed)
qgsvaluemapsearchwidgetwrapper.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
20
21#include "qgsfields.h"
22#include "qgsfieldvalidator.h"
23
24#include <QSettings>
25#include <QSizePolicy>
26
28 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
29
30{
31}
32
34{
35 auto combo = new QComboBox( parent );
36 combo->setMinimumContentsLength( 1 );
37 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
38 return combo;
39}
40
41void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
42{
43 if ( mComboBox )
44 {
45 if ( idx == 0 )
46 {
48 emit valueCleared();
49 }
50 else
51 {
52 setExpression( mComboBox->itemData( idx ).toString() );
53 emit valueChanged();
54 }
56 }
57}
58
60{
61 return true;
62}
63
65{
66 return mExpression;
67}
68
70{
71 return true;
72}
73
74QgsSearchWidgetWrapper::FilterFlags QgsValueMapSearchWidgetWrapper::supportedFlags() const
75{
77}
78
79QgsSearchWidgetWrapper::FilterFlags QgsValueMapSearchWidgetWrapper::defaultFlags() const
80{
81 return EqualTo;
82}
83
84QString QgsValueMapSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
85{
86 //clear any unsupported flags
87 flags &= supportedFlags();
88
89 const QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type();
90 const QString fieldName = createFieldIdentifier();
91
92 if ( flags & IsNull )
93 return fieldName + " IS NULL";
94 if ( flags & IsNotNull )
95 return fieldName + " IS NOT NULL";
96
97 //if deselect value, always pass
98 if ( mComboBox->currentIndex() == 0 )
99 return QString();
100
101 const QString currentKey = mComboBox->currentData().toString();
102
103 switch ( fldType )
104 {
105 case QVariant::Int:
106 case QVariant::UInt:
107 case QVariant::Double:
108 case QVariant::LongLong:
109 case QVariant::ULongLong:
110 {
111 if ( flags & EqualTo )
112 return fieldName + '=' + currentKey;
113 else if ( flags & NotEqualTo )
114 return fieldName + "<>" + currentKey;
115 break;
116 }
117
118 default:
119 {
120 if ( flags & EqualTo )
121 return fieldName + "='" + currentKey + '\'';
122 else if ( flags & NotEqualTo )
123 return fieldName + "<>'" + currentKey + '\'';
124 break;
125 }
126 }
127
128 return QString();
129}
130
132{
133 mComboBox->setCurrentIndex( 0 );
134}
135
137{
138 mComboBox->setEnabled( enabled );
139}
140
142{
143 mComboBox = qobject_cast<QComboBox *>( editor );
144
145 if ( mComboBox )
146 {
148 mComboBox->insertItem( 0, tr( "Please select" ), QString() );
149
150 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged );
151 }
152}
153
154void QgsValueMapSearchWidgetWrapper::setExpression( const QString &expression )
155{
156 QString exp = expression;
157 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
158 QString str;
159
160 str = QStringLiteral( "%1 = '%2'" )
161 .arg( QgsExpression::quotedColumnRef( fieldName ),
162 exp.replace( '\'', QLatin1String( "''" ) ) );
163
165}
166
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QString name
Definition: qgsfield.h:60
QVariant::Type type
Definition: qgsfield.h:58
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
Shows a search widget on a filter form.
@ IsNull
Supports searching for null values.
@ IsNotNull
Supports searching for non-null values.
@ EqualTo
Supports equal to.
@ 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.
void clearExpression()
clears the expression to search for all features
static void populateComboBox(QComboBox *comboBox, const QVariantMap &configuration, bool skipNull)
Populates a comboBox with the appropriate entries based on a value map configuration.
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.
QgsValueMapSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsValueMapSearchWidgetWrapper.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
void setExpression(const QString &exp) override
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
bool valid() const override
Returns true if the widget has been properly initialized.
QString expression() const override
Will be used to access the widget's value.
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.
#define str(x)
Definition: qgis.cpp:37