QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsjsoneditwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsjsoneditwrapper.cpp
3  --------------------------------------
4  Date : 3.5.2021
5  Copyright : (C) 2021 Damiano Lombardi
6  Email : damiano 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 
16 #include "qgsjsoneditwrapper.h"
17 
18 #include "qgsjsoneditwidget.h"
19 
20 QgsJsonEditWrapper::QgsJsonEditWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
21  : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
22 {
23 }
24 
25 QVariant QgsJsonEditWrapper::value() const
26 {
27  if ( !mJsonEditWidget )
28  return QVariant();
29 
30  return mJsonEditWidget->property( RAW_VALUE_PROPERTY.toUtf8().data() );
31 }
32 
33 QWidget *QgsJsonEditWrapper::createWidget( QWidget *parent )
34 {
35  QgsJsonEditWidget *jsonEditWidget = new QgsJsonEditWidget( parent );
36  jsonEditWidget->setView( static_cast<QgsJsonEditWidget::View>( config( QStringLiteral( "DefaultView" ) ).toInt() ) );
37  jsonEditWidget->setFormatJsonMode( static_cast<QgsJsonEditWidget::FormatJson>( config( QStringLiteral( "FormatJson" ) ).toInt() ) );
38  return jsonEditWidget;
39 }
40 
41 void QgsJsonEditWrapper::initWidget( QWidget *editor )
42 {
43  mJsonEditWidget = qobject_cast<QgsJsonEditWidget *>( editor );
44  if ( !mJsonEditWidget )
45  {
46  mJsonEditWidget = new QgsJsonEditWidget( editor );
47  }
48 }
49 
51 {
52  return ( mJsonEditWidget );
53 }
54 
56 {
57  if ( !mJsonEditWidget )
58  return;
59 
60  mJsonEditWidget->blockSignals( true );
61  mJsonEditWidget->setJsonText( QStringLiteral( "<mixed values>" ) );
62  mJsonEditWidget->blockSignals( false );
63 }
64 
66 {
67  setFormFeature( feature );
68  setValue( feature.attribute( fieldIdx() ) );
69 }
70 
71 void QgsJsonEditWrapper::updateValues( const QVariant &value, const QVariantList & )
72 {
73  if ( !mJsonEditWidget )
74  return;
75 
76  mJsonEditWidget->setProperty( RAW_VALUE_PROPERTY.toUtf8().data(), value );
77  mJsonEditWidget->setJsonText( field().displayString( value ) );
78 }
79 
80 void QgsJsonEditWrapper::setEnabled( bool enabled )
81 {
82  // No need to disable JsonEditWidget as it is already read only
83  Q_UNUSED( enabled )
84 }
85 
qgsjsoneditwidget.h
QgsEditorWidgetWrapper::setValue
virtual void setValue(const QVariant &value)
Is called when the value of the widget needs to be changed.
Definition: qgseditorwidgetwrapper.cpp:79
QgsJsonEditWrapper::initWidget
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Definition: qgsjsoneditwrapper.cpp:41
QgsJsonEditWrapper::valid
bool valid() const override
Returns true if the widget has been properly initialized.
Definition: qgsjsoneditwrapper.cpp:50
QgsEditorWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgseditorwidgetwrapper.h:47
QgsEditorWidgetWrapper::fieldIdx
int fieldIdx() const
Access the field index.
Definition: qgseditorwidgetwrapper.cpp:34
QgsJsonEditWrapper::QgsJsonEditWrapper
QgsJsonEditWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsJsonEditWrapper.
Definition: qgsjsoneditwrapper.cpp:20
QgsJsonEditWrapper::setFeature
void setFeature(const QgsFeature &feature) override
Definition: qgsjsoneditwrapper.cpp:65
QgsEditorWidgetWrapper::field
QgsField field() const
Access the field.
Definition: qgseditorwidgetwrapper.cpp:39
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:327
QgsJsonEditWidget::FormatJson
FormatJson
Format mode in the text view.
Definition: qgsjsoneditwidget.h:45
QgsJsonEditWrapper::createWidget
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
Definition: qgsjsoneditwrapper.cpp:33
QgsEditorWidgetWrapper::setFormFeature
void setFormFeature(const QgsFeature &feature)
Set the feature currently being edited to feature.
Definition: qgseditorwidgetwrapper.h:353
QgsJsonEditWrapper::value
QVariant value() const override
Will be used to access the widget's value.
Definition: qgsjsoneditwrapper.cpp:25
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
QgsJsonEditWrapper::showIndeterminateState
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
Definition: qgsjsoneditwrapper.cpp:55
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsJsonEditWidget::View
View
View mode, text or tree.
Definition: qgsjsoneditwidget.h:38
QgsJsonEditWrapper::setEnabled
void setEnabled(bool enabled) override
Definition: qgsjsoneditwrapper.cpp:80
qgsjsoneditwrapper.h