mirror of
https://github.com/Swordfish90/cool-retro-term.git
synced 2025-01-18 20:20:45 +00:00
Initial (already working) implementation of json profile import/export.
This commit is contained in:
parent
058bafe033
commit
90546e49e8
50
app/FileIO.h
Normal file
50
app/FileIO.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#ifndef FILEIO_H
|
||||||
|
#define FILEIO_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QTextStream>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
|
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
|
@ -15,3 +15,6 @@ RESOURCES += qml/resources.qrc
|
|||||||
target.path += /usr/bin/
|
target.path += /usr/bin/
|
||||||
|
|
||||||
INSTALLS += target
|
INSTALLS += target
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
FileIO.h
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <FileIO.h>
|
||||||
|
|
||||||
|
|
||||||
QString getNamedArgument(QStringList args, QString name) {
|
QString getNamedArgument(QStringList args, QString name) {
|
||||||
int index = args.indexOf(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("workdir", getNamedArgument(args, "--workdir"));
|
||||||
engine.rootContext()->setContextProperty("shellProgram", getNamedArgument(args, "--program"));
|
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.
|
// Manage import paths for Linux and OSX.
|
||||||
QStringList importPathList = engine.importPathList();
|
QStringList importPathList = engine.importPathList();
|
||||||
importPathList.prepend(QCoreApplication::applicationDirPath() + "/imports/");
|
importPathList.prepend(QCoreApplication::applicationDirPath() + "/imports/");
|
||||||
|
@ -181,8 +181,8 @@ Item{
|
|||||||
return JSON.stringify(settings);
|
return JSON.stringify(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
function composeProfileString(){
|
function composeProfileObject(){
|
||||||
var settings = {
|
var profile = {
|
||||||
background_color: _background_color,
|
background_color: _background_color,
|
||||||
font_color: _font_color,
|
font_color: _font_color,
|
||||||
brightness_flickering: brightness_flickering,
|
brightness_flickering: brightness_flickering,
|
||||||
@ -205,7 +205,11 @@ Item{
|
|||||||
fontIndex: fontIndexes[rasterization],
|
fontIndex: fontIndexes[rasterization],
|
||||||
fontWidth: fontWidth
|
fontWidth: fontWidth
|
||||||
}
|
}
|
||||||
return JSON.stringify(settings);
|
return profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
function composeProfileString(){
|
||||||
|
return JSON.stringify(composeProfileObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadSettings(){
|
function loadSettings(){
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
import QtQuick 2.2
|
import QtQuick 2.2
|
||||||
import QtQuick.Controls 1.1
|
import QtQuick.Controls 1.1
|
||||||
import QtQuick.Layouts 1.1
|
import QtQuick.Layouts 1.1
|
||||||
|
import QtQuick.Dialogs 1.1
|
||||||
|
|
||||||
Tab{
|
Tab{
|
||||||
ColumnLayout{
|
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{
|
InsertNameDialog{
|
||||||
id: insertname
|
id: insertname
|
||||||
onNameSelected: shadersettings.addNewCustomProfile(name)
|
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{
|
GroupBox{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user