diff --git a/Frontend/DashboardsViewer/content/screens/Dashboard.qml b/Frontend/DashboardsViewer/content/screens/Dashboard.qml index 55a0880..df22a82 100644 --- a/Frontend/DashboardsViewer/content/screens/Dashboard.qml +++ b/Frontend/DashboardsViewer/content/screens/Dashboard.qml @@ -6,7 +6,7 @@ import DashboardsViewer.ConfigParser 1.0 DashboardForm { id: dashboard - property string jsonFileName : "dashboard.json" + property string definition : "" signal buttonClicked() @@ -14,9 +14,10 @@ DashboardForm { id: parser } - loadButton.onClicked: function() { - console.log("should load document", jsonFileName) - var dynamicQML = parser.parseConfig(jsonFileName); + function loadDocument() { + console.log("should load document", definition) + + var dynamicQML = parser.parseConfigString(definition); console.log("dynamicQML", dynamicQML) if (dynamicQML.length > 0) diff --git a/Frontend/DashboardsViewer/content/screens/DashboardForm.ui.qml b/Frontend/DashboardsViewer/content/screens/DashboardForm.ui.qml index 555bbd3..120f717 100644 --- a/Frontend/DashboardsViewer/content/screens/DashboardForm.ui.qml +++ b/Frontend/DashboardsViewer/content/screens/DashboardForm.ui.qml @@ -15,7 +15,6 @@ Item { height: 768 anchors.fill: parent - property alias loadButton: loadButton property alias closeButton: closeButton property alias mainView: dashboardStackView property alias dashboardStackView: dashboardStackView @@ -30,15 +29,6 @@ Item { horizontalAlignment: Text.AlignHCenter } - Button { - id: loadButton - anchors.left: parent.left - anchors.top: parent.top - width: 150 - height: 60 - text: qsTr("Load") - } - Button { id: closeButton anchors.left: loadButton.right diff --git a/Frontend/DashboardsViewer/content/screens/DashboardSelector.qml b/Frontend/DashboardsViewer/content/screens/DashboardSelector.qml index 18ef131..eb7a8b5 100644 --- a/Frontend/DashboardsViewer/content/screens/DashboardSelector.qml +++ b/Frontend/DashboardsViewer/content/screens/DashboardSelector.qml @@ -5,18 +5,6 @@ import "../dataSources" DashboardSelectorForm { id:selectorForm - /* - btnOK.onClicked:{ - stackview.push("Dashboard.qml") - - // get current pushed element - var currentElement = stackview.currentItem; - - // Set the element property - currentElement.jsonFileName = fileName; - } - */ - cmbSites.onCurrentIndexChanged: function() { console.log("cmbSites.onCurrentIndexChanged", cmbSites.currentIndex); @@ -53,12 +41,12 @@ DashboardSelectorForm { onItemClicked: function(id, definition){ stackview.push("Dashboard.qml") - // get current pushed element + // Get the current pushed element (the Dashboard) var currentElement = stackview.currentItem; - // Set the element property - // TODO: Handle string definition direction - //currentElement.jsonFileName = definition; + // Set the Actual definition + currentElement.definition = definition; + currentElement.loadDocument(); } diff --git a/Frontend/DashboardsViewer/qml_cpp_modules/config_parser/src/ConfigParser.cpp b/Frontend/DashboardsViewer/qml_cpp_modules/config_parser/src/ConfigParser.cpp index d424198..23c99ef 100644 --- a/Frontend/DashboardsViewer/qml_cpp_modules/config_parser/src/ConfigParser.cpp +++ b/Frontend/DashboardsViewer/qml_cpp_modules/config_parser/src/ConfigParser.cpp @@ -29,8 +29,14 @@ bool ConfigParser::isValidString(const QString &input) { return regex.match(input).hasMatch(); } +QVariantList ConfigParser::parseConfigString(const QString &configString) +{ + //Convert input string to byte array + return processConfigByteArray(configString.toUtf8()); +} + -QVariantList ConfigParser::parseConfig(const QString &configPath) +QVariantList ConfigParser::parseConfigFile(const QString &configPath) { qDebug() << "ConfigParser::parseConfig() called with configPath: " << configPath; @@ -44,69 +50,8 @@ QVariantList ConfigParser::parseConfig(const QString &configPath) //Read JSON file QByteArray data = file.readAll(); - QJsonParseError error; - QJsonDocument doc = QJsonDocument::fromJson(data, &error); + return processConfigByteArray(data); - qDebug() << "loading document with error : " << error.errorString(); - if (error.error != QJsonParseError::NoError) - { - qDebug() << "Error: Unable to parse JSON file: " << configPath; - return QVariantList(); - } - - QVariantList output; - QJsonObject json = doc.object(); - - // Create a QTextStream to operate on the buffer - QBuffer buffer; - buffer.open(QIODevice::ReadWrite); - QTextStream textStream(&buffer); - - // Write import statements - textStream << "import QtQuick;\n"; - textStream << "import QtQuick.Controls;\n"; - textStream << "import QtQuick.Layouts;\n"; - textStream << "import OpenTeraLibs.UserClient;\n"; - textStream << "import DashboardsViewer;\n"; - textStream << "import content;\n"; - - // Write the root object, make sure it will fill parent - textStream << "BaseWidget { //Begin root object \n"; - textStream << " id: rootObject;\n"; - textStream << " anchors.fill: parent;\n"; - - // Write Main Layout - QJsonObject layout = json["layout"].toObject(); - writeLayout(layout, textStream); - - // Write dataSources - QJsonArray dataSources = json["dataSources"].toArray(); - qDebug() << "data-sources array size: " << dataSources.size(); - writeDataSources(dataSources, textStream); - - // Write connections - QJsonArray connections = json["connections"].toArray(); - qDebug() << "connections array size: " << connections.size(); - writeConnections(connections, textStream); - - // End root object - textStream << "} // End Root Object\n"; - - - textStream.flush(); - - // Reset the buffer position to the start of the buffer - buffer.seek(0); - // Read the buffer contents into a string - QString qmlString = buffer.readAll(); - - qDebug() << "qmlString: " << qmlString; - - // Add the string to the output list - output.append(QVariant(qmlString)); - - //Return all generated widgets in a string list - return output; } void ConfigParser::writeLayout(const QJsonObject &layout, QTextStream &stream) @@ -319,5 +264,72 @@ void ConfigParser::writeDataSources(const QJsonArray &dataSources, QTextStream & } } +QVariantList ConfigParser::processConfigByteArray(const QByteArray &data) +{ + QJsonParseError error; + QJsonDocument doc = QJsonDocument::fromJson(data, &error); + + qDebug() << "loading document with error : " << error.errorString(); + if (error.error != QJsonParseError::NoError) + { + qDebug() << "Error: Unable to parse json data" << data; + return QVariantList(); + } + + QVariantList output; + QJsonObject json = doc.object(); + + // Create a QTextStream to operate on the buffer + QBuffer buffer; + buffer.open(QIODevice::ReadWrite); + QTextStream textStream(&buffer); + + // Write import statements + textStream << "import QtQuick;\n"; + textStream << "import QtQuick.Controls;\n"; + textStream << "import QtQuick.Layouts;\n"; + textStream << "import OpenTeraLibs.UserClient;\n"; + textStream << "import DashboardsViewer;\n"; + textStream << "import content;\n"; + + // Write the root object, make sure it will fill parent + textStream << "BaseWidget { //Begin root object \n"; + textStream << " id: rootObject;\n"; + textStream << " anchors.fill: parent;\n"; + + // Write Main Layout + QJsonObject layout = json["layout"].toObject(); + writeLayout(layout, textStream); + + // Write dataSources + QJsonArray dataSources = json["dataSources"].toArray(); + qDebug() << "data-sources array size: " << dataSources.size(); + writeDataSources(dataSources, textStream); + + // Write connections + QJsonArray connections = json["connections"].toArray(); + qDebug() << "connections array size: " << connections.size(); + writeConnections(connections, textStream); + + // End root object + textStream << "} // End Root Object\n"; + + + textStream.flush(); + + // Reset the buffer position to the start of the buffer + buffer.seek(0); + // Read the buffer contents into a string + QString qmlString = buffer.readAll(); + + qDebug() << "qmlString: " << qmlString; + + // Add the string to the output list + output.append(QVariant(qmlString)); + + //Return all generated widgets in a string list + return output; +} + diff --git a/Frontend/DashboardsViewer/qml_cpp_modules/config_parser/src/ConfigParser.h b/Frontend/DashboardsViewer/qml_cpp_modules/config_parser/src/ConfigParser.h index 3d2f6c7..9a77fd0 100644 --- a/Frontend/DashboardsViewer/qml_cpp_modules/config_parser/src/ConfigParser.h +++ b/Frontend/DashboardsViewer/qml_cpp_modules/config_parser/src/ConfigParser.h @@ -17,7 +17,8 @@ class ConfigParser : public QObject explicit ConfigParser(QObject *parent = nullptr); ~ConfigParser() override; - Q_INVOKABLE QVariantList parseConfig(const QString &configPath); + Q_INVOKABLE QVariantList parseConfigFile(const QString &configPath); + Q_INVOKABLE QVariantList parseConfigString(const QString &configString); private: @@ -27,6 +28,8 @@ class ConfigParser : public QObject void writeProperties(const QJsonObject &properties, QTextStream &stream); void writeConnections(const QJsonArray &connections, QTextStream &stream); void writeDataSources(const QJsonArray &dataSources, QTextStream &stream); + + QVariantList processConfigByteArray(const QByteArray &data); }; #endif