mirror of
				https://github.com/Swordfish90/cool-retro-term.git
				synced 2025-10-31 07:04:20 +00:00 
			
		
		
		
	Improvement: initial implementation of profiles json IO.
This commit is contained in:
		| @@ -2,7 +2,12 @@ QT += qml quick widgets sql | ||||
| TARGET = cool-retro-term  | ||||
|  | ||||
| DESTDIR = $$OUT_PWD/../ | ||||
| SOURCES = main.cpp | ||||
|  | ||||
| HEADERS += \ | ||||
|     fileio.h | ||||
|  | ||||
| SOURCES = main.cpp \ | ||||
|     fileio.cpp | ||||
|  | ||||
| macx:ICON = icons/crt.icns | ||||
|  | ||||
|   | ||||
							
								
								
									
										37
									
								
								app/fileio.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								app/fileio.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| #include "fileio.h" | ||||
|  | ||||
| FileIO::FileIO() | ||||
| { | ||||
| } | ||||
|  | ||||
| bool FileIO::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 FileIO::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; | ||||
| } | ||||
							
								
								
									
										21
									
								
								app/fileio.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								app/fileio.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| #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); | ||||
|     QString read(const QString& sourceUrl); | ||||
| }; | ||||
|  | ||||
| #endif // FILEIO_H | ||||
| @@ -9,6 +9,7 @@ | ||||
| #include <QDebug> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #include <fileio.h> | ||||
|  | ||||
| QString getNamedArgument(QStringList args, QString name, QString defaultName) | ||||
| { | ||||
| @@ -26,6 +27,7 @@ int main(int argc, char *argv[]) | ||||
|     setenv("QT_QPA_PLATFORMTHEME", "", 1); | ||||
|     QApplication app(argc, argv); | ||||
|     QQmlApplicationEngine engine; | ||||
|     FileIO fileIO; | ||||
|  | ||||
|     // Manage command line arguments from the cpp side | ||||
|     QStringList args = app.arguments(); | ||||
| @@ -52,6 +54,7 @@ int main(int argc, char *argv[]) | ||||
|     engine.rootContext()->setContextProperty("defaultCmdArgs", commandArgs); | ||||
|  | ||||
|     engine.rootContext()->setContextProperty("workdir", getNamedArgument(args, "--workdir", "$HOME")); | ||||
|     engine.rootContext()->setContextProperty("fileIO", &fileIO); | ||||
|  | ||||
|     // Manage import paths for Linux and OSX. | ||||
|     QStringList importPathList = engine.importPathList(); | ||||
|   | ||||
| @@ -208,7 +208,7 @@ QtObject{ | ||||
|         return stringify(settings); | ||||
|     } | ||||
|  | ||||
|     function composeProfileString(){ | ||||
|     function composeProfileObject(){ | ||||
|         var settings = { | ||||
|             backgroundColor: _backgroundColor, | ||||
|             fontColor: _fontColor, | ||||
| @@ -232,7 +232,11 @@ QtObject{ | ||||
|             fontName: fontNames[rasterization], | ||||
|             fontWidth: fontWidth | ||||
|         } | ||||
|         return stringify(settings); | ||||
|         return settings; | ||||
|     } | ||||
|  | ||||
|     function composeProfileString() { | ||||
|         return 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{ | ||||
| @@ -77,10 +78,62 @@ Tab{ | ||||
|                             profilesView.activated(currentIndex); | ||||
|                         } | ||||
|                     } | ||||
|                     Button{ | ||||
|                         text: qsTr("Import") | ||||
|                         onClicked: { | ||||
|                             fileDialog.selectExisting = true; | ||||
|                             fileDialog.callBack = function (url) {loadFile(url);}; | ||||
|                             fileDialog.open(); | ||||
|                         } | ||||
|                         function loadFile(url) { | ||||
|                             if (true) | ||||
|                                 console.log("Loading file: " + url); | ||||
|                             var profileStirng = fileIO.read(url); | ||||
|                             appSettings.loadProfileString(profileStirng); | ||||
|                         } | ||||
|                     } | ||||
|                     Button{ | ||||
|                         text: qsTr("Export") | ||||
|                         onClicked: { | ||||
|                             fileDialog.selectExisting = false; | ||||
|                             fileDialog.callBack = function (url) {storeFile(url);}; | ||||
|                             fileDialog.open(); | ||||
|                         } | ||||
|                         function storeFile(url) { | ||||
|                             if (true) | ||||
|                                 console.log("Storing file: " + url); | ||||
|                             var profileObject = appSettings.composeProfileObject(); | ||||
|                             fileIO.write(url, JSON.stringify(profileObject, undefined, 2)); | ||||
|                         } | ||||
|                     } | ||||
|                     InsertNameDialog{ | ||||
|                         id: insertname | ||||
|                         onNameSelected: appSettings.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; | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user