Loading...
Searching...
No Matches
SettingsDelegateModel.hpp
1#pragma once
2#include <score/command/SettingsCommand.hpp>
3#include <score/plugins/UuidKey.hpp>
4
5#include <ossia/detail/for_each_in_tuple.hpp>
6
7#include <QObject>
8#include <QSettings>
9
10#include <score_lib_base_export.h>
11
12#include <verdigris>
13
14namespace score
15{
16class SettingsDelegateFactory;
17class SCORE_LIB_BASE_EXPORT SettingsDelegateModel : public QObject
18{
19public:
20 explicit SettingsDelegateModel(
21 const UuidKey<SettingsDelegateFactory>& k, QObject* parent)
22 : QObject{parent}
23 , m_key{k}
24 {
25 }
26
27 virtual ~SettingsDelegateModel();
28 UuidKey<SettingsDelegateFactory> concreteKey() const noexcept { return m_key; }
29
30private:
32};
33
34template <typename Parameter>
36{
37public:
38 using parameter_type = Parameter;
39 using model_type = typename Parameter::model_type;
40 using data_type = typename Parameter::param_type;
41 using argument_type = typename boost::call_traits<data_type>::param_type;
42 QString key;
43 data_type def;
44};
45
46template <typename T>
48
49#if !defined(__EMSCRIPTEN__)
50template <typename T, typename Model>
51void setupDefaultSettings(QSettings& set, const T& tuple, Model& model)
52{
53 ossia::for_each_in_tuple(tuple, [&](auto& e) {
54 using type = std::remove_reference_t<decltype(e)>;
55 using data_type = typename type::data_type;
56 using param_type = typename type::parameter_type;
57
58 // If we cannot find the key, it means that it's a new setting.
59 // Hence we set the default value both in the QSettings and in the model.
60 if(!set.contains(e.key))
61 {
62 set.setValue(e.key, QVariant::fromValue(e.def));
63 (model.*param_type::init)(e.def);
64 }
65 else
66 {
67 // We fetch the value from the settings.
68 auto val = set.value(e.key).template value<data_type>();
69 (model.*param_type::init)(val);
70 }
71 });
72}
73#else
74template <typename T, typename Model>
75void setupDefaultSettings(QSettings&, const T& tuple, Model& model)
76{
77 ossia::for_each_in_tuple(tuple, [&](auto& e) {
78 using type = std::remove_reference_t<decltype(e)>;
79 using data_type = typename type::data_type;
80 using param_type = typename type::parameter_type;
81 (model.*param_type::set)(e.def);
82 });
83}
84#endif
85}
86
87#define SETTINGS_PARAMETER_IMPL(Name) const score::sp<Model::p_##Name> Name
88
89#define SCORE_SETTINGS_COMMAND(ModelType, Name) \
90 struct Set##ModelType##Name final \
91 : public score::SettingsCommand<ModelType::p_##Name> \
92 { \
93 static constexpr const bool is_deferred = false; \
94 SCORE_SETTINGS_COMMAND_DECL(Set##ModelType##Name) \
95 };
96
97#define SCORE_SETTINGS_PARAMETER(ModelType, Name) SCORE_SETTINGS_COMMAND(ModelType, Name)
98
99#define SCORE_SETTINGS_DEFERRED_COMMAND(ModelType, Name) \
100 struct Set##ModelType##Name final \
101 : public score::SettingsCommand<ModelType::p_##Name> \
102 { \
103 static constexpr const bool is_deferred = true; \
104 SCORE_SETTINGS_COMMAND_DECL(Set##ModelType##Name) \
105 };
106
107#define SCORE_SETTINGS_DEFERRED_PARAMETER(ModelType, Name) \
108 SCORE_SETTINGS_DEFERRED_COMMAND(ModelType, Name)
109
110#define SCORE_SETTINGS_PROPERTY(Type, Name) \
111 W_PROPERTY( \
112 Type, Name, &W_ThisType::get##Name, &W_ThisType::set##Name, W_Notify, \
113 &W_ThisType::Name##Changed) \
114 struct p_##Name \
115 { \
116 using param_type = Type; \
117 using model_type = W_ThisType; \
118 static const constexpr auto name = #Name; \
119 static const constexpr auto get = &W_ThisType::get##Name; \
120 static const constexpr auto set = &W_ThisType::set##Name; \
121 static const constexpr auto notify = &W_ThisType::Name##Changed; \
122 static const constexpr auto init = &W_ThisType::init##Name; \
123 };
124
125#define SCORE_SETTINGS_PARAMETER_HPP(Export, Type, Name) \
126public: \
127 Type get##Name() const; \
128 void init##Name(Type); \
129 void set##Name(Type); \
130 void Name##Changed(Type arg) E_SIGNAL2(Export##S, Export, Name##Changed, arg); \
131 SCORE_SETTINGS_PROPERTY(Type, Name) \
132private:
133
134#if !defined(__EMSCRIPTEN__)
135#define SCORE_SETTINGS_PARAMETER_CPP(Type, ModelType, Name) \
136 Type ModelType::get##Name() const \
137 { \
138 return m_##Name; \
139 } \
140 \
141 void ModelType::init##Name(Type val) \
142 { \
143 m_##Name = val; \
144 \
145 Name##Changed(val); \
146 } \
147 void ModelType::set##Name(Type val) \
148 { \
149 if(val == m_##Name) \
150 return; \
151 \
152 m_##Name = val; \
153 \
154 QSettings s; \
155 s.setValue(Parameters::Name.key, QVariant::fromValue(m_##Name)); \
156 Name##Changed(val); \
157 }
158#else
159#define SCORE_SETTINGS_PARAMETER_CPP(Type, ModelType, Name) \
160 Type ModelType::get##Name() const \
161 { \
162 return m_##Name; \
163 } \
164 \
165 void ModelType::init##Name(Type val) \
166 { \
167 m_##Name = val; \
168 Name##Changed(val); \
169 } \
170 void ModelType::set##Name(Type val) \
171 { \
172 if(val == m_##Name) \
173 return; \
174 \
175 init##Name(std::move(val)); \
176 }
177#endif
178
179#define SCORE_PROJECTSETTINGS_PARAMETER_CPP(Type, ModelType, Name) \
180 Type ModelType::get##Name() const \
181 { \
182 return m_##Name; \
183 } \
184 \
185 void ModelType::init##Name(Type val) \
186 { \
187 m_##Name = val; \
188 Name##Changed(val); \
189 } \
190 void ModelType::set##Name(Type val) \
191 { \
192 if(val == m_##Name) \
193 return; \
194 \
195 init##Name(std::move(val)); \
196 }
Definition UuidKey.hpp:345
Definition SettingsDelegateModel.hpp:18
Base toolkit upon which the software is built.
Definition Application.cpp:112
Definition SettingsDelegateModel.hpp:36