QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsvaluerelationsearchwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvaluerelationsearchwidgetwrapper.cpp
3 --------------------------------------
4 Date : 5.1.2014
5 Copyright : (C) 2014 Matthias Kuhn
6 Email : matthias at opengis dot ch
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_qgsvaluerelationsearchwidgetwrapper.cpp"
18
19#include "qgsfields.h"
21#include "qgsvectorlayer.h"
22#include "qgsfilterlineedit.h"
24#include "qgssettings.h"
25#include "qgsapplication.h"
26
27#include <QStringListModel>
28#include <QCompleter>
29
31 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
32
33{
34}
35
37{
38 return !mLineEdit;
39}
40
45
47{
48 QVariant v;
49
50 if ( mComboBox )
51 {
52 int cbxIdx = mComboBox->currentIndex();
53 if ( cbxIdx > -1 )
54 {
55 v = mComboBox->currentData();
56 }
57 }
58
59 if ( mLineEdit )
60 {
61 const auto constMCache = mCache;
62 for ( const QgsValueRelationFieldFormatter::ValueRelationItem &i : constMCache )
63 {
64 if ( i.value == mLineEdit->text() )
65 {
66 v = i.key;
67 break;
68 }
69 }
70 }
71
72 return v;
73}
74
79
84
86{
87 QString fieldName = createFieldIdentifier();
88
89 //clear any unsupported flags
90 flags &= supportedFlags();
91 if ( flags & IsNull )
92 return fieldName + " IS NULL";
93 if ( flags & IsNotNull )
94 return fieldName + " IS NOT NULL";
95
96 QVariant v = value();
97 if ( !v.isValid() )
98 return QString();
99
100 switch ( v.userType() )
101 {
102 case QMetaType::Type::Int:
103 case QMetaType::Type::UInt:
104 case QMetaType::Type::Double:
105 case QMetaType::Type::LongLong:
106 case QMetaType::Type::ULongLong:
107 {
108 if ( flags & EqualTo )
109 return fieldName + '=' + v.toString();
110 else if ( flags & NotEqualTo )
111 return fieldName + "<>" + v.toString();
112 break;
113 }
114
115 default:
116 {
117 if ( flags & EqualTo )
118 return fieldName + "='" + v.toString() + '\'';
119 else if ( flags & NotEqualTo )
120 return fieldName + "<>'" + v.toString() + '\'';
121 break;
122 }
123 }
124
125 return QString();
126}
127
129{
130 if ( mComboBox )
131 {
132 mComboBox->setCurrentIndex( 0 );
133 }
134 if ( mLineEdit )
135 {
136 mLineEdit->setText( QString() );
137 }
138}
139
141{
142 if ( mComboBox )
143 {
144 mComboBox->setEnabled( enabled );
145 }
146 if ( mLineEdit )
147 {
148 mLineEdit->setEnabled( enabled );
149 }
150}
151
153{
154 return true;
155}
156
158{
159 QVariant vl = value();
160 if ( !vl.isValid() )
161 {
163 emit valueCleared();
164 }
165 else
166 {
168 emit valueChanged();
169 }
171}
172
174{
175 QString exp = expression;
176 QString nullValue = QgsApplication::nullRepresentation();
177 QString fieldName = layer()->fields().at( mFieldIdx ).name();
178
179 QString str;
180 if ( exp == nullValue )
181 {
182 str = QStringLiteral( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
183 }
184 else
185 {
186 str = QStringLiteral( "%1 = '%3'" )
187 .arg( QgsExpression::quotedColumnRef( fieldName ),
188 exp.replace( '\'', QLatin1String( "''" ) )
189 );
190 }
192}
193
195{
196 if ( config( QStringLiteral( "AllowMulti" ) ).toBool() )
197 {
198 return new QgsFilterLineEdit( parent );
199 }
200 else if ( config( QStringLiteral( "UseCompleter" ) ).toBool() )
201 {
202 return new QgsFilterLineEdit( parent );
203 }
204 else
205 {
206 QComboBox *combo = new QComboBox( parent );
207 combo->setMinimumContentsLength( 1 );
208 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
209 return combo;
210 }
211}
212
214{
216
217 mComboBox = qobject_cast<QComboBox *>( editor );
218 mLineEdit = qobject_cast<QLineEdit *>( editor );
219
220 if ( mComboBox )
221 {
222 mComboBox->addItem( tr( "Please Select" ), QVariant() ); // creates an invalid to allow selecting all features
223 if ( config( QStringLiteral( "AllowNull" ) ).toBool() )
224 {
225 mComboBox->addItem( tr( "(no selection)" ), QgsVariantUtils::createNullVariant( layer()->fields().at( mFieldIdx ).type() ) );
226 }
227
228 const auto constMCache = mCache;
229 for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : constMCache )
230 {
231 mComboBox->addItem( element.value, element.key );
232 }
233
234 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueRelationSearchWidgetWrapper::onValueChanged );
235 }
236 else if ( mLineEdit )
237 {
238 QStringList values;
239 values.reserve( mCache.size() );
240 for ( const QgsValueRelationFieldFormatter::ValueRelationItem &i : std::as_const( mCache ) )
241 {
242 values << i.value;
243 }
244
245 QStringListModel *m = new QStringListModel( values, mLineEdit );
246 QCompleter *completer = new QCompleter( m, mLineEdit );
247 completer->setCaseSensitivity( Qt::CaseInsensitive );
248 mLineEdit->setCompleter( completer );
249 connect( mLineEdit, &QLineEdit::textChanged, this, &QgsValueRelationSearchWidgetWrapper::onValueChanged );
250 }
251}
252
253
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QString name
Definition qgsfield.h:62
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
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
QVariant createCache(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const override
Create a cache for a given field.
bool applyDirectly() override
If this is true, then this search widget should take effect directly when its expression changes.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QgsValueRelationSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsValueRelationSearchWidgetWrapper.
QgsSearchWidgetWrapper::FilterFlags supportedFlags() const override
Returns filter flags supported by the search widget.
QString expression() const override
Will be used to access the widget's value.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QgsSearchWidgetWrapper::FilterFlags defaultFlags() const override
Returns the filter flags which should be set by default for the search widget.
bool valid() const override
Returns true if the widget has been properly initialized.
void onValueChanged()
Called when current value of search widget changes.
QString createExpression(QgsSearchWidgetWrapper::FilterFlags flags) const override
Creates a filter expression based on the current state of the search widget and the specified filter ...
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
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.
#define str(x)
Definition qgis.cpp:39