QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
17 #include "qgsvaluemapconfigdlg.h"
19 #include "qgsapplication.h"
20 
21 #include <QSettings>
22 
23 QgsValueMapWidgetWrapper::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 
53 QWidget *QgsValueMapWidgetWrapper::createWidget( QWidget *parent )
54 {
55  return new QComboBox( parent );
56 }
57 
58 void QgsValueMapWidgetWrapper::initWidget( QWidget *editor )
59 {
60  mComboBox = qobject_cast<QComboBox *>( editor );
61 
62  if ( mComboBox )
63  {
64  QgsValueMapConfigDlg::populateComboBox( mComboBox, config(), false );
65  connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
66  this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
67  }
68 }
69 
71 {
72  return mComboBox;
73 }
74 
75 void QgsValueMapWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
76 {
77  QString v;
78  if ( value.isNull() )
80  else
81  v = value.toString();
82 
83  if ( mComboBox )
84  {
85  if ( mComboBox->findData( v ) == -1 )
86  {
87  if ( value.isNull( ) )
88  {
89  mComboBox->addItem( QgsApplication::nullRepresentation().prepend( '(' ).append( ')' ), v );
90  }
91  else
92  {
93  mComboBox->addItem( QString( v ).prepend( '(' ).append( ')' ), v );
94  }
95  }
96  mComboBox->setCurrentIndex( mComboBox->findData( v ) );
97  }
98 }
QgsValueMapFieldFormatter::NULL_VALUE
static const QString NULL_VALUE
Will be saved in the configuration when a value is NULL.
Definition: qgsvaluemapfieldformatter.h:47
QgsValueMapWidgetWrapper::QgsValueMapWidgetWrapper
QgsValueMapWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsValueMapWidgetWrapper.
Definition: qgsvaluemapwidgetwrapper.cpp:23
QgsValueMapWidgetWrapper::value
QVariant value() const override
Will be used to access the widget's value.
Definition: qgsvaluemapwidgetwrapper.cpp:30
QgsEditorWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgseditorwidgetwrapper.h:48
qgsapplication.h
QgsValueMapConfigDlg::populateComboBox
static void populateComboBox(QComboBox *comboBox, const QVariantMap &configuration, bool skipNull)
Populates a comboBox with the appropriate entries based on a value map configuration.
Definition: qgsvaluemapconfigdlg.cpp:186
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:1851
qgsvaluemapwidgetwrapper.h
qgsvaluemapfieldformatter.h
QgsEditorWidgetWrapper::field
QgsField field() const
Access the field.
Definition: qgseditorwidgetwrapper.cpp:39
QgsEditorWidgetWrapper::emitValueChanged
void emitValueChanged()
Will call the value() method to determine the emitted value.
Definition: qgseditorwidgetwrapper.cpp:91
QgsWidgetWrapper::config
QVariantMap config() const
Returns the whole config.
Definition: qgswidgetwrapper.cpp:81
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsValueMapWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsvaluemapwidgetwrapper.cpp:70
qgsvaluemapconfigdlg.h
QgsValueMapWidgetWrapper::showIndeterminateState
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
Definition: qgsvaluemapwidgetwrapper.cpp:45
QgsValueMapWidgetWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsvaluemapwidgetwrapper.cpp:53
QgsValueMapWidgetWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsvaluemapwidgetwrapper.cpp:58