From da0f6e9c59bfcaaaacc29e8b8ed48de2b44a21bb Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Fri, 26 Jan 2018 18:18:42 +0200 Subject: [PATCH] Fix adding new keys --- src/app/qmlutils.cpp | 16 ++++++++++++ src/app/qmlutils.h | 2 ++ src/modules/value-editor/largetextmodel.cpp | 22 +++++++++++----- src/modules/value-editor/largetextmodel.h | 4 ++- src/modules/value-editor/valueviewmodel.cpp | 9 ------- src/modules/value-editor/valueviewmodel.h | 4 +-- src/qml/value-editor/AddKeyDialog.qml | 5 ++-- .../value-editor/editors/AbstractEditor.qml | 4 +++ .../value-editor/editors/HashItemEditor.qml | 5 ++++ .../value-editor/editors/MultilineEditor.qml | 26 ++++++++++++++----- .../value-editor/editors/SingleItemEditor.qml | 4 +++ .../editors/SortedSetItemEditor.qml | 4 +++ 12 files changed, 77 insertions(+), 28 deletions(-) diff --git a/src/app/qmlutils.cpp b/src/app/qmlutils.cpp index e668d276..eeb763cb 100644 --- a/src/app/qmlutils.cpp +++ b/src/app/qmlutils.cpp @@ -6,6 +6,8 @@ #include #include +#include "value-editor/largetextmodel.h" + bool QmlUtils::isBinaryString(const QVariant &value) { if (!value.canConvert(QVariant::ByteArray)) { @@ -149,3 +151,17 @@ void QmlUtils::addNewValueToDynamicChart(QtCharts::QXYSeries *series, double val ax->setMax(QDateTime::currentDateTime()); } } + +QObject *QmlUtils::wrapLargeText(const QByteArray &text) +{ + auto w = new ValueEditor::LargeTextWrappingModel(QString::fromUtf8(text)); + w->setParent(this); + return w; +} + +void QmlUtils::deleteTextWrapper(QObject *w) +{ + if (w && w->parent() == this) { + w->deleteLater(); + } +} diff --git a/src/app/qmlutils.h b/src/app/qmlutils.h index 483a5448..4fee0ee4 100644 --- a/src/app/qmlutils.h +++ b/src/app/qmlutils.h @@ -20,4 +20,6 @@ public: Q_INVOKABLE QString getPathFromUrl(const QUrl &url); Q_INVOKABLE void copyToClipboard(const QString &text); Q_INVOKABLE void addNewValueToDynamicChart(QtCharts::QXYSeries* series, double value); + Q_INVOKABLE QObject* wrapLargeText(const QByteArray &text); + Q_INVOKABLE void deleteTextWrapper(QObject* w); }; diff --git a/src/modules/value-editor/largetextmodel.cpp b/src/modules/value-editor/largetextmodel.cpp index e7c75a2f..88e8e0fa 100644 --- a/src/modules/value-editor/largetextmodel.cpp +++ b/src/modules/value-editor/largetextmodel.cpp @@ -1,15 +1,15 @@ #include "largetextmodel.h" - +#include ValueEditor::LargeTextWrappingModel::LargeTextWrappingModel(const QString &text, uint chunkSize) : m_chunkSize(chunkSize) { - m_textRows.reserve(text.size()/chunkSize); + setText(text); +} - for (uint chunkIndex=0; chunkIndex < text.size()/chunkSize + 1; chunkIndex++) - { - m_textRows.append(text.mid(chunkIndex * chunkSize, chunkSize)); - } +ValueEditor::LargeTextWrappingModel::~LargeTextWrappingModel() +{ + qDebug() << "{DELETE}: Largetext model"; } QHash ValueEditor::LargeTextWrappingModel::roleNames() const @@ -36,6 +36,16 @@ QVariant ValueEditor::LargeTextWrappingModel::data(const QModelIndex &index, int 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::cleanUp() { emit beginRemoveRows(QModelIndex(), 0, rowCount()); diff --git a/src/modules/value-editor/largetextmodel.h b/src/modules/value-editor/largetextmodel.h index 7b82bbbc..505f5276 100644 --- a/src/modules/value-editor/largetextmodel.h +++ b/src/modules/value-editor/largetextmodel.h @@ -14,7 +14,7 @@ class LargeTextWrappingModel : public QAbstractListModel public: LargeTextWrappingModel(const QString& text=QString(), uint chunkSize=10000); - ~LargeTextWrappingModel() {} + ~LargeTextWrappingModel(); QHash roleNames() const; @@ -22,6 +22,8 @@ public: QVariant data(const QModelIndex &index, int role) const; + void setText(const QString& text); + public slots: void cleanUp(); diff --git a/src/modules/value-editor/valueviewmodel.cpp b/src/modules/value-editor/valueviewmodel.cpp index 5dc9f502..8b39f7bd 100644 --- a/src/modules/value-editor/valueviewmodel.cpp +++ b/src/modules/value-editor/valueviewmodel.cpp @@ -2,8 +2,6 @@ #include #include -#include "largetextmodel.h" - ValueEditor::ValueViewModel::ValueViewModel(Model &model) : QAbstractListModel((QObject*)model.getConnector().data()), m_model(model), @@ -55,13 +53,6 @@ QVariantList ValueEditor::ValueViewModel::getColumnNames() return result; } -QObject *ValueEditor::ValueViewModel::wrapLargeText(const QByteArray &text) -{ - auto w = new LargeTextWrappingModel(QString::fromUtf8(text)); - w->setParent(this); - return w; -} - bool ValueEditor::ValueViewModel::isMultiRow() { return m_model.isMultiRow(); diff --git a/src/modules/value-editor/valueviewmodel.h b/src/modules/value-editor/valueviewmodel.h index e1611d3b..58809460 100644 --- a/src/modules/value-editor/valueviewmodel.h +++ b/src/modules/value-editor/valueviewmodel.h @@ -34,9 +34,7 @@ public: Q_INVOKABLE int pageSize(); // general operations - Q_INVOKABLE QVariantList getColumnNames(); - - Q_INVOKABLE QObject* wrapLargeText(const QByteArray &text); + Q_INVOKABLE QVariantList getColumnNames(); signals: void rowsLoaded(int start, int count); diff --git a/src/qml/value-editor/AddKeyDialog.qml b/src/qml/value-editor/AddKeyDialog.qml index d1a52100..61b52c44 100644 --- a/src/qml/value-editor/AddKeyDialog.qml +++ b/src/qml/value-editor/AddKeyDialog.qml @@ -42,9 +42,6 @@ Dialog { objectName: "rdm_add_key_type_field" } - Text { - text: qsTr("Value:") - } Loader { id: valueAddEditor Layout.fillWidth: true @@ -55,6 +52,7 @@ Dialog { onLoaded: { item.state = "new" + item.initEmpty() } } @@ -88,6 +86,7 @@ Dialog { if (!err) { newKeyName.text = '' valueAddEditor.item.reset() + valueAddEditor.item.initEmpty() root.close() } else { addError.text = err diff --git a/src/qml/value-editor/editors/AbstractEditor.qml b/src/qml/value-editor/editors/AbstractEditor.qml index a3e5ce77..91151be3 100644 --- a/src/qml/value-editor/editors/AbstractEditor.qml +++ b/src/qml/value-editor/editors/AbstractEditor.qml @@ -14,6 +14,10 @@ ColumnLayout { State { name: "edit"} // Editing existing key ] + function initEmpty() { + console.exception("Not implemented") + } + function validateValue(callback) { console.exception("Not implemented") } diff --git a/src/qml/value-editor/editors/HashItemEditor.qml b/src/qml/value-editor/editors/HashItemEditor.qml index dae6a26c..f57aa656 100644 --- a/src/qml/value-editor/editors/HashItemEditor.qml +++ b/src/qml/value-editor/editors/HashItemEditor.qml @@ -34,6 +34,11 @@ AbstractEditor { objectName: "rdm_key_hash_text_field" } + function initEmpty() { + keyText.initEmpty() + textArea.initEmpty() + } + function validateValue(callback) { return textArea.validate(function (textAreaValid) { keyText.validate(function (keyTextValid) { diff --git a/src/qml/value-editor/editors/MultilineEditor.qml b/src/qml/value-editor/editors/MultilineEditor.qml index 302f7615..5a7dbded 100644 --- a/src/qml/value-editor/editors/MultilineEditor.qml +++ b/src/qml/value-editor/editors/MultilineEditor.qml @@ -16,6 +16,14 @@ ColumnLayout property string fieldLabel: qsTr("Value:") property bool isEdited: false property var value + property int valueSizeLimit: 150000 + + function initEmpty() { + // init editor with empty model + textView.model = qmlUtils.wrapLargeText("") + textView.readOnly = false + textView.textFormat = TextEdit.PlainText + } function validate(callback) { loadRawValue(function (error, raw) { @@ -55,7 +63,7 @@ ColumnLayout return; } - if (binaryUtils.binaryStringLength(root.value) > 150000) { + if (binaryUtils.binaryStringLength(root.value) > valueSizeLimit) { root.showFormatters = false formatterSelector.currentIndex = 0 } else { @@ -87,14 +95,14 @@ ColumnLayout if (format === "json") { // 1 is JSON return formatterSelector.model[1].instance.getFormatted(formatted, function (formattedJson, r, f) { - textView.model = keyTab.keyModel.wrapLargeText(formattedJson) + textView.model = qmlUtils.wrapLargeText(formattedJson) textView.readOnly = isReadOnly textView.textFormat = TextEdit.PlainText root.isEdited = false uiBlocker.visible = false }) } else { - textView.model = keyTab.keyModel.wrapLargeText(formatted) + textView.model = qmlUtils.wrapLargeText(formatted) textView.readOnly = isReadOnly root.isEdited = false @@ -111,6 +119,11 @@ ColumnLayout function reset() { if (textView.model) textView.model.cleanUp() + + if (textView.model) { + qmlUtils.deleteTextWrapper(textView.model) + } + textView.model = null root.value = "" root.isEdited = false @@ -152,10 +165,11 @@ ColumnLayout } } - Text { visible: !showFormatters; text: qsTr("Large value (>150kB). Formatters is not available."); color: "red"; } + Text { visible: !showFormatters && binaryUtils.binaryStringLength(root.value) > valueSizeLimit; text: qsTr("Large value (>150kB). Formatters is not available."); color: "red"; } } Rectangle { + id: texteditorWrapper Layout.fillWidth: true Layout.fillHeight: true Layout.preferredHeight: 100 @@ -181,7 +195,7 @@ ColumnLayout NewTextArea { id: textAreaPart width: textView.width - height: textAreaPart.contentHeight + height: textAreaPart.contentHeight < texteditorWrapper.height? texteditorWrapper.height - 5 : textAreaPart.contentHeight enabled: root.enabled text: value @@ -216,5 +230,5 @@ ColumnLayout } MouseArea { anchors.fill: parent } - } + } } diff --git a/src/qml/value-editor/editors/SingleItemEditor.qml b/src/qml/value-editor/editors/SingleItemEditor.qml index bce65677..d6910a1c 100644 --- a/src/qml/value-editor/editors/SingleItemEditor.qml +++ b/src/qml/value-editor/editors/SingleItemEditor.qml @@ -21,6 +21,10 @@ AbstractEditor { showFormatters: root.state == "edit" } + function initEmpty() { + textEditor.initEmpty() + } + function validateValue(callback) { return textEditor.validate(callback); } diff --git a/src/qml/value-editor/editors/SortedSetItemEditor.qml b/src/qml/value-editor/editors/SortedSetItemEditor.qml index 88692da1..2488e534 100644 --- a/src/qml/value-editor/editors/SortedSetItemEditor.qml +++ b/src/qml/value-editor/editors/SortedSetItemEditor.qml @@ -54,6 +54,10 @@ AbstractEditor { objectName: "rdm_key_zset_text_field" } + function initEmpty() { + textArea.initEmpty() + } + function validateValue(callback) { return textArea.validate(callback); }