QGIS API Documentation  3.23.0-Master (c716e02dd3)
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 
Manages an editor widget Widget and wrapper share the same parent.
void setFormFeature(const QgsFeature &feature)
Set the feature currently being edited to feature.
int fieldIdx() const
Access the field index.
QgsField field() const
Access the field.
virtual void setValue(const QVariant &value)
Is called when the value of the widget needs to be changed.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:320
The QgsJsonEditWidget is a widget to display JSON data in a code highlighted text or tree form.
void setJsonText(const QString &jsonText)
Set the JSON text in the widget to jsonText.
View
View mode, text or tree.
void setFormatJsonMode(FormatJson formatJson)
Set the formatJson mode.
void setView(View view) const
Set the view mode.
FormatJson
Format mode in the text view.
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.
bool valid() const override
Returns true if the widget has been properly initialized.
QgsJsonEditWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsJsonEditWrapper.
void setEnabled(bool enabled) override
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
void setFeature(const QgsFeature &feature) override
Represents a vector layer which manages a vector based data sets.
QVariantMap config() const
Returns the whole config.