From b35a8af9ab6208f05b26b02ac73df0448f3c500c Mon Sep 17 00:00:00 2001
From: jonymt <johanmt@yandex.ru>
Date: Sat, 7 Sep 2019 10:34:33 +0200
Subject: [PATCH] [+] added handler

---
 .../DapUiQmlScreenConsoleForm.qml             | 49 +++++++++++
 .../DapUiQmlScreenDashboard.qml               |  7 ++
 KelvinDashboardGUI/DapUiQmlWidgetConsole.cpp  |  6 ++
 KelvinDashboardGUI/DapUiQmlWidgetConsole.h    | 19 +++++
 KelvinDashboardGUI/DapUiQmlWidgetConsole.qml  | 27 ------
 .../DapUiQmlWidgetConsoleForm.ui.qml          | 83 -------------------
 KelvinDashboardGUI/KelvinDashboardGUI.pro     |  2 +
 KelvinDashboardGUI/main.cpp                   |  3 +-
 KelvinDashboardGUI/qml.qrc                    |  5 +-
 .../DapChainConsoleHandler.cpp                | 15 ++++
 .../DapChainConsoleHandler.h                  | 17 ++++
 .../DapChainDashboardService.cpp              |  8 ++
 .../DapChainDashboardService.h                |  5 ++
 .../KelvinDashboardService.pro                |  6 +-
 14 files changed, 136 insertions(+), 116 deletions(-)
 create mode 100644 KelvinDashboardGUI/DapUiQmlScreenConsoleForm.qml
 create mode 100644 KelvinDashboardGUI/DapUiQmlWidgetConsole.cpp
 create mode 100644 KelvinDashboardGUI/DapUiQmlWidgetConsole.h
 delete mode 100644 KelvinDashboardGUI/DapUiQmlWidgetConsole.qml
 delete mode 100644 KelvinDashboardGUI/DapUiQmlWidgetConsoleForm.ui.qml
 create mode 100644 KelvinDashboardService/DapChainConsoleHandler.cpp
 create mode 100644 KelvinDashboardService/DapChainConsoleHandler.h

diff --git a/KelvinDashboardGUI/DapUiQmlScreenConsoleForm.qml b/KelvinDashboardGUI/DapUiQmlScreenConsoleForm.qml
new file mode 100644
index 000000000..17d0965d5
--- /dev/null
+++ b/KelvinDashboardGUI/DapUiQmlScreenConsoleForm.qml
@@ -0,0 +1,49 @@
+import QtQuick 2.9
+import QtQml 2.12
+import QtQuick.Controls 2.2
+import QtQuick.Layouts 1.12
+
+Page {
+    TextEdit {
+        id: txtCommands
+        property int positionLine: 2
+        anchors.fill: parent
+
+        text: "> "
+        wrapMode: TextEdit.WordWrap
+
+        onTextChanged: {
+            if(txtCommands.cursorPosition === txtCommands.positionLine)
+            {
+                txtCommands.text += " ";
+                txtCommands.cursorPosition = txtCommands.text.length
+            }
+        }
+
+        function acceptedResopose(responce)
+        {
+            txtCommands.readOnly = false;
+            txtCommands.append(responce);
+            txtCommands.append("> ");
+            console.debug(txtCommands.positionLine);
+            console.debug(txtCommands.cursorPosition);
+            txtCommands.positionLine = txtCommands.cursorPosition + 1;
+        }
+
+        Keys.onPressed: {
+
+            switch(event.key)
+            {
+            case Qt.Key_Up: console.debug("UP"); break;
+            case Qt.Key_Down: console.debug("Down"); break;
+            default: break;
+            }
+        }
+
+        Keys.onReturnPressed: {
+            console.debug("ENTER");
+            txtCommands.readOnly = true;
+            acceptedResopose("New resp");
+        }
+    }
+}
diff --git a/KelvinDashboardGUI/DapUiQmlScreenDashboard.qml b/KelvinDashboardGUI/DapUiQmlScreenDashboard.qml
index ad42c4374..9b79c70f1 100755
--- a/KelvinDashboardGUI/DapUiQmlScreenDashboard.qml
+++ b/KelvinDashboardGUI/DapUiQmlScreenDashboard.qml
@@ -51,6 +51,13 @@ Page {
                         page: "DapUiQmlScreenHistory.qml"
                         source: "qrc:/Resources/Icons/defaul_icon.png"
                     }
