QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsuniquevaluewidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsuniquevaluewidgetwrapper.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_qgsuniquevaluewidgetwrapper.cpp"
18
19#include "qgsvectorlayer.h"
20#include "qgsfilterlineedit.h"
21#include "qgsapplication.h"
22
23#include <QCompleter>
24#include <QSettings>
25
26QgsUniqueValuesWidgetWrapper::QgsUniqueValuesWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
27 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
28
29{
30}
31
33{
34 QVariant value;
35
36 if ( mComboBox )
37 value = mComboBox->currentData();
38
39 if ( mLineEdit )
40 {
41 if ( mLineEdit->text() == QgsApplication::nullRepresentation() )
43 else
44 value = mLineEdit->text();
45 }
46
47 return value;
48}
49
51{
52 if ( config( QStringLiteral( "Editable" ) ).toBool() )
53 return new QgsFilterLineEdit( parent );
54 else
55 {
56 QComboBox *combo = new QComboBox( parent );
57 combo->setMinimumContentsLength( 1 );
58 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
59 return combo;
60 }
61}
62
64{
65 mComboBox = qobject_cast<QComboBox *>( editor );
66 mLineEdit = qobject_cast<QLineEdit *>( editor );
67
68 QStringList sValues;
69
70 const QSet< QVariant> values = layer()->uniqueValues( fieldIdx() );
71
72 const auto constValues = values;
73 for ( const QVariant &v : constValues )
74 {
75 if ( mComboBox )
76 {
77 mComboBox->addItem( v.toString(), v );
78 }
79
80 if ( mLineEdit )
81 {
82 sValues << v.toString();
83 }
84 }
85
86 if ( mLineEdit )
87 {
88 QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
89 if ( fle && !( field().type() == QMetaType::Type::Int || field().type() == QMetaType::Type::Double || field().type() == QMetaType::Type::LongLong || field().type() == QMetaType::Type::QDate ) )
90 {
92 }
93
94 QCompleter *c = new QCompleter( sValues );
95 c->setCaseSensitivity( Qt::CaseInsensitive );
96 c->setCompletionMode( QCompleter::PopupCompletion );
97 mLineEdit->setCompleter( c );
98
99 connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value )
100 {
102 emit valueChanged( value );
104 emit valuesChanged( value );
105 } );
106 }
107
108 if ( mComboBox )
109 {
110 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
111 this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
112 }
113}
114
116{
117 return mComboBox || mLineEdit;
118}
119
121{
122 if ( mComboBox )
123 {
124 whileBlocking( mComboBox )->setCurrentIndex( -1 );
125 }
126 if ( mLineEdit )
127 {
128 whileBlocking( mLineEdit )->setText( QString() );
129 }
130}
131
132void QgsUniqueValuesWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
133{
134 if ( mComboBox )
135 {
136 mComboBox->setCurrentIndex( mComboBox->findData( value ) );
137 }
138
139 if ( mLineEdit )
140 {
142 mLineEdit->setText( QgsApplication::nullRepresentation() );
143 else
144 mLineEdit->setText( value.toString() );
145 }
146}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
Manages an editor widget Widget and wrapper share the same parent.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
int fieldIdx() const
Access the field index.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QVariant value() const override
Will be used to access the widget's value.
QgsUniqueValuesWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsUniqueValuesWidgetWrapper.
bool valid() const override
Returns true if the widget has been properly initialized.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
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.
QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const FINAL
Calculates a list of unique values contained within an attribute in the layer.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QVariantMap config() const
Returns the whole config.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5862