QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
38{
39 auto combo = new QComboBox( parent );
40 combo->setMinimumContentsLength( 1 );
41 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
42 return combo;
43}
44
45void QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged( int idx )
46{
47 if ( mComboBox )
48 {
49 if ( idx == 0 )
50 {
52 emit valueCleared();
53 }
54 else
55 {
56 setExpression( mComboBox->itemData( idx ).toString() );
57 emit valueChanged();
58 }
60 }
61}
62
64{
65 return true;
66}
67
72
74{
75 return true;
76}
77
82
87
89{
90 //clear any unsupported flags
91 flags &= supportedFlags();
92
93 const QMetaType::Type fldType = layer()->fields().at( mFieldIdx ).type();
94 const QString fieldName = createFieldIdentifier();
95
96 if ( flags & IsNull )
97 return fieldName + " IS NULL";
98 if ( flags & IsNotNull )
99 return fieldName + " IS NOT NULL";
100
101 //if deselect value, always pass
102 if ( mComboBox->currentIndex() == 0 )
103 return QString();
104
105 const QString currentKey = mComboBox->currentData().toString();
106
107 switch ( fldType )
108 {
109 case QMetaType::Type::Int:
110 case QMetaType::Type::UInt:
111 case QMetaType::Type::Double:
112 case QMetaType::Type::LongLong:
113 case QMetaType::Type::ULongLong:
114 {
115 if ( flags & EqualTo )
116 return fieldName + '=' + currentKey;
117 else if ( flags & NotEqualTo )
118 return fieldName + "<>" + currentKey;
119 break;
120 }
121
122 default:
123 {
124 if ( flags & EqualTo )
125 return fieldName + "='" + currentKey + '\'';
126 else if ( flags & NotEqualTo )
127 return fieldName + "<>'" + currentKey + '\'';
128 break;
129 }
130 }
131
132 return QString();
133}
134
136{
137 mComboBox->setCurrentIndex( 0 );
138}
139
141{
142 mComboBox->setEnabled( enabled );
143}
144
146{
147 mComboBox = qobject_cast<QComboBox *>( editor );
148
149 if ( mComboBox )
150 {
152 mComboBox->insertItem( 0, tr( "Please select" ), QString() );
153
154 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueMapSearchWidgetWrapper::comboBoxIndexChanged );
155 }
156}
157
159{
160 QString exp = expression;
161 const QString fieldName = layer()->fields().at( mFieldIdx ).name();
162 QString str;
163
164 str = u"%1 = '%2'"_s.arg( QgsExpression::quotedColumnRef( fieldName ), exp.replace( '\'', "''"_L1 ) );
165
166 mExpression = str;
167}
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.