+
+                    ListElement {
+                        name:  qsTr("Console")
+                        page: "DapUiQmlScreenConsoleForm.qml"
+                        source: "qrc:/Resources/Icons/defaul_icon.png"
+                    }
+
                     ListElement {
                         name:  qsTr("About")
                         page: "DapQmlScreenAbout.qml"
diff --git a/KelvinDashboardGUI/DapUiQmlWidgetConsole.cpp b/KelvinDashboardGUI/DapUiQmlWidgetConsole.cpp
new file mode 100644
index 000000000..373b4b90f
--- /dev/null
+++ b/KelvinDashboardGUI/DapUiQmlWidgetConsole.cpp
@@ -0,0 +1,6 @@
+#include "DapUiQmlWidgetConsole.h"
+
+DapUiQmlWidgetConsole::DapUiQmlWidgetConsole(QObject *parent) : QObject(parent)
+{
+
+}
diff --git a/KelvinDashboardGUI/DapUiQmlWidgetConsole.h b/KelvinDashboardGUI/DapUiQmlWidgetConsole.h
new file mode 100644
index 000000000..bbd8f1576
--- /dev/null
+++ b/KelvinDashboardGUI/DapUiQmlWidgetConsole.h
@@ -0,0 +1,19 @@
+#ifndef DAPUIQMLSCREENCONSOLEFORM_H
+#define DAPUIQMLSCREENCONSOLEFORM_H
+
+#include <QObject>
+#include <QPlainTextEdit>
+
+class DapUiQmlWidgetConsole : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit DapUiQmlWidgetConsole(QObject *parent = nullptr);
+
+public slots:
+
+signals:
+};
+
+#endif // DAPUIQMLSCREENCONSOLEFORM_H
diff --git a/KelvinDashboardGUI/DapUiQmlWidgetConsole.qml b/KelvinDashboardGUI/DapUiQmlWidgetConsole.qml
deleted file mode 100644
index ea079704e..000000000
--- a/KelvinDashboardGUI/DapUiQmlWidgetConsole.qml
+++ /dev/null
@@ -1,27 +0,0 @@
-import QtQuick 2.9
-import QtQuick.Controls 1.4
-import QtQuick.Controls 2.2
-import QtQuick.Window 2.0
-import QtQuick.Controls.Styles 1.3
-import QtQuick.Controls.Styles 1.4
-import Qt.labs.platform 1.0
-import KelvinDashboard 1.0
-
-
-DapUiQmlWidgetConsoleForm {
-    id: dapQmlWidgetConsole
-    execute.onClicked: {
-        dapServiceController.executeCommand(command.text)
-        execute.enabled = false;
-
-    }
-    Connections {
-           target: dapServiceController
-           onResultChanged: {
-                command.clear()
-                result.text = dapServiceController.Result
-                execute.enabled = true
-            }
-    }
-
-}
diff --git a/KelvinDashboardGUI/DapUiQmlWidgetConsoleForm.ui.qml b/KelvinDashboardGUI/DapUiQmlWidgetConsoleForm.ui.qml
deleted file mode 100644
index 0fe42cea0..000000000
--- a/KelvinDashboardGUI/DapUiQmlWidgetConsoleForm.ui.qml
+++ /dev/null
@@ -1,83 +0,0 @@
-import QtQuick 2.9
-import QtQuick.Controls 2.2
-import QtQuick.Layouts 1.1
-
-Page {
-    id: dapUiQmlWidgetConsole
-    property alias result: result
-    property alias command: command
-    property alias execute: execute
-
-
-    Rectangle {
-        id: rectangle
-        y: 0
-        width: 640
-        height: 480
-        color: "#ffffff"
-        anchors.left: parent.left
-        anchors.leftMargin: 0
-        anchors.verticalCenter: parent.verticalCenter
-        border.width: 0
-
-        Button {
-            id: execute
-            x: 250
-            y: 366
-            text: qsTr("Execute")
-            anchors.horizontalCenterOffset: 0
-            anchors.bottom: parent.bottom
-            anchors.bottomMargin: 74
-            anchors.horizontalCenter: parent.horizontalCenter
-        }
-
-        TextEdit {
-            id: command
-            x: 290
-            y: 50
-            width: 606
-            height: 208
-            anchors.horizontalCenterOffset: 4
-            anchors.horizontalCenter: parent.horizontalCenter
-            font.pixelSize: 12
-        }
-
-        Label {
-            id: result
-            y: 422
-            width: 606
-            height: 50
-            text: qsTr("")
-            anchors.left: parent.left
-            anchors.leftMargin: 21
-            anchors.bottom: parent.bottom
-            anchors.bottomMargin: 8
-        }
-
-        Label {
-            id: commandText
-            x: 21
-            y: 24
-            width: 82
-            height: 14
-            text: qsTr("Command:")
-            anchors.left: parent.left
-            anchors.leftMargin: 21
-        }
-
-        Label {
-            id: resultText
-            x: 21
-            y: 409
-            text: qsTr("Result:")
-        }
-    }
-}
-
-
-
-
-/*##^## Designer {
-    D{i:0;autoSize:true;height:480;width:640}
-}
- ##^##*/
diff --git a/KelvinDashboardGUI/KelvinDashboardGUI.pro b/KelvinDashboardGUI/KelvinDashboardGUI.pro
index 37c5639d6..bf431dd61 100755
--- a/KelvinDashboardGUI/KelvinDashboardGUI.pro
+++ b/KelvinDashboardGUI/KelvinDashboardGUI.pro
@@ -45,6 +45,7 @@ SOURCES += \
     DapScreenHistoryFilterModel.cpp \
     DapScreenHistoryModel.cpp \
     DapUiQmlWidgetChainTransactions.cpp \
