QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 return new QComboBox( parent );
36}
37
38void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
39{
40 if ( mComboBox )
41 {
42 if ( idx == 0 )
43 {
45 emit valueCleared();
46 }
47 else
48 {
49 setExpression( mComboBox->itemData( idx ).toString() );
50 emit valueChanged();
51 }
53 }
54}
55
57{
58 return true;
59}
60
62{
63 return mExpression;
64}
65
67{
68 return true;
69}
70
71QgsSearchWidgetWrapper::FilterFlags QgsValueMapSearchWidgetWrapper::supportedFlags() const
72{
74}
75
76QgsSearchWidgetWrapper::FilterFlags QgsValueMapSearchWidgetWrapper::defaultFlags() const
77{
78 return EqualTo;
79}
80
81QString QgsValueMapSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const
82{
83 //clear any unsupported flags
84 flags &= supportedFlags();
85
86 const QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type();
87 const QString fieldName = createFieldIdentifier();
88
89 if ( flags & IsNull )
90 return fieldName + " IS NULL";
91 if ( flags & IsNotNull )
92 return fieldName + " IS NOT NULL";
93
94 //if deselect value, always pass
95 if ( mComboBox->currentIndex() == 0 )
96 return QString();
97
98 const QString currentKey = mComboBox->currentData().toString();
99
100 switch ( fldType )
101 {
102 case QVariant::Int:
103 case QVariant::UInt:
104 case QVariant::Double:
105 case QVariant::LongLong:
106 case QVariant::ULongLong:
107 {
108 if ( flags & EqualTo )
109 return fieldName + '=' + currentKey;
110 else if ( flags & NotEqualTo )
111 return fieldName + "<>" + currentKey;
112 break;
113 }
114
115 default:
116 {
117 if ( flags & EqualTo )
118 return fieldName + "='" + currentKey + '\'';
119 else if ( flags & NotEqualTo )
120 return fieldName + "<>'" + currentKey + '\'';
121 break;
122 }
123 }
124
125 return QString();
126}
127
129{
130 mComboBox->setCurrentIndex( 0 );
131}
132
134{
135 mComboBox->setEnabled( enabled );
136}
137
139{
140 mComboBox = qobject_cast<QComboBox *>( editor );
141
142 if ( mComboBox )
143 {
145 mComboBox->insertItem( 0, tr( "Please select" ), QString() );
146
147 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged );
148 }
149}
150
151void QgsValueMapSearchWidgetWrapper::setExpression( const QString &expression )
152{
153 QString exp = expression;
154 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
155 QString str;
156
157 str = QStringLiteral( "%1 = '%2'" )
158 .arg( QgsExpression::quotedColumnRef( fieldName ),
159 exp.replace( '\'', QLatin1String( "''" ) ) );
160
162}
163
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