Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve value rendering #4723

Merged
merged 2 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/app/qmlutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <QApplication>
#include <QClipboard>
#include <QDateTime>
#include <QFileInfo>
#include <QDebug>
#include <QFileInfo>
#include <QtCharts/QDateTimeAxis>

#include "apputils.h"
Expand Down Expand Up @@ -105,12 +105,11 @@ QVariant QmlUtils::toUtf(const QVariant &value) {
}

QString QmlUtils::getPathFromUrl(const QUrl &url) {
return url.isLocalFile() ? url.toLocalFile() : url.path();
return url.isLocalFile() ? url.toLocalFile() : url.path();
}

bool QmlUtils::fileExists(const QString &path)
{
return QFileInfo::exists(path);
bool QmlUtils::fileExists(const QString &path) {
return QFileInfo::exists(path);
}

void QmlUtils::copyToClipboard(const QString &text) {
Expand Down Expand Up @@ -157,9 +156,17 @@ void QmlUtils::addNewValueToDynamicChart(QtCharts::QXYSeries *series,
}

QObject *QmlUtils::wrapLargeText(const QByteArray &text) {
// NOTE(u_glide): Use 150Kb chunks
auto w =
new ValueEditor::LargeTextWrappingModel(QString::fromUtf8(text), 153600);
// NOTE(u_glide): Use 150Kb chunks by default

int chunkSize = 153600;

// Work-around to prevent html corruption
if (text.startsWith("<pre")) {
chunkSize = text.size();
}

auto w = new ValueEditor::LargeTextWrappingModel(QString::fromUtf8(text),
chunkSize);
w->setParent(this);
return w;
}
Expand Down
90 changes: 33 additions & 57 deletions src/modules/value-editor/largetextmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,79 +1,55 @@
#include "largetextmodel.h"
#include <QDebug>

ValueEditor::LargeTextWrappingModel::LargeTextWrappingModel(const QString &text, uint chunkSize)
: m_chunkSize(chunkSize)
{
setText(text);
ValueEditor::LargeTextWrappingModel::LargeTextWrappingModel(const QString &text,
uint chunkSize)
: m_chunkSize(chunkSize) {
setText(text);
}

ValueEditor::LargeTextWrappingModel::~LargeTextWrappingModel()
{
}
ValueEditor::LargeTextWrappingModel::~LargeTextWrappingModel() {}

QHash<int, QByteArray> ValueEditor::LargeTextWrappingModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Qt::UserRole + 1] = "value";
return roles;
QHash<int, QByteArray> ValueEditor::LargeTextWrappingModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[Qt::UserRole + 1] = "value";
return roles;
}

int ValueEditor::LargeTextWrappingModel::rowCount(const QModelIndex &) const
{
return m_textRows.size();
int ValueEditor::LargeTextWrappingModel::rowCount(const QModelIndex &) const {
int size= m_text.size() / m_chunkSize +
(m_text.size() % m_chunkSize == 0 ? 0 : 1);

return size > 0 ? size : 1;
}

QVariant ValueEditor::LargeTextWrappingModel::data(const QModelIndex &index, int role) const
{
if (!isIndexValid(index))
return QVariant();
QVariant ValueEditor::LargeTextWrappingModel::data(const QModelIndex &index,
int role) const {
if (!isIndexValid(index)) return QVariant();

if (role == Qt::UserRole + 1) {
return m_textRows[index.row()];
}
if (role == Qt::UserRole + 1) {
return m_text.mid(index.row() * m_chunkSize, m_chunkSize);
}

return QVariant();
return QVariant();
}

void ValueEditor::LargeTextWrappingModel::setText(const QString &text)
{
m_textRows.reserve(text.size()/m_chunkSize);

for (uint chunkIndex=0; chunkIndex < text.size()/m_chunkSize + 1; chunkIndex++)
{
m_textRows.append(text.mid(chunkIndex * m_chunkSize, m_chunkSize));
}
void ValueEditor::LargeTextWrappingModel::setText(const QString &text) {
m_text = text;
}

void ValueEditor::LargeTextWrappingModel::cleanUp()
{
emit beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
m_textRows.clear();
emit endRemoveRows();
void ValueEditor::LargeTextWrappingModel::cleanUp() {
emit beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
m_text.clear();
emit endRemoveRows();
}

QString ValueEditor::LargeTextWrappingModel::getText()
{
QString result;
result.reserve(m_textRows.size() * m_chunkSize);

for(auto textRow : m_textRows)
{
result.append(textRow);
}

return result;
}
QString ValueEditor::LargeTextWrappingModel::getText() { return m_text; }

void ValueEditor::LargeTextWrappingModel::setTextChunk(uint row, QString text)
{
if (row < m_textRows.size()) {
m_textRows[row] = text;
emit dataChanged(createIndex(row, 0), createIndex(row, 0));
}
void ValueEditor::LargeTextWrappingModel::setTextChunk(uint row, QString text) {
m_text.replace(row * m_chunkSize, m_chunkSize, text);
}

bool ValueEditor::LargeTextWrappingModel::isIndexValid(const QModelIndex &index) const
{
return 0 <= index.row() && index.row() < rowCount();
bool ValueEditor::LargeTextWrappingModel::isIndexValid(
const QModelIndex &index) const {
return 0 <= index.row() && index.row() < rowCount();
}
44 changes: 22 additions & 22 deletions src/modules/value-editor/largetextmodel.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
#pragma once
#include <QAbstractListModel>
#include <QSharedPointer>
#include <QHash>
#include <QList>
#include <QSharedPointer>

namespace ValueEditor {

class LargeTextWrappingModel : public QAbstractListModel
{
// TODO(u_glide): Process out of memory exceptions
class LargeTextWrappingModel : public QAbstractListModel {
// TODO(u_glide): Process out of memory exceptions

Q_OBJECT
public:
LargeTextWrappingModel(const QString& text=QString(), uint chunkSize=10000);
Q_OBJECT
public:
LargeTextWrappingModel(const QString &text = QString(),
uint chunkSize = 10000);

~LargeTextWrappingModel();
~LargeTextWrappingModel();

QHash<int, QByteArray> roleNames() const;
QHash<int, QByteArray> roleNames() const;

int rowCount(const QModelIndex &parent= QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;

QVariant data(const QModelIndex &index, int role) const;
QVariant data(const QModelIndex &index, int role) const;

void setText(const QString& text);
void setText(const QString &text);

public slots:
void cleanUp();
public slots:
void cleanUp();

QString getText();
QString getText();

void setTextChunk(uint row, QString text);
void setTextChunk(uint row, QString text);

private:
bool isIndexValid(const QModelIndex &index) const;
private:
bool isIndexValid(const QModelIndex &index) const;

private:
uint m_chunkSize;
QList<QString> m_textRows;
private:
uint m_chunkSize;
QString m_text;
};

}
} // namespace ValueEditor
96 changes: 60 additions & 36 deletions src/qml/value-editor/editors/MultilineEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ColumnLayout
// init editor with empty model
textView.model = qmlUtils.wrapLargeText("")
textView.readOnly = false
textView.textFormat = TextEdit.PlainText
textView.textFormat = TextEdit.PlainText
}

function validationRule(raw)
Expand Down Expand Up @@ -174,35 +174,24 @@ ColumnLayout

uiBlocker.visible = true

formatter.instance.getFormatted(root.value, function (error, formatted, isReadOnly, format) {
if (formatter['name'] === 'JSON') {
jsonFormattingWorker.sendMessage(String(root.value))
} else {
formatter.instance.getFormatted(root.value, function (error, formatted, isReadOnly, format) {

function process(error, formatted, stub, format) {
if (error || !formatted) {
uiBlocker.visible = false
formatterSelector.currentIndex = isBin? 2 : 0 // Reset formatter to plain text
notification.showError(error || qsTranslate("RDM","Unknown formatter error (Empty response)"))
return
function process(error, formatted, stub, format) {
jsonFormattingWorker.processFormatted(error, formatted, stub, format)
}

textView.textFormat = (format === "html")
? TextEdit.RichText
: TextEdit.PlainText;

defaultFormatterSettings.defaultFormatterIndex = formatterSelector.currentIndex
textView.model = qmlUtils.wrapLargeText(formatted)
textView.readOnly = isReadOnly
root.isEdited = false
uiBlocker.visible = false
}

if (format === "json") {
textView.format = format
Formatters.json.getFormatted(formatted, process)
} else {
textView.format = format
process(error, formatted, isReadOnly, format);
}
})

if (format === "json") {
jsonFormattingWorker.sendMessage(String(formatted))
} else {
process(error, formatted, isReadOnly, format);
}
})
}
}

function reset() {
Expand All @@ -229,6 +218,36 @@ ColumnLayout
validationError.visible = false
}

WorkerScript {
id: jsonFormattingWorker

source: "./formatters/json-tools.js"
onMessage: {
textView.format = messageObject.format
processFormatted(messageObject.error, messageObject.formatted, messageObject.isReadOnly, messageObject.format);
}

function processFormatted(error, formatted, isReadOnly, format) {
if (error || !formatted) {
uiBlocker.visible = false
formatterSelector.currentIndex = isBin? 2 : 0 // Reset formatter to plain text
notification.showError(error || qsTranslate("RDM","Unknown formatter error (Empty response)"))
return
}

textView.textFormat = (format === "html")
? TextEdit.RichText
: TextEdit.PlainText;

defaultFormatterSettings.defaultFormatterIndex = formatterSelector.currentIndex
textView.model = qmlUtils.wrapLargeText(formatted)
textView.readOnly = isReadOnly
root.isEdited = false
uiBlocker.visible = false
}
}


RowLayout{
Layout.fillWidth: true

Expand Down Expand Up @@ -303,21 +322,26 @@ ColumnLayout
property string format

delegate:
NewTextArea {
id: textAreaPart
objectName: "rdm_key_multiline_text_field_" + index
Item {
width: texteditorWrapper.width
height: textAreaPart.contentHeight < texteditorWrapper.height? texteditorWrapper.height - 5 : textAreaPart.contentHeight

enabled: root.enabled
text: value
NewTextArea {
anchors.fill: parent
id: textAreaPart
objectName: "rdm_key_multiline_text_field_" + index


enabled: root.enabled
text: value

textFormat: textView.textFormat
readOnly: textView.readOnly
textFormat: textView.textFormat
readOnly: textView.readOnly

onTextChanged: {
root.isEdited = true
textView.model && textView.model.setTextChunk(index, textAreaPart.text)
onTextChanged: {
root.isEdited = true
textView.model && textView.model.setTextChunk(index, textAreaPart.text)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/qml/value-editor/editors/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getEditorByTypeString(keyType) {
return "./editors/HashItemEditor.qml"
} else if (keyType === "stream") {
return "./editors/StreamItemEditor.qml"
} else {
} else if (keyType) {
console.error("Editor for type " + keyType + " is not defined!")
}
}
Expand Down
14 changes: 2 additions & 12 deletions src/qml/value-editor/editors/formatters/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,11 @@ var json = {
title: "JSON",

getFormatted: function (raw, callback) {
try {
// NOTE(u_glide): Minify json before processing to get rid of double formatted JSON
return callback("", JSONFormatter.prettyPrint(JSONFormatter.minify(String(raw))), false, FORMAT_HTML)
} catch (e) {
return callback(qsTranslate("RDM", "Invalid JSON: ") + e)
}
console.error("Call JSON worker script")
},

isValid: function (raw, callback) {
try {
JSONFormatter.prettyPrint(String(raw))
return callback(true)
} catch (e) {
return callback(false)
}
console.error("Call JSON worker script")
},

getRaw: function (formatted, callback) {
Expand Down
Loading