Settings are now stored, and reloaded at each execution

This commit is contained in:
Filippo Scognamiglio 2013-12-28 00:21:12 +01:00
parent 9bcfb93e1c
commit ae66af1104
4 changed files with 123 additions and 6 deletions

View File

@ -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

View File

@ -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();
}
}

View File

@ -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');
});
}
}

View File

@ -27,8 +27,6 @@ ApplicationWindow{
}
}
}
}
visible: true