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#define SCORE_SETTINGS_PARAMETER_CPP(Type, ModelType, Name) \
135 Type ModelType::get##Name() const \
136 { \
137 return m_##Name; \
138 } \
139 \
140 void ModelType::init##Name(Type val) \
141 { \
142 m_##Name = val; \
143 \
144 Name##Changed(val); \
145 } \
146 void ModelType::set##Name(Type val) \
147 { \
148 if(val == m_##Name) \
149 return; \
150 \
151 m_##Name = val; \
152 \
153 QSettings s; \
154 s.setValue(Parameters::Name.key, QVariant::fromValue(m_##Name)); \
155 Name##Changed(val); \
156 }
157
158#define SCORE_PROJECTSETTINGS_PARAMETER_CPP(Type, ModelType, Name) \
159 Type ModelType::get##Name() const \
160 { \
161 return m_##Name; \
162 } \
163 \
164 void ModelType::init##Name(Type val) \
165 { \
166 m_##Name = val; \
167 Name##Changed(val); \
168 } \
169 void ModelType::set##Name(Type val) \
170 { \
171 if(val == m_##Name) \
172 return; \
173 \
174 init##Name(std::move(val)); \
175 }
Definition UuidKey.hpp:345
Definition SettingsDelegateModel.hpp:18
Base toolkit upon which the software is built.
Definition Application.cpp:83
Definition SettingsDelegateModel.hpp:36