forked from jahnf/Projecteur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cc
327 lines (288 loc) · 10.3 KB
/
settings.cc
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// This file is part of Projecteur - https://github.com/jahnf/projecteur - See LICENSE.md and README.md
#include "settings.h"
#include <QCoreApplication>
#include <QQmlPropertyMap>
#include <QSettings>
#include <QDebug>
namespace {
namespace settings {
constexpr char showSpot[] = "showSpot";
constexpr char spotSize[] = "spotSize";
constexpr char showCenterDot[] = "showCenterDot";
constexpr char dotSize[] = "dotSize";
constexpr char dotColor[] = "dotColor";
constexpr char shadeColor[] = "shadeColor";
constexpr char shadeOpacity[] = "shadeOpacity";
constexpr char screen[] = "screen";
constexpr char cursor[] = "cursor";
constexpr char spotShape[] = "spotShape";
constexpr char spotRotation[] ="spotRotation";
namespace defaultValue {
constexpr bool showSpot = true;
constexpr int spotSize = 32;
constexpr bool showCenterDot = false;
constexpr int dotSize = 5;
constexpr auto dotColor = Qt::red;
constexpr char shadeColor[] = "#222222";
constexpr double shadeOpacity = 0.3;
constexpr int screen = 0;
constexpr Qt::CursorShape cursor = Qt::BlankCursor;
constexpr char spotShape[] = "spotshapes/Circle.qml";
constexpr double spotRotation = 0.0;
}
}
}
Settings::Settings(QObject* parent)
: QObject(parent)
, m_settings(new QSettings(QCoreApplication::applicationName(),
QCoreApplication::applicationName(), this))
, m_shapeSettingsRoot(new QQmlPropertyMap(this))
, m_spotShapes{ SpotShape(::settings::defaultValue::spotShape, "Circle", tr("Circle"), false),
SpotShape("spotshapes/Square.qml", "Square", tr("(Rounded) Square"), true,
{SpotShapeSetting(tr("Border-radius (%)"), "radius", 20, 0, 100, 0)} ),
SpotShape("spotshapes/Star.qml", "Star", tr("Star"), true,
{SpotShapeSetting(tr("Star points"), "points", 5, 3, 100, 0),
SpotShapeSetting(tr("Inner radius (%)"), "innerRadius", 50, 5, 100, 0)} ),
SpotShape("spotshapes/Ngon.qml", "Ngon", tr("N-gon"), true,
{SpotShapeSetting(tr("Sides"), "sides", 3, 3, 100, 0)} ) }
{
shapeSettingsInitialize();
load();
}
Settings::~Settings()
{
}
void Settings::setDefaults()
{
setShowSpot(settings::defaultValue::showSpot);
setSpotSize(settings::defaultValue::spotSize);
setShowCenterDot(settings::defaultValue::showCenterDot);
setDotSize(settings::defaultValue::dotSize);
setDotColor(QColor(settings::defaultValue::dotColor));
setShadeColor(QColor(settings::defaultValue::shadeColor));
setShadeOpacity(settings::defaultValue::shadeOpacity);
setScreen(settings::defaultValue::screen);
setCursor(settings::defaultValue::cursor);
setSpotShape(settings::defaultValue::spotShape);
setSpotRotation(settings::defaultValue::spotRotation);
shapeSettingsSetDefaults();
}
void Settings::shapeSettingsSetDefaults()
{
for (const auto& shape : m_spotShapes)
{
for (const auto& settingDefinition : shape.shapeSettings())
{
if (auto propertyMap = shapeSettings(shape.name()))
{
const QString key = settingDefinition.settingsKey();
if (propertyMap->property(key.toLocal8Bit()).isValid()) {
propertyMap->setProperty(key.toLocal8Bit(), settingDefinition.defaultValue());
} else {
propertyMap->insert(key, settingDefinition.defaultValue());
}
}
}
}
shapeSettingsPopulateRoot();
}
void Settings::shapeSettingsLoad()
{
for (const auto& shape : m_spotShapes)
{
for (const auto& settingDefinition : shape.shapeSettings())
{
if (auto propertyMap = shapeSettings(shape.name()))
{
const QString key = settingDefinition.settingsKey();
const QString settingsKey = QString("Shape.%1/%2").arg(shape.name()).arg(key);
const QVariant loadedValue = m_settings->value(settingsKey, settingDefinition.defaultValue());
if (propertyMap->property(key.toLocal8Bit()).isValid()) {
propertyMap->setProperty(key.toLocal8Bit(), loadedValue);
} else {
propertyMap->insert(key, loadedValue);
}
}
}
}
shapeSettingsPopulateRoot();
}
void Settings::shapeSettingsInitialize()
{
for (const auto& shape : m_spotShapes)
{
if (shape.shapeSettings().size() && !m_shapeSettings.contains(shape.name()))
{
auto pm = new QQmlPropertyMap(this);
connect(pm, &QQmlPropertyMap::valueChanged, [this, shape, pm](const QString& key, const QVariant& value)
{
const auto& s = shape.shapeSettings();
auto it = std::find_if(s.cbegin(), s.cend(), [&key](const SpotShapeSetting& sss) {
return key == sss.settingsKey();
});
if (it != s.cend())
{
if (it->defaultValue().type() == QVariant::Int)
{
const auto setValue = value.toInt();
const auto min = it->minValue().toInt();
const auto max = it->maxValue().toInt();
const auto newValue = qMin(qMax(min, setValue), max);
if (newValue != setValue) {
pm->setProperty(key.toLocal8Bit(), newValue);
}
m_settings->setValue(QString("Shape.%1/%2").arg(shape.name()).arg(key), newValue);
}
}
});
m_shapeSettings.insert(shape.name(), pm);
}
}
shapeSettingsPopulateRoot();
}
void Settings::load()
{
setShowSpot(m_settings->value(::settings::showSpot, settings::defaultValue::showSpot).toBool());
setSpotSize(m_settings->value(::settings::spotSize, settings::defaultValue::spotSize).toInt());
setShowCenterDot(m_settings->value(::settings::showCenterDot, settings::defaultValue::showCenterDot).toBool());
setDotSize(m_settings->value(::settings::dotSize, settings::defaultValue::dotSize).toInt());
setDotColor(m_settings->value(::settings::dotColor, QColor(settings::defaultValue::dotColor)).value<QColor>());
setShadeColor(m_settings->value(::settings::shadeColor, QColor(settings::defaultValue::shadeColor)).value<QColor>());
setShadeOpacity(m_settings->value(::settings::shadeOpacity, settings::defaultValue::shadeOpacity).toDouble());
setScreen(m_settings->value(::settings::screen, settings::defaultValue::screen).toInt());
setCursor(static_cast<Qt::CursorShape>(m_settings->value(::settings::cursor, static_cast<int>(settings::defaultValue::cursor)).toInt()));
setSpotShape(m_settings->value(::settings::spotShape, settings::defaultValue::spotShape).toString());
setSpotRotation(m_settings->value(::settings::spotRotation, settings::defaultValue::spotRotation).toDouble());
shapeSettingsLoad();
}
void Settings::setShowSpot(bool show)
{
if (show == m_showSpot)
return;
m_showSpot = show;
m_settings->setValue(::settings::showSpot, m_showSpot);
emit showSpotChanged(m_showSpot);
}
void Settings::setSpotSize(int size)
{
if (size == m_spotSize)
return;
m_spotSize = qMin(qMax(3, size), 100);
m_settings->setValue(::settings::spotSize, m_spotSize);
emit spotSizeChanged(m_spotSize);
}
void Settings::setShowCenterDot(bool show)
{
if (show == m_showCenterDot)
return;
m_showCenterDot = show;
m_settings->setValue(::settings::showCenterDot, m_showCenterDot);
emit showCenterDotChanged(m_showCenterDot);
}
void Settings::setDotSize(int size)
{
if (size == m_dotSize)
return;
m_dotSize = qMin(qMax(3, size), 100);
m_settings->setValue(::settings::dotSize, m_dotSize);
emit dotSizeChanged(m_dotSize);
}
void Settings::setDotColor(const QColor& color)
{
if (color == m_dotColor)
return;
m_dotColor = color;
m_settings->setValue(::settings::dotColor, m_dotColor);
emit dotColorChanged(m_dotColor);
}
void Settings::setShadeColor(const QColor& color)
{
if (color == m_shadeColor)
return;
m_shadeColor = color;
m_settings->setValue(::settings::shadeColor, m_shadeColor);
emit shadeColorChanged(m_shadeColor);
}
void Settings::setShadeOpacity(double opacity)
{
if (opacity > m_shadeOpacity || opacity < m_shadeOpacity)
{
m_shadeOpacity = qMin(qMax(0.0, opacity), 1.0);
m_settings->setValue(::settings::shadeOpacity, m_shadeOpacity);
emit shadeOpacityChanged(m_shadeOpacity);
}
}
void Settings::setScreen(int screen)
{
if (screen == m_screen)
return;
m_screen = qMin(qMax(0, screen), 10);
m_settings->setValue(::settings::screen, m_screen);
emit screenChanged(m_screen);
}
void Settings::setCursor(Qt::CursorShape cursor)
{
if (cursor == m_cursor)
return;
m_cursor = qMin(qMax(static_cast<Qt::CursorShape>(0), cursor), Qt::LastCursor);
m_settings->setValue(::settings::cursor, static_cast<int>(m_cursor));
emit cursorChanged(m_cursor);
}
void Settings::setSpotShape(const QString& spotShapeQmlComponent)
{
if (m_spotShape == spotShapeQmlComponent)
return;
const auto it = std::find_if(spotShapes().cbegin(), spotShapes().cend(),
[&spotShapeQmlComponent](const SpotShape& s) {
return s.qmlComponent() == spotShapeQmlComponent;
});
if (it != spotShapes().cend()) {
m_spotShape = it->qmlComponent();
m_settings->setValue(::settings::spotShape, m_spotShape);
emit spotShapeChanged(m_spotShape);
setSpotRotationAllowed(it->allowRotation());
}
}
void Settings::setSpotRotation(double rotation)
{
if (rotation > m_spotRotation || rotation < m_spotRotation)
{
m_spotRotation = qMin(qMax(0.0, rotation), 360.0);
m_settings->setValue(::settings::spotRotation, m_spotRotation);
emit spotRotationChanged(m_spotRotation);
}
}
QObject* Settings::shapeSettingsRootObject()
{
return m_shapeSettingsRoot;
}
QQmlPropertyMap* Settings::shapeSettings(const QString &shapeName)
{
const auto it = m_shapeSettings.find(shapeName);
if (it != m_shapeSettings.cend()) {
return it.value();
}
return nullptr;
}
void Settings::shapeSettingsPopulateRoot()
{
for (auto it = m_shapeSettings.cbegin(), end = m_shapeSettings.cend(); it != end; ++it)
{
if (m_shapeSettingsRoot->property(it.key().toLocal8Bit()).isValid()) {
m_shapeSettingsRoot->setProperty(it.key().toLocal8Bit(), QVariant::fromValue(it.value()));
} else {
m_shapeSettingsRoot->insert(it.key(), QVariant::fromValue(it.value()));
}
}
}
bool Settings::spotRotationAllowed() const
{
return m_spotRotationAllowed;
}
void Settings::setSpotRotationAllowed(bool allowed)
{
if (allowed == m_spotRotationAllowed)
return;
m_spotRotationAllowed = allowed;
emit spotRotationAllowedChanged(allowed);
}