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