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