QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 ) {
101 emit valueChanged( value );
103 emit valuesChanged( value );
104 } );
105 }
106
107 if ( mComboBox )
108 {
109 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
110 }
111}
112
114{
115 return mComboBox || mLineEdit;
116}
117
119{
120 if ( mComboBox )
121 {
122 whileBlocking( mComboBox )->setCurrentIndex( -1 );
123 }
124 if ( mLineEdit )
125 {
126 whileBlocking( mLineEdit )->setText( QString() );
127 }
128}
129
130void QgsUniqueValuesWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
131{
132 if ( mComboBox )
133 {
134 mComboBox->setCurrentIndex( mComboBox->findData( value ) );
135 }
136
137 if ( mLineEdit )
138 {
140 mLineEdit->setText( QgsApplication::nullRepresentation() );
141 else
142 mLineEdit->setText( value.toString() );
143 }
144}
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:6643
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6642
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5970