+    DapUiQmlWidgetConsole.cpp \
         main.cpp \
     DapUiQmlWidgetChainBallance.cpp \
     DapUiQmlWidgetChainBlockExplorer.cpp \
@@ -86,6 +87,7 @@ HEADERS += \
     DapUiQmlScreenDashboard.h \
     DapUiQmlWidgetChainOperations.h \
     DapUiQmlWidgetChainTransactions.h \
+    DapUiQmlWidgetConsole.h \
     DapUiQmlWidgetModel.h \
     DapUiQmlWidget.h \
     DapScreenDialog.h \
diff --git a/KelvinDashboardGUI/main.cpp b/KelvinDashboardGUI/main.cpp
index 0239d20a4..e413577b3 100755
--- a/KelvinDashboardGUI/main.cpp
+++ b/KelvinDashboardGUI/main.cpp
@@ -23,7 +23,7 @@
 #include "DapChainNodeNetworkModel.h"
 #include "DapChainNodeNetworkExplorer.h"
 #include "DapScreenHistoryFilterModel.h"
-
+#include "DapUiQmlWidgetConsole.h"
 
 #include <QRegExp>
 
@@ -66,6 +66,7 @@ int main(int argc, char *argv[])
 //    qmlRegisterType<DapScreenHistoryModel>("")
     qmlRegisterSingletonType<DapUiQmlWidgetModel>("KelvinDashboard", 1, 0, "DapUiQmlWidgetModel", DapUiQmlWidgetModel::singletonProvider);
     qmlRegisterType<DapScreenHistoryModel>("DapTransactionHistory", 1, 0, "DapTransactionModel");
+    qmlRegisterType<DapUiQmlWidgetConsole>("QmlWidgetConsole", 1, 0, "DapUiQmlWidgetConsole");
     
     QQmlApplicationEngine engine;
 //    qreal dpi = QGuiApplication::primaryScreen()->physicalDotsPerInch();
diff --git a/KelvinDashboardGUI/qml.qrc b/KelvinDashboardGUI/qml.qrc
index a3f93cb2c..4c6ab326c 100755
--- a/KelvinDashboardGUI/qml.qrc
+++ b/KelvinDashboardGUI/qml.qrc
@@ -24,10 +24,9 @@
         <file>DapUiQmlWidgetChainNodeLogsForm.ui.qml</file>
         <file>DapUiQmlScreenDialogSendToken.qml</file>
         <file>DapUiQmlScreenDialogRemoveWallet.qml</file>
-        <file>DapUiQmlWidgetConsoleForm.ui.qml</file>
-        <file>DapUiQmlWidgetConsole.qml</file>
         <file>DapUiQmlWidgetNodeNetworkExplorer.qml</file>
         <file>DapUiQmlScreenHistory.qml</file>
-<file>Resources/Icons/defaul_icon.png</file>
+        <file>Resources/Icons/defaul_icon.png</file>
+        <file>DapUiQmlScreenConsoleForm.qml</file>
     </qresource>
 </RCC>
