QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
Loading...
Searching...
No Matches
qgsenumerationwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsenumerationwidgetwrapper.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"
20#include "qgsvectorlayer.h"
21
22#include "moc_qgsenumerationwidgetwrapper.cpp"
23
25 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
26
27{}
28
29
31{
32 QVariant value;
33
34 if ( mComboBox )
35 value = mComboBox->currentData();
36
37 return value;
38}
39
41{
42 if ( mComboBox )
43 {
44 whileBlocking( mComboBox )->setCurrentIndex( -1 );
45 }
46}
47
49{
50 QComboBox *combo = new QComboBox( parent );
51 combo->setMinimumContentsLength( 1 );
52 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
53 return combo;
54}
55
57{
58 mComboBox = qobject_cast<QComboBox *>( editor );
59
60 if ( mComboBox )
61 {
62 const QgsField field( layer()->fields().at( fieldIdx() ) );
63 // also add NULL entry if not restricted by constraints
64 if ( !( field.constraints().constraints() & QgsFieldConstraints::ConstraintNotNull ) )
65 {
67 }
68
69 const QList<QPair<QString, QString>> values( layer()->dataProvider()->codedValues( fieldIdx() ) );
70 for ( const QPair<QString, QString> &pair : values )
71 {
72 mComboBox->addItem( pair.second, pair.first );
73 }
74 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
75 }
76}
77
79{
80 return mComboBox;
81}
82
83void QgsEnumerationWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
84{
85 if ( mComboBox )
86 {
87 mComboBox->setCurrentIndex( mComboBox->findData( value ) );
88 }
89}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
int fieldIdx() const
Access the field index.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
QVariant value() const override
Will be used to access the widget's value.
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.
bool valid() const override
Returns true if the widget has been properly initialized.
QgsEnumerationWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsEnumerationWidgetWrapper.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
@ ConstraintNotNull
Field may not be null.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
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.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:7451