Loading...
Searching...
No Matches
ControlWidgetDomains.hpp
1#pragma once
2#include <Process/Dataflow/Port.hpp>
3
4#include <score/tools/Debug.hpp>
5
6#include <ossia/detail/math.hpp>
7#include <ossia/network/domain/domain.hpp>
8
9#include <QDoubleSpinBox>
10#include <QSpinBox>
11
12#include <type_traits>
13
14namespace WidgetFactory
15{
16
17template <typename T>
18static constexpr auto getMin(auto& slider) noexcept
19{
20 if constexpr(requires { slider.getMin(); })
21 return slider.getMin();
22 else
23 return slider.domain().get().template convert_min<T>();
24}
25
26template <typename T>
27static constexpr auto getMax(auto& slider) noexcept
28{
29 if constexpr(requires { slider.getMax(); })
30 return slider.getMax();
31 else
32 return slider.domain().get().template convert_max<T>();
33}
34
35template <typename T>
36static constexpr auto getInit(auto& slider) noexcept
37{
38 if constexpr(requires { slider.getInit(); })
39 return slider.getInit();
40 else if constexpr(requires { slider.init(); })
41 return ossia::convert<T>(slider.init());
42 else
43 return getMin<T>(slider);
44}
45
47{
48 static constexpr double to01(double min, double range, double val) noexcept
49 {
50 return (val - min) / range;
51 }
52
53 static constexpr double from01(double min, double range, double val) noexcept
54 {
55 return min + val * range;
56 }
57
58 template <typename T>
59 static double to01(const T& slider, double val) noexcept
60 {
61 auto min = getMin<double>(slider);
62 auto max = getMax<double>(slider);
63 if(max - min == 0)
64 max = min + 1;
65 return to01(min, max - min, val);
66 }
67
68 template <typename T>
69 static double from01(const T& slider, double val) noexcept
70 {
71 auto min = getMin<double>(slider);
72 auto max = getMax<double>(slider);
73 if(max - min == 0)
74 max = min + 1;
75 return from01(min, max - min, val);
76 }
77
78 template <typename T>
79 static std::array<double, 2> to01(const T& slider, ossia::vec2f val) noexcept
80 {
81 std::array<double, 2> res;
82 const auto min = getMin<ossia::vec2f>(slider);
83 const auto max = getMax<ossia::vec2f>(slider);
84 res[0] = to01(min[0], double(max[0]) - double(min[0]), val[0]);
85 res[1] = to01(min[1], double(max[1]) - double(min[1]), val[1]);
86 return res;
87 }
88
89 template <typename T>
90 static ossia::vec2f from01(const T& slider, std::array<double, 2> val) noexcept
91 {
92 ossia::vec2f res;
93 const auto min = getMin<ossia::vec2f>(slider);
94 const auto max = getMax<ossia::vec2f>(slider);
95 res[0] = from01(min[0], double(max[0]) - double(min[0]), val[0]);
96 res[1] = from01(min[1], double(max[1]) - double(min[1]), val[1]);
97 return res;
98 }
99
100 template <typename T>
101 static std::array<double, 3> to01(const T& slider, ossia::vec3f val) noexcept
102 {
103 std::array<double, 3> res;
104 const auto min = getMin<ossia::vec3f>(slider);
105 const auto max = getMax<ossia::vec3f>(slider);
106 res[0] = to01(min[0], double(max[0]) - double(min[0]), val[0]);
107 res[1] = to01(min[1], double(max[1]) - double(min[1]), val[1]);
108 res[2] = to01(min[2], double(max[2]) - double(min[2]), val[2]);
109 return res;
110 }
111
112 template <typename T>
113 static ossia::vec3f from01(const T& slider, std::array<double, 3> val) noexcept
114 {
115 ossia::vec3f res;
116 const auto min = getMin<ossia::vec3f>(slider);
117 const auto max = getMax<ossia::vec3f>(slider);
118 res[0] = from01(min[0], double(max[0]) - double(min[0]), val[0]);
119 res[1] = from01(min[1], double(max[1]) - double(min[1]), val[1]);
120 res[2] = from01(min[2], double(max[2]) - double(min[2]), val[2]);
121 return res;
122 }
123
124 template <typename T>
125 static std::array<double, 4> to01(const T& slider, ossia::vec4f val) noexcept
126 {
127 std::array<double, 4> res;
128 const auto min = getMin<ossia::vec4f>(slider);
129 const auto max = getMax<ossia::vec4f>(slider);
130 res[0] = to01(min[0], double(max[0]) - double(min[0]), val[0]);
131 res[1] = to01(min[1], double(max[1]) - double(min[1]), val[1]);
132 res[2] = to01(min[2], double(max[2]) - double(min[2]), val[2]);
133 res[3] = to01(min[3], double(max[3]) - double(min[3]), val[3]);
134 return res;
135 }
136
137 template <typename T>
138 static ossia::vec4f from01(const T& slider, std::array<double, 4> val) noexcept
139 {
140 ossia::vec4f res;
141 const auto min = getMin<ossia::vec4f>(slider);
142 const auto max = getMax<ossia::vec4f>(slider);
143 res[0] = from01(min[0], double(max[0]) - double(min[0]), val[0]);
144 res[1] = from01(min[1], double(max[1]) - double(min[1]), val[1]);
145 res[2] = from01(min[2], double(max[2]) - double(min[2]), val[2]);
146 res[3] = from01(min[3], double(max[3]) - double(min[3]), val[3]);
147 return res;
148 }
149
150 template <typename T, std::size_t N>
151 static std::array<float, N> from01(const T& slider, std::array<double, N> val) noexcept
152 {
153 std::array<float, N> res;
154 const auto min = getMin<std::array<double, N>>(slider);
155 const auto max = getMax<std::array<double, N>>(slider);
156 for(std::size_t i = 0; i < N; i++)
157 res[i] = from01(min[i], double(max[i]) - double(min[i]), val[i]);
158 return res;
159 }
160};
161
163{
164 static double to01(double min, double range, double val) noexcept
165 {
166 return ossia::log_to_normalized(min, range, val);
167 }
168
169 static double from01(double min, double range, double val) noexcept
170 {
171 return ossia::normalized_to_log(min, range, val);
172 }
173
174 template <typename T>
175 static double to01(const T& slider, double val) noexcept
176 {
177 auto min = getMin<double>(slider);
178 auto max = getMax<double>(slider);
179 if(max - min == 0)
180 max = min + 1;
181 return to01(min, max - min, val);
182 }
183
184 template <typename T>
185 static double from01(const T& slider, double val) noexcept
186 {
187 auto min = getMin<double>(slider);
188 auto max = getMax<double>(slider);
189 if(max - min == 0)
190 max = min + 1;
191 return from01(min, max - min, val);
192 }
193};
194
195template <typename Norm_T>
197{
198 double min{};
199 double max{};
200 template <typename T>
201 FixedNormalizer(const T& slider)
202 {
203 min = getMin<double>(slider);
204 max = getMax<double>(slider);
205 if(max - min == 0)
206 max = min + 1;
207 }
208
209 constexpr double to01(double val) const noexcept
210 {
211 return Norm_T::to01(min, max - min, val);
212 }
213
214 constexpr double from01(double val) const noexcept
215 {
216 return Norm_T::from01(min, max - min, val);
217 }
218};
219
220template <typename Norm_T, typename Slider_T>
222{
223 const Slider_T& slider;
224
225 UpdatingNormalizer(const Slider_T& sl)
226 : slider{sl}
227 {
228 }
229
230 constexpr double to01(double val) const noexcept { return Norm_T::to01(slider, val); }
231
232 constexpr double from01(double val) const noexcept
233 {
234 return Norm_T::from01(slider, val);
235 }
236};
237
238template <typename Control_T, typename Widget_T>
239static void initWidgetProperties(Control_T& inlet, Widget_T& widget)
240{
241 if constexpr(requires { widget.setNoValueChangeOnMove(true); })
242 widget.setNoValueChangeOnMove(inlet.noValueChangeOnMove);
243}
244
245template <typename Widget_T>
246static void setRange(Widget_T& widget, auto&& min, auto&& max, auto&& init)
247{
248 widget.setRange(min, max, init);
249}
250
251static inline void setRange(QSpinBox& widget, auto&& min, auto&& max, auto&& init)
252{
253 widget.setRange(min, max);
254}
255
256static inline void setRange(QDoubleSpinBox& widget, auto&& min, auto&& max, auto&& init)
257{
258 widget.setRange(min, max);
259}
260
261template <typename T, typename Control_T, typename Widget_T>
262static void bindFloatDomain(const T& slider, Control_T& inlet, Widget_T& widget)
263{
264 auto min = getMin<float>(slider);
265 auto max = getMax<float>(slider);
266 if(max - min == 0)
267 max = min + 1;
268
269 setRange(widget, min, max, getInit<float>(slider));
270
271 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
272 {
273 SCORE_ASSERT(&slider == &inlet);
274 QObject::connect(&inlet, &Control_T::domainChanged, &widget, [&slider, &widget] {
275 auto min = getMin<float>(slider);
276 auto max = getMax<float>(slider);
277 if(max - min == 0)
278 max = min + 1;
279
280 setRange(widget, min, max, getInit<float>(slider));
281 });
282 }
283}
284
285template <typename T, typename Control_T, typename Widget_T>
286static void bindIntDomain(const T& slider, Control_T& inlet, Widget_T& widget)
287{
288 auto min = getMin<int>(slider);
289 auto max = getMax<int>(slider);
290 if(max - min == 0)
291 max = min + 1;
292
293 setRange(widget, min, max, getInit<int>(slider));
294
295 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
296 {
297 SCORE_ASSERT(&slider == &inlet);
298 QObject::connect(&inlet, &Control_T::domainChanged, &widget, [&slider, &widget] {
299 auto min = getMin<int>(slider);
300 auto max = getMax<int>(slider);
301 if(max - min == 0)
302 max = min + 1;
303
304 setRange(widget, min, max, getInit<int>(slider));
305 });
306 }
307}
308
309template <typename T, typename Control_T, typename Widget_T>
310static void bindVec2Domain(const T& slider, Control_T& inlet, Widget_T& widget)
311{
312 auto update_range = [&widget, &inlet, &slider] {
313 auto min = ossia::get_min(inlet.domain());
314 auto max = ossia::get_max(inlet.domain());
315 auto min_float = min.template target<float>();
316 auto max_float = max.template target<float>();
317 if(min_float && max_float)
318 {
319 if(*max_float - *min_float == 0)
320 *max_float = *min_float + 1;
321 widget.setRange(
322 {*min_float, *min_float}, {*max_float, *max_float}, {*min_float, *min_float});
323 }
324 else
325 {
326 auto min_vec2 = min.template target<ossia::vec2f>();
327 auto max_vec2 = max.template target<ossia::vec2f>();
328 if(min_vec2 && max_vec2)
329 {
330 auto& min = *min_vec2;
331 auto& max = *max_vec2;
332 for(std::size_t i = 0; i < min.size(); i++)
333 {
334 if(max[i] - min[i] == 0)
335 max[i] = min[i] + 1;
336 }
337
338 auto init_vec2 = getInit<ossia::vec2f>(slider);
339 widget.setRange(min, max, init_vec2);
340 }
341 }
342 };
343
344 update_range();
345
346 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
347 {
348 SCORE_ASSERT(&slider == &inlet);
349 QObject::connect(&inlet, &Control_T::domainChanged, &widget, update_range);
350 }
351}
352
353template <typename T, typename Control_T, typename Widget_T>
354static void bindVec3Domain(const T& slider, Control_T& inlet, Widget_T& widget)
355{
356 auto update_range = [&widget, &inlet, &slider] {
357 auto min = ossia::get_min(inlet.domain());
358 auto max = ossia::get_max(inlet.domain());
359 auto min_float = min.template target<float>();
360 auto max_float = max.template target<float>();
361 if(min_float && max_float)
362 {
363 if(*max_float - *min_float == 0)
364 *max_float = *min_float + 1;
365 widget.setRange(
366 {*min_float, *min_float, *min_float}, {*max_float, *max_float, *max_float});
367 }
368 else
369 {
370 auto min_vec3 = min.template target<ossia::vec3f>();
371 auto max_vec3 = max.template target<ossia::vec3f>();
372 if(min_vec3 && max_vec3)
373 {
374 auto& min = *min_vec3;
375 auto& max = *max_vec3;
376 for(std::size_t i = 0; i < min.size(); i++)
377 {
378 if(max[i] - min[i] == 0)
379 max[i] = min[i] + 1;
380 }
381
382 auto init_vec3 = getInit<ossia::vec3f>(slider);
383 widget.setRange(min, max, init_vec3);
384 }
385 }
386 };
387
388 update_range();
389
390 if constexpr(std::is_base_of_v<Process::ControlInlet, T>)
391 {
392 SCORE_ASSERT(&slider == &inlet);
393 QObject::connect(&inlet, &Control_T::domainChanged, &widget, update_range);
394 }
395}
396}
Definition ControlWidgetDomains.hpp:197
Definition ControlWidgetDomains.hpp:47
Definition ControlWidgetDomains.hpp:163
Definition ControlWidgetDomains.hpp:222
Definition MIDISync.hpp:126