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 );
45 mTreeWidget->setContextMenuPolicy( Qt::ActionsContextMenu );
46 mTreeWidget->addAction( mCopyValueAction );
47 mTreeWidget->addAction( mCopyKeyAction );
49 connect( mTextToolButton, &QToolButton::clicked,
this, &QgsJsonEditWidget::textToolButtonClicked );
50 connect( mTreeToolButton, &QToolButton::clicked,
this, &QgsJsonEditWidget::treeToolButtonClicked );
52 connect( mCopyValueAction, &QAction::triggered,
this, &QgsJsonEditWidget::copyValueActionTriggered );
53 connect( mCopyKeyAction, &QAction::triggered,
this, &QgsJsonEditWidget::copyKeyActionTriggered );
55 connect( mCodeEditorJson, &QgsCodeEditorJson::textChanged,
this, &QgsJsonEditWidget::codeEditorJsonTextChanged );
58 connect( mCodeEditorJson, &QsciScintilla::indicatorClicked,
this, &QgsJsonEditWidget::codeEditorJsonIndicatorClicked );
59 connect( mCodeEditorJson, &QsciScintillaBase::SCN_DWELLSTART,
this, &QgsJsonEditWidget::codeEditorJsonDwellStart );
60 connect( mCodeEditorJson, &QsciScintillaBase::SCN_DWELLEND,
this, &QgsJsonEditWidget::codeEditorJsonDwellEnd );
65 return mCodeEditorJson;
71 mClickableLinkList.clear();
73 const QJsonDocument jsonDocument = QJsonDocument::fromJson( mJsonText.toUtf8() );
75 mCodeEditorJson->blockSignals(
true );
76 if ( jsonDocument.isNull() )
78 mCodeEditorJson->setText( mJsonText );
82 switch ( mFormatJsonMode )
85 mCodeEditorJson->setText( jsonDocument.toJson( QJsonDocument::Indented ) );
88 mCodeEditorJson->setText( jsonDocument.toJson( QJsonDocument::Compact ) );
91 mCodeEditorJson->setText( mJsonText );
95 mCodeEditorJson->blockSignals(
false );
97 refreshTreeView( jsonDocument );
111 mStackedWidget->setCurrentWidget( mStackedWidgetPageText );
112 mTextToolButton->setChecked(
true );
113 mTreeToolButton->setChecked(
false );
118 mStackedWidget->setCurrentWidget( mStackedWidgetPageTree );
119 mTreeToolButton->setChecked(
true );
120 mTextToolButton->setChecked(
false );
128 mFormatJsonMode = formatJson;
133 mControlsWidget->setVisible( visible );
136void QgsJsonEditWidget::textToolButtonClicked(
bool checked )
144void QgsJsonEditWidget::treeToolButtonClicked(
bool checked )
152void QgsJsonEditWidget::copyValueActionTriggered()
154 if ( !mTreeWidget->currentItem() )
157 const QJsonValue jsonValue = QJsonValue::fromVariant( mTreeWidget->currentItem()->data(
static_cast<int>( TreeWidgetColumn::Value ), Qt::UserRole ) );
159 switch ( jsonValue.type() )
161 case QJsonValue::Null:
162 case QJsonValue::Bool:
163 case QJsonValue::Double:
164 case QJsonValue::Undefined:
165 QApplication::clipboard()->setText( mTreeWidget->currentItem()->text(
static_cast<int>( TreeWidgetColumn::Value ) ) );
167 case QJsonValue::String:
168 QApplication::clipboard()->setText( jsonValue.toString() );
170 case QJsonValue::Array:
172 const QJsonDocument jsonDocument( jsonValue.toArray() );
173 QApplication::clipboard()->setText( jsonDocument.toJson() );
176 case QJsonValue::Object:
178 const QJsonDocument jsonDocument( jsonValue.toObject() );
179 QApplication::clipboard()->setText( jsonDocument.toJson() );
185void QgsJsonEditWidget::copyKeyActionTriggered()
187 if ( !mTreeWidget->currentItem() )
190 QApplication::clipboard()->setText( mTreeWidget->currentItem()->text(
static_cast<int>( TreeWidgetColumn::Key ) ) );
193void QgsJsonEditWidget::codeEditorJsonTextChanged()
195 mJsonText = mCodeEditorJson->text();
196 const QJsonDocument jsonDocument = QJsonDocument::fromJson( mJsonText.toUtf8() );
197 refreshTreeView( jsonDocument );
200void QgsJsonEditWidget::codeEditorJsonIndicatorClicked(
int line,
int index, Qt::KeyboardModifiers state )
202 if ( !state.testFlag( Qt::ControlModifier ) )
205 const int position = mCodeEditorJson->positionFromLineIndex( line, index );
206 const int clickableLinkListIndex = mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORVALUEAT,
207 SCINTILLA_UNDERLINE_INDICATOR_INDEX,
209 if ( clickableLinkListIndex <= 0 )
212 QDesktopServices::openUrl( mClickableLinkList.at( clickableLinkListIndex - 1 ) );
215void QgsJsonEditWidget::codeEditorJsonDwellStart(
int position,
int x,
int y )
220 const int clickableLinkListIndex = mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORVALUEAT,
221 SCINTILLA_UNDERLINE_INDICATOR_INDEX,
223 if ( clickableLinkListIndex <= 0 )
226 QToolTip::showText( QCursor::pos(),
227 tr(
"%1\nCTRL + click to follow link" ).arg( mClickableLinkList.at( clickableLinkListIndex - 1 ) ) );
230void QgsJsonEditWidget::codeEditorJsonDwellEnd(
int position,
int x,
int y )
235 QToolTip::hideText();
238void QgsJsonEditWidget::refreshTreeView(
const QJsonDocument &jsonDocument )
240 mTreeWidget->clear();
242 if ( jsonDocument.isNull() )
245 mTextToolButton->setDisabled(
true );
246 mTreeToolButton->setDisabled(
true );
247 mTreeToolButton->setToolTip( tr(
"Invalid JSON, tree view not available" ) );
252 mTextToolButton->setEnabled(
true );
253 mTreeToolButton->setEnabled(
true );
254 mTreeToolButton->setToolTip( tr(
"Tree view" ) );
257 if ( jsonDocument.isObject() )
259 const QStringList keys = jsonDocument.object().keys();
260 for (
const QString &key : keys )
262 const QJsonValue jsonValue = jsonDocument.object().value( key );
263 QTreeWidgetItem *treeWidgetItem =
new QTreeWidgetItem( mTreeWidget, QStringList() << key );
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 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 mTreeWidget->setItemWidget( treeWidgetItem,
static_cast<int>( TreeWidgetColumn::Value ), label );
339 mClickableLinkList.append( jsonValueString );
340 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_SETINDICATORVALUE,
static_cast< int >( mClickableLinkList.size() ) );
341 mCodeEditorJson->SendScintilla( QsciScintillaBase::SCI_INDICATORFILLRANGE,
342 mCodeEditorJson->text().indexOf( jsonValueString ),
343 jsonValueString.size() );
347 case QJsonValue::Array:
349 const QJsonArray jsonArray = jsonValue.toArray();
350 const auto arraySize = jsonArray.size();
354 constexpr decltype( arraySize ) MAX_ELTS = 200;
357 if ( arraySize > MAX_ELTS )
358 mEnableUrlHighlighting =
false;
359 for (
auto index =
decltype( arraySize ) {0}; index < arraySize; index++ )
361 QTreeWidgetItem *treeWidgetItemChild =
new QTreeWidgetItem( treeWidgetItem, QStringList() << QString::number( index ) );
362 if ( arraySize <= MAX_ELTS || ( index < MAX_ELTS / 2 || index + MAX_ELTS / 2 > arraySize ) )
364 refreshTreeViewItem( treeWidgetItemChild, jsonArray.at( index ) );
365 treeWidgetItem->addChild( treeWidgetItemChild );
366 treeWidgetItem->setExpanded(
true );
368 else if ( index == MAX_ELTS / 2 )
370 index = arraySize - MAX_ELTS / 2;
371 refreshTreeViewItem( treeWidgetItemChild, tr(
"... truncated ..." ) );
372 treeWidgetItem->addChild( treeWidgetItemChild );
373 treeWidgetItem->setExpanded(
true );
378 case QJsonValue::Object:
380 const QJsonObject jsonObject = jsonValue.toObject();
381 const QStringList keys = jsonObject.keys();
382 for (
const QString &key : keys )
384 QTreeWidgetItem *treeWidgetItemChild =
new QTreeWidgetItem( treeWidgetItem, QStringList() << key );
385 refreshTreeViewItem( treeWidgetItemChild, jsonObject.value( key ) );
386 treeWidgetItem->addChild( treeWidgetItemChild );
387 treeWidgetItem->setExpanded(
true );
391 case QJsonValue::Undefined:
392 refreshTreeViewItemValue( treeWidgetItem,
393 QStringLiteral(
"Undefined value" ),
399void QgsJsonEditWidget::refreshTreeViewItemValue( QTreeWidgetItem *treeWidgetItem,
const QString &jsonValueString,
const QColor &textColor )
401 QLabel *label =
new QLabel( jsonValueString );
402 if ( textColor.isValid() )
403 label->setStyleSheet( QStringLiteral(
"color: %1;" ).arg( textColor.name() ) );
404 mTreeWidget->setItemWidget( treeWidgetItem,
static_cast<int>( TreeWidgetColumn::Value ), label );
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.