From ae66af1104568f91cc9aa014582da117c1c57ebf Mon Sep 17 00:00:00 2001 From: Filippo Scognamiglio Date: Sat, 28 Dec 2013 00:21:12 +0100 Subject: [PATCH] Settings are now stored, and reloaded at each execution --- cool-old-term.pro | 4 +- qml/cool-old-term/ShaderSettings.qml | 53 ++++++++++++++++++++- qml/cool-old-term/Storage.qml | 70 ++++++++++++++++++++++++++++ qml/cool-old-term/TerminalWindow.qml | 2 - 4 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 qml/cool-old-term/Storage.qml diff --git a/cool-old-term.pro b/cool-old-term.pro index 9f69083..13fb266 100644 --- a/cool-old-term.pro +++ b/cool-old-term.pro @@ -47,5 +47,5 @@ OTHER_FILES += \ qml/cool-old-term/frames/images/black-frame.png \ qml/cool-old-term/frames/images/black-frame-normals.png \ qml/cool-old-term/frames/NoFrame.qml \ - qml/cool-old-term/MainContainer.qml \ - qml/cool-old-term/TerminalWindow.qml + qml/cool-old-term/TerminalWindow.qml \ + qml/cool-old-term/Storage.qml diff --git a/qml/cool-old-term/ShaderSettings.qml b/qml/cool-old-term/ShaderSettings.qml index 3dee4fa..815b3ac 100644 --- a/qml/cool-old-term/ShaderSettings.qml +++ b/qml/cool-old-term/ShaderSettings.qml @@ -3,8 +3,8 @@ import QtQuick 2.1 Item{ property real ambient_light: 0.2 - property color background_color: "#002200" - property color font_color: "#00ff00" + property string background_color: "#002200" + property string font_color: "#00ff00" property real screen_flickering: 0.1 property real noise_strength: 0.1 @@ -23,6 +23,7 @@ Item{ property var fonts_list: fontlist onFont_indexChanged: { + //Reload the window to avoid ugly glitches terminalwindowloader.source = ""; terminalwindowloader.source = "TerminalWindow.qml"; } @@ -63,4 +64,52 @@ Item{ pixelSize: 25 } } + + Storage{id: storage} + + function retrieveFromDB(){ + var settings = storage.getSetting("CURRENT_SETTINGS"); + if(!settings) return; + + settings = JSON.parse(settings); + + + ambient_light = settings.ambient_light ? settings.ambient_light : ambient_light; + background_color = settings.background_color ? settings.background_color : background_color; + font_color = settings.font_color ? settings.font_color : font_color; + + screen_flickering = settings.screen_flickering ? settings.screen_flickering : screen_flickering; + noise_strength = settings.noise_strength ? settings.noise_strength : noise_strength; + screen_distortion = settings.screen_distortion ? settings.screen_distortion : screen_distortion; + glowing_line_strength = settings.glowing_line_strength ? settings.glowing_line_strength : glowing_line_strength; + scanlines = settings.scanlines ? settings.scanlines : scanlines; + + frames_index = settings.frames_index ? settings.frames_index : frames_index; + font_index = settings.font_index ? settings.font_index : font_index; + } + + function storeToDb(){ + var settings = { + ambient_light : ambient_light, + background_color: background_color, + font_color: font_color, + screen_flickering: screen_flickering, + noise_strength: noise_strength, + screen_distortion: screen_distortion, + glowing_line_strength: glowing_line_strength, + scanlines: scanlines, + frames_index: frames_index, + font_index: font_index + } + + storage.setSetting("CURRENT_SETTINGS", JSON.stringify(settings)); + } + + Component.onCompleted: { + retrieveFromDB(); + } + Component.onDestruction: { + storeToDb(); + //storage.dropSettings(); + } } diff --git a/qml/cool-old-term/Storage.qml b/qml/cool-old-term/Storage.qml new file mode 100644 index 0000000..22aaa16 --- /dev/null +++ b/qml/cool-old-term/Storage.qml @@ -0,0 +1,70 @@ +import QtQuick 2.1 +import QtQuick.LocalStorage 2.0 + +Item { + property bool initialized: false + + function getDatabase() { + return LocalStorage.openDatabaseSync("coololdterm", "1.0", "StorageDatabase", 100000); + } + + // At the start of the application, we can initialize the tables we need if they haven't been created yet + function initialize() { + var db = getDatabase(); + db.transaction( + function(tx) { + // Create the settings table if it doesn't already exist + // If the table exists, this is skipped + tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)'); + }); + + initialized = true; + } + + // This function is used to write a setting into the database + function setSetting(setting, value) { + if(!initialized) initialize(); + + // setting: string representing the setting name (eg: “username”) + // value: string representing the value of the setting (eg: “myUsername”) + var db = getDatabase(); + var res = ""; + db.transaction(function(tx) { + var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]); + //console.log(rs.rowsAffected) + if (rs.rowsAffected > 0) { + res = "OK"; + } else { + res = "Error"; + } + } + ); + // The function returns “OK” if it was successful, or “Error” if it wasn't + return res; + } + // This function is used to retrieve a setting from the database + function getSetting(setting) { + if(!initialized) initialize(); + var db = getDatabase(); + var res=""; + db.transaction(function(tx) { + var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]); + if (rs.rows.length > 0) { + res = rs.rows.item(0).value; + } else { + res = undefined; + } + }) + // The function returns “Unknown” if the setting was not found in the database + // For more advanced projects, this should probably be handled through error codes + return res + } + + function dropSettings(){ + var db = getDatabase(); + db.transaction( + function(tx) { + tx.executeSql('DROP TABLE settings'); + }); + } +} diff --git a/qml/cool-old-term/TerminalWindow.qml b/qml/cool-old-term/TerminalWindow.qml index 95aa0ed..27cc056 100644 --- a/qml/cool-old-term/TerminalWindow.qml +++ b/qml/cool-old-term/TerminalWindow.qml @@ -27,8 +27,6 @@ ApplicationWindow{ } } } - - } visible: true