Skip to content

Commit

Permalink
Cell editor fixes and improvements
Browse files Browse the repository at this point in the history
Valid JSON is detected as a new first-class data type. See issue #1173.

Text data length is always calculated in characters and not in bytes (UTF-8
gives different results).

Text in read-only mode is also selectable by keyboard and the caret is
visible. See issue #1123
  • Loading branch information
mgrojo committed Dec 5, 2017
1 parent 8fbf44b commit 678079a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
43 changes: 34 additions & 9 deletions src/EditDialog.cpp
Expand Up @@ -144,6 +144,8 @@ void EditDialog::loadData(const QByteArray& data)
break;

case Text:
case JSON:

// Set enabled any of the text widgets
ui->editorText->setEnabled(true);
jsonEdit->setEnabled(true);
Expand Down Expand Up @@ -571,7 +573,11 @@ void EditDialog::editTextChanged()

// Update the cell info in the bottom left manually. This is because
// updateCellInfo() only works with QByteArray's (for now)
int dataLength = ui->editorText->toPlainText().length();
int dataLength;
if (dataSource == TextBuffer)
dataLength = ui->editorText->toPlainText().length();
else
dataLength = jsonEdit->text().length();
ui->labelType->setText(tr("Type of data currently in cell: Text / Numeric"));
ui->labelSize->setText(tr("%n char(s)", "", dataLength));
}
Expand All @@ -596,7 +602,12 @@ int EditDialog::checkDataType(const QByteArray& data)

// Check if it's text only
if (QString(cellData).toUtf8() == cellData) { // Is there a better way to check this?
return Text;

QJsonDocument jsonDoc = QJsonDocument::fromJson(QString(cellData).toUtf8());
if (!jsonDoc.isNull())
return JSON;
else
return Text;
}

// It's none of the above, so treat it as general binary data
Expand Down Expand Up @@ -634,7 +645,11 @@ void EditDialog::setReadOnly(bool ro)
ui->buttonApply->setEnabled(!ro);
ui->buttonNull->setEnabled(!ro);
ui->buttonImport->setEnabled(!ro);

ui->editorText->setReadOnly(ro);
Qt::TextInteractionFlags textFlags = ro? Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard : Qt::TextEditorInteraction;
ui->editorText->setTextInteractionFlags(textFlags);

ui->editorBinary->setEnabled(!ro); // We disable the entire hex editor here instead of setting it to read only because it doesn't have a setReadOnly() method
jsonEdit->setReadOnly(ro);
}
Expand Down Expand Up @@ -665,24 +680,34 @@ void EditDialog::updateCellInfo(const QByteArray& data)
return;
}

// Determine the length of the cell data
int dataLength = cellData.length();

// Use a switch statement for the other data types to keep things neat :)
switch (dataType) {
case Null:
// NULL data type
ui->labelType->setText(tr("Type of data currently in cell: NULL"));
ui->labelSize->setText(tr("%n byte(s)", "", dataLength));
ui->labelSize->setText(tr("%n byte(s)", "", 0));
break;

case Text:
case Text: {
// Text only
// Determine the length of the cell text in characters (possibly different to number of bytes).
int textLength = QString(cellData).length();
ui->labelType->setText(tr("Type of data currently in cell: Text / Numeric"));
ui->labelSize->setText(tr("%n char(s)", "", dataLength));
ui->labelSize->setText(tr("%n char(s)", "", textLength));
break;

}
case JSON: {
// Valid JSON
// Determine the length of the cell text in characters (possibly different to number of bytes).
int jsonLength = QString(cellData).length();
ui->labelType->setText(tr("Type of data currently in cell: Valid JSON"));
ui->labelSize->setText(tr("%n char(s)", "", jsonLength));
break;
}
default:

// Determine the length of the cell data
int dataLength = cellData.length();
// If none of the above data types, consider it general binary data
ui->labelType->setText(tr("Type of data currently in cell: Binary"));
ui->labelSize->setText(tr("%n byte(s)", "", dataLength));
Expand Down
3 changes: 2 additions & 1 deletion src/EditDialog.h
Expand Up @@ -67,7 +67,8 @@ private slots:
Binary,
Image,
Null,
Text
Text,
JSON
};

enum EditModes {
Expand Down

0 comments on commit 678079a

Please sign in to comment.