mirror of
				https://github.com/Swordfish90/cool-retro-term.git
				synced 2025-10-31 07:04:20 +00:00 
			
		
		
		
	Initial (already working) implementation of json profile import/export.
This commit is contained in:
		
							
								
								
									
										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/ | ||||
|  | ||||
| INSTALLS += target | ||||
|  | ||||
| HEADERS += \ | ||||
|     FileIO.h | ||||
|   | ||||
| @@ -9,6 +9,8 @@ | ||||
| #include <QDebug> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #include <FileIO.h> | ||||
|  | ||||
|  | ||||
| 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/"); | ||||
|   | ||||
| @@ -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(){ | ||||
|   | ||||
| @@ -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{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user