QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
25  : QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
26  , mComboBox( nullptr )
27  , mLineEdit( nullptr )
28 {
29 }
30 
32 {
34 
35  if ( mComboBox )
36  value = mComboBox->itemData( mComboBox->currentIndex() );
37 
38  if ( mLineEdit )
39  {
40  if ( mLineEdit->text() == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
41  value = QVariant( field().type() );
42  else
43  value = mLineEdit->text();
44  }
45 
46  return value;
47 }
48 
50 {
51  if ( config( "Editable" ).toBool() )
52  return new QgsFilterLineEdit( parent );
53  else
54  return new QComboBox( parent );
55 }
56 
58 {
59  mComboBox = qobject_cast<QComboBox*>( editor );
60  mLineEdit = qobject_cast<QLineEdit*>( editor );
61 
62  QStringList sValues;
63 
64  QList<QVariant> values;
65 
66  layer()->uniqueValues( fieldIdx(), values );
67 
68  Q_FOREACH ( const QVariant& v, values )
69  {
70  if ( mComboBox )
71  {
72  mComboBox->addItem( v.toString(), v );
73  }
74 
75  if ( mLineEdit )
76  {
77  sValues << v.toString();
78  }
79  }
80 
81  if ( mLineEdit )
82  {
83  QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
84  if ( fle && !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
85  {
86  fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
87  }
88 
89  QCompleter* c = new QCompleter( sValues );
90  c->setCaseSensitivity( Qt::CaseInsensitive );
91  c->setCompletionMode( QCompleter::PopupCompletion );
92  mLineEdit->setCompleter( c );
93 
94  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
95  }
96 
97  if ( mComboBox )
98  {
99  connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) );
100  }
101 }
102 
104 {
105  return mComboBox || mLineEdit;
106 }
107 
109 {
110  if ( mComboBox )
111  {
112  whileBlocking( mComboBox )->setCurrentIndex( -1 );
113  }
114  if ( mLineEdit )
115  {
116  whileBlocking( mLineEdit )->setText( QString() );
117  }
118 }
119 
121 {
122  if ( mComboBox )
123  {
124  mComboBox->setCurrentIndex( mComboBox->findData( value ) );
125  }
126 
127  if ( mLineEdit )
128  {
129  if ( value.isNull() )
130  mLineEdit->setText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
131  else
132  mLineEdit->setText( value.toString() );
133  }
134 }
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
void valueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
void uniqueValues(int index, QList< QVariant > &uniqueValues, int limit=-1)
Calculates a list of unique values contained within an attribute in the layer.
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.
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void addItem(const QString &text, const QVariant &userData)
bool isNull() const
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
QVariant itemData(int index, int role) const
void setCompletionMode(CompletionMode mode)
void setValue(const QVariant &value) override
QVariant value(const QString &key, const QVariant &defaultValue) const
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:333
int findData(const QVariant &data, int role, QFlags< Qt::MatchFlag > flags) const
bool valid() const override
Return true if the widget has been properly initialized.
QgsEditorWidgetConfig config() const
Returns the whole config.
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
Access the QgsVectorLayer, you are working on.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:97
QString toString() const