20#include <QDesktopServices>
29 , mCopyValueAction( new QAction( tr(
"Copy Value" ), this ) )
30 , mCopyKeyAction( new QAction( tr(
"Copy Key" ), this ) )
36 mCodeEditorJson->setReadOnly(
true );
37 mCodeEditorJson->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
38 mCodeEditorJson->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
39 mCodeEditorJson->indicatorDefine( QsciScintilla::PlainIndicator, SCINTILLA_UNDERLINE_INDICATOR_INDEX );
40 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_SETINDICATORCURRENT, SCINTILLA_UNDERLINE_INDICATOR_INDEX );
41 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_SETMOUSEDWELLTIME, 400 );
43 mTreeWidget->setContextMenuPolicy( Qt::ActionsContextMenu );
44 mTreeWidget->addAction( mCopyValueAction );
45 mTreeWidget->addAction( mCopyKeyAction );
47 connect( mTextToolButton, &QToolButton::clicked,
this, &QgsJsonEditWidget::textToolButtonClicked );
48 connect( mTreeToolButton, &QToolButton::clicked,
this, &QgsJsonEditWidget::treeToolButtonClicked );
50 connect( mCopyValueAction, &QAction::triggered,
this, &QgsJsonEditWidget::copyValueActionTriggered );
51 connect( mCopyKeyAction, &QAction::triggered,
this, &QgsJsonEditWidget::copyKeyActionTriggered );
53 connect( mCodeEditorJson, &QgsCodeEditorJson::textChanged,
this, &QgsJsonEditWidget::codeEditorJsonTextChanged );
56 connect( mCodeEditorJson, &QsciScintilla::indicatorClicked,
this, &QgsJsonEditWidget::codeEditorJsonIndicatorClicked );
57 connect( mCodeEditorJson, &QsciScintillaBase::SCN_DWELLSTART,
this, &QgsJsonEditWidget::codeEditorJsonDwellStart );
58 connect( mCodeEditorJson, &QsciScintillaBase::SCN_DWELLEND,
this, &QgsJsonEditWidget::codeEditorJsonDwellEnd );
63 return mCodeEditorJson;
69 mClickableLinkList.clear();
71 const QJsonDocument jsonDocument = QJsonDocument::fromJson( mJsonText.toUtf8() );
73 mCodeEditorJson->blockSignals(
true );
74 if ( jsonDocument.isNull() )
76 mCodeEditorJson->setText( mJsonText );
80 switch ( mFormatJsonMode )
83 mCodeEditorJson->setText( jsonDocument.toJson( QJsonDocument::Indented ) );
86 mCodeEditorJson->setText( jsonDocument.toJson( QJsonDocument::Compact ) );
89 mCodeEditorJson->setText( mJsonText );
93 mCodeEditorJson->blockSignals(
false );
95 refreshTreeView( jsonDocument );
109 mStackedWidget->setCurrentWidget( mStackedWidgetPageText );
110 mTextToolButton->setChecked(
true );
111 mTreeToolButton->setChecked(
false );
116 mStackedWidget->setCurrentWidget( mStackedWidgetPageTree );
117 mTreeToolButton->setChecked(
true );
118 mTextToolButton->setChecked(
false );
126 mFormatJsonMode = formatJson;
131 mControlsWidget->setVisible( visible );
134void QgsJsonEditWidget::textToolButtonClicked(
bool checked )
142void QgsJsonEditWidget::treeToolButtonClicked(
bool checked )
150void QgsJsonEditWidget::copyValueActionTriggered()
152 if ( !mTreeWidget->currentItem() )
155 const QJsonValue jsonValue = QJsonValue::fromVariant( mTreeWidget->currentItem()->data(
static_cast<int>( TreeWidgetColumn::Value ), Qt::UserRole ) );
157 switch ( jsonValue.type() )
159 case QJsonValue::Null:
160 case QJsonValue::Bool:
161 case QJsonValue::Double:
162 case QJsonValue::Undefined:
163 QApplication::clipboard()->setText( mTreeWidget->currentItem()->text(
static_cast<int>( TreeWidgetColumn::Value ) ) );
165 case QJsonValue::String:
166 QApplication::clipboard()->setText( jsonValue.toString() );
168 case QJsonValue::Array:
170 const QJsonDocument jsonDocument( jsonValue.toArray() );
171 QApplication::clipboard()->setText( jsonDocument.toJson() );
174 case QJsonValue::Object:
176 const QJsonDocument jsonDocument( jsonValue.toObject() );
177 QApplication::clipboard()->setText( jsonDocument.toJson() );
183void QgsJsonEditWidget::copyKeyActionTriggered()
185 if ( !mTreeWidget->currentItem() )
188 QApplication::clipboard()->setText( mTreeWidget->currentItem()->text(
static_cast<int>( TreeWidgetColumn::Key ) ) );
191void QgsJsonEditWidget::codeEditorJsonTextChanged()
193 mJsonText = mCodeEditorJson->text();
194 const QJsonDocument jsonDocument = QJsonDocument::fromJson( mJsonText.toUtf8() );
195 refreshTreeView( jsonDocument );
198void QgsJsonEditWidget::codeEditorJsonIndicatorClicked(
int line,
int index, Qt::KeyboardModifiers state )
200 if ( !state.testFlag( Qt::ControlModifier ) )
203 const int position = mCodeEditorJson->positionFromLineIndex( line, index );
204 const int clickableLinkListIndex = mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORVALUEAT,
205 SCINTILLA_UNDERLINE_INDICATOR_INDEX,
207 if ( clickableLinkListIndex <= 0 )
210 QDesktopServices::openUrl( mClickableLinkList.at( clickableLinkListIndex - 1 ) );
213void QgsJsonEditWidget::codeEditorJsonDwellStart(
int position,
int x,
int y )
218 const int clickableLinkListIndex = mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORVALUEAT,
219 SCINTILLA_UNDERLINE_INDICATOR_INDEX,
221 if ( clickableLinkListIndex <= 0 )
224 QToolTip::showText( QCursor::pos(),
225 tr(
"%1\nCTRL + click to follow link" ).arg( mClickableLinkList.at( clickableLinkListIndex - 1 ) ) );
228void QgsJsonEditWidget::codeEditorJsonDwellEnd(
int position,
int x,
int y )
233 QToolTip::hideText();
236void QgsJsonEditWidget::refreshTreeView(
const QJsonDocument &jsonDocument )
238 mTreeWidget->clear();
240 if ( jsonDocument.isNull() )
243 mTextToolButton->setDisabled(
true );
244 mTreeToolButton->setDisabled(
true );
245 mTreeToolButton->setToolTip( tr(
"Invalid JSON, tree view not available" ) );
250 mTextToolButton->setEnabled(
true );
251 mTreeToolButton->setEnabled(
true );
252 mTreeToolButton->setToolTip( tr(
"Tree view" ) );
255 if ( jsonDocument.isObject() )
257 const QStringList keys = jsonDocument.object().keys();
258 for (
const QString &key : keys )
260 const QJsonValue jsonValue = jsonDocument.object().value( key );
261 QTreeWidgetItem *treeWidgetItem =
new QTreeWidgetItem( mTreeWidget, QStringList() << key );
262 treeWidgetItem->setFont( 0, monospaceFont() );
263 refreshTreeViewItem( treeWidgetItem, jsonValue );
264 mTreeWidget->addTopLevelItem( treeWidgetItem );
265 mTreeWidget->expandItem( treeWidgetItem );
268 else if ( jsonDocument.isArray() )
270 const QJsonArray array = jsonDocument.array();
271 const auto arraySize = array.size();
275 constexpr decltype( arraySize ) MAX_ELTS = 200;
278 if ( arraySize > MAX_ELTS )
279 mEnableUrlHighlighting =
false;
280 for (
auto index =
decltype( arraySize ) {0}; index < arraySize; index++ )
282 QTreeWidgetItem *treeWidgetItem =
new QTreeWidgetItem( mTreeWidget, QStringList() << QString::number( index ) );
283 treeWidgetItem->setFont( 0, monospaceFont() );
284 if ( arraySize <= MAX_ELTS || ( index < MAX_ELTS / 2 || index + MAX_ELTS / 2 > arraySize ) )
286 refreshTreeViewItem( treeWidgetItem, array.at( index ) );
287 mTreeWidget->addTopLevelItem( treeWidgetItem );
288 mTreeWidget->expandItem( treeWidgetItem );
290 else if ( index == MAX_ELTS / 2 )
292 index = arraySize - MAX_ELTS / 2;
293 refreshTreeViewItem( treeWidgetItem, tr(
"... truncated ..." ) );
294 mTreeWidget->addTopLevelItem( treeWidgetItem );
295 mTreeWidget->expandItem( treeWidgetItem );
300 mTreeWidget->resizeColumnToContents(
static_cast<int>( TreeWidgetColumn::Key ) );
303void QgsJsonEditWidget::refreshTreeViewItem( QTreeWidgetItem *treeWidgetItem,
const QJsonValue &jsonValue )
305 treeWidgetItem->setData(
static_cast<int>( TreeWidgetColumn::Value ), Qt::UserRole, jsonValue.toVariant() );
307 switch ( jsonValue.type() )
309 case QJsonValue::Null:
310 refreshTreeViewItemValue( treeWidgetItem,
311 QStringLiteral(
"null" ),
314 case QJsonValue::Bool:
315 refreshTreeViewItemValue( treeWidgetItem,
316 jsonValue.toBool() ? QStringLiteral(
"true" ) : QStringLiteral(
"false" ),
319 case QJsonValue::Double:
320 refreshTreeViewItemValue( treeWidgetItem,
321 QString::number( jsonValue.toDouble() ),
324 case QJsonValue::String:
326 const QString jsonValueString = jsonValue.toString();
327 if ( !mEnableUrlHighlighting || QUrl( jsonValueString ).scheme().isEmpty() )
329 refreshTreeViewItemValue( treeWidgetItem,
335 QLabel *label =
new QLabel( QString(
"<a href='%1'>%1</a>" ).arg( jsonValueString ) );
336 label->setOpenExternalLinks(
true );
337 label->setFont( monospaceFont() );
338 mTreeWidget->setItemWidget( treeWidgetItem,
static_cast<int>( TreeWidgetColumn::Value ), label );
340 mClickableLinkList.append( jsonValueString );
341 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_SETINDICATORVALUE,
static_cast< int >( mClickableLinkList.size() ) );
342 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORFILLRANGE,
343 mCodeEditorJson->text().indexOf( jsonValueString ),
344 jsonValueString.size() );
348 case QJsonValue::Array:
350 const QJsonArray jsonArray = jsonValue.toArray();
351 const auto arraySize = jsonArray.size();
355 constexpr decltype( arraySize ) MAX_ELTS = 200;
358 if ( arraySize > MAX_ELTS )
359 mEnableUrlHighlighting =
false;
360 for (
auto index =
decltype( arraySize ) {0}; index < arraySize; index++ )
362 QTreeWidgetItem *treeWidgetItemChild =
new QTreeWidgetItem( treeWidgetItem, QStringList() << QString::number( index ) );
363 treeWidgetItemChild->setFont( 0, monospaceFont() );
364 if ( arraySize <= MAX_ELTS || ( index < MAX_ELTS / 2 || index + MAX_ELTS / 2 > arraySize ) )
366 refreshTreeViewItem( treeWidgetItemChild, jsonArray.at( index ) );
367 treeWidgetItem->addChild( treeWidgetItemChild );
368 treeWidgetItem->setExpanded(
true );
370 else if ( index == MAX_ELTS / 2 )
372 index = arraySize - MAX_ELTS / 2;
373 refreshTreeViewItem( treeWidgetItemChild, tr(
"... truncated ..." ) );
374 treeWidgetItem->addChild( treeWidgetItemChild );
375 treeWidgetItem->setExpanded(
true );
380 case QJsonValue::Object:
382 const QJsonObject jsonObject = jsonValue.toObject();
383 const QStringList keys = jsonObject.keys();
384 for (
const QString &key : keys )
386 QTreeWidgetItem *treeWidgetItemChild =
new QTreeWidgetItem( treeWidgetItem, QStringList() << key );
387 treeWidgetItemChild->setFont( 0, monospaceFont() );
388 refreshTreeViewItem( treeWidgetItemChild, jsonObject.value( key ) );
389 treeWidgetItem->addChild( treeWidgetItemChild );
390 treeWidgetItem->setExpanded(
true );
394 case QJsonValue::Undefined:
395 refreshTreeViewItemValue( treeWidgetItem,
396 QStringLiteral(
"Undefined value" ),
402void QgsJsonEditWidget::refreshTreeViewItemValue( QTreeWidgetItem *treeWidgetItem,
const QString &jsonValueString,
const QColor &textColor )
404 QLabel *label =
new QLabel( jsonValueString );
405 label->setFont( monospaceFont() );
407 if ( textColor.isValid() )
408 label->setStyleSheet( QStringLiteral(
"color: %1;" ).arg( textColor.name() ) );
409 mTreeWidget->setItemWidget( treeWidgetItem,
static_cast<int>( TreeWidgetColumn::Value ), label );
412QFont QgsJsonEditWidget::monospaceFont()
const
416 f.setPointSize( font().pointSize() );
Defines a color scheme for use in QgsCodeEditor widgets.
@ DoubleQuote
Double quote color.
A JSON editor based on QScintilla2.
A text editor based on QScintilla2.
static QFont getMonospaceFont()
Returns the monospaced font to use for code editors.
static QColor color(QgsCodeEditorColorScheme::ColorRole role)
Returns the color to use in the editor for the specified role.