QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
17
18#include "qgsfields.h"
19#include "qgsfieldvalidator.h"
23
24#include <QSettings>
25#include <QSizePolicy>
26
27#include "moc_qgsvaluemapsearchwidgetwrapper.cpp"
28
30 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
31
32{
33}
34
36{
37 auto combo = new QComboBox( parent );
38 combo->setMinimumContentsLength( 1 );
39 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
40 return combo;
41}
42
43void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
44{
45 if ( mComboBox )
46 {
47 if ( idx == 0 )
48 {
50 emit valueCleared();
51 }
52 else
53 {
54 setExpression( mComboBox->itemData( idx ).toString() );
55 emit valueChanged();
56 }
58 }
59}
60
62{
63 return true;
64}
65
70
72{
73 return true;
74}
75
80
85
87{
88 //clear any unsupported flags
89 flags &= supportedFlags();
90
91 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
92 const QString fieldName = createFieldIdentifier();
93
94 if ( flags & IsNull )
95 return fieldName + " IS NULL";
96 if ( flags & IsNotNull )
97 return fieldName + " IS NOT NULL";
98
99 //if deselect value, always pass
100 if ( mComboBox->currentIndex() == 0 )
101 return QString();
102
103 const QString currentKey = mComboBox->currentData().toString();
104
105 switch ( fldType )
106 {
107 case QMetaType::Type::Int:
108 case QMetaType::Type::UInt:
109 case QMetaType::Type::Double:
110 case QMetaType::Type::LongLong:
111 case QMetaType::Type::ULongLong:
112 {
113 if ( flags & EqualTo )
114 return fieldName + '=' + currentKey;
115 else if ( flags & NotEqualTo )
116 return fieldName + "<>" + currentKey;
117 break;
118 }
119
120 default:
121 {
122 if ( flags & EqualTo )
123 return fieldName + "='" + currentKey + '\'';
124 else if ( flags & NotEqualTo )
125 return fieldName + "<>'" + currentKey + '\'';
126 break;
127 }
128 }
129
130 return QString();
131}
132
134{
135 mComboBox->setCurrentIndex( 0 );
136}
137
139{
140 mComboBox->setEnabled( enabled );
141}
142
144{
145 mComboBox = qobject_cast<QComboBox *>( editor );
146
147 if ( mComboBox )
148 {
150 mComboBox->insertItem( 0, tr( "Please select" ), QString() );
151
152 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged );
153 }
154}
155
157{
158 QString exp = expression;
159 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
160 QString str;
161
162 str = QStringLiteral( "%1 = '%2'" )
163 .arg( QgsExpression::quotedColumnRef( fieldName ), exp.replace( '\'', QLatin1String( "''" ) ) );
164
165 mExpression = str;
166}
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
QMetaType::Type type
Definition qgsfield.h:61
QString name
Definition qgsfield.h:63
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
@ IsNull
Supports searching for null values.
@ IsNotNull
Supports searching for non-null values.
@ NotEqualTo
Supports not equal to.
void valueChanged()
Emitted when a user changes the value of the search widget.
QgsSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Create a new widget wrapper.
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
QFlags< FilterFlag > FilterFlags
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
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.
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 dataset.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.