Skip to content

Commit

Permalink
Format JSON configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed May 6, 2021
1 parent 6fdb0a7 commit 1dda4d5
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 133 deletions.
2 changes: 0 additions & 2 deletions src/gui/CMakeLists.txt
Expand Up @@ -165,7 +165,6 @@ set(QGIS_GUI_SRCS
editorwidgets/qgskeyvaluewidgetfactory.cpp
editorwidgets/qgskeyvaluewidgetwrapper.cpp
editorwidgets/qgsjsoneditconfigdlg.cpp
editorwidgets/qgsjsoneditsearchwidgetwrapper.cpp
editorwidgets/qgsjsoneditwidgetfactory.cpp
editorwidgets/qgsjsoneditwrapper.cpp
editorwidgets/qgsjsoneditwidget.cpp
Expand Down Expand Up @@ -963,7 +962,6 @@ set(QGIS_GUI_HDRS
editorwidgets/qgskeyvaluewidgetfactory.h
editorwidgets/qgskeyvaluewidgetwrapper.h
editorwidgets/qgsjsoneditconfigdlg.h
editorwidgets/qgsjsoneditsearchwidgetwrapper.h
editorwidgets/qgsjsoneditwidgetfactory.h
editorwidgets/qgsjsoneditwrapper.h
editorwidgets/qgsjsoneditwidget.h
Expand Down
2 changes: 1 addition & 1 deletion src/gui/codeeditors/qgscodeeditorjson.cpp
Expand Up @@ -27,7 +27,7 @@ QgsCodeEditorJson::QgsCodeEditorJson( QWidget *parent )
{
if ( !parent )
{
setTitle( tr( "JavaScript Editor" ) );
setTitle( tr( "JSON Editor" ) );
}
setFoldingVisible( true );
QgsCodeEditorJson::initializeLexer();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/codeeditors/qgscodeeditorjson.h
Expand Up @@ -35,7 +35,7 @@ class GUI_EXPORT QgsCodeEditorJson : public QgsCodeEditor

public:

//! Constructor for QgsCodeEditorJavascript
//! Constructor for QgsCodeEditorJson
QgsCodeEditorJson( QWidget *parent SIP_TRANSFERTHIS = nullptr );

protected:
Expand Down
3 changes: 3 additions & 0 deletions src/gui/editorwidgets/qgsjsoneditconfigdlg.cpp
Expand Up @@ -20,6 +20,7 @@ QgsJsonEditConfigDlg::QgsJsonEditConfigDlg( QgsVectorLayer *vl, int fieldIdx, QW
{
setupUi( this );
connect( mDefaultViewComboBox, QOverload<int>::of( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
connect( mFormatJsonComboBox, QOverload<int>::of( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
}


Expand All @@ -28,11 +29,13 @@ QVariantMap QgsJsonEditConfigDlg::config()
QVariantMap cfg;

cfg.insert( QStringLiteral( "DefaultView" ), mDefaultViewComboBox->currentIndex() );
cfg.insert( QStringLiteral( "FormatJson" ), mFormatJsonComboBox->currentIndex() );

return cfg;
}

void QgsJsonEditConfigDlg::setConfig( const QVariantMap &config )
{
mDefaultViewComboBox->setCurrentIndex( config.value( QStringLiteral( "DefaultView" ) ).toInt() );
mFormatJsonComboBox->setCurrentIndex( config.value( QStringLiteral( "FormatJson" ) ).toInt() );
}
29 changes: 0 additions & 29 deletions src/gui/editorwidgets/qgsjsoneditsearchwidgetwrapper.cpp

This file was deleted.

55 changes: 0 additions & 55 deletions src/gui/editorwidgets/qgsjsoneditsearchwidgetwrapper.h

This file was deleted.

70 changes: 50 additions & 20 deletions src/gui/editorwidgets/qgsjsoneditwidget.cpp
Expand Up @@ -28,7 +28,7 @@ QgsJsonEditWidget::QgsJsonEditWidget( QWidget *parent )
connect( mTextToolButton, &QToolButton::clicked, this, &QgsJsonEditWidget::textToolButtonClicked );
connect( mTreeToolButton, &QToolButton::clicked, this, &QgsJsonEditWidget::treeToolButtonClicked );

connect( mTextEdit, &QgsCodeEditorJson::textChanged, this, &QgsJsonEditWidget::refreshTreeView );
connect( mCodeEditorJson, &QgsCodeEditorJson::textChanged, this, &QgsJsonEditWidget::codeEditorJsonTextChanged );
}

QgsJsonEditWidget::~QgsJsonEditWidget()
Expand All @@ -38,17 +38,36 @@ QgsJsonEditWidget::~QgsJsonEditWidget()

void QgsJsonEditWidget::setJsonText( const QString &jsonText )
{
if ( mTextEdit->text() == jsonText )
return;
const QJsonDocument jsonDocument = QJsonDocument::fromJson( jsonText.toUtf8() );

mTextEdit->setText( jsonText );
mCodeEditorJson->blockSignals( true );
if ( jsonDocument.isNull() )
{
mCodeEditorJson->setText( jsonText );
}
else
{
switch ( mFormatJsonMode )
{
case FormatJson::Indented:
mCodeEditorJson->setText( jsonDocument.toJson( QJsonDocument::Indented ) );
break;
case FormatJson::Compact:
mCodeEditorJson->setText( jsonDocument.toJson( QJsonDocument::Compact ) );
break;
case FormatJson::Disabled:
mCodeEditorJson->setText( jsonText );
break;
}
}
mCodeEditorJson->blockSignals( false );

refreshTreeView();
refreshTreeView( jsonDocument );
}

QString QgsJsonEditWidget::jsonText() const
{
return mTextEdit->text();
return mCodeEditorJson->text();
}

bool QgsJsonEditWidget::validJson() const
Expand Down Expand Up @@ -77,6 +96,11 @@ void QgsJsonEditWidget::setView( QgsJsonEditWidget::View view ) const
}
}

void QgsJsonEditWidget::setFormatJsonMode( QgsJsonEditWidget::FormatJson formatJson )
{
mFormatJsonMode = formatJson;
}

void QgsJsonEditWidget::textToolButtonClicked( bool checked )
{
if ( checked )
Expand All @@ -93,14 +117,15 @@ void QgsJsonEditWidget::treeToolButtonClicked( bool checked )
setView( View::Text );
}

void QgsJsonEditWidget::refreshTreeView()
void QgsJsonEditWidget::codeEditorJsonTextChanged()
{
mTreeWidget->clear();

if ( mTextEdit->text().isEmpty() )
return;
const QJsonDocument jsonDocument = QJsonDocument::fromJson( mCodeEditorJson->text().toUtf8() );
refreshTreeView( jsonDocument );
}

QJsonDocument jsonDocument = QJsonDocument::fromJson( mTextEdit->text().toUtf8() );
void QgsJsonEditWidget::refreshTreeView( const QJsonDocument &jsonDocument )
{
mTreeWidget->clear();

if ( jsonDocument.isNull() )
{
Expand All @@ -110,13 +135,17 @@ void QgsJsonEditWidget::refreshTreeView()
mTreeToolButton->setToolTip( tr( "Invalid JSON, tree view not available" ) );
return;
}
mTextToolButton->setEnabled( true );
mTreeToolButton->setEnabled( true );
mTreeToolButton->setToolTip( tr( "Tree view" ) );
else
{
mTextToolButton->setEnabled( true );
mTreeToolButton->setEnabled( true );
mTreeToolButton->setToolTip( tr( "Tree view" ) );
}

if ( jsonDocument.isObject() )
{
for ( const QString &key : jsonDocument.object().keys() )
const QStringList keys = jsonDocument.object().keys();
for ( const QString &key : keys )
{
QJsonValue jsonValue = jsonDocument.object().value( key );
QTreeWidgetItem *treeWidgetItem = new QTreeWidgetItem( mTreeWidget, QStringList() << key );
Expand All @@ -143,7 +172,7 @@ void QgsJsonEditWidget::refreshTreeViewItemValue( const QJsonValue &jsonValue, Q
treeWidgetItemParent->setText( ( int )TreeWidgetColumn::Value, QStringLiteral( "null" ) );
break;
case QJsonValue::Bool:
treeWidgetItemParent->setText( ( int )TreeWidgetColumn::Value, jsonValue.toBool() ? "true" : "false" );
treeWidgetItemParent->setText( ( int )TreeWidgetColumn::Value, jsonValue.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );
break;
case QJsonValue::Double:
treeWidgetItemParent->setText( ( int )TreeWidgetColumn::Value, QString::number( jsonValue.toDouble() ) );
Expand All @@ -153,7 +182,7 @@ void QgsJsonEditWidget::refreshTreeViewItemValue( const QJsonValue &jsonValue, Q
break;
case QJsonValue::Array:
{
QJsonArray jsonArray = jsonValue.toArray();
const QJsonArray jsonArray = jsonValue.toArray();
for ( int index = 0; index < jsonArray.size(); index++ )
{
QTreeWidgetItem *treeWidgetItem = new QTreeWidgetItem( treeWidgetItemParent, QStringList() << QString::number( index ) );
Expand All @@ -164,8 +193,9 @@ void QgsJsonEditWidget::refreshTreeViewItemValue( const QJsonValue &jsonValue, Q
break;
case QJsonValue::Object:
{
QJsonObject jsonObject = jsonValue.toObject();
for ( const QString &key : jsonObject.keys() )
const QJsonObject jsonObject = jsonValue.toObject();
const QStringList keys = jsonObject.keys();
for ( const QString &key : keys )
{
QTreeWidgetItem *treeWidgetItem = new QTreeWidgetItem( treeWidgetItemParent, QStringList() << key );
refreshTreeViewItemValue( jsonObject.value( key ), treeWidgetItem );
Expand Down
14 changes: 13 additions & 1 deletion src/gui/editorwidgets/qgsjsoneditwidget.h
Expand Up @@ -40,6 +40,13 @@ class GUI_EXPORT QgsJsonEditWidget : public QWidget, private Ui::QgsJsonEditWidg
Tree = 1
};

enum class FormatJson : int
{
Indented = 0,
Compact = 1,
Disabled = 2
};

explicit QgsJsonEditWidget( QWidget *parent SIP_TRANSFERTHIS );

~QgsJsonEditWidget() override;
Expand All @@ -53,12 +60,14 @@ class GUI_EXPORT QgsJsonEditWidget : public QWidget, private Ui::QgsJsonEditWidg

void setView( View view ) const;

void setFormatJsonMode( FormatJson formatJson );

private slots:

void textToolButtonClicked( bool checked );
void treeToolButtonClicked( bool checked );

void refreshTreeView();
void codeEditorJsonTextChanged();

private:

Expand All @@ -68,7 +77,10 @@ class GUI_EXPORT QgsJsonEditWidget : public QWidget, private Ui::QgsJsonEditWidg
Value = 1
};

void refreshTreeView( const QJsonDocument &jsonDocument );
void refreshTreeViewItemValue( const QJsonValue &jsonValue, QTreeWidgetItem *treeWidgetItemParent );

FormatJson mFormatJsonMode = FormatJson::Indented;
};

#endif // QGSJSONEDITWIDGET_H
6 changes: 0 additions & 6 deletions src/gui/editorwidgets/qgsjsoneditwidgetfactory.cpp
Expand Up @@ -17,7 +17,6 @@

#include "qgsjsoneditwrapper.h"
#include "qgsjsoneditconfigdlg.h"
#include "qgsjsoneditsearchwidgetwrapper.h"

QgsJsonEditWidgetFactory::QgsJsonEditWidgetFactory( const QString &name )
: QgsEditorWidgetFactory( name )
Expand All @@ -29,11 +28,6 @@ QgsEditorWidgetWrapper *QgsJsonEditWidgetFactory::create( QgsVectorLayer *vl, in
return new QgsJsonEditWrapper( vl, fieldIdx, editor, parent );
}

QgsSearchWidgetWrapper *QgsJsonEditWidgetFactory::createSearchWidget( QgsVectorLayer *vl, int fieldIdx, QWidget *parent ) const
{
return new QgsJsonEditSearchWidgetWrapper( vl, fieldIdx, parent );
}

QgsEditorConfigWidget *QgsJsonEditWidgetFactory::configWidget( QgsVectorLayer *vl, int fieldIdx, QWidget *parent ) const
{
return new QgsJsonEditConfigDlg( vl, fieldIdx, parent );
Expand Down
1 change: 0 additions & 1 deletion src/gui/editorwidgets/qgsjsoneditwidgetfactory.h
Expand Up @@ -38,7 +38,6 @@ class GUI_EXPORT QgsJsonEditWidgetFactory : public QgsEditorWidgetFactory
QgsJsonEditWidgetFactory( const QString &name );

QgsEditorWidgetWrapper *create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const override;
QgsSearchWidgetWrapper *createSearchWidget( QgsVectorLayer *vl, int fieldIdx, QWidget *parent ) const override;
QgsEditorConfigWidget *configWidget( QgsVectorLayer *vl, int fieldIdx, QWidget *parent ) const override;

unsigned int fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const override;
Expand Down
11 changes: 1 addition & 10 deletions src/gui/editorwidgets/qgsjsoneditwrapper.cpp
Expand Up @@ -16,16 +16,6 @@
#include "qgsjsoneditwrapper.h"

#include "qgsjsoneditwidget.h"
#include "qgsfields.h"
#include "qgsfieldvalidator.h"
#include "qgsfilterlineedit.h"
#include "qgsapplication.h"
#include "qgsjsonutils.h"
#include "qgsmessagebar.h"
#include "qgslogger.h"

#include <QSettings>
#include <nlohmann/json.hpp>

QgsJsonEditWrapper::QgsJsonEditWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
: QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
Expand All @@ -44,6 +34,7 @@ QWidget *QgsJsonEditWrapper::createWidget( QWidget *parent )
{
QgsJsonEditWidget *jsonEditWidget = new QgsJsonEditWidget( parent );
jsonEditWidget->setView( static_cast<QgsJsonEditWidget::View>( config( QStringLiteral( "DefaultView" ) ).toInt() ) );
jsonEditWidget->setFormatJsonMode( static_cast<QgsJsonEditWidget::FormatJson>( config( QStringLiteral( "FormatJson" ) ).toInt() ) );
return jsonEditWidget;
}

Expand Down
1 change: 0 additions & 1 deletion src/gui/editorwidgets/qgsjsoneditwrapper.h
Expand Up @@ -69,7 +69,6 @@ class GUI_EXPORT QgsJsonEditWrapper : public QgsEditorWidgetWrapper

private:

QgsAttributeForm *mForm = nullptr;
void updateValues( const QVariant &val, const QVariantList & = QVariantList() ) override;

QgsJsonEditWidget *mJsonEditWidget = nullptr;
Expand Down

0 comments on commit 1dda4d5

Please sign in to comment.