QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "moc_qgsvaluemapsearchwidgetwrapper.cpp"
21
22#include "qgsfields.h"
23#include "qgsfieldvalidator.h"
24
25#include <QSettings>
26#include <QSizePolicy>
27
29 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
30
31{
32}
33
35{
36 auto combo = new QComboBox( parent );
37 combo->setMinimumContentsLength( 1 );
38 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
39 return combo;
40}
41
42void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
43{
44 if ( mComboBox )
45 {
46 if ( idx == 0 )
47 {
49 emit valueCleared();
50 }
51 else
52 {
53 setExpression( mComboBox->itemData( idx ).toString() );
54 emit valueChanged();
55 }
57 }
58}
59
61{
62 return true;
63}
64
69
71{
72 return true;
73}
74
79
84
86{
87 //clear any unsupported flags
88 flags &= supportedFlags();
89
90 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
91 const QString fieldName = createFieldIdentifier();
92
93 if ( flags & IsNull )
94 return fieldName + " IS NULL";
95 if ( flags & IsNotNull )
96 return fieldName + " IS NOT NULL";
97
98 //if deselect value, always pass
99 if ( mComboBox->currentIndex() == 0 )
100 return QString();
101
102 const QString currentKey = mComboBox->currentData().toString();
103
104 switch ( fldType )
105 {
106 case QMetaType::Type::Int:
107 case QMetaType::Type::UInt:
108 case QMetaType::Type::Double:
109 case QMetaType::Type::LongLong:
110 case QMetaType::Type::ULongLong:
111 {
112 if ( flags & EqualTo )
113 return fieldName + '=' + currentKey;
114 else if ( flags & NotEqualTo )
115 return fieldName + "<>" + currentKey;
116 break;
117 }
118
119 default:
120 {
121 if ( flags & EqualTo )
122 return fieldName + "='" + currentKey + '\'';
123 else if ( flags & NotEqualTo )
124 return fieldName + "<>'" + currentKey + '\'';
125 break;
126 }
127 }
128
129 return QString();
130}
131
133{
134 mComboBox->setCurrentIndex( 0 );
135}
136
138{
139 mComboBox->setEnabled( enabled );
140}
141
143{
144 mComboBox = qobject_cast<QComboBox *>( editor );
145
146 if ( mComboBox )
147 {
149 mComboBox->insertItem( 0, tr( "Please select" ), QString() );
150
151 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged );
152 }
153}
154
155void QgsValueMapSearchWidgetWrapper::setExpression( const QString &expression )
156{
157 QString exp = expression;
158 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
159 QString str;
160
161 str = QStringLiteral( "%1 = '%2'" )
162 .arg( QgsExpression::quotedColumnRef( fieldName ), exp.replace( '\'', QLatin1String( "''" ) ) );
163
164 mExpression = str;
165}
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
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).
Shows a search widget on a filter form.
@ 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.
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 data sets.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.