diff --git a/KelvinDashboardService/DapChainConsoleHandler.cpp b/KelvinDashboardService/DapChainConsoleHandler.cpp
new file mode 100644
index 000000000..8b3403e7c
--- /dev/null
+++ b/KelvinDashboardService/DapChainConsoleHandler.cpp
@@ -0,0 +1,15 @@
+#include "DapChainConsoleHandler.h"
+
+DapChainConsoleHandler::DapChainConsoleHandler(QObject *parent) : QObject(parent)
+{
+
+}
+
+QString DapChainConsoleHandler::getResult(const QString& aQuery) const
+{
+    QProcess process;
+    process.start(QString(CLI_PATH) + " " + aQuery);
+    process.waitForFinished(-1);
+
+    return QString::fromStdString(process.readAll().toStdString());
+}
diff --git a/KelvinDashboardService/DapChainConsoleHandler.h b/KelvinDashboardService/DapChainConsoleHandler.h
new file mode 100644
index 000000000..6864a5d90
--- /dev/null
+++ b/KelvinDashboardService/DapChainConsoleHandler.h
@@ -0,0 +1,17 @@
+#ifndef DAPCHAINCONSOLEHANDLER_H
+#define DAPCHAINCONSOLEHANDLER_H
+
+#include <QObject>
+#include <QProcess>
+
+class DapChainConsoleHandler : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit DapChainConsoleHandler(QObject *parent = nullptr);
+
+    QString getResult(const QString& aQuery) const;
+};
+
+#endif // DAPCHAINCONSOLEHANDLER_H
diff --git a/KelvinDashboardService/DapChainDashboardService.cpp b/KelvinDashboardService/DapChainDashboardService.cpp
index 6f01f3b2e..4120c1bc7 100755
--- a/KelvinDashboardService/DapChainDashboardService.cpp
+++ b/KelvinDashboardService/DapChainDashboardService.cpp
@@ -16,6 +16,9 @@ DapChainDashboardService::DapChainDashboardService() : DapRpcService(nullptr)
     m_pDapChainHistoryHandler = new DapChainHistoryHandler {this};
     QObject::connect(m_pDapChainHistoryHandler, &DapChainHistoryHandler::requsetWallets, this, &DapChainDashboardService::doRequestWallets);
     QObject::connect(m_pDapChainHistoryHandler, &DapChainHistoryHandler::changeHistory, this, &DapChainDashboardService::doSendNewHistory);
+
+    m_pDapChainConsoleHandler = new DapChainConsoleHandler(this);
+
 }
 
 bool DapChainDashboardService::start()
@@ -89,6 +92,11 @@ QVariant DapChainDashboardService::getHistory() const
     return m_pDapChainHistoryHandler->getHistory();
 }
 
+QString DapChainDashboardService::getQueryResult(const QString& aQuery) const
+{
+    return m_pDapChainConsoleHandler->getResult(aQuery);
+}
+
 void DapChainDashboardService::doRequestWallets()
 {
     m_pDapChainHistoryHandler->onRequestNewHistory(m_pDapChainWalletHandler->getWallets());
diff --git a/KelvinDashboardService/DapChainDashboardService.h b/KelvinDashboardService/DapChainDashboardService.h
index 22ce4d496..573826f8b 100755
--- a/KelvinDashboardService/DapChainDashboardService.h
+++ b/KelvinDashboardService/DapChainDashboardService.h
@@ -27,6 +27,7 @@
 #include "DapChainWalletHandler.h"
 #include "DapChainNodeNetworkHandler.h"
 #include "DapChainHistoryHandler.h"
+#include "DapChainConsoleHandler.h"
 
 #include <QLocalServer>
 typedef class DapRpcLocalServer DapUiService;
@@ -49,6 +50,8 @@ class DapChainDashboardService : public DapRpcService
 
     DapChainHistoryHandler* m_pDapChainHistoryHandler {nullptr};
 
+    DapChainConsoleHandler* m_pDapChainConsoleHandler {nullptr};
+
 public:
     /// Standard сonstructor.
     explicit DapChainDashboardService();
@@ -92,6 +95,8 @@ public slots:
 
     QVariant getHistory() const;
 
+    QString getQueryResult(const QString& aQuery) const;
+
 private slots:
     void doRequestWallets();
     void doSendNewHistory(const QVariant& aData);
diff --git a/KelvinDashboardService/KelvinDashboardService.pro b/KelvinDashboardService/KelvinDashboardService.pro
index bce4996d1..e021179c1 100755
--- a/KelvinDashboardService/KelvinDashboardService.pro
+++ b/KelvinDashboardService/KelvinDashboardService.pro
@@ -48,7 +48,8 @@ SOURCES += \
     $$PWD/DapChainNode.cpp \
     $$PWD/DapChainNodeCache.cpp \
     $$PWD/DapChainWalletHandler.cpp \
-    $$PWD/DapChainLogHandler.cpp
+    $$PWD/DapChainLogHandler.cpp \
+    DapChainConsoleHandler.cpp
 
 HEADERS += \
     $$PWD/DapChainDashboardService.h \
@@ -57,7 +58,8 @@ HEADERS += \
     $$PWD/DapChainNodeCache.h \
     $$PWD/DapChainNodeNetworkHandler.h \
     $$PWD/DapChainWalletHandler.h \
-    $$PWD/DapChainLogHandler.h
+    $$PWD/DapChainLogHandler.h \
+    DapChainConsoleHandler.h
 
 include (../libdap/libdap.pri)
 include (../libdap-crypto/libdap-crypto.pri)
-- 
GitLab