QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgsapplication.h"
19#include "qgsfilterlineedit.h"
20#include "qgsvectorlayer.h"
21
22#include <QCompleter>
23#include <QSettings>
24#include <QString>
25
26#include "moc_qgsuniquevaluewidgetwrapper.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
32
33{
34}
35
37{
38 QVariant value;
39
40 if ( mComboBox )
41 value = mComboBox->currentData();
42
43 if ( mLineEdit )
44 {
45 if ( mLineEdit->text() == QgsApplication::nullRepresentation() )
47 else
48 value = mLineEdit->text();
49 }
50
51 return value;
52}
53
55{
56 if ( config( u"Editable"_s ).toBool() )
57 return new QgsFilterLineEdit( parent );
58 else
59 {
60 QComboBox *combo = new QComboBox( parent );
61 combo->setMinimumContentsLength( 1 );
62 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
63 return combo;
64 }
65}
66
68{
69 mComboBox = qobject_cast<QComboBox *>( editor );
70 mLineEdit = qobject_cast<QLineEdit *>( editor );
71
72 QStringList sValues;
73
74 const QSet<QVariant> values = layer()->uniqueValues( fieldIdx() );
75
76 const auto constValues = values;
77 for ( const QVariant &v : constValues )
78 {
79 if ( mComboBox )
80 {
81 mComboBox->addItem( v.toString(), v );
82 }
83
84 if ( mLineEdit )
85 {
86 sValues << v.toString();
87 }
88 }
89
90 if ( mLineEdit )
91 {
92 QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
93 if ( fle && !( field().type() == QMetaType::Type::Int || field().type() == QMetaType::Type::Double || field().type() == QMetaType::Type::LongLong || field().type() == QMetaType::Type::QDate ) )
94 {
96 }
97
98 QCompleter *c = new QCompleter( sValues );
99 c->setCaseSensitivity( Qt::CaseInsensitive );
100 c->setCompletionMode( QCompleter::PopupCompletion );
101 mLineEdit->setCompleter( c );
102
103 connect( mLineEdit, &QLineEdit::textChanged, this, [this]( const QString &value ) {
105 emit valueChanged( value );
107 emit valuesChanged( value );
108 } );
109 }
110
111 if ( mComboBox )
112 {
113 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
114 }
115}
116
118{
119 return mComboBox || mLineEdit;
120}
121
123{
124 if ( mComboBox )
125 {
126 whileBlocking( mComboBox )->setCurrentIndex( -1 );
127 }
128 if ( mLineEdit )
129 {
130 whileBlocking( mLineEdit )->setText( QString() );
131 }
132}
133
134void QgsUniqueValuesWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
135{
136 if ( mComboBox )
137 {
138 mComboBox->setCurrentIndex( mComboBox->findData( value ) );
139 }
140
141 if ( mLineEdit )
142 {
144 mLineEdit->setText( QgsApplication::nullRepresentation() );
145 else
146 mLineEdit->setText( value.toString() );
147 }
148}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
int fieldIdx() const
Access the field index.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
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 dataset.
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:7451
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7450
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6804