-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings_manager.cpp
105 lines (91 loc) · 2.79 KB
/
settings_manager.cpp
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
#include "settings_manager.h"
#include "debug.h"
void settings_manager::set(const QString &key, const QVariant &value)
{
QVariant old_value = get(key);
setValue(key, value);
settings_event event(key, value, old_value);
global_listeners.distribute_event(&event);
if(listeners.contains(key)){
listeners[key]->distribute_static_result_event(&event);
}
if(persistent_listeners.contains(key)){
persistent_listeners[key]->distribute_event(&event);
}
}
QVariant settings_manager::get(const QString &key)
{
return value(key);
}
void settings_manager::add_listener(QObject *object, const QStringList &keys)
{
for(const auto &key : keys){
add_listener_implementation(object, key, listeners);
}
}
void settings_manager::add_listener(QObject *object, const QString &key)
{
add_listener_implementation(object, key, listeners);
}
void settings_manager::remove_listener(QObject *object, const QStringList &keys)
{
for(const auto &key : keys){
remove_listener_implementation(object, key, listeners);
}
}
void settings_manager::remove_listener(QObject *object, const QString &key)
{
remove_listener_implementation(object, key, listeners);
}
void settings_manager::add_persistent_listener(QObject *object, const QStringList &keys)
{
for(const auto &key : keys){
add_listener_implementation(object, key, persistent_listeners);
}
}
void settings_manager::add_persistent_listener(QObject *object, const QString &key)
{
add_listener_implementation(object, key, persistent_listeners);
}
void settings_manager::remove_persistent_listener(QObject *object, const QStringList &keys)
{
for(const auto &key : keys){
remove_listener_implementation(object, key, persistent_listeners);
}
}
void settings_manager::remove_persistent_listener(QObject *object, const QString &key)
{
remove_listener_implementation(object, key, persistent_listeners);
}
void settings_manager::add_listener_implementation(QObject *object, const QString &key, listener_map &map)
{
if(key.isEmpty()){
global_listeners.add_to_group(object);
}else{
if(!map.contains(key)){
map.insert(key, new object_group(&parent));
}
object_group *group = map[key];
group->add_to_group(object);
if(group->size() == 1){
settings_manager settings;
QVariant value = settings.get(key);
settings_event event(key, value, value);
group->distribute_static_result_event(&event);
}
}
}
void settings_manager::remove_listener_implementation(QObject *object, const QString &key, listener_map &map)
{
if(key.isEmpty()){
global_listeners.remove_from_group(object);
}else{
if(map.contains(key)){
map[key]->remove_from_group(object);
}
}
}
settings_manager::listener_map settings_manager::listeners;
settings_manager::listener_map settings_manager::persistent_listeners;
object_group settings_manager::global_listeners;
QObject settings_manager::parent;