QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  mComboBox->view()->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
66  connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
67  this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
68  }
69 }
70 
72 {
73  return mComboBox;
74 }
75 
76 void QgsValueMapWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
77 {
78  QString v;
79  if ( value.isNull() )
81  else
82  v = value.toString();
83 
84  if ( mComboBox )
85  {
86  if ( mComboBox->findData( v ) == -1 )
87  {
88  if ( value.isNull( ) )
89  {
90  mComboBox->addItem( QgsApplication::nullRepresentation().prepend( '(' ).append( ')' ), v );
91  }
92  else
93  {
94  mComboBox->addItem( QString( v ).prepend( '(' ).append( ')' ), v );
95  }
96  }
97  mComboBox->setCurrentIndex( mComboBox->findData( v ) );
98  }
99 }
QgsValueMapFieldFormatter::NULL_VALUE
static const QString NULL_VALUE
Will be saved in the configuration when a value is NULL.
Definition: qgsvaluemapfieldformatter.h:62
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:47
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:188
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
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:82
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsValueMapWidgetWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsvaluemapwidgetwrapper.cpp:71
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