-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgokindobject.cpp
More file actions
165 lines (149 loc) · 4.79 KB
/
algokindobject.cpp
File metadata and controls
165 lines (149 loc) · 4.79 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "algokindobject.h"
static constexpr const char* BLINK_ALGO_NAME = "blink";
static constexpr const char* BREATH_ALGO_NAME = "breath";
static constexpr const char* CONTDOWN_ALGO_NAME = "countdown";
static constexpr const char* CYLON_ALGO_NAME = "cylon";
static constexpr const char* OFF_ALGO_NAME = "off";
static constexpr const char* RAMPUP_ALGO_NAME = "rampup";
static constexpr const char* SOLID_ALGO_NAME = "solid";
const char* AlgoKindObject::getName(AlgoKind algo) {
switch(algo) {
case AlgoKind::Blink:
return BLINK_ALGO_NAME;
case AlgoKind::Breath:
return BREATH_ALGO_NAME;
case AlgoKind::CountDown:
return CONTDOWN_ALGO_NAME;
case AlgoKind::Cylon:
return CYLON_ALGO_NAME;
case AlgoKind::Off:
return OFF_ALGO_NAME;
case AlgoKind::RampUp:
return RAMPUP_ALGO_NAME;
case AlgoKind::Solid:
return SOLID_ALGO_NAME;
}
return OFF_ALGO_NAME;
}
auto AlgoKindObject::fromName(QString const& name) -> std::optional<AlgoKind> {
auto lcaseName = name.toLower();
if(lcaseName == BLINK_ALGO_NAME) {
return AlgoKind::Blink;
}
if(lcaseName == BREATH_ALGO_NAME) {
return AlgoKind::Breath;
}
if(lcaseName == CONTDOWN_ALGO_NAME) {
return AlgoKind::CountDown;
}
if(lcaseName == CYLON_ALGO_NAME) {
return AlgoKind::Cylon;
}
if(lcaseName == OFF_ALGO_NAME) {
return AlgoKind::Off;
}
if(lcaseName == RAMPUP_ALGO_NAME) {
return AlgoKind::RampUp;
}
if(lcaseName == SOLID_ALGO_NAME) {
return AlgoKind::Solid;
}
return std::optional<AlgoKind>();
}
static constexpr const char* BLINK_DISPLAY_NAME = "Blink";
static constexpr const char* BREATH_DISPLAY_NAME = "Breath";
static constexpr const char* CONTDOWN_DISPLAY_NAME = "Count Down";
static constexpr const char* CYLON_DISPLAY_NAME = "Cylon";
static constexpr const char* OFF_DISPLAY_NAME = "Off";
static constexpr const char* RAMPUP_DISPLAY_NAME = "Ramp Up";
static constexpr const char* SOLID_DISPLAY_NAME = "Solid";
const char* AlgoKindObject::getDisplayName(AlgoKind algo) {
switch(algo) {
case AlgoKind::Off:
return OFF_DISPLAY_NAME;
case AlgoKind::Blink:
return BLINK_DISPLAY_NAME;
case AlgoKind::Breath:
return BREATH_DISPLAY_NAME;
case AlgoKind::CountDown:
return CONTDOWN_DISPLAY_NAME;
case AlgoKind::Cylon:
return CYLON_DISPLAY_NAME;
case AlgoKind::RampUp:
return RAMPUP_DISPLAY_NAME;
case AlgoKind::Solid:
return SOLID_DISPLAY_NAME;
}
return BLINK_DISPLAY_NAME;
}
auto AlgoKindObject::fromDisplayName(QString const& name) -> AlgoKind {
if (name == BLINK_DISPLAY_NAME) {
return AlgoKind::Blink;
}
if (name == BREATH_DISPLAY_NAME) {
return AlgoKind::Breath;
}
if (name == CONTDOWN_DISPLAY_NAME) {
return AlgoKind::CountDown;
}
if (name == CYLON_DISPLAY_NAME) {
return AlgoKind::Cylon;
}
if (name == OFF_DISPLAY_NAME) {
return AlgoKind::Off;
}
if (name == RAMPUP_DISPLAY_NAME) {
return AlgoKind::RampUp;
}
if (name == SOLID_DISPLAY_NAME) {
return AlgoKind::Solid;
}
return AlgoKind::Off;
}
AlgoKindObject::AlgoKindObject(AlgoKind valueAndDefault, QString const& settingsKey, QObject *parent)
: QObject{parent}
, m_value(valueAndDefault)
, m_default(valueAndDefault)
, m_settingsKey(settingsKey)
{
}
auto AlgoKindObject::value() const -> AlgoKind{
return m_value;
}
QString const& AlgoKindObject::settingsKey() const {
return m_settingsKey;
}
auto AlgoKindObject::defaultValue() const -> AlgoKind {
return m_default;
}
void AlgoKindObject::setValue(AlgoKind value) {
if (m_value != value) {
m_value = value;
emit valueChanged(m_value);
}
}
void AlgoKindObject::attachSettings(QSettings *settings) {
auto defaultValueName = getName(m_default);
auto loadedSetting = settings->value(m_settingsKey, defaultValueName).value<QString>();
auto loadedAlgoKind = fromName(loadedSetting);
AlgoKind curve;
if(!loadedAlgoKind.has_value()) {
qDebug() << "Failed to load value from file for:"
<< m_settingsKey << "using default value"
<< getName(m_default)
<< "instead";
curve = m_default;
} else {
curve = loadedAlgoKind.value();
}
connect(this, &AlgoKindObject::valueChanged,
[=](AlgoKind newValue) {
qDebug() << "Updating " << m_settingsKey << ": " << getName(newValue);
settings->setValue(m_settingsKey, getName(newValue));
settings->sync();
});
// Updating local values
setValue(curve);
// Always write the setting back.
settings->setValue(m_settingsKey, getName(m_value));
}