-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunificationkindobject.cpp
More file actions
117 lines (102 loc) · 3.4 KB
/
unificationkindobject.cpp
File metadata and controls
117 lines (102 loc) · 3.4 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "unificationkindobject.h"
static constexpr const char* BOTHSAME_NAME = "bothsame";
static constexpr const char* SEPARATE_NAME = "separate";
static constexpr const char* SUPERSTRIP_NAME = "superstrip";
const char* UnificationKindObject::getName(Kind s) {
switch(s) {
case Kind::BothSame:
return BOTHSAME_NAME;
case Kind::Separate:
return SEPARATE_NAME;
case Kind::SuperStrip:
return SUPERSTRIP_NAME;
}
return BOTHSAME_NAME;
}
auto UnificationKindObject::fromName(QString const& s) -> std::optional<Kind> {
auto name = s.toLower();
if (name == BOTHSAME_NAME) {
return Kind::BothSame;
}
if (name == SEPARATE_NAME) {
return Kind::Separate;
}
if (name == SUPERSTRIP_NAME) {
return Kind::SuperStrip;
}
return std::optional<Kind>();
}
static constexpr const char* BOTHSAME_DISPLAY_NAME = "Both LED strips are the same";
static constexpr const char* SEPARATE_DISPLAY_NAME = "Using individual settings";
static constexpr const char* SUPERSTRIP_DISPLAY_NAME = "Treat as one large strip";
const char* UnificationKindObject::getDisplayName(Kind s) {
switch(s) {
case Kind::BothSame:
return BOTHSAME_DISPLAY_NAME;
case Kind::Separate:
return SEPARATE_DISPLAY_NAME;
case Kind::SuperStrip:
return SUPERSTRIP_DISPLAY_NAME;
}
return BOTHSAME_DISPLAY_NAME;
}
auto UnificationKindObject::fromDisplayName(QString const& s) -> Kind {
if(s == BOTHSAME_DISPLAY_NAME) {
return Kind::BothSame;
}
if(s == SEPARATE_DISPLAY_NAME) {
return Kind::Separate;
}
if(s == SUPERSTRIP_DISPLAY_NAME) {
return Kind::SuperStrip;
}
// Default behavior in the event that we don't recognize the display name.
return Kind::SuperStrip;
}
UnificationKindObject::UnificationKindObject(Kind valueAndDefault, QString const& settingsKey, QObject *parent)
: QObject{parent}
, m_value(valueAndDefault)
, m_default(valueAndDefault)
, m_settingsKey(settingsKey)
{
}
auto UnificationKindObject::value() const -> Kind {
return m_value;
}
QString const& UnificationKindObject::settingsKey() const {
return m_settingsKey;
}
auto UnificationKindObject::defaultValue() const -> Kind {
return m_default;
}
void UnificationKindObject::setValue(Kind value) {
if (m_value != value) {
m_value = value;
emit valueChanged(m_value);
}
}
void UnificationKindObject::attachSettings(QSettings *settings) {
auto defaultValueName = getName(m_default);
auto loadedSetting = settings->value(m_settingsKey, defaultValueName).value<QString>();
auto loadedCurve = fromName(loadedSetting);
Kind style;
if(!loadedCurve.has_value()) {
qDebug() << "Failed to load value from file for:"
<< m_settingsKey << "using default value"
<< getName(m_default)
<< "instead";
style = m_default;
} else {
style = loadedCurve.value();
}
connect(this, &UnificationKindObject::valueChanged,
[=](Kind newValue) {
qDebug() << "Updating " << m_settingsKey << ": " << getName(newValue);
settings->setValue(m_settingsKey, getName(newValue));
settings->sync();
});
// Updating local values
setValue(style);
// Always write the setting back.
settings->setValue(m_settingsKey, getName(m_value));
}