QGIS API Documentation  3.23.0-Master (c716e02dd3)
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 }
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.
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:1485