QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
27
28#include "moc_qgsvaluemapsearchwidgetwrapper.cpp"
29
30using namespace Qt::StringLiterals;
31
33 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
34
35{
36}
37
39{
40 auto combo = new QComboBox( parent );
41 combo->setMinimumContentsLength( 1 );
42 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
43 return combo;
44}
45
46void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
47{
48 if ( mComboBox )
49 {
50 if ( idx == 0 )
51 {
53 emit valueCleared();
54 }
55 else
56 {
57 setExpression( mComboBox->itemData( idx ).toString() );
58 emit valueChanged();
59 }
61 }
62}
63
65{
66 return true;
67}
68
73
75{
76 return true;
77}
78
83
88
90{
91 //clear any unsupported flags
92 flags &= supportedFlags();
93
94 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
95 const QString fieldName = createFieldIdentifier();
96
97 if ( flags & IsNull )
98 return fieldName + " IS NULL";
99 if ( flags & IsNotNull )
100 return fieldName + " IS NOT NULL";
101
102 //if deselect value, always pass
103 if ( mComboBox->currentIndex() == 0 )
104 return QString();
105
106 const QString currentKey = mComboBox->currentData().toString();
107
108 switch ( fldType )
109 {
110 case QMetaType::Type::Int:
111 case QMetaType::Type::UInt:
112 case QMetaType::Type::Double:
113 case QMetaType::Type::LongLong:
114 case QMetaType::Type::ULongLong:
115 {
116 if ( flags & EqualTo )
117 return fieldName + '=' + currentKey;
118 else if ( flags & NotEqualTo )
119 return fieldName + "<>" + currentKey;
120 break;
121 }
122
123 default:
124 {
125 if ( flags & EqualTo )
126 return fieldName + "='" + currentKey + '\'';
127 else if ( flags & NotEqualTo )
128 return fieldName + "<>'" + currentKey + '\'';
129 break;
130 }
131 }
132
133 return QString();
134}
135
137{
138 mComboBox->setCurrentIndex( 0 );
139}
140
142{
143 mComboBox->setEnabled( enabled );
144}
145
147{
148 mComboBox = qobject_cast<QComboBox *>( editor );
149
150 if ( mComboBox )
151 {
153 mComboBox->insertItem( 0, tr( "Please select" ), QString() );
154
155 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged );
156 }
157}
158
160{
161 QString exp = expression;
162 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
163 QString str;
164
165 str = u"%1 = '%2'"_s
166 .arg( QgsExpression::quotedColumnRef( fieldName ), exp.replace( '\'', "''"_L1 ) );
167
168 mExpression = str;
169}
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
QMetaType::Type type
Definition qgsfield.h:63
QString name
Definition qgsfield.h:65
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.