2021-04-08 10:29:21 +08:00
|
|
|
#include "settings.h"
|
2021-04-13 11:23:37 +08:00
|
|
|
#include <QApplication>
|
2021-04-08 10:29:21 +08:00
|
|
|
#include <QTextCodec>
|
2021-04-13 11:23:37 +08:00
|
|
|
#include "utils.h"
|
2021-04-08 10:29:21 +08:00
|
|
|
|
|
|
|
Settings* pSettings;
|
|
|
|
|
|
|
|
Settings::Settings():
|
2021-04-13 11:23:37 +08:00
|
|
|
mSettings(QSettings::IniFormat,QSettings::UserScope,"Red Panda C++"),
|
|
|
|
mDirs(this),
|
|
|
|
mEditor(this)
|
2021-04-08 10:29:21 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
// default values for editors
|
2021-04-13 11:23:37 +08:00
|
|
|
mEditor.setDefault(SETTING_EDITOR_DEFAULT_ENCODING, QTextCodec::codecForLocale()->name());
|
|
|
|
mEditor.setDefault(SETTING_EDITOR_AUTO_INDENT,true);
|
|
|
|
|
2021-04-08 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-13 11:23:37 +08:00
|
|
|
void Settings::setDefault(const QString&group,const QString &key, const QVariant &value) {
|
|
|
|
mSettings.beginGroup(group);
|
|
|
|
auto act = finally([this] {
|
|
|
|
this->mSettings.endGroup();
|
|
|
|
});
|
2021-04-08 10:29:21 +08:00
|
|
|
if (!mSettings.contains(key)) {
|
|
|
|
mSettings.setValue(key,value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:23:37 +08:00
|
|
|
void Settings::setValue(const QString& group, const QString &key, const QVariant &value) {
|
|
|
|
mSettings.beginGroup(group);
|
|
|
|
auto act = finally([this] {
|
|
|
|
this->mSettings.endGroup();
|
|
|
|
});
|
2021-04-08 10:29:21 +08:00
|
|
|
mSettings.setValue(key,value);
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:23:37 +08:00
|
|
|
QVariant Settings::value(const QString& group, const QString &key) {
|
|
|
|
mSettings.beginGroup(group);
|
|
|
|
auto act = finally([this] {
|
|
|
|
this->mSettings.endGroup();
|
|
|
|
});
|
2021-04-08 10:29:21 +08:00
|
|
|
return mSettings.value(key);
|
|
|
|
}
|
2021-04-13 11:23:37 +08:00
|
|
|
|
|
|
|
Settings::Dirs &Settings::dirs()
|
|
|
|
{
|
|
|
|
return mDirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::Editor &Settings::editor()
|
|
|
|
{
|
|
|
|
return mEditor;
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::Dirs::Dirs(Settings *settings):
|
|
|
|
_Base(settings, SETTING_DIRS)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString Settings::Dirs::app() const
|
|
|
|
{
|
|
|
|
QApplication::instance()->applicationDirPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::_Base::_Base(Settings *settings, const QString &groupName):
|
|
|
|
mSettings(settings),
|
|
|
|
mGroup(groupName)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::_Base::setDefault(const QString &key, const QVariant &value)
|
|
|
|
{
|
|
|
|
mSettings->setDefault(mGroup,key,value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::_Base::setValue(const QString &key, const QVariant &value)
|
|
|
|
{
|
|
|
|
mSettings->setValue(mGroup,key,value);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Settings::_Base::value(const QString &key)
|
|
|
|
{
|
|
|
|
return mSettings->value(mGroup,key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::Editor::Editor(Settings *settings): _Base(settings, SETTING_EDITOR)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Settings::Editor::defaultEncoding()
|
|
|
|
{
|
|
|
|
return value(SETTING_EDITOR_DEFAULT_ENCODING).toByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::Editor::setDefaultEncoding(const QByteArray &encoding)
|
|
|
|
{
|
|
|
|
setValue(SETTING_EDITOR_DEFAULT_ENCODING,encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Settings::Editor::autoIndent()
|
|
|
|
{
|
|
|
|
return value(SETTING_EDITOR_AUTO_INDENT).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::Editor::setAutoIndent(bool indent)
|
|
|
|
{
|
|
|
|
setValue(SETTING_EDITOR_AUTO_INDENT,indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|