2019-06-23 10:55:08 +02: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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-06-11 11:49:09 +02:00
|
|
|
QString value = this->settingsManager->value(key, false).toString();
|
2019-06-23 10:55:08 +02:00
|
|
|
// close the value-group
|
|
|
|
this->settingsManager->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);
|
2021-06-11 11:49:09 +02:00
|
|
|
if(value == "false") {
|
2019-06-23 10:55:08 +02:00
|
|
|
// if it is nor 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;
|
|
|
|
}
|