From 8e313cfe9960411bf2555d4099f71e3ccb66fa51 Mon Sep 17 00:00:00 2001 From: Fanda Vacek Date: Mon, 9 Oct 2023 21:42:08 +0200 Subject: [PATCH] Method call parameters cannot be set #1 done --- CMakeLists.txt | 6 ++++ images/copy.svg | 64 +++++++++++++++++++++++++++++++++++++ images/methods.svg | 61 ++++++++++++++++++++++++++++++++++++ images/params-some.svg | 68 ++++++++++++++++++++++++++++++++++++++++ images/params.svg | 61 ++++++++++++++++++++++++++++++++++++ images/pencil.svg | 2 +- images/play.svg | 61 ++++++++++++++++++++++++++++++++++++ qml/BrokerDelegate.qml | 8 ++++- qml/Main.qml | 8 ++++- qml/MethodDelegate.qml | 35 ++++++++++++++++++--- qml/MethodsPane.qml | 7 +++-- qml/NodeDelegate.qml | 9 ++++-- qml/NodesPane.qml | 1 + qml/TextViewPane.qml | 71 ++++++++++++++++++++++++++++++++++++------ src/application.cpp | 27 ++++++++++++++-- src/application.h | 6 +++- src/main.cpp | 3 +- src/settings.cpp | 2 +- src/version.h | 6 ++++ 19 files changed, 477 insertions(+), 29 deletions(-) create mode 100644 images/copy.svg create mode 100644 images/methods.svg create mode 100644 images/params-some.svg create mode 100644 images/params.svg create mode 100644 images/play.svg create mode 100644 src/version.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c62cd4a..17ec136 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ add_subdirectory(3rdparty/libshv) qt_add_executable(shvspym shvspym.qrc + src/version.h src/main.cpp src/settings.cpp src/application.cpp @@ -53,6 +54,11 @@ qt_add_qml_module(shvspym images/plus.svg images/eye.svg images/menu.svg + images/copy.svg + images/play.svg + images/params.svg + images/params-some.svg + images/methods.svg ) diff --git a/images/copy.svg b/images/copy.svg new file mode 100644 index 0000000..94360ee --- /dev/null +++ b/images/copy.svg @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + diff --git a/images/methods.svg b/images/methods.svg new file mode 100644 index 0000000..4e5696c --- /dev/null +++ b/images/methods.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + diff --git a/images/params-some.svg b/images/params-some.svg new file mode 100644 index 0000000..7f05ea6 --- /dev/null +++ b/images/params-some.svg @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + diff --git a/images/params.svg b/images/params.svg new file mode 100644 index 0000000..fb5561b --- /dev/null +++ b/images/params.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + diff --git a/images/pencil.svg b/images/pencil.svg index 2002a5d..b7243f9 100644 --- a/images/pencil.svg +++ b/images/pencil.svg @@ -55,7 +55,7 @@ diff --git a/images/play.svg b/images/play.svg new file mode 100644 index 0000000..aff6886 --- /dev/null +++ b/images/play.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + diff --git a/qml/BrokerDelegate.qml b/qml/BrokerDelegate.qml index f4e5784..41304f9 100644 --- a/qml/BrokerDelegate.qml +++ b/qml/BrokerDelegate.qml @@ -56,5 +56,11 @@ Rectangle { } } } - + Rectangle { + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + height: 2 + color: "lightgray" + } } diff --git a/qml/Main.qml b/qml/Main.qml index b123311..0fa0e7f 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -74,7 +74,13 @@ ApplicationWindow { } } } - + Rectangle { + anchors.right: parent.right + anchors.left: parent.left + anchors.bottom: parent.bottom + height: 1 + color: app.settings.delegateColor + } } StackView { id: stackView diff --git a/qml/MethodDelegate.qml b/qml/MethodDelegate.qml index da563c8..2d55a36 100644 --- a/qml/MethodDelegate.qml +++ b/qml/MethodDelegate.qml @@ -12,6 +12,7 @@ Rectangle { required property string index required property string name required property string shvPath + property var params: null required property int flags property bool isGetter: flags & 2 @@ -28,6 +29,7 @@ Rectangle { anchors.right: parent.right anchors.leftMargin: 6 Layout.fillWidth: true + spacing: 0 Column { id: column Layout.fillWidth: true @@ -49,6 +51,7 @@ Rectangle { } Text { id: fldResult + property bool isError: false font.pixelSize: app.settings.fontSize } Component { @@ -60,18 +63,39 @@ Rectangle { TapHandler { onTapped: { console.log("result:" , fldResult.text) - stackView.push(resultPane, {text: fldResult.text, headerText: qsTr("RPC Result")}); + stackView.push(resultPane, {text: fldResult.text, headerText: qsTr("RPC Result"), showError: fldResult.isError}); } } } - Button { + MyButton { + id: buttonParams + width: buttonCall.height + height: width + color: root.color + border.width: 0 + //iconMargin: 15 + iconSource: root.params? "../images/params-some.svg": "../images/params.svg" + onTapped: { + let cpon = app.variantToCpon(root.params); + let pane = stackView.push(resultPane, {text: cpon, headerText: qsTr("Method parameters"), checkCpon: true, readOnly: false}); + pane.textCommited.connect((text) => { root.params = app.cponToVariant(text) }) + } + } + MyButton { id: buttonCall - text: qsTr("Call") - onClicked: { + color: app.settings.buttonColor + width: root.height - 6 + height: width + iconMargin: 10 + iconSource: "../images/play.svg" + onTapped: { //let shv_path = root.shvPath? root.shvPath + '/' + root.nodeName: root.nodeName - root.requestId = app.callMethod(root.shvPath, root.name) + root.requestId = app.callMethod(root.shvPath, root.name, root.params) } } + Rectangle { + width: 5 + } } Component.onCompleted: { @@ -87,6 +111,7 @@ Rectangle { function onMethodCallResult(rq_id, result, is_error) { if(rq_id === root.requestId) { console.log(rq_id, JSON.stringify(result), is_error); + fldResult.isError = is_error fldResult.text = result; root.color = is_error? "darksalmon": root.backgroundColor() } diff --git a/qml/MethodsPane.qml b/qml/MethodsPane.qml index c570716..4fdb1f8 100644 --- a/qml/MethodsPane.qml +++ b/qml/MethodsPane.qml @@ -14,8 +14,8 @@ Pane { Rectangle { id: header - height: 54 - color: "#278f00" + height: cancelBt.height + color: app.settings.headerColor anchors.left: parent.left anchors.right: parent.right anchors.top: parent.top @@ -25,9 +25,10 @@ Pane { spacing: 5 MyButton { id: cancelBt + color: "transparent" width: header.height height: width - iconMargin: 15 + iconMargin: 10 iconSource: "../images/back.svg" onTapped: pane.back() } diff --git a/qml/NodeDelegate.qml b/qml/NodeDelegate.qml index 3da9b73..e5510d3 100644 --- a/qml/NodeDelegate.qml +++ b/qml/NodeDelegate.qml @@ -34,12 +34,15 @@ Rectangle { app.callLs(shv_path) } } - Button { + MyButton { id: button - text: qsTr("Methods") anchors.verticalCenter: parent.verticalCenter anchors.right: parent.right - onClicked: { + anchors.rightMargin: 4 + //iconMargin: 10 + iconSource: "../images/methods.svg" + //color: "transparent" + onTapped: { let shv_path = root.shvPath? root.shvPath + '/' + root.nodeName: root.nodeName app.callDir(shv_path) } diff --git a/qml/NodesPane.qml b/qml/NodesPane.qml index 9d5d753..b7aa207 100644 --- a/qml/NodesPane.qml +++ b/qml/NodesPane.qml @@ -26,6 +26,7 @@ Pane { spacing: 5 MyButton { id: cancelBt + color: "transparent" iconMargin: 10 iconSource: "../images/back.svg" width: header.height diff --git a/qml/TextViewPane.qml b/qml/TextViewPane.qml index 48b3185..27537cd 100644 --- a/qml/TextViewPane.qml +++ b/qml/TextViewPane.qml @@ -3,20 +3,23 @@ import QtQuick.Controls import QtQuick.Layouts Pane { - id: pane + id: root property string headerText - property string text property bool showError: false + property bool checkCpon: false + property alias text: textEdit.text + property alias readOnly: textEdit.readOnly signal back() + signal textCommited(text: string) padding: 0 Rectangle { id: header height: 50 - color: pane.showError? "red": app.settings.headerColor + color: root.showError? "red": app.settings.headerColor anchors.left: parent.left anchors.right: parent.right anchors.top: parent.top @@ -30,29 +33,67 @@ Pane { height: width iconMargin: 15 iconSource: "../images/back.svg" - color: header.color - onTapped: pane.back() + color: "transparent" + onTapped: root.back() } Text { Layout.fillWidth: true color: app.settings.headerTextColor - text: pane.showError? qsTr("Error"): pane.headerText + text: root.showError? qsTr("Error"): root.headerText font.bold: true } - Button { - text: qsTr("Copy") - onClicked: { + MyButton { + width: header.height + height: width + iconSource: "../images/copy.svg" + color: "transparent" + onTapped: { textEdit.selectAll() textEdit.copy() textEdit.deselect() } } + MyButton { + visible: !root.readOnly + width: header.height + height: width + iconSource: "../images/ok.svg" + iconMargin: 10 + color: "transparent" + onTapped: { + textEdit.focus = false + let text = textEdit.text + if(root.checkCpon) { + let errmsg = app.checkCpon(text) + errRect.errorMsg = errmsg + if(errmsg) { + return; + } + } + root.textCommited(text) + root.back() + } + } + } + } + Rectangle { + id: errRect + property string errorMsg + anchors.left: parent.left + anchors.right: parent.right + anchors.top: header.bottom + anchors.bottom: parent.bottom + visible: errorMsg && !textEdit.focus + color: "salmon" + Text { + text: parent.errorMsg + anchors.fill: parent + verticalAlignment: Text.AlignVCenter } } TextEdit { id: textEdit - text: pane.text anchors.left: parent.left anchors.right: parent.right anchors.top: header.bottom @@ -61,5 +102,15 @@ Pane { wrapMode: Text.WordWrap readOnly: true textFormat: Text.PlainText + /* + onTextChanged: { + // text changed does not contain current line + console.log("text changed:", textEdit.text) + if(root.checkCpon) { + let errmsg = app.checkCpon(text) + errRect.errorMsg = errmsg + } + } + */ } } diff --git a/src/application.cpp b/src/application.cpp index b64e026..073e26d 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -66,9 +66,9 @@ void Application::callDir(const QString &shv_path) ); } -int Application::callMethod(const QString &shv_path, const QString &method) +int Application::callMethod(const QString &shv_path, const QString &method, const QVariant ¶ms) { - return callRpcMethod(shv_path, method, {}, this, + return callRpcMethod(shv_path, method, params, this, [this, shv_path](int rq_id, const auto &result) { auto cpon = shv::coreqt::rpc::qVariantToRpcValue(result).toCpon(); shvInfo() << cpon; @@ -81,6 +81,29 @@ int Application::callMethod(const QString &shv_path, const QString &method) ); } +QString Application::variantToCpon(const QVariant &v) +{ + if(!v.isValid() || v.isNull()) + return {}; + auto rv = shv::coreqt::rpc::qVariantToRpcValue(v); + auto cpon = rv.toCpon(" "); + return QString::fromStdString(cpon); +} + +QVariant Application::cponToVariant(const QString &cpon) +{ + std::string err; + auto rv = shv::chainpack::RpcValue::fromCpon(cpon.toStdString(), &err); + return shv::coreqt::rpc::rpcValueToQVariant(rv); +} + +QString Application::checkCpon(const QString &cpon) +{ + std::string err; + shv::chainpack::RpcValue::fromCpon(cpon.toStdString(), &err); + return QString::fromStdString(err); +} + QString Application::appVersion() const { return QCoreApplication::applicationVersion(); diff --git a/src/application.h b/src/application.h index f88678d..77f7276 100644 --- a/src/application.h +++ b/src/application.h @@ -26,7 +26,11 @@ class Application : public QGuiApplication Q_INVOKABLE void connectToBroker(int connection_id); Q_INVOKABLE void callLs(const QString &shv_path); Q_INVOKABLE void callDir(const QString &shv_path); - Q_INVOKABLE int callMethod(const QString &shv_path, const QString &method); + Q_INVOKABLE int callMethod(const QString &shv_path, const QString &method, const QVariant ¶ms); + + Q_INVOKABLE QString variantToCpon(const QVariant &v); + Q_INVOKABLE QVariant cponToVariant(const QString &cpon); + Q_INVOKABLE QString checkCpon(const QString &cpon); Q_SIGNAL void brokerConnectedChanged(bool is_connected); Q_SIGNAL void connetToBrokerError(const QString &errmsg); diff --git a/src/main.cpp b/src/main.cpp index aaee70c..ae5dbfe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "application.h" +#include "version.h" #include #include @@ -9,7 +10,7 @@ int main(int argc, char *argv[]) QCoreApplication::setOrganizationName("Elektroline"); QCoreApplication::setOrganizationDomain("elektroline.cz"); QCoreApplication::setApplicationName("shvspym"); - QCoreApplication::setApplicationVersion("0.0.2"); + QCoreApplication::setApplicationVersion(APP_VERSION); Application app(argc, argv); //app.setWindowIcon(QIcon(":/images/shvspy.svg")); diff --git a/src/settings.cpp b/src/settings.cpp index 65e240f..935c8c5 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -11,7 +11,7 @@ void Settings::setThemeColor(const QColor &c) m_backgroundColor = Qt::white; m_headerColor = c; m_headerTextColor = Qt::white; - m_buttonColor = c; + m_buttonColor = c.lighter(); m_buttonBorderColor = c; m_delegateAltColor = c.lighter().lighter().lighter(); m_delegateColor = Qt::white; diff --git a/src/version.h b/src/version.h new file mode 100644 index 0000000..ac66314 --- /dev/null +++ b/src/version.h @@ -0,0 +1,6 @@ +#ifndef VERSION_H +#define VERSION_H + +#define APP_VERSION "1.0.0" + +#endif // VERSION_H