QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsvaluemapwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvaluemapwidgetwrapper.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
19#include "qgsapplication.h"
20
21#include <QSettings>
22
23QgsValueMapWidgetWrapper::QgsValueMapWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
24 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
25
26{
27}
28
29
31{
32 QVariant v;
33
34 if ( mComboBox )
35 {
36 v = mComboBox->currentData();
37 }
38
40 v = QVariant( field().type() );
41
42 return v;
43}
44
46{
47 if ( mComboBox )
48 {
49 whileBlocking( mComboBox )->setCurrentIndex( -1 );
50 }
51}
52
53QWidget *QgsValueMapWidgetWrapper::createWidget( QWidget *parent )
54{
55 QComboBox *combo = new QComboBox( parent );
56 combo->setMinimumContentsLength( 1 );
57 combo->setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
58 return combo;
59}
60
62{
63 mComboBox = qobject_cast<QComboBox *>( editor );
64
65 if ( mComboBox )
66 {
68 mComboBox->view()->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
69 connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
70 this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
71 }
72}
73
75{
76 return mComboBox;
77}
78
79void QgsValueMapWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
80{
81 QString v;
84 else
85 v = value.toString();
86
87 if ( mComboBox )
88 {
89 if ( mComboBox->findData( v ) == -1 )
90 {
92 {
93 mComboBox->addItem( QgsApplication::nullRepresentation().prepend( '(' ).append( ')' ), v );
94 }
95 else
96 {
97 mComboBox->addItem( QString( v ).prepend( '(' ).append( ')' ), v );
98 }
99 }
100 mComboBox->setCurrentIndex( mComboBox->findData( v ) );
101 }
102}
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Manages an editor widget Widget and wrapper share the same parent.
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
static void populateComboBox(QComboBox *comboBox, const QVariantMap &configuration, bool skipNull)
Populates a comboBox with the appropriate entries based on a value map configuration.
static const QString NULL_VALUE
Will be saved in the configuration when a value is NULL.
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.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
QgsValueMapWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsValueMapWidgetWrapper.
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.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based data sets.
QVariantMap config() const
Returns the whole config.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:5111