app/sources/appsettings.cpp

66 lines
1.9 KiB
C++

#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
QString value = this->settingsManager->value(key, false).toString();
// 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);
if(value == "false") {
// 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;
}