Skip to content
Permalink
Browse files
Merge pull request #43164 from domi4484/jsonEdit
[FEATURE] Json edit widget
  • Loading branch information
m-kuhn committed May 14, 2021
2 parents c022ae9 + 3992e18 commit 32af8100e510f5780dc0170f8da1aafe3ecfa003
@@ -0,0 +1,12 @@
# The following has been generated automatically from src/gui/editorwidgets/qgsjsoneditwidget.h
# monkey patching scoped based enum
QgsJsonEditWidget.View.Text.__doc__ = "JSON data displayed as text."
QgsJsonEditWidget.View.Tree.__doc__ = "JSON data displayed as tree. Tree view is disabled for invalid JSON data."
QgsJsonEditWidget.View.__doc__ = 'View mode, text or tree.\n\n' + '* ``Text``: ' + QgsJsonEditWidget.View.Text.__doc__ + '\n' + '* ``Tree``: ' + QgsJsonEditWidget.View.Tree.__doc__
# --
# monkey patching scoped based enum
QgsJsonEditWidget.FormatJson.Indented.__doc__ = "JSON data formatted with regular indentation"
QgsJsonEditWidget.FormatJson.Compact.__doc__ = "JSON data formatted as a compact one line string"
QgsJsonEditWidget.FormatJson.Disabled.__doc__ = "JSON data is not formatted"
QgsJsonEditWidget.FormatJson.__doc__ = 'Format mode in the text view\n\n' + '* ``Indented``: ' + QgsJsonEditWidget.FormatJson.Indented.__doc__ + '\n' + '* ``Compact``: ' + QgsJsonEditWidget.FormatJson.Compact.__doc__ + '\n' + '* ``Disabled``: ' + QgsJsonEditWidget.FormatJson.Disabled.__doc__
# --
@@ -0,0 +1,44 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/codeeditors/qgscodeeditorjson.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsCodeEditorJson : QgsCodeEditor
{
%Docstring(signature="appended")
A JSON editor based on QScintilla2. Adds syntax highlighting and
code autocompletion.

.. versionadded:: 3.20
%End

%TypeHeaderCode
#include "qgscodeeditorjson.h"
%End
public:

QgsCodeEditorJson( QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsCodeEditorJson
%End

protected:
virtual void initializeLexer();


};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/codeeditors/qgscodeeditorjson.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -0,0 +1,77 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/editorwidgets/qgsjsoneditwidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsJsonEditWidget : QWidget
{
%Docstring(signature="appended")
The :py:class:`QgsJsonEditWidget` is a widget to display JSON data in a code highlighted text or tree form.

.. versionadded:: 3.20
%End

%TypeHeaderCode
#include "qgsjsoneditwidget.h"
%End
public:

enum class View
{
Text,
Tree
};

enum class FormatJson
{
Indented,
Compact,
Disabled
};

explicit QgsJsonEditWidget( QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsJsonEditWidget.

:param parent: parent widget
%End

void setJsonText( const QString &jsonText );
%Docstring
Set the JSON text in the widget to ``jsonText``.
%End

QString jsonText() const;
%Docstring
Returns the JSON text.
%End

void setView( View view ) const;
%Docstring
Set the ``view`` mode.

.. seealso:: View
%End

void setFormatJsonMode( FormatJson formatJson );
%Docstring
Set the ``formatJson`` mode.

.. seealso:: FormatJson
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/editorwidgets/qgsjsoneditwidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -270,6 +270,9 @@
%Include auto_generated/codeeditors/qgscodeeditorjs.sip
%End
%If ( HAVE_QSCI_SIP )
%Include auto_generated/codeeditors/qgscodeeditorjson.sip
%End
%If ( HAVE_QSCI_SIP )
%Include auto_generated/codeeditors/qgscodeeditorpython.sip
%End
%If ( HAVE_QSCI_SIP )
@@ -290,6 +293,7 @@
%Include auto_generated/editorwidgets/qgsdefaultsearchwidgetwrapper.sip
%Include auto_generated/editorwidgets/qgsdoublespinbox.sip
%Include auto_generated/editorwidgets/qgshtmlwidgetwrapper.sip
%Include auto_generated/editorwidgets/qgsjsoneditwidget.sip
%Include auto_generated/editorwidgets/qgsmultiedittoolbutton.sip
%Include auto_generated/editorwidgets/qgsrelationaggregatesearchwidgetwrapper.sip
%Include auto_generated/editorwidgets/qgsrelationreferencesearchwidgetwrapper.sip
@@ -121,6 +121,7 @@ set(QGIS_GUI_SRCS
codeeditors/qgscodeeditorcss.cpp
codeeditors/qgscodeeditorhtml.cpp
codeeditors/qgscodeeditorjs.cpp
codeeditors/qgscodeeditorjson.cpp
codeeditors/qgscodeeditorpython.cpp
codeeditors/qgscodeeditorsql.cpp
codeeditors/qgscodeeditorexpression.cpp
@@ -164,6 +165,10 @@ set(QGIS_GUI_SRCS
editorwidgets/qgshtmlwidgetwrapper.cpp
editorwidgets/qgskeyvaluewidgetfactory.cpp
editorwidgets/qgskeyvaluewidgetwrapper.cpp
editorwidgets/qgsjsoneditconfigdlg.cpp
editorwidgets/qgsjsoneditwidgetfactory.cpp
editorwidgets/qgsjsoneditwrapper.cpp
editorwidgets/qgsjsoneditwidget.cpp
editorwidgets/qgslistwidgetfactory.cpp
editorwidgets/qgslistwidgetwrapper.cpp
editorwidgets/qgslistconfigdlg.cpp
@@ -915,6 +920,7 @@ set(QGIS_GUI_HDRS
codeeditors/qgscodeeditorexpression.h
codeeditors/qgscodeeditorhtml.h
codeeditors/qgscodeeditorjs.h
codeeditors/qgscodeeditorjson.h
codeeditors/qgscodeeditorpython.h
codeeditors/qgscodeeditorsql.h

@@ -956,6 +962,10 @@ set(QGIS_GUI_HDRS
editorwidgets/qgshtmlwidgetwrapper.h
editorwidgets/qgskeyvaluewidgetfactory.h
editorwidgets/qgskeyvaluewidgetwrapper.h
editorwidgets/qgsjsoneditconfigdlg.h
editorwidgets/qgsjsoneditwidgetfactory.h
editorwidgets/qgsjsoneditwrapper.h
editorwidgets/qgsjsoneditwidget.h
editorwidgets/qgslistwidgetfactory.h
editorwidgets/qgslistwidgetwrapper.h
editorwidgets/qgsmultiedittoolbutton.h
@@ -0,0 +1,66 @@
/***************************************************************************
qgscodeeditorjson.cpp - A JSON editor based on QScintilla
--------------------------------------
Date : 4.5.2021
Copyright : (C) 2021 Damiano Lombardi
Email : damiano at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsapplication.h"
#include "qgscodeeditorjson.h"

#include <QWidget>
#include <QString>
#include <QFont>
#include <Qsci/qscilexerjson.h>


QgsCodeEditorJson::QgsCodeEditorJson( QWidget *parent )
: QgsCodeEditor( parent )
{
if ( !parent )
{
setTitle( tr( "JSON Editor" ) );
}
setFoldingVisible( true );
QgsCodeEditorJson::initializeLexer();
}

void QgsCodeEditorJson::initializeLexer()
{
QsciLexerJSON *lexer = new QsciLexerJSON( this );

QFont font = lexerFont();
lexer->setDefaultFont( font );
lexer->setFont( font, -1 );

font.setItalic( true );
lexer->setFont( font, QsciLexerJSON::CommentBlock );
lexer->setFont( font, QsciLexerJSON::CommentLine );

lexer->setDefaultColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ) );
lexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
lexer->setPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ), -1 );

lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Class ), QsciLexerJSON::Property );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerJSON::Keyword );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerJSON::Operator );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerJSON::Number );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentBlock ), QsciLexerJSON::CommentBlock );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerJSON::CommentLine );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerJSON::String );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerJSON::IRI );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerJSON::UnclosedString );
lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Error ), QsciLexerJSON::Error );

setLexer( lexer );
setLineNumbersVisible( true );
runPostLexerConfigurationTasks();
}
@@ -0,0 +1,46 @@
/***************************************************************************
qgscodeeditorjson.h - A JSON editor based on QScintilla
--------------------------------------
Date : 4.5.2021
Copyright : (C) 2021 Damiano Lombardi
Email : damiano at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSCODEEDITORJSON_H
#define QGSCODEEDITORJSON_H

#include "qgscodeeditor.h"
#include "qgis_sip.h"
#include "qgis_gui.h"

SIP_IF_MODULE( HAVE_QSCI_SIP )


/**
* \ingroup gui
* \brief A JSON editor based on QScintilla2. Adds syntax highlighting and
* code autocompletion.
* \since QGIS 3.20
*/
class GUI_EXPORT QgsCodeEditorJson : public QgsCodeEditor
{
Q_OBJECT

public:

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

protected:
void initializeLexer() override;

};

#endif // QGSCODEEDITORJSON_H
@@ -33,6 +33,7 @@
#include "qgsexternalresourcewidgetfactory.h"
#include "qgshiddenwidgetfactory.h"
#include "qgskeyvaluewidgetfactory.h"
#include "qgsjsoneditwidgetfactory.h"
#include "qgslistwidgetfactory.h"
#include "qgsrangewidgetfactory.h"
#include "qgsrelationreferencefactory.h"
@@ -66,6 +67,7 @@ void QgsEditorWidgetRegistry::initEditors( QgsMapCanvas *mapCanvas, QgsMessageBa
registerWidget( QStringLiteral( "KeyValue" ), new QgsKeyValueWidgetFactory( tr( "Key/Value" ) ) );
registerWidget( QStringLiteral( "List" ), new QgsListWidgetFactory( tr( "List" ) ) );
registerWidget( QStringLiteral( "Binary" ), new QgsBinaryWidgetFactory( tr( "Binary (BLOB)" ), messageBar ) );
registerWidget( QStringLiteral( "JsonEdit" ), new QgsJsonEditWidgetFactory( tr( "Json Edit" ) ) );
}

QgsEditorWidgetRegistry::~QgsEditorWidgetRegistry()
@@ -0,0 +1,41 @@
/***************************************************************************
qgsjsoneditconfigdlg.cpp
--------------------------------------
Date : 3.5.2021
Copyright : (C) 2021 Damiano Lombardi
Email : damiano at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsjsoneditconfigdlg.h"

QgsJsonEditConfigDlg::QgsJsonEditConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
{
setupUi( this );
connect( mDefaultViewComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
connect( mFormatJsonComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
}


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() );
}

0 comments on commit 32af810

Please sign in to comment.