17#include "moc_qgsjsoneditwidget.cpp"
21#include <QDesktopServices>
30 , mCopyValueAction( new QAction( tr(
"Copy Value" ), this ) )
31 , mCopyKeyAction( new QAction( tr(
"Copy Key" ), this ) )
37 mCodeEditorJson->setReadOnly(
true );
38 mCodeEditorJson->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
39 mCodeEditorJson->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
40 mCodeEditorJson->indicatorDefine( QsciScintilla::PlainIndicator, SCINTILLA_UNDERLINE_INDICATOR_INDEX );
41 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_SETINDICATORCURRENT, SCINTILLA_UNDERLINE_INDICATOR_INDEX );
42 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_SETMOUSEDWELLTIME, 400 );
44 mTreeWidget->setContextMenuPolicy( Qt::ActionsContextMenu );
45 mTreeWidget->addAction( mCopyValueAction );
46 mTreeWidget->addAction( mCopyKeyAction );
48 connect( mTextToolButton, &QToolButton::clicked,
this, &QgsJsonEditWidget::textToolButtonClicked );
49 connect( mTreeToolButton, &QToolButton::clicked,
this, &QgsJsonEditWidget::treeToolButtonClicked );
51 connect( mCopyValueAction, &QAction::triggered,
this, &QgsJsonEditWidget::copyValueActionTriggered );
52 connect( mCopyKeyAction, &QAction::triggered,
this, &QgsJsonEditWidget::copyKeyActionTriggered );
54 connect( mCodeEditorJson, &QgsCodeEditorJson::textChanged,
this, &QgsJsonEditWidget::codeEditorJsonTextChanged );
57 connect( mCodeEditorJson, &QsciScintilla::indicatorClicked,
this, &QgsJsonEditWidget::codeEditorJsonIndicatorClicked );
58 connect( mCodeEditorJson, &QsciScintillaBase::SCN_DWELLSTART,
this, &QgsJsonEditWidget::codeEditorJsonDwellStart );
59 connect( mCodeEditorJson, &QsciScintillaBase::SCN_DWELLEND,
this, &QgsJsonEditWidget::codeEditorJsonDwellEnd );
64 return mCodeEditorJson;
70 mClickableLinkList.clear();
72 const QJsonDocument jsonDocument = QJsonDocument::fromJson( mJsonText.toUtf8() );
74 mCodeEditorJson->blockSignals(
true );
75 if ( jsonDocument.isNull() )
77 mCodeEditorJson->setText( mJsonText );
81 switch ( mFormatJsonMode )
84 mCodeEditorJson->setText( jsonDocument.toJson( QJsonDocument::Indented ) );
87 mCodeEditorJson->setText( jsonDocument.toJson( QJsonDocument::Compact ) );
90 mCodeEditorJson->setText( mJsonText );
94 mCodeEditorJson->blockSignals(
false );
96 refreshTreeView( jsonDocument );
110 mStackedWidget->setCurrentWidget( mStackedWidgetPageText );
111 mTextToolButton->setChecked(
true );
112 mTreeToolButton->setChecked(
false );
117 mStackedWidget->setCurrentWidget( mStackedWidgetPageTree );
118 mTreeToolButton->setChecked(
true );
119 mTextToolButton->setChecked(
false );
127 mFormatJsonMode = formatJson;
132 mControlsWidget->setVisible( visible );
135void QgsJsonEditWidget::textToolButtonClicked(
bool checked )
143void QgsJsonEditWidget::treeToolButtonClicked(
bool checked )
151void QgsJsonEditWidget::copyValueActionTriggered()
153 if ( !mTreeWidget->currentItem() )
156 const QJsonValue jsonValue = QJsonValue::fromVariant( mTreeWidget->currentItem()->data(
static_cast<int>( TreeWidgetColumn::Value ), Qt::UserRole ) );
158 switch ( jsonValue.type() )
160 case QJsonValue::Null:
161 case QJsonValue::Bool:
162 case QJsonValue::Double:
163 case QJsonValue::Undefined:
164 QApplication::clipboard()->setText( mTreeWidget->currentItem()->text(
static_cast<int>( TreeWidgetColumn::Value ) ) );
166 case QJsonValue::String:
167 QApplication::clipboard()->setText( jsonValue.toString() );
169 case QJsonValue::Array:
171 const QJsonDocument jsonDocument( jsonValue.toArray() );
172 QApplication::clipboard()->setText( jsonDocument.toJson() );
175 case QJsonValue::Object:
177 const QJsonDocument jsonDocument( jsonValue.toObject() );
178 QApplication::clipboard()->setText( jsonDocument.toJson() );
184void QgsJsonEditWidget::copyKeyActionTriggered()
186 if ( !mTreeWidget->currentItem() )
189 QApplication::clipboard()->setText( mTreeWidget->currentItem()->text(
static_cast<int>( TreeWidgetColumn::Key ) ) );
192void QgsJsonEditWidget::codeEditorJsonTextChanged()
194 mJsonText = mCodeEditorJson->text();
195 const QJsonDocument jsonDocument = QJsonDocument::fromJson( mJsonText.toUtf8() );
196 refreshTreeView( jsonDocument );
199void QgsJsonEditWidget::codeEditorJsonIndicatorClicked(
int line,
int index, Qt::KeyboardModifiers state )
201 if ( !state.testFlag( Qt::ControlModifier ) )
204 const int position = mCodeEditorJson->positionFromLineIndex( line, index );
205 const int clickableLinkListIndex = mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORVALUEAT,
206 SCINTILLA_UNDERLINE_INDICATOR_INDEX,
208 if ( clickableLinkListIndex <= 0 )
211 QDesktopServices::openUrl( mClickableLinkList.at( clickableLinkListIndex - 1 ) );
214void QgsJsonEditWidget::codeEditorJsonDwellStart(
int position,
int x,
int y )
219 const int clickableLinkListIndex = mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORVALUEAT,
220 SCINTILLA_UNDERLINE_INDICATOR_INDEX,
222 if ( clickableLinkListIndex <= 0 )
225 QToolTip::showText( QCursor::pos(),
226 tr(
"%1\nCTRL + click to follow link" ).arg( mClickableLinkList.at( clickableLinkListIndex - 1 ) ) );
229void QgsJsonEditWidget::codeEditorJsonDwellEnd(
int position,
int x,
int y )
234 QToolTip::hideText();
237void QgsJsonEditWidget::refreshTreeView(
const QJsonDocument &jsonDocument )
239 mTreeWidget->clear();
241 if ( jsonDocument.isNull() )
244 mTextToolButton->setDisabled(
true );
245 mTreeToolButton->setDisabled(
true );
246 mTreeToolButton->setToolTip( tr(
"Invalid JSON, tree view not available" ) );
251 mTextToolButton->setEnabled(
true );
252 mTreeToolButton->setEnabled(
true );
253 mTreeToolButton->setToolTip( tr(
"Tree view" ) );
256 if ( jsonDocument.isObject() )
258 const QStringList keys = jsonDocument.object().keys();
259 for (
const QString &key : keys )
261 const QJsonValue jsonValue = jsonDocument.object().value( key );
262 QTreeWidgetItem *treeWidgetItem =
new QTreeWidgetItem( mTreeWidget, QStringList() << key );
263 treeWidgetItem->setFont( 0, monospaceFont() );
264 refreshTreeViewItem( treeWidgetItem, jsonValue );
265 mTreeWidget->addTopLevelItem( treeWidgetItem );
266 mTreeWidget->expandItem( treeWidgetItem );
269 else if ( jsonDocument.isArray() )
271 const QJsonArray array = jsonDocument.array();
272 const auto arraySize = array.size();
276 constexpr decltype( arraySize ) MAX_ELTS = 200;
279 if ( arraySize > MAX_ELTS )
280 mEnableUrlHighlighting =
false;
281 for (
auto index =
decltype( arraySize ) {0}; index < arraySize; index++ )
283 QTreeWidgetItem *treeWidgetItem =
new QTreeWidgetItem( mTreeWidget, QStringList() << QString::number( index ) );
284 treeWidgetItem->setFont( 0, monospaceFont() );
285 if ( arraySize <= MAX_ELTS || ( index < MAX_ELTS / 2 || index + MAX_ELTS / 2 > arraySize ) )
287 refreshTreeViewItem( treeWidgetItem, array.at( index ) );
288 mTreeWidget->addTopLevelItem( treeWidgetItem );
289 mTreeWidget->expandItem( treeWidgetItem );
291 else if ( index == MAX_ELTS / 2 )
293 index = arraySize - MAX_ELTS / 2;
294 refreshTreeViewItem( treeWidgetItem, tr(
"... truncated ..." ) );
295 mTreeWidget->addTopLevelItem( treeWidgetItem );
296 mTreeWidget->expandItem( treeWidgetItem );
301 mTreeWidget->resizeColumnToContents(
static_cast<int>( TreeWidgetColumn::Key ) );
304void QgsJsonEditWidget::refreshTreeViewItem( QTreeWidgetItem *treeWidgetItem,
const QJsonValue &jsonValue )
306 treeWidgetItem->setData(
static_cast<int>( TreeWidgetColumn::Value ), Qt::UserRole, jsonValue.toVariant() );
308 switch ( jsonValue.type() )
310 case QJsonValue::Null:
311 refreshTreeViewItemValue( treeWidgetItem,
312 QStringLiteral(
"null" ),
315 case QJsonValue::Bool:
316 refreshTreeViewItemValue( treeWidgetItem,
317 jsonValue.toBool() ? QStringLiteral(
"true" ) : QStringLiteral(
"false" ),
320 case QJsonValue::Double:
321 refreshTreeViewItemValue( treeWidgetItem,
322 QString::number( jsonValue.toDouble() ),
325 case QJsonValue::String:
327 const QString jsonValueString = jsonValue.toString();
328 if ( !mEnableUrlHighlighting || QUrl( jsonValueString ).scheme().isEmpty() )
330 refreshTreeViewItemValue( treeWidgetItem,
336 QLabel *label =
new QLabel( QString(
"<a href='%1'>%1</a>" ).arg( jsonValueString ) );
337 label->setOpenExternalLinks(
true );
338 label->setFont( monospaceFont() );
339 mTreeWidget->setItemWidget( treeWidgetItem,
static_cast<int>( TreeWidgetColumn::Value ), label );
341 mClickableLinkList.append( jsonValueString );
342 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_SETINDICATORVALUE,
static_cast< int >( mClickableLinkList.size() ) );
343 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORFILLRANGE,
344 mCodeEditorJson->text().indexOf( jsonValueString ),
345 jsonValueString.size() );
349 case QJsonValue::Array:
351 const QJsonArray jsonArray = jsonValue.toArray();
352 const auto arraySize = jsonArray.size();
356 constexpr decltype( arraySize ) MAX_ELTS = 200;
359 if ( arraySize > MAX_ELTS )
360 mEnableUrlHighlighting =
false;
361 for (
auto index =
decltype( arraySize ) {0}; index < arraySize; index++ )
363 QTreeWidgetItem *treeWidgetItemChild =
new QTreeWidgetItem( treeWidgetItem, QStringList() << QString::number( index ) );
364 treeWidgetItemChild->setFont( 0, monospaceFont() );
365 if ( arraySize <= MAX_ELTS || ( index < MAX_ELTS / 2 || index + MAX_ELTS / 2 > arraySize ) )
367 refreshTreeViewItem( treeWidgetItemChild, jsonArray.at( index ) );
368 treeWidgetItem->addChild( treeWidgetItemChild );
369 treeWidgetItem->setExpanded(
true );
371 else if ( index == MAX_ELTS / 2 )
373 index = arraySize - MAX_ELTS / 2;
374 refreshTreeViewItem( treeWidgetItemChild, tr(
"... truncated ..." ) );
375 treeWidgetItem->addChild( treeWidgetItemChild );
376 treeWidgetItem->setExpanded(
true );
381 case QJsonValue::Object:
383 const QJsonObject jsonObject = jsonValue.toObject();
384 const QStringList keys = jsonObject.keys();
385 for (
const QString &key : keys )
387 QTreeWidgetItem *treeWidgetItemChild =
new QTreeWidgetItem( treeWidgetItem, QStringList() << key );
388 treeWidgetItemChild->setFont( 0, monospaceFont() );
389 refreshTreeViewItem( treeWidgetItemChild, jsonObject.value( key ) );
390 treeWidgetItem->addChild( treeWidgetItemChild );
391 treeWidgetItem->setExpanded(
true );
395 case QJsonValue::Undefined:
396 refreshTreeViewItemValue( treeWidgetItem,
397 QStringLiteral(
"Undefined value" ),
403void QgsJsonEditWidget::refreshTreeViewItemValue( QTreeWidgetItem *treeWidgetItem,
const QString &jsonValueString,
const QColor &textColor )
405 QLabel *label =
new QLabel( jsonValueString );
406 label->setFont( monospaceFont() );
408 if ( textColor.isValid() )
409 label->setStyleSheet( QStringLiteral(
"color: %1;" ).arg( textColor.name() ) );
410 mTreeWidget->setItemWidget( treeWidgetItem,
static_cast<int>( TreeWidgetColumn::Value ), label );
413QFont QgsJsonEditWidget::monospaceFont()
const
417 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.