mathtrainingstuff/sources/appsettings.cpp

87 lines
2.5 KiB
C++
Raw Permalink Normal View History

2018-12-19 20:30:50 +01:00
#include "headers/appsettings.h"
AppSettings::AppSettings(QObject* parent)
:QObject(parent)
{
// This is the Constructor of the AppSettings class
// get writable path to store the settings.ini file
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
qDebug() << "app settings path: " << path;
// create or open the settings.ini file
this->settingsManager = new QSettings(path+"/settings.ini", QSettings::IniFormat);
// set the values to their defaults if they haven't been created yet
this->setDefault("highscore", "0");
// create or open the settings.ini file
this->themeSettingsManager = new QSettings(":/themes/default.ini", QSettings::IniFormat);
}
QString AppSettings::read(const QString &key)
{
// function to read values from the settings file
// open the value-group
this->settingsManager->beginGroup("AppSettings");
// read the value
QString value = this->settingsManager->value(key , false).toString();
// close the value-group
this->settingsManager->endGroup();
// return the value
return(value);
}
QString AppSettings::theme(QString key)
{
// function to get style settings from a theme file
// open the value-group
this->themeSettingsManager->beginGroup("MathTrainingStuffTheme");
// read the value
QString value = this->themeSettingsManager->value(key, false).toString();
// close the value-group
this->themeSettingsManager->endGroup();
// return the value
return(value);
}
void AppSettings::write(const QString &key, const QVariant &value)
{
// function to write values to the settings file
// open the value-group
this->settingsManager->beginGroup("AppSettings");
// write the value
this->settingsManager->setValue(key, value);
// close the value-group
this->settingsManager->endGroup();
}
void AppSettings::setDefault(const QString &key, const QVariant &defaultValue)
{
// function to create a key (/ setting) with a default value if it hasnt been ceated yet
// read the current value
QString value = this->read(key);
if(value == "false"){
// if it is not defined yet, the read function will return "false" (as a string)
// -> if that is the case -> create the key with the default value
this->write(key, defaultValue);
}
}
AppSettings::~AppSettings()
{
// This is the Destructor of the AppSettings class
// delete the settings manager
delete settingsManager;
// delete the theme Manager
delete themeSettingsManager;
}