QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgsvectorlayer.h"
19 #include "qgsfilterlineedit.h"
20 
21 #include <QCompleter>
22 #include <QSettings>
23 
24 QgsUniqueValuesWidgetWrapper::QgsUniqueValuesWidgetWrapper( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent )
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26 
27 {
28 }
29 
31 {
32  QVariant value;
33 
34  if ( mComboBox )
35  value = mComboBox->currentData();
36 
37  if ( mLineEdit )
38  {
39  if ( mLineEdit->text() == QgsApplication::nullRepresentation() )
40  value = QVariant( field().type() );
41  else
42  value = mLineEdit->text();
43  }
44 
45  return value;
46 }
47 
48 QWidget *QgsUniqueValuesWidgetWrapper::createWidget( QWidget *parent )
49 {
50  if ( config( QStringLiteral( "Editable" ) ).toBool() )
51  return new QgsFilterLineEdit( parent );
52  else
53  return new QComboBox( parent );
54 }
55 
57 {
58  mComboBox = qobject_cast<QComboBox *>( editor );
59  mLineEdit = qobject_cast<QLineEdit *>( editor );
60 
61  QStringList sValues;
62 
63  QSet< QVariant> values = layer()->uniqueValues( fieldIdx() );
64 
65  Q_FOREACH ( const QVariant &v, values )
66  {
67  if ( mComboBox )
68  {
69  mComboBox->addItem( v.toString(), v );
70  }
71 
72  if ( mLineEdit )
73  {
74  sValues << v.toString();
75  }
76  }
77 
78  if ( mLineEdit )
79  {
80  QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit *>( editor );
81  if ( fle && !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
82  {
84  }
85 
86  QCompleter *c = new QCompleter( sValues );
87  c->setCaseSensitivity( Qt::CaseInsensitive );
88  c->setCompletionMode( QCompleter::PopupCompletion );
89  mLineEdit->setCompleter( c );
90 
91  connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value ) { emit valueChanged( value ); } );
92  }
93 
94  if ( mComboBox )
95  {
96  connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
97  this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
98  }
99 }
100 
102 {
103  return mComboBox || mLineEdit;
104 }
105 
107 {
108  if ( mComboBox )
109  {
110  whileBlocking( mComboBox )->setCurrentIndex( -1 );
111  }
112  if ( mLineEdit )
113  {
114  whileBlocking( mLineEdit )->setText( QString() );
115  }
116 }
117 
119 {
120  if ( mComboBox )
121  {
122  mComboBox->setCurrentIndex( mComboBox->findData( value ) );
123  }
124 
125  if ( mLineEdit )
126  {
127  if ( value.isNull() )
128  mLineEdit->setText( QgsApplication::nullRepresentation() );
129  else
130  mLineEdit->setText( value.toString() );
131  }
132 }
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
Manages an editor widget Widget and wrapper share the same parent.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const override
Calculates a list of unique values contained within an attribute in the layer.
QVariantMap config() const
Returns the whole config.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
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
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
void setValue(const QVariant &value) override
QgsUniqueValuesWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
QVariant value() const override
Will be used to access the widget&#39;s value.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:224
bool valid() const override
Returns true if the widget has been properly initialized.
void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
int fieldIdx() const
Access the field index.
void setNullValue(const QString &nullValue)
Sets the string representation for null values in the widget.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type
Definition: qgsfield.h:55