diff --git a/src/EditDialog.cpp b/src/EditDialog.cpp index 5659fe5b..708f29cc 100644 --- a/src/EditDialog.cpp +++ b/src/EditDialog.cpp @@ -11,6 +11,7 @@ #include #include #include +#include EditDialog::EditDialog(QWidget* parent) : QDialog(parent), @@ -163,11 +164,19 @@ void EditDialog::loadData(const QByteArray& data) case JsonEditor: // The JSON widget buffer is now the main data source dataSource = JsonBuffer; + { + QJsonDocument jsonDoc = QJsonDocument::fromJson(QByteArray(data.constData(), data.size())); - // Load the text into the text editor - textData = QString::fromUtf8(data.constData(), data.size()); - jsonEdit->setText(textData); - + if (mustIndentAndCompact && !jsonDoc.isNull()) { + // Load indented JSON into the JSON editor + textData = QString(jsonDoc.toJson(QJsonDocument::Indented)); + jsonEdit->setText(textData); + } else { + // Fallback case. The data is not yet valid JSON. + textData = QString::fromUtf8(data.constData(), data.size()); + jsonEdit->setText(textData); + } + } // Select all of the text by default jsonEdit->selectAll(); @@ -407,7 +416,15 @@ void EditDialog::accept() } else { // It's not NULL, so proceed with normal text string checking QString oldData = currentIndex.data(Qt::EditRole).toString(); - QString newData = jsonEdit->text(); + + QString newData; + QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonEdit->text().toUtf8()); + if (mustIndentAndCompact && !jsonDoc.isNull()) + // Compact the JSON data before storing + newData = QString(jsonDoc.toJson(QJsonDocument::Compact)); + else + newData = jsonEdit->text(); + if (oldData != newData) // The data is different, so commit it back to the database emit recordTextUpdated(currentIndex, newData.toUtf8(), false); @@ -436,8 +453,18 @@ void EditDialog::editModeChanged(int newMode) break; case JsonEditor: // Switching to the JSON editor + // Convert the text widget buffer for the JSON widget - jsonEdit->setText(ui->editorText->toPlainText().toUtf8()); + // * If the dataSource is the TextBuffer, the contents could + // be still compacted so we just pass it to our loadData() + // function to handle, for indenting if necessary * + // Switch to the selected editor first, as loadData() relies + // on it being current + ui->editorStack->setCurrentIndex(newMode); + + // Load the data into the appropriate widget, as done by loadData() + loadData(ui->editorText->toPlainText().toUtf8()); + // jsonEdit->setText(ui->editorText->toPlainText().toUtf8()); // The JSON widget buffer is now the main data source dataSource = JsonBuffer; @@ -671,4 +698,6 @@ void EditDialog::reloadSettings() hexEdit->setFont(hexFont); jsonEdit->reloadSettings(); + + mustIndentAndCompact = Settings::getValue("databrowser", "indent_compact").toBool(); } diff --git a/src/EditDialog.h b/src/EditDialog.h index 49802133..be784ef9 100644 --- a/src/EditDialog.h +++ b/src/EditDialog.h @@ -55,6 +55,7 @@ private: int dataType; bool textNullSet; bool isReadOnly; + bool mustIndentAndCompact; enum DataSources { TextBuffer, diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index dfdb0235..50c940fc 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -92,6 +92,7 @@ void PreferencesDialog::loadSettings() loadColorSetting(ui->fr_bin_bg, "bin_bg"); ui->spinSymbolLimit->setValue(Settings::getValue("databrowser", "symbol_limit").toInt()); + ui->checkIndentCompact->setChecked(Settings::getValue("databrowser", "indent_compact").toBool()); ui->txtNull->setText(Settings::getValue("databrowser", "null_text").toString()); ui->editFilterEscape->setText(Settings::getValue("databrowser", "filter_escape").toString()); ui->spinFilterDelay->setValue(Settings::getValue("databrowser", "filter_delay").toInt()); @@ -193,6 +194,7 @@ void PreferencesDialog::saveSettings() saveColorSetting(ui->fr_bin_fg, "bin_fg"); saveColorSetting(ui->fr_bin_bg, "bin_bg"); Settings::setValue("databrowser", "symbol_limit", ui->spinSymbolLimit->value()); + Settings::setValue("databrowser", "indent_compact", ui->checkIndentCompact->isChecked()); Settings::setValue("databrowser", "null_text", ui->txtNull->text()); Settings::setValue("databrowser", "filter_escape", ui->editFilterEscape->text()); Settings::setValue("databrowser", "filter_delay", ui->spinFilterDelay->value()); diff --git a/src/PreferencesDialog.ui b/src/PreferencesDialog.ui index 080b6413..156802f0 100644 --- a/src/PreferencesDialog.ui +++ b/src/PreferencesDialog.ui @@ -343,7 +343,7 @@ - Font si&ze: + Font si&ze spinDataBrowserFontSize @@ -385,6 +385,20 @@ + + + + Indent text on loading. Compact on saving. + + + + + + + JSON cell editor + + + diff --git a/src/Settings.cpp b/src/Settings.cpp index 94137820..ae42dc5d 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -168,6 +168,8 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name) return 10; if(name == "symbol_limit") return 5000; + if(name == "compact_indent") + return false; if(name == "null_text") return "NULL"; if(name == "filter_escape")