Skip to content

Commit

Permalink
Refs #2, Automatic load definition, working
Browse files Browse the repository at this point in the history
  • Loading branch information
doumdi committed Mar 15, 2024
1 parent 7755503 commit 4fa77d5
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 94 deletions.
9 changes: 5 additions & 4 deletions Frontend/DashboardsViewer/content/screens/Dashboard.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import DashboardsViewer.ConfigParser 1.0
DashboardForm {

id: dashboard
property string jsonFileName : "dashboard.json"
property string definition : ""

signal buttonClicked()

ConfigParser {
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)
Expand Down
10 changes: 0 additions & 10 deletions Frontend/DashboardsViewer/content/screens/DashboardForm.ui.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 4 additions & 16 deletions Frontend/DashboardsViewer/content/screens/DashboardSelector.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}



Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

0 comments on commit 4fa77d5

Please sign in to comment.