-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSettingsEntry.cpp
More file actions
97 lines (82 loc) · 2.82 KB
/
Copy pathSettingsEntry.cpp
File metadata and controls
97 lines (82 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "src/util/SettingsEntry.h"
#include "src/util/SettingsSection.h"
#include "src/util/SettingsCategory.h"
#include "src/util/Settings.h"
//#include "src/Application.h" // an alternative way to obtain QSettings without category pointer
#include "qsettings.h"
SettingsEntry::SettingsEntry(SettingsSection& section, const QString& name, const QVariant& defaultValue, const QString& label,
const QString& help, const QString& widgetHint, bool changeRequiresRestart, int sortingWeight,
SettingOptionList&& optionList)
: _section(section)
, _name(name)
, _label(label.isEmpty() ? name : label)
, _help(help)
, _widgetHint(widgetHint)
, _value(defaultValue)
, _editedValue(defaultValue)
, _defaultValue(defaultValue)
, _optionList(std::move(optionList))
, _sortingWeight(sortingWeight)
, _changeRequiresRestart(changeRequiresRestart)
{
}
SettingsEntry::~SettingsEntry()
{
}
// Retrieves a unique path in the qsettings tree, this can be used by persistence providers for example
QString SettingsEntry::getPath() const
{
return _section.getPath() + "/" + _name;
}
bool SettingsEntry::sanitizeValue(QVariant& val) const
{
// Exactly the same type, can use as is
if (val.type() == _defaultValue.type())
return true;
// Compatible type is converted to our type
// NB: failed convert() results in an empty val of requested type
return (val.convert(_defaultValue.type()));
}
void SettingsEntry::setValue(const QVariant& val, bool storeImmediately)
{
const QVariant prevValue = _value;
QVariant copy = val;
if (!sanitizeValue(copy)) return;
_value = copy;
_editedValue = copy;
if (storeImmediately) store();
if (prevValue != _value)
emit valueChanged(_value);
}
void SettingsEntry::setEditedValue(const QVariant& val)
{
QVariant copy = val;
if (!sanitizeValue(copy)) return;
_editedValue = copy;
}
void SettingsEntry::applyChanges()
{
if (_value != _editedValue)
{
setValue(_editedValue);
if (_changeRequiresRestart)
{
//qobject_cast<Application*>(qApp)->getSettings().markRequiresRestart();
_section.getCategory().getSettings().markRequiresRestart();
}
}
}
void SettingsEntry::load()
{
//QSettings* qsettings = qobject_cast<Application*>(qApp)->getSettings().getQSettings();
QSettings* qsettings = _section.getCategory().getSettings().getQSettings();
QVariant val = qsettings->value(getPath());
if (val.isNull()) val = _defaultValue;
setValue(val, false);
}
void SettingsEntry::store()
{
//QSettings* qsettings = qobject_cast<Application*>(qApp)->getSettings().getQSettings();
QSettings* qsettings = _section.getCategory().getSettings().getQSettings();
qsettings->setValue(getPath(), _value);
}