53 lines
1.4 KiB
C++
53 lines
1.4 KiB
C++
#include "configinterface.h"
|
|
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
ConfigInterface::ConfigInterface(QObject *parent)
|
|
: QObject{parent}
|
|
{
|
|
}
|
|
|
|
QString ConfigInterface::fileName() const
|
|
{
|
|
return m_fileName;
|
|
}
|
|
|
|
void ConfigInterface::setFileName(const QString &newFileName)
|
|
{
|
|
if (m_fileName == newFileName)
|
|
return;
|
|
m_fileName = newFileName;
|
|
std::fstream input(newFileName.toStdString(), std::fstream::in);
|
|
if(!input.is_open()) {
|
|
return;
|
|
}
|
|
std::string data;
|
|
std::string temp;
|
|
while(std::getline(input, temp)) {
|
|
data += temp;
|
|
}
|
|
input.flush();
|
|
input.close();
|
|
QByteArray arr_data(data);
|
|
QJsonDocument doc = QJsonDocument::fromJson(arr_data);
|
|
// The following works even the qml part is not initailzed/connected
|
|
// do not change back to just emit config()
|
|
QMetaObject::invokeMethod(this, &ConfigInterface::config, Qt::QueuedConnection, doc.object());
|
|
emit fileNameChanged();
|
|
}
|
|
|
|
void ConfigInterface::saveConfig(QJsonObject config)
|
|
{
|
|
QJsonDocument doc;
|
|
doc.setObject(config);
|
|
std::string data = doc.toJson().toStdString();
|
|
std::fstream output(m_fileName.toStdString(), std::fstream::out);
|
|
if(!output.is_open()) {
|
|
return;
|
|
}
|
|
output << data;
|
|
output.flush();
|
|
output.close();
|
|
QMetaObject::invokeMethod(this, &ConfigInterface::config, Qt::QueuedConnection, doc.object());
|
|
}
|