QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgsapplication.h"
19#include "qgsfields.h"
20#include "qgsfilterlineedit.h"
21#include "qgssettings.h"
24#include "qgsvectorlayer.h"
25
26#include <QCompleter>
27#include <QString>
28#include <QStringListModel>
29
30#include "moc_qgsvaluerelationsearchwidgetwrapper.cpp"
31
32using namespace Qt::StringLiterals;
33
35 : QgsSearchWidgetWrapper( vl, fieldIdx, parent )
36
37{
38}
39
41{
42 return !mLineEdit;
43}
44
49
51{
52 QVariant v;
53
54 if ( mComboBox )
55 {
56 int cbxIdx = mComboBox->currentIndex();
57 if ( cbxIdx > -1 )
58 {
59 v = mComboBox->currentData();
60 }
61 }
62
63 if ( mLineEdit )
64 {
65 const auto constMCache = mCache;
66 for ( const QgsValueRelationFieldFormatter::ValueRelationItem &i : constMCache )
67 {
68 if ( i.value == mLineEdit->text() )
69 {
70 v = i.key;
71 break;
72 }
73 }
74 }
75
76 return v;
77}
78
83
88
90{
91 QString fieldName = createFieldIdentifier();
92
93 //clear any unsupported flags
94 flags &= supportedFlags();
95 if ( flags & IsNull )
96 return fieldName + " IS NULL";
97 if ( flags & IsNotNull )
98 return fieldName + " IS NOT NULL";
99
100 QVariant v = value();
101 if ( !v.isValid() )
102 return QString();
103
104 switch ( v.userType() )
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 + '=' + v.toString();
114 else if ( flags & NotEqualTo )
115 return fieldName + "<>" + v.toString();
116 break;
117 }
118
119 default:
120 {
121 if ( flags & EqualTo )
122 return fieldName + "='" + v.toString() + '\'';
123 else if ( flags & NotEqualTo )
124 return fieldName + "<>'" + v.toString() + '\'';
125 break;
126 }
127 }
128
129 return QString();
130}
131
133{
134 if ( mComboBox )
135 {
136 mComboBox->setCurrentIndex( 0 );
137 }
138 if ( mLineEdit )
139 {
140 mLineEdit->setText( QString() );
141 }
142}
143
145{
146 if ( mComboBox )
147 {
148 mComboBox->setEnabled( enabled );
149 }
150 if ( mLineEdit )
151 {
152 mLineEdit->setEnabled( enabled );
153 }
154}
155
157{
158 return true;
159}
160
162{
163 QVariant vl = value();
164 if ( !vl.isValid() )
165 {
167 emit valueCleared();
168 }
169 else
170 {
172 emit valueChanged();
173 }
175}
176
178{
179 QString exp = expression;
180 QString nullValue = QgsApplication::nullRepresentation();
181 QString fieldName = layer()->fields().at( mFieldIdx ).name();
182
183 QString str;
184 if ( exp == nullValue )
185 {
186 str = u"%1 IS NULL"_s.arg( QgsExpression::quotedColumnRef( fieldName ) );
187 }
188 else
189 {
190 str = u"%1 = '%3'"_s
191 .arg( QgsExpression::quotedColumnRef( fieldName ), exp.replace( '\'', "''"_L1 ) );
192 }
193 mExpression = str;
194}
195
197{
198 if ( config( u"AllowMulti"_s ).toBool() )
199 {
200 return new QgsFilterLineEdit( parent );
201 }
202 else if ( config( u"UseCompleter"_s ).toBool() )
203 {
204 return new QgsFilterLineEdit( parent );
205 }
206 else
207 {
208 QComboBox *combo = new QComboBox( parent );
209 combo->setMinimumContentsLength( 1 );
210 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
211 return combo;
212 }
213}
214
216{
218
219 mComboBox = qobject_cast<QComboBox *>( editor );
220 mLineEdit = qobject_cast<QLineEdit *>( editor );
221
222 if ( mComboBox )
223 {
224 mComboBox->addItem( tr( "Please Select" ), QVariant() ); // creates an invalid to allow selecting all features
225 if ( config( u"AllowNull"_s ).toBool() )
226 {
227 mComboBox->addItem( tr( "(no selection)" ), QgsVariantUtils::createNullVariant( layer()->fields().at( mFieldIdx ).type() ) );
228 }
229
230 const auto constMCache = mCache;
231 for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : constMCache )
232 {
233 mComboBox->addItem( element.value, element.key );
234 }
235
236 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueRelationSearchWidgetWrapper::onValueChanged );
237 }
238 else if ( mLineEdit )
239 {
240 QStringList values;
241 values.reserve( mCache.size() );
242 for ( const QgsValueRelationFieldFormatter::ValueRelationItem &i : std::as_const( mCache ) )
243 {
244 values << i.value;
245 }
246
247 QStringListModel *m = new QStringListModel( values, mLineEdit );
248 QCompleter *completer = new QCompleter( m, mLineEdit );
249 completer->setCaseSensitivity( Qt::CaseInsensitive );
250 mLineEdit->setCompleter( completer );
251 connect( mLineEdit, &QLineEdit::textChanged, this, &QgsValueRelationSearchWidgetWrapper::onValueChanged );
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:65
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...
@ 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
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 dataset.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.