-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettingsmanager.cpp
More file actions
78 lines (63 loc) · 2.24 KB
/
Copy pathsettingsmanager.cpp
File metadata and controls
78 lines (63 loc) · 2.24 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
#include "settingsmanager.h"
SettingsManager::SettingsManager(QObject *parent)
: QObject(parent)
{
qDebug() << "Settings file location:" << m_settings.fileName();
loadSettings();
}
SettingsManager::~SettingsManager()
{
m_settings.sync(); // Explicit sync if needed
}
void SettingsManager::loadSettings()
{
qDebug() << "Loading settings...";
m_value.clear();
// Default values if keys are not found
if(!m_settings.contains("recentDevice/type")) m_settings.setValue("recentDevice/type","");
if(!m_settings.contains("recentDevice/name")) m_settings.setValue("recentDevice/name","");
if(!m_settings.contains("recentDevice/address")) m_settings.setValue("recentDevice/address","");
// Default values if keys are not found
m_value["recentDevice/type"] = m_settings.value("recentDevice/type","");
m_value["recentDevice/name"] = m_settings.value("recentDevice/name","");
m_value["recentDevice/address"] = m_settings.value("recentDevice/address","");
// qDebug() << "Loaded settings map:" << m_value;
}
QVariantMap SettingsManager::value() const
{
return m_value;
}
void SettingsManager::setValue(const QVariantMap &settings)
{
m_value = settings;
}
void SettingsManager::setSetting(const QString &key, const QVariant &value)
{
// qDebug() << "setsettings key" << key << "val=" << value ;
if (!m_value.contains(key) || m_value.value(key) != value)
{
if (!key.isEmpty())
{
m_value[key] = value;
m_settings.setValue(key, value);
// m_settings.sync(); // for immediate write
qDebug() << "Setting changed and saved:" << key << "=" << value << "to QSettings key" << key;
}
else
{
qDebug() << "Warning: No QSettings key mapping found for QML setting key:" << key;
// Optionally, you could store it in QSettings under the same key if it's simple.
// m_settings.setValue(key, value);
}
emit settingChanged(key);
emit valueChanged();
}
}
QVariant SettingsManager::getSetting(const QString &key, const QVariant &defaultValue) const
{
if (m_value.contains(key))
{
return m_value.value(key);
}
return m_settings.value(key, defaultValue);
}