diff --git a/app/FileIO.h b/app/FileIO.h new file mode 100644 index 0000000..8b612e1 --- /dev/null +++ b/app/FileIO.h @@ -0,0 +1,50 @@ +#ifndef FILEIO_H +#define FILEIO_H + +#include +#include +#include +#include + +class FileIO : public QObject +{ + Q_OBJECT + +public: + FileIO() {} + +public slots: + bool write(const QString& sourceUrl, const QString& data) { + if (sourceUrl.isEmpty()) + return false; + + QUrl url(sourceUrl); + QFile file(url.toLocalFile()); + if (!file.open(QFile::WriteOnly | QFile::Truncate)) + return false; + + QTextStream out(&file); + out << data; + file.close(); + return true; + } + + QString read(const QString& sourceUrl) { + if (sourceUrl.isEmpty()) + return ""; + + QUrl url(sourceUrl); + QFile file(url.toLocalFile()); + if (!file.open(QFile::ReadOnly)) + return ""; + + QTextStream in(&file); + QString result = in.readAll(); + + file.close(); + + return result; + } +}; + +#endif // FILEIO_H diff --git a/app/app.pro b/app/app.pro index d400020..502cbfe 100644 --- a/app/app.pro +++ b/app/app.pro @@ -15,3 +15,6 @@ RESOURCES += qml/resources.qrc target.path += /usr/bin/ INSTALLS += target + +HEADERS += \ + FileIO.h diff --git a/app/main.cpp b/app/main.cpp index ff93931..6f4912c 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -9,6 +9,8 @@ #include #include +#include + QString getNamedArgument(QStringList args, QString name) { int index = args.indexOf(name); @@ -37,6 +39,10 @@ int main(int argc, char *argv[]) engine.rootContext()->setContextProperty("workdir", getNamedArgument(args, "--workdir")); engine.rootContext()->setContextProperty("shellProgram", getNamedArgument(args, "--program")); + // Used to read and write files + FileIO fileIO; + engine.rootContext()->setContextProperty("fileio", &fileIO); + // Manage import paths for Linux and OSX. QStringList importPathList = engine.importPathList(); importPathList.prepend(QCoreApplication::applicationDirPath() + "/imports/"); diff --git a/app/qml/ApplicationSettings.qml b/app/qml/ApplicationSettings.qml index 0555478..8b7b504 100644 --- a/app/qml/ApplicationSettings.qml +++ b/app/qml/ApplicationSettings.qml @@ -181,8 +181,8 @@ Item{ return JSON.stringify(settings); } - function composeProfileString(){ - var settings = { + function composeProfileObject(){ + var profile = { background_color: _background_color, font_color: _font_color, brightness_flickering: brightness_flickering, @@ -205,7 +205,11 @@ Item{ fontIndex: fontIndexes[rasterization], fontWidth: fontWidth } - return JSON.stringify(settings); + return profile; + } + + function composeProfileString(){ + return JSON.stringify(composeProfileObject()); } function loadSettings(){ diff --git a/app/qml/SettingsGeneralTab.qml b/app/qml/SettingsGeneralTab.qml index 8c6cb47..73f4f6c 100644 --- a/app/qml/SettingsGeneralTab.qml +++ b/app/qml/SettingsGeneralTab.qml @@ -21,6 +21,7 @@ import QtQuick 2.2 import QtQuick.Controls 1.1 import QtQuick.Layouts 1.1 +import QtQuick.Dialogs 1.1 Tab{ ColumnLayout{ @@ -62,10 +63,67 @@ Tab{ } } } + RowLayout{ + Layout.fillWidth: true + Button{ + Layout.fillWidth: true + text: qsTr("Import From File") + onClicked: { + fileDialog.selectExisting = true; + fileDialog.callBack = function (url) {loadFile(url);}; + fileDialog.open(); + } + + function loadFile(url) { + console.log("Loading file: " + url); + var profileStirng = fileio.read(url); + shadersettings.loadProfileString(profileStirng); + } + } + Button{ + Layout.fillWidth: true + text: qsTr("Export To File") + onClicked: { + fileDialog.selectExisting = false; + fileDialog.callBack = function (url) {storeFile(url);}; + fileDialog.open(); + } + + function storeFile(url) { + console.log("Storing file: " + url); + var profileObject = shadersettings.composeProfileObject(); + fileio.write(url, JSON.stringify(profileObject, undefined, 2)); + } + } + } InsertNameDialog{ id: insertname onNameSelected: shadersettings.addNewCustomProfile(name) } + Loader { + property var callBack + property bool selectExisting: false + id: fileDialog + + sourceComponent: FileDialog{ + nameFilters: ["Json files (*.json)"] + selectMultiple: false + selectFolder: false + selectExisting: fileDialog.selectExisting + onAccepted: callBack(fileUrl); + } + + onSelectExistingChanged: reload() + + function open() { + item.open(); + } + + function reload() { + active = false; + active = true; + } + } } } GroupBox{