Loading...
Searching...
No Matches
score-plugin-avnd/Crousti/Layer.hpp
1#pragma once
2#include <Process/Commands/SetControlValue.hpp>
3#include <Process/Dataflow/WidgetInlets.hpp>
4#include <Process/LayerPresenter.hpp>
5#include <Process/LayerView.hpp>
6
7#include <Control/Layout.hpp>
8#include <Crousti/MessageBus.hpp>
9#include <Crousti/ControlViews.hpp>
10#include <Crousti/Painter.hpp>
11#include <Crousti/ProcessModel.hpp>
12#include <Crousti/UiEdits.hpp>
13
14#include <score/command/Dispatchers/CommandDispatcher.hpp>
15#include <score/graphics/layouts/GraphicsBoxLayout.hpp>
16#include <score/graphics/layouts/GraphicsGridLayout.hpp>
17#include <score/graphics/layouts/GraphicsSplitLayout.hpp>
18#include <score/graphics/layouts/GraphicsStripDetailLayout.hpp>
19#include <score/graphics/layouts/GraphicsTabLayout.hpp>
20#include <score/graphics/layouts/LayoutSelection.hpp>
21#include <score/graphics/RectItem.hpp>
22#include <score/graphics/widgets/QGraphicsCombo.hpp>
23#include <score/graphics/widgets/QGraphicsEnum.hpp>
24#include <score/graphics/widgets/QGraphicsLineEdit.hpp>
25#include <score/tools/File.hpp>
26
27#include <Crousti/HostServices.hpp>
28
29#include <avnd/common/aggregates.hpp>
30#include <avnd/common/enum_reflection.hpp>
31
32#include <QFileInfo>
33#include <QMimeData>
34#include <QPointer>
35#include <QRegularExpression>
36#include <QTimer>
37#include <QUrl>
38#include <QFontMetricsF>
39#include <avnd/concepts/file_port.hpp>
40#include <avnd/concepts/folder_items.hpp>
41#include <avnd/concepts/layout.hpp>
42#include <avnd/concepts/midifile.hpp>
43#include <avnd/concepts/soundfile.hpp>
44#include <halp/layout.hpp>
45
46#include <limits>
47#include <optional>
48
49namespace oscr
50{
51template <typename Item>
53 = avnd::container_layout<Item> || avnd::hbox_layout<Item> || avnd::group_layout<Item>
54 || avnd::vbox_layout<Item> || avnd::split_layout<Item> || avnd::grid_layout<Item>
55 || avnd::tab_layout<Item> || avnd::section_layout<Item>
56 || avnd::table_layout<Item> || avnd::strip_detail_layout<Item>;
57
58template <typename Info>
60{
61};
62template <typename Info>
63 requires requires { sizeof(typename Info::ui::bus); }
64struct MessageBusUi<Info>
65{
66 typename Info::ui::bus bus;
67};
68
69template <typename Info, typename RootLayout>
71 : RootLayout
72 , MessageBusUi<Info>
73{
74 using RootLayout::RootLayout;
79 {
80 delete uiConnections;
81 uiConnections = nullptr;
82 const auto children = this->childItems();
83 for(auto* item : children)
84 delete item;
85 }
86
87 typename Info::ui ui;
90 QObject* uiConnections{};
94};
95
96template <typename Item>
97static auto createRecursiveLayout(QGraphicsItem* parent)
98{
99 if constexpr(avnd::container_layout<Item>)
100 {
101 return new score::GraphicsLayout{parent};
102 }
103 else if constexpr(avnd::hbox_layout<Item> || avnd::group_layout<Item>)
104 {
105 return new score::GraphicsHBoxLayout{parent};
106 }
107 else if constexpr(avnd::vbox_layout<Item>)
108 {
109 return new score::GraphicsVBoxLayout{parent};
110 }
111 else if constexpr(avnd::section_layout<Item>)
112 {
113 return new score::GraphicsSectionLayout{parent};
114 }
115 else if constexpr(avnd::table_layout<Item>)
116 {
117 return new score::GraphicsTableLayout{parent};
118 }
119 else if constexpr(avnd::strip_detail_layout<Item>)
120 {
121 return new score::GraphicsStripDetailLayout{parent};
122 }
123 else if constexpr(avnd::split_layout<Item>)
124 {
125 return new score::GraphicsSplitLayout{parent};
126 }
127 else if constexpr(avnd::grid_layout<Item>)
128 {
129 if constexpr(requires { Item::columns(); })
130 {
131 return new score::GraphicsGridColumnsLayout{parent};
132 }
133 else if constexpr(requires { Item::rows(); })
134 {
135 return new score::GraphicsGridRowsLayout{parent};
136 }
137 }
138 else if constexpr(avnd::tab_layout<Item>)
139 {
140 return new score::GraphicsTabLayout{parent};
141 }
142 else if constexpr(avnd::has_layout<Item>)
143 {
144 return new score::GraphicsLayout{parent};
145 }
146 else
147 {
148 // static_assert(Item::no_layout_provided);
149 return new score::GraphicsVBoxLayout{parent};
150 }
151}
152
153template <typename Item>
154static void setupRecursiveLayout(auto* new_l)
155{
156 if constexpr(avnd::grid_layout<Item>)
157 {
158 if constexpr(requires { Item::columns(); })
159 {
160 new_l->setColumns(Item::columns());
161 }
162 else if constexpr(requires { Item::rows(); })
163 {
164 new_l->setRows(Item::rows());
165 }
166 }
167 else if constexpr(avnd::section_layout<Item>)
168 {
169 // Defaults a plugin can override with halp_meta(padding / spacing)
170 new_l->setPadding(6.);
171 new_l->setSpacing(4.);
172 if constexpr(requires { Item::name(); })
173 {
174 const std::string_view name = Item::name();
175 new_l->setTitle(QString::fromUtf8(name.data(), qsizetype(name.size())));
176 }
177 }
178 else if constexpr(avnd::table_layout<Item>)
179 {
180 // Defaults a plugin can override with halp_meta(padding / spacing)
181 new_l->setPadding(4.);
182 new_l->setSpacing(6.);
183 if constexpr(requires { Item::name(); })
184 {
185 const std::string_view name = Item::name();
186 new_l->setTitle(QString::fromUtf8(name.data(), qsizetype(name.size())));
187 }
188 if constexpr(requires { Item::columns(); })
189 {
190 QStringList titles;
191 for(std::string_view t : Item::columns())
192 titles.push_back(QString::fromUtf8(t.data(), qsizetype(t.size())));
193 new_l->setColumnTitles(std::move(titles));
194 }
195 }
196 else if constexpr(avnd::tab_layout<Item>)
197 {
198 if constexpr(avnd::tag_hide_tabs<Item>)
199 new_l->setTabBarVisible(false);
200 [=]<typename... Ts>(avnd::typelist<Ts...> args) {
201 (new_l->addTab(Ts::name()), ...);
202 }(avnd::as_typelist<Item>{});
203 }
204}
205
206template <typename T>
208template <typename T, typename V>
209struct pmf_member_type<V T::*>
210{
211 using type = V;
212};
213
214template <typename T>
215using pmf_member_type_t = typename pmf_member_type<T>::type;
216
217template <typename T>
219{
220 const score::DocumentContext& ctx;
221 void operator()(const ossia::value& v, auto& dst) const
222 {
223 T p;
224 oscr::from_ossia_value(p, v, dst);
225 }
226};
227
228// File & folder ports: paths are stored in the document as templates
229// (<PROJECT>:..., <LIBRARY>:..., or document-relative); resolve them so the
230// ui items always receive a usable absolute path.
231template <typename T>
232 requires(
233 avnd::soundfile_port<T> || avnd::midifile_port<T> || avnd::file_port<T>
234 || requires { T::widget::folder; })
236{
237 const score::DocumentContext& ctx;
238 void operator()(const ossia::value& v, auto& dst) const
239 {
240 T p;
241 if(auto str = v.target<std::string>(); str && !str->empty())
242 oscr::from_ossia_value(
243 p,
244 ossia::value{
245 score::locateFilePath(QString::fromStdString(*str), ctx).toStdString()},
246 dst);
247 else
248 oscr::from_ossia_value(p, v, dst);
249 }
250};
251
252// The inverse of SetGUIValue: an item hands back the absolute path it was
253// given, and the document must keep storing a template, or the project stops
254// being relocatable. Non-file ports pass through untouched.
255template <typename T>
257{
258 const score::DocumentContext& ctx;
259 ossia::value operator()(ossia::value v) const { return v; }
260};
261
262template <typename T>
263 requires(
264 avnd::soundfile_port<T> || avnd::midifile_port<T> || avnd::file_port<T>
265 || requires { T::widget::folder; })
267{
268 const score::DocumentContext& ctx;
269 ossia::value operator()(ossia::value v) const
270 {
271 if(auto str = v.target<std::string>(); str && !str->empty())
272 return ossia::value{
273 score::relativizeFilePath(QString::fromStdString(*str), ctx).toStdString()};
274 return v;
275 }
276};
277
279template <typename V>
280static constexpr int entryIndex(V v) noexcept
281{
282 if constexpr(std::is_enum_v<V>)
283 {
284 constexpr auto values = avnd::enum_values<V>();
285 for(std::size_t i = 0; i < values.size(); ++i)
286 if(values[i] == v)
287 return int(i);
288 return -1;
289 }
290 else
291 {
292 return static_cast<int>(v);
293 }
294}
295
297template <typename Item>
298static Process::ControlPresentation controlPresentation()
299{
301 if constexpr(requires {
302 { Item::presentation } -> std::convertible_to<halp::look>;
303 })
304 {
305 constexpr halp::look look = Item::presentation;
306 if(look.label[0] != 0)
307 p.label = QString::fromUtf8(look.label);
308 p.labelVisible = !look.hide_label;
309 if(look.size == halp::control_size::compact)
310 p.size = score::ControlSize::Small;
311 else if(look.size == halp::control_size::large)
312 p.size = score::ControlSize::Large;
313 p.valueOnHover = look.value == halp::value_display::hover;
314 if(look.widget == halp::control_widget::knob)
316 else if(look.widget == halp::control_widget::combo)
318 else if(look.widget == halp::control_widget::none)
320 }
321 return p;
322}
323
324template <typename Info>
326{
327 using inputs_type = typename avnd::input_introspection<Info>::type;
328 using outputs_type = typename avnd::output_introspection<Info>::type;
329 inputs_type temp_inputs{};
330 outputs_type temp_outputs{};
331
332 typename Info::ui* rootUi{};
334 QObject* connections{};
335 UiEdits* uiEdits{};
336
339
341 std::map<std::string, std::shared_ptr<score::LayoutSelection>, std::less<>> selections;
342 std::map<score::GraphicsTableLayout*, std::shared_ptr<score::LayoutSelection>>
343 tableSelections;
344
346 std::map<QGraphicsItem*, QTimer*> dependentRelayouts;
347
348 std::shared_ptr<score::LayoutSelection> selectionNamed(std::string_view name)
349 {
350 auto it = selections.find(name);
351 if(it == selections.end())
352 {
353 auto sel = std::make_shared<score::LayoutSelection>();
354 // Stored on the root item, which survives UI rebuilds
355 const QByteArray key = "layoutSelection:" + QByteArray{name.data(), qsizetype(name.size())};
356 sel->current = context.property(key.constData()).toInt();
357 sel->listeners.push_back(
358 [ctx = &context, key](int i) { ctx->setProperty(key.constData(), i); });
359 it = selections.emplace(std::string{name}, std::move(sel)).first;
360 }
361 return it->second;
362 }
363
367 template <typename Port_T, typename Item>
369 QGraphicsItem* parent, Port_T* port, const Process::ControlLayout& lay, Item& item)
370 {
371 if constexpr(requires { sizeof(Item::value); })
372 {
373 using avnd_port_type = pmf_member_type_t<decltype(item.model)>;
374 SetGUIValue<avnd_port_type>{doc}(port->value(), item.value);
375 // The context outlives a UI rebuild (e.g. a preset reloading the
376 // ports); the widgets the lambda updates do not.
377 QObject* const lifetime = connections ? connections : &context;
378 if constexpr(requires { rootUi->on_control_update(); })
379 {
380 QObject::connect(
381 port, &Port_T::valueChanged, lifetime,
382 [rui = rootUi, layout = this->layout, &item, edits = uiEdits,
383 &ctx = static_cast<const score::DocumentContext&>(this->doc)](
384 const ossia::value& v) {
385 SetGUIValue<avnd_port_type>{ctx}(v, item.value);
386
387 {
388 UiEdits::Reacting r{edits};
389 rui->on_control_update();
390 }
391 layout->update();
392 });
393 }
394 else
395 {
396 QObject::connect(
397 port, &Port_T::valueChanged, lifetime,
398 [layout = this->layout, &item,
399 &ctx = static_cast<const score::DocumentContext&>(this->doc)](
400 const ossia::value& v) {
401 SetGUIValue<avnd_port_type>{ctx}(v, item.value);
402 layout->update();
403 });
404 }
405
406 // A custom item with an execution_value is given there what execution
407 // gives the port, and nothing once execution stops: it shows it apart
408 // from its value, which stays the one the user edits. on_control_update
409 // is not called: the value it reacts to does not change.
410 if constexpr(requires {
411 item.execution_value = std::optional<std::decay_t<decltype(item.value)>>{};
412 })
413 {
414 using value_type = std::decay_t<decltype(item.value)>;
415 auto show = [layout = this->layout, &item](std::optional<value_type> v) {
416 // Values stream in at the control rate: repaint only on a change
417 if constexpr(requires { item.execution_value == v; })
418 if(item.execution_value == v)
419 return;
420 item.execution_value = std::move(v);
421 // A custom item repaints itself through the update its host gives it
422 if constexpr(requires { bool(item.update); item.update(); })
423 {
424 if(item.update)
425 {
426 item.update();
427 return;
428 }
429 }
430 else if constexpr(requires { item.update(); })
431 {
432 item.update();
433 return;
434 }
435 layout->update();
436 };
437 QObject::connect(
438 port, &Port_T::executionValueChanged, lifetime,
439 [show, &ctx = static_cast<const score::DocumentContext&>(this->doc)](
440 const ossia::value& v) {
441 value_type val{};
442 SetGUIValue<avnd_port_type>{ctx}(v, val);
443 show(std::move(val));
444 });
445 QObject::connect(
446 port, &Port_T::executionReset, lifetime, [show] { show(std::nullopt); });
447 }
448
449 // An edit from the ui, in the document's history: the edits of one
450 // gesture are one undo step (see UiEdits)
451 if constexpr(requires { item.set = {}; })
452 {
453 item.set = [port, edits = uiEdits,
454 &ctx = static_cast<const score::DocumentContext&>(this->doc)](
455 const auto& val) {
456 if(edits)
457 edits->set(
458 *port, GetGUIValue<avnd_port_type>{ctx}(oscr::to_ossia_value(val)));
459 };
460 }
461 }
462
463 if constexpr(requires { Item::dynamic_size; })
464 {
465 if(auto obj = dynamic_cast<score::ResizeableItem*>(parent))
466 {
467 if(auto edit = qgraphicsitem_cast<score::QGraphicsLineEdit*>(lay.control))
468 QObject::connect(
469 edit, &score::QGraphicsLineEdit::sizeChanged, obj,
470 &score::ResizeableItem::childrenSizeChanged);
471 }
472 }
473 if constexpr(requires { Item::width; })
474 {
475 if(auto edit = qgraphicsitem_cast<score::QGraphicsLineEdit*>(lay.control))
476 edit->setTextWidth(Item::width());
477 // A combobox whose entries change, e.g. the files of a folder: a width
478 // of its own rather than the one of its first entries
479 else if(auto combo = dynamic_cast<score::QGraphicsCombo*>(lay.control))
480 {
481 auto r = combo->boundingRect();
482 r.setWidth(Item::width());
483 combo->setRect(r);
484 if(auto container = dynamic_cast<score::EmptyRectItem*>(lay.container))
485 container->fitChildrenRect();
486 }
487 }
488 // An enumeration shown as buttons, so many a row
489 if constexpr(requires { Item::columns(); })
490 {
491 if(auto e = dynamic_cast<score::QGraphicsEnum*>(lay.control))
492 {
493 e->columns = Item::columns();
494 e->updateRect();
495 if(auto container = dynamic_cast<score::EmptyRectItem*>(lay.container))
496 container->fitChildrenRect();
497 }
498 }
499 }
500
503 template <typename Item>
504 void setupCondition(score::GraphicsLayout& lay, auto... recursive_members)
505 {
506 using Condition = decltype(Item::condition());
507 const int index = avnd::index_in_struct(
508 temp_inputs, recursive_members..., Condition::condition_control);
509 auto& model = static_cast<const ProcessModel<Info>&>(this->proc);
510 for(auto* base : model.avnd_input_idx_to_model_ports(index))
511 {
512 auto* port = qobject_cast<Process::ControlInlet*>(base);
513 if(!port)
514 continue;
515
516 auto matches = [port](const ossia::value& v) {
517 int idx = -1;
518 if(auto* e = qobject_cast<Process::Enum*>(port))
519 idx = e->indexOfValue(v);
520 else if(auto* c = qobject_cast<Process::ComboBox*>(port))
521 idx = c->indexOfValue(v);
522 const bool is_choice = qobject_cast<Process::Enum*>(port)
523 || qobject_cast<Process::ComboBox*>(port);
524 for(auto expected : Condition::condition_values())
525 {
526 if(is_choice ? idx == entryIndex(expected)
527 : ossia::convert<double>(v) == static_cast<double>(expected))
528 return true;
529 }
530 return false;
531 };
532 auto apply = [&lay, matches](const ossia::value& v) {
533 const bool on = matches(v);
534 if constexpr(Condition::condition_hides)
535 {
536 lay.setVisible(on);
537 }
538 else
539 {
540 lay.setEnabled(on);
541 lay.setOpacity(on ? 1. : 0.35);
542 }
543 };
544 apply(port->value());
545 // Scoped to the item, rebuilt on inlet change
546 QObject::connect(port, &Process::ControlInlet::valueChanged, &lay, apply);
547 break;
548 }
549 }
550
551 template <typename Item>
552 Process::ControlPresentation presentationFor() const
553 {
554 auto p = controlPresentation<Item>();
555 if(tableDepth > 0 && !p.label)
556 p.labelVisible = false;
557 return p;
558 }
559
561 template <typename Item, typename Field>
562 static consteval bool shownAsCombo()
563 {
564 halp::control_widget look = halp::control_widget::automatic;
565 if constexpr(requires {
566 { Item::presentation } -> std::convertible_to<halp::look>;
567 })
568 look = halp::look{Item::presentation}.widget;
569 if(look == halp::control_widget::none)
570 return false;
571 if constexpr(avnd::folder_items_parameter<Field>)
572 return false;
573 else
574 {
575 constexpr auto widget = avnd::get_widget<Field>().widget;
576 return widget == avnd::widget_type::combobox
577 || (widget == avnd::widget_type::choices
578 && look == halp::control_widget::combo);
579 }
580 }
581
585 template <typename Field, typename Item>
587 Process::ControlInlet* port, const Process::ControlLayout& lay, Item& item)
588 {
589 using Models = decltype(Item::dependent_models());
590 constexpr auto count = std::tuple_size_v<Models>;
591 using Values = decltype(std::apply([](auto... member) {
592 return std::tuple{(pmf_member_type_t<decltype(member)>{}.value)...};
593 }, Item::dependent_models()));
594 auto ports = std::make_shared<std::vector<Process::ControlInlet*>>();
595 ports->reserve(count);
596 std::apply([&](auto... member) {
597 (addControlInlet(*ports, member), ...);
598 }, Item::dependent_models());
599 if(ports->size() != count)
600 return;
601 auto readValues = [ports, &doc = this->doc] {
602 Values values{};
603 [&]<std::size_t... I>(std::index_sequence<I...>) {
605 (*ports)[I]->value(), std::get<I>(values)), ...);
606 }(std::make_index_sequence<count>{});
607 return values;
608 };
609 auto* combo = dynamic_cast<score::QGraphicsCombo*>(lay.control);
612 static constexpr int unlisted = std::numeric_limits<int>::min();
613 std::shared_ptr<std::vector<int>> indices;
615 std::shared_ptr<std::optional<Values>> gestureStart;
616 if constexpr(Item::dependent_choices())
617 {
618 static_assert(
619 shownAsCombo<Item, Field>(),
620 "dependent_choices() needs a control shown as a combo box");
621 SCORE_ASSERT(combo);
622 indices = std::make_shared<std::vector<int>>();
623 gestureStart = std::make_shared<std::optional<Values>>();
624 // Replace the ordinary index binding, not the ordinary native widget.
625 // The filtered visible index is not the port's global enum index.
626 QObject::disconnect(combo, &score::QGraphicsCombo::sliderMoved, nullptr, nullptr);
627 QObject::disconnect(combo, &score::QGraphicsCombo::sliderReleased, nullptr, nullptr);
628 QObject::disconnect(combo, &score::QGraphicsCombo::valueEdited, nullptr, nullptr);
629 QObject::disconnect(port, nullptr, combo, nullptr);
630 combo->setEditable(false);
631 }
632 QObject* lifetime = connections ? connections : &context;
633 auto& relayoutTimer = dependentRelayouts[layout];
634 if(!relayoutTimer)
635 {
636 relayoutTimer = new QTimer{lifetime};
637 relayoutTimer->setSingleShot(true);
638 QObject::connect(
639 relayoutTimer, &QTimer::timeout, relayoutTimer,
640 [anchor = QPointer<score::ResizeableItem>{
641 dynamic_cast<score::ResizeableItem*>(lay.container)}] {
642 if(anchor)
644 });
645 }
646 auto refresh = [lay, port, combo, indices, readValues, relayoutTimer] {
647 const auto values = readValues();
648 const auto label = Item::control_label(values);
649 if(lay.label)
650 lay.label->setText(QString::fromUtf8(label.data(), qsizetype(label.size())));
651 if constexpr(Item::dependent_choices())
652 {
653 const auto choices = Item::control_choices(values);
654 const int selected = Item::selected(values);
655 QStringList names;
656 indices->clear();
657 int visibleIndex = -1;
658 for(const auto& choice : choices)
659 {
660 if(choice.value == selected)
661 visibleIndex = int(indices->size());
662 indices->push_back(choice.value);
663 names.push_back(QString::fromUtf8(choice.name.data(), qsizetype(choice.name.size())));
664 }
665 // A selection that the choices do not offer, e.g. from a preset or
666 // from execution: shown as it is, after them, rather than as another.
667 if(visibleIndex < 0)
668 {
669 visibleIndex = int(indices->size());
670 indices->push_back(unlisted);
671 // An enumeration's entries are its value's index
672 auto* e = qobject_cast<Process::Enum*>(port);
673 names.push_back(
674 e && selected >= 0 && selected < std::ssize(e->values)
675 ? e->values[selected]
676 : displayText(
677 *port, ossia::value{selected}, QFontMetricsF{combo->font()},
678 220.));
679 }
680 if(names != combo->array)
681 {
682 combo->array = std::move(names);
683 auto initialRect = combo->boundingRect();
684 initialRect.setWidth(80.);
685 combo->setRect(initialRect);
686 combo->init();
687 auto rect = combo->boundingRect();
688 rect.setWidth(std::clamp(rect.width(), 80., 220.));
689 combo->setRect(rect);
690 }
691 combo->setValue(visibleIndex);
692 combo->update();
693 }
694 if(auto* container = dynamic_cast<score::EmptyRectItem*>(lay.container))
695 container->fitChildrenRect();
696 score::setLayoutExcluded(*lay.container, !Item::visible(values));
697 // Tabs have not registered all their pages while controls are created.
698 // Queue geometry until construction ends; restarting this row's timer
699 // also coalesces the dependent writes of one categorical edit.
700 relayoutTimer->start(0);
701 };
702 for(auto* dependency : *ports)
703 QObject::connect(
704 dependency, &Process::ControlInlet::valueChanged, lifetime,
705 [refresh](const ossia::value&) { refresh(); });
706 if constexpr(Item::dependent_choices())
707 {
708 QObject::connect(
709 combo, &score::QGraphicsCombo::sliderMoved, lifetime,
710 [combo, indices, gestureStart, readValues, ports, edits = uiEdits,
711 &doc = this->doc] {
712 const int index = combo->value();
713 if(!edits || index < 0 || index >= int(indices->size()))
714 return;
715 // UiEdits groups the event or held-button drag automatically, including
716 // a widget destroyed before its release (e.g. a preset rebuilding UI).
717 combo->moving = true;
718 // Each step of a scrub resolves from where it started, not from what
719 // the previous step resolved: passing over an entry loses nothing.
720 if(!*gestureStart)
721 *gestureStart = readValues();
722 const auto& start = **gestureStart;
723 const int value = (*indices)[index];
724 const auto after = value == unlisted ? start : Item::edited(start, value);
725 const auto current = readValues();
726 [&]<std::size_t... I>(std::index_sequence<I...>) {
727 ([&] {
728 if(std::get<I>(current) != std::get<I>(after))
729 edits->set(*(*ports)[I],
731 oscr::to_ossia_value(std::get<I>(after))));
732 }(), ...);
733 }(std::make_index_sequence<count>{});
734 });
735 QObject::connect(
736 combo, &score::QGraphicsCombo::sliderReleased, lifetime,
737 [combo, gestureStart, edits = uiEdits, refresh] {
738 gestureStart->reset();
739 if(combo->moving)
740 {
741 combo->moving = false;
742 if(edits)
743 edits->commit();
744 }
745 refresh();
746 });
747 }
748 refresh();
749 }
750
751 template <typename Item>
752 void createControl(Item& item, auto... member)
753 {
754 if constexpr(requires { ((inputs_type{}).*....*member); })
755 {
756 int index = avnd::index_in_struct(temp_inputs, member...);
757 auto& proc = static_cast<const ProcessModel<Info>&>(this->proc);
758 auto ports = proc.avnd_input_idx_to_model_ports(index);
759 for(auto p : ports)
760 {
761 auto [port, qitem] = makeInlet(p, presentationFor<Item>());
762 {
763 SCORE_ASSERT(port);
764 SCORE_ASSERT(qitem.container);
765 setupControl(this->layout, port, qitem, item);
766 setupItem(item, *qitem.container);
767 if constexpr(requires { Item::dependent_models(); })
769 std::remove_cvref_t<decltype(((inputs_type{}).*....*member))>>(
770 port, qitem, item);
771 }
772 }
773 }
774 else if constexpr(requires { ((outputs_type{}).*....*member); })
775 {
776 int index = avnd::index_in_struct(temp_outputs, member...);
777 auto& proc = static_cast<const ProcessModel<Info>&>(this->proc);
778 auto ports = proc.avnd_output_idx_to_model_ports(index);
779 for(auto p : ports)
780 {
781 if(auto [port, qitem] = makeOutlet(p, presentationFor<Item>()); port)
782 {
783 SCORE_ASSERT(qitem.container);
784 setupControl(this->layout, port, qitem, item);
785 setupItem(item, *qitem.container);
786 }
787 // An outlet without a widget, e.g. a value outlet: its port and name,
788 // where the node would otherwise show a bare port on its side
789 else if(auto lay = makePort(*p, presentationFor<Item>()); lay.container)
790 {
791 setupItem(item, *lay.container);
792 }
793 }
794 }
795 else
796 {
797 static_assert(sizeof...(member) < 0, "not_a_member_of_inputs_or_outputs");
798 }
799 }
800
801 template <typename Item, typename T>
802 void createWidget(Item& it, const T& member)
803 {
804 if constexpr(requires {
805 { member } -> std::convertible_to<std::string_view>;
806 })
807 {
808 auto res = makeLabel(member);
809 setupItem(it, *res);
810 }
811 else if constexpr(requires {
812 { member.text } -> std::convertible_to<std::string_view>;
813 })
814 {
815 auto res = makeLabel(member.text);
816 setupItem(it, *res);
817 }
818 else
819 {
820 createControl(it, member);
821 }
822 }
823
824 template <typename Item, typename... T>
825 requires(sizeof...(T) > 1)
826 void createWidget(Item& item, T... recursive_members)
827 {
828 createControl(item, recursive_members...);
829 }
830
833 {
835 for(auto* item = this->layout; item && !cell; item = item->parentItem())
836 cell = dynamic_cast<score::GraphicsStripCell*>(item);
837 return cell;
838 }
839
841 void acceptFileDrops(Process::FileChooserBase& port)
842 {
843 auto* cell = enclosingCell();
844 if(!cell)
845 return;
846
847 // "Sound files (*.wav *.flac)" -> wav, flac. None: any file.
848 QStringList extensions;
849 static const QRegularExpression pattern{QStringLiteral("\\*\\.([A-Za-z0-9]+)")};
850 for(auto it = pattern.globalMatch(port.filters()); it.hasNext();)
851 extensions.push_back(it.next().captured(1).toLower());
852
853 auto file_of = [extensions](const QMimeData& data) -> QString {
854 for(const QUrl& url : data.urls())
855 {
856 if(!url.isLocalFile())
857 continue;
858 const QString path = url.toLocalFile();
859 if(extensions.isEmpty()
860 || extensions.contains(QFileInfo{path}.suffix().toLower()))
861 return path;
862 }
863 return {};
864 };
865 cell->setDropHandler(
866 [file_of](const QMimeData& data) { return !file_of(data).isEmpty(); },
867 [file_of, &port, &doc = this->doc](const QMimeData& data) {
868 const QString path = file_of(data);
869 if(path.isEmpty())
870 return;
871 CommandDispatcher<>{doc.commandStack}.submit<Process::SetControlValue>(
872 port, ossia::value{score::relativizeFilePath(path, doc).toStdString()});
873 });
874 }
875
877 static QString displayText(
878 const Process::ControlInlet& port, const ossia::value& v,
879 const QFontMetricsF& metrics, qreal max_width = 72.)
880 {
881 if(auto* c = qobject_cast<const Process::ComboBox*>(&port))
882 {
883 const int i = c->indexOfValue(v);
884 if(i >= 0 && i < std::ssize(c->alternatives))
885 return c->alternatives[i].first;
886 }
887 else if(auto* e = qobject_cast<const Process::Enum*>(&port))
888 {
889 const int i = e->indexOfValue(v);
890 if(i >= 0 && i < std::ssize(e->values))
891 return e->values[i];
892 }
893 if(auto str = v.target<std::string>())
894 {
895 QString text = QString::fromStdString(*str);
896 // Files: base name only
897 if(text.contains(QLatin1Char('/')) || text.contains(QLatin1Char('\\')))
898 text = QFileInfo{text}.completeBaseName();
899 else if(text.isEmpty())
900 text = QStringLiteral("\u2014");
901 return metrics.elidedText(text, Qt::ElideRight, max_width);
902 }
903 return QString::number(ossia::convert<double>(v), 'g', 4);
904 }
905
906 template <typename Item>
907 void createDisplay(Item& item, auto... member)
908 {
909 if constexpr(requires { ((inputs_type{}).*....*member); })
910 {
911 const int index = avnd::index_in_struct(temp_inputs, member...);
912 auto& proc = static_cast<const ProcessModel<Info>&>(this->proc);
913 for(auto* p : proc.avnd_input_idx_to_model_ports(index))
914 {
915 auto* port = qobject_cast<Process::ControlInlet*>(p);
916 if(!port)
917 continue;
918 if(auto* file = qobject_cast<Process::FileChooserBase*>(port))
919 acceptFileDrops(*file);
920 if constexpr(
921 requires { Item::style; } && Item::style == halp::display_style::bar)
922 {
923 auto* bar = new oscr::ValueBarItem;
924 auto show = [bar, port](const ossia::value& v) {
925 bar->setValue(oscr::normalizedValue(*port, v));
926 };
927 show(port->value());
928 QObject::connect(port, &Process::ControlInlet::valueChanged, bar, show);
929 setupItem(item, *bar);
930 }
931 else if constexpr(
932 requires { Item::style; } && Item::style == halp::display_style::title)
933 {
934 // No item: shown as the cell's title
935 if(auto* cell = enclosingCell())
936 {
937 auto show = [cell, port, metrics = QFontMetricsF{score::Skin::instance().Medium8Pt}](
938 const ossia::value& v) {
939 const auto str = v.target<std::string>();
940 cell->setShownTitle(
941 str && !str->empty() ? displayText(*port, v, metrics, 1e6) : QString{});
942 };
943 show(port->value());
944 QObject::connect(port, &Process::ControlInlet::valueChanged, cell, show);
945 }
946 }
947 else
948 {
949 auto* text
950 = new score::SimpleTextItem{score::Skin::instance().Base4.main, nullptr};
951 auto show = [text, port, metrics = QFontMetricsF{score::Skin::instance().Medium8Pt}](
952 const ossia::value& v) {
953 text->setText(displayText(*port, v, metrics));
954 };
955 show(port->value());
956 QObject::connect(port, &Process::ControlInlet::valueChanged, text, show);
957 setupItem(item, *text);
958 }
959 break;
960 }
961 }
962 }
963
964 void addControlInlet(std::vector<Process::ControlInlet*>& ports, auto... member)
965 {
966 if constexpr(requires { ((inputs_type{}).*....*member); })
967 {
968 const int index = avnd::index_in_struct(temp_inputs, member...);
969 auto& proc = static_cast<const ProcessModel<Info>&>(this->proc);
970 for(auto* p : proc.avnd_input_idx_to_model_ports(index))
971 {
972 if(auto* port = qobject_cast<Process::ControlInlet*>(p))
973 {
974 ports.push_back(port);
975 return;
976 }
977 }
978 }
979 else
980 {
981 static_assert(sizeof...(member) < 0, "not_a_member_of_inputs");
982 }
983 }
984
985 template <typename Item>
986 void createCustomMultiControl(Item& item, auto... recursive_members)
987 {
988 std::vector<Process::ControlInlet*> ports;
989 std::apply(
990 [&](auto... models) { (addControlInlet(ports, recursive_members..., models), ...); },
991 Item::models());
992 auto* qitem = new oscr::CustomMultiControl<Item&>{item, std::move(ports), *uiEdits};
993 if constexpr(requires {
994 Item::selection();
995 item.select = std::function<void(int)>{};
996 item.on_selection_changed(0);
997 })
998 {
999 auto selection = selectionNamed(Item::selection());
1000 item.select = [selection](int i) { selection->select(i); };
1001 selection->listen([guard = QPointer<QObject>{qitem}, &item](int i) {
1002 if(guard)
1003 item.on_selection_changed(i);
1004 });
1005 }
1006 setupItem(item, *qitem);
1007 }
1008
1009 template <typename Item>
1010 void createCustom(Item& item)
1011 {
1012 static_assert(!requires { item.transaction; });
1013 if constexpr(requires { item.send_to_timeline = [](halp::timeline_clip) { }; })
1014 {
1015 item.send_to_timeline
1016 = [&proc = this->proc, &doc = this->doc](halp::timeline_clip clip) {
1017 oscr::sendToTimeline(proc, doc, clip);
1018 };
1019 }
1020 auto res = new oscr::CustomItem<Item&>{item};
1021 res->setDocument(this->doc);
1022 setupItem(item, *res);
1023 if constexpr(requires { Item::watched(); })
1024 std::apply([&](auto... m) { (watchControl(item, m), ...); }, Item::watched());
1025 }
1026
1030 template <typename Item>
1031 void watchControl(Item& item, auto member)
1032 {
1033 using field_type = std::remove_cvref_t<decltype(temp_inputs.*member)>;
1034 const int index = avnd::index_in_struct(temp_inputs, member);
1035 auto& proc = static_cast<const ProcessModel<Info>&>(this->proc);
1036 for(auto* p : proc.avnd_input_idx_to_model_ports(index))
1037 {
1038 if(auto* port = qobject_cast<Process::ControlInlet*>(p))
1039 {
1040 auto show = [&item, &doc = this->doc](const ossia::value& v) {
1041 field_type field{};
1042 SetGUIValue<field_type>{doc}(v, field.value);
1043 item.watch(field);
1044 };
1045 show(port->value());
1046 QObject::connect(port, &Process::ControlInlet::valueChanged, connections, show);
1047 return;
1048 }
1049 }
1050 }
1051
1052 template <typename Item>
1053 void createCustomControl(Item& item, auto... member)
1054 {
1055 if constexpr(requires { ((inputs_type{}).*....*member); })
1056 {
1057 int index = avnd::index_in_struct(temp_inputs, member...);
1058
1059 auto& proc = static_cast<const ProcessModel<Info>&>(this->proc);
1060 auto ports = proc.avnd_input_idx_to_model_ports(index);
1061 for(auto p : ports)
1062 {
1063 if(auto* port = qobject_cast<Process::ControlInlet*>(p))
1064 {
1065 auto qitem = new oscr::CustomControl<Item&>{item, *port, this->doc, *uiEdits};
1066 Process::ControlLayout lay{.container = qitem};
1067 if(auto* f = portFactory.get(port->concreteKey()))
1068 {
1069 lay.port_item = f->makePortItem(*port, this->doc, this->layout, &context);
1070 }
1071 setupControl(this->layout, port, lay, item);
1072 setupItem(item, *qitem);
1073 }
1074 }
1075 }
1076 else if constexpr(requires { ((outputs_type{}).*....*member); })
1077 {
1078 int index = avnd::index_in_struct(temp_outputs, member...);
1079
1080 auto& proc = static_cast<const ProcessModel<Info>&>(this->proc);
1081 auto ports = proc.avnd_output_idx_to_model_ports(index);
1082 for(auto p : ports)
1083 {
1084 if(auto* port = qobject_cast<Process::ControlOutlet*>(p))
1085 {
1086 auto qitem = new oscr::CustomControl<Item&>{item, *port, this->doc, *uiEdits};
1087 Process::ControlLayout lay{.container = qitem};
1088 if(auto* f = portFactory.get(port->concreteKey()))
1089 {
1090 lay.port_item = f->makePortItem(*port, this->doc, this->layout, &context);
1091 }
1092 setupControl(this->layout, port, lay, item);
1093 setupItem(item, *qitem);
1094 }
1095 }
1096 }
1097 else
1098 {
1099 static_assert(sizeof...(member) < 0, "not_a_member_of_inputs_or_outputs");
1100 }
1101 }
1102
1103 template <typename Item>
1104 void subLayout(Item& item, score::GraphicsLayout* new_l, auto... recursive_members)
1105 {
1106 auto old_l = layout;
1107 setupLayout(item, *new_l);
1108 setupItem(item, *new_l);
1109 layout = new_l;
1110 createdLayouts.push_back(new_l);
1111
1112 // A named row of a table starts with its name
1113 if(auto table = dynamic_cast<score::GraphicsTableLayout*>(old_l))
1114 {
1115 if constexpr(requires { Item::name(); })
1116 {
1117 makeLabel(Item::name())->setParentItem(new_l);
1118 table->setRowTitles(true);
1119 }
1120 if(auto row = dynamic_cast<score::GraphicsSelectableRow*>(new_l))
1121 {
1122 auto it = tableSelections.find(table);
1123 if(it != tableSelections.end())
1124 {
1125 // Index: number of rows before it
1126 int index = -1;
1127 for(auto* child : table->childItems())
1128 if(dynamic_cast<score::GraphicsSelectableRow*>(child))
1129 ++index;
1130 auto sel = it->second;
1131 row->onActivated = [sel, index] { sel->select(index); };
1132 sel->listen([row, index](int i) { row->setSelected(i == index); });
1133 }
1134 }
1135 }
1136 if constexpr(avnd::table_layout<Item> && requires { Item::columns(); })
1137 ++tableDepth;
1138
1139 if constexpr(requires { Item::condition(); })
1140 setupCondition<Item>(*new_l, recursive_members...);
1141
1142 if constexpr(avnd::table_layout<Item>)
1143 {
1144 forEachMember(item, [](auto& row) {
1145 static_assert(
1146 avnd::hbox_layout<std::remove_cvref_t<decltype(row)>>,
1147 "The members of a halp table are its rows: structs with "
1148 "halp_meta(layout, halp::layouts::hbox)");
1149 });
1150 }
1151
1152 // Rows of a table naming a selection select their unit when pressed
1153 if constexpr(avnd::table_layout<Item> && requires { Item::selection(); })
1154 {
1155 auto* table = static_cast<score::GraphicsTableLayout*>(new_l);
1156 table->setRowsSelectable(true);
1157 tableSelections[table] = selectionNamed(Item::selection());
1158 }
1159
1160 if constexpr(avnd::tab_layout<Item> && requires { Item::selection(); })
1161 {
1162 static_assert(!requires { Item::model; },
1163 "Tabs use either a UI selection or a control model");
1164 auto* tabs = static_cast<score::GraphicsTabLayout*>(new_l);
1165 auto selection = selectionNamed(Item::selection());
1166 selection->listen([guard = QPointer<score::GraphicsTabLayout>{tabs}](int i) {
1167 if(guard)
1168 guard->setCurrentIndex(i);
1169 });
1170 tabs->onCurrentIndexChanged = [selection](int i) { selection->select(i); };
1171 }
1172
1173 if constexpr(avnd::tab_layout<Item> && requires { Item::model; })
1174 {
1175 auto* tabs = static_cast<score::GraphicsTabLayout*>(new_l);
1176 const int index
1177 = avnd::index_in_struct(temp_inputs, recursive_members..., Item::model);
1178
1179 // Optional: static constexpr auto when() { return std::array{values...}; }
1180 // Default: value i shows page i.
1181 std::vector<int> page_of;
1182 {
1183 int page = 0;
1184 forEachMember(item, [&](auto& p) {
1185 using page_type = std::remove_cvref_t<decltype(p)>;
1186 if constexpr(requires { page_type::when(); })
1187 {
1188 for(auto v : page_type::when())
1189 {
1190 const int i = entryIndex(v);
1191 if(i < 0)
1192 continue;
1193 if(i >= std::ssize(page_of))
1194 page_of.resize(i + 1, -1);
1195 page_of[i] = page;
1196 }
1197 }
1198 ++page;
1199 });
1200 }
1201 auto to_page = [page_of](int i) {
1202 return (i >= 0 && i < std::ssize(page_of) && page_of[i] >= 0) ? page_of[i] : i;
1203 };
1204 auto to_value_index = [page_of](int page) {
1205 for(int i = 0; i < std::ssize(page_of); i++)
1206 if(page_of[i] == page)
1207 return i;
1208 return page;
1209 };
1210 // A root tab layout outlives a UI rebuild: its connections go with the build
1211 auto& model = static_cast<const ProcessModel<Info>&>(this->proc);
1212 for(auto* base : model.avnd_input_idx_to_model_ports(index))
1213 {
1214 if(auto* port = dynamic_cast<Process::Enum*>(base))
1215 {
1216 auto select = [tabs, port, to_page](const ossia::value& value) {
1217 if(const int i = port->indexOfValue(value); i >= 0)
1218 tabs->setCurrentIndex(to_page(i));
1219 };
1220 select(port->value());
1221 QObject::connect(
1222 port, &Process::ControlInlet::valueChanged,
1223 connections ? connections : static_cast<QObject*>(tabs), select);
1224 if constexpr(!avnd::tag_hide_tabs<Item>)
1225 tabs->onCurrentIndexChanged
1226 = [port, to_value_index, &doc = this->doc](int i) {
1227 const ossia::value value = port->valueAtIndex(to_value_index(i));
1228 if(value.valid() && port->value() != value)
1229 CommandDispatcher<>{doc.commandStack}.submit<Process::SetControlValue>(
1230 *port, value);
1231 };
1232 break;
1233 }
1234 if(auto* port = dynamic_cast<Process::ComboBox*>(base))
1235 {
1236 auto select = [tabs, port, to_page](const ossia::value& value) {
1237 if(const int i = port->indexOfValue(value); i >= 0)
1238 tabs->setCurrentIndex(to_page(i));
1239 };
1240 select(port->value());
1241 QObject::connect(
1242 port, &Process::ControlInlet::valueChanged,
1243 connections ? connections : static_cast<QObject*>(tabs), select);
1244 if constexpr(!avnd::tag_hide_tabs<Item>)
1245 tabs->onCurrentIndexChanged
1246 = [port, to_value_index, &doc = this->doc](int i) {
1247 const ossia::value value = port->valueAtIndex(to_value_index(i));
1248 if(value.valid() && port->value() != value)
1249 CommandDispatcher<>{doc.commandStack}.submit<Process::SetControlValue>(
1250 *port, value);
1251 };
1252 break;
1253 }
1254 }
1255 }
1256
1257 if constexpr(avnd::strip_detail_layout<Item>)
1258 {
1259 auto& strip = *static_cast<score::GraphicsStripDetailLayout*>(new_l);
1260 createdLayouts.push_back(&strip.strip());
1261 // Each page: a strip cell with its summary, then the page
1262 auto page = [&](auto& p) {
1263 auto* cell = new score::GraphicsStripCell{nullptr};
1264 using page_type = std::remove_cvref_t<decltype(p)>;
1265 if constexpr(requires { page_type::name(); })
1266 {
1267 const std::string_view name = page_type::name();
1268 cell->setTitle(QString::fromUtf8(name.data(), qsizetype(name.size())));
1269 }
1270 strip.addCell(cell);
1271 createdLayouts.push_back(cell);
1272 // A member, so that it lives as long as the UI
1273 if constexpr(requires { p.summary; })
1274 {
1275 layout = cell;
1276 walkMembers(p.summary, recursive_members..., p.group);
1277 layout = new_l;
1278 }
1279 this->walkLayout(p, recursive_members...);
1280 };
1281 forEachMember(item, page);
1282
1283 // e.g. table rows choose the page
1284 if constexpr(requires { Item::selection(); })
1285 {
1286 auto sel = selectionNamed(Item::selection());
1287 sel->listen([&strip](int i) { strip.setCurrentIndex(i); });
1288 strip.onCurrentIndexChanged = [sel](int i) { sel->select(i); };
1289 if constexpr(avnd::tag_hide_tabs<Item>)
1290 strip.setStripVisible(false);
1291 }
1292 }
1293 else
1294 {
1295 walkMembers(item, recursive_members...);
1296 }
1297
1298 if constexpr(avnd::table_layout<Item> && requires { Item::columns(); })
1299 --tableDepth;
1300 layout = old_l;
1301 }
1302
1303 template <typename Item, typename F>
1304 static void forEachMember(Item& item, F&& f)
1305 {
1306#if AVND_USE_BOOST_PFR
1307 static constexpr int N = avnd::pfr::tuple_size_v<Item>;
1308 auto t = avnd::pfr::detail::tie_as_tuple(item);
1309 [&]<std::size_t... I>(std::index_sequence<I...>) {
1310 using namespace std;
1311 using namespace avnd::pfr;
1312 (f(get<I>(t)), ...);
1313 }(std::make_index_sequence<N>{});
1314#else
1315 auto&& [... members] = item;
1316 (f(members), ...);
1317#endif
1318 }
1319
1320 template <typename Item>
1321 void walkMembers(Item& item, auto... recursive_members)
1322 {
1323 forEachMember(item, [&](auto& m) { this->walkLayout(m, recursive_members...); });
1324 }
1325
1326 template <typename Item>
1327 auto initRecursiveLayout()
1328 {
1329 if constexpr(avnd::hbox_layout<Item>)
1330 {
1331 // The rows of a table driving a selection can be selected
1332 if(auto table = dynamic_cast<score::GraphicsTableLayout*>(this->layout);
1333 table && tableSelections.contains(table))
1334 {
1336 setupRecursiveLayout<Item>(row);
1337 return row;
1338 }
1339 }
1340 auto new_l = createRecursiveLayout<Item>(this->layout);
1341 setupRecursiveLayout<Item>(new_l);
1342 return new_l;
1343 }
1344
1345 template <typename Item>
1346 void walkLayout(Item& item, auto... recursive_members)
1347 {
1348 if constexpr(avnd::spacing_layout<Item>)
1349 {
1350 auto widg = new score::EmptyRectItem{layout};
1351 // Static (halp_meta) or per-instance, like halp::spacing{.width, .height}
1352 double w = 1., h = 1.;
1353 if constexpr(requires { Item::width(); })
1354 w = Item::width();
1355 else if constexpr(requires { item.width; })
1356 w = item.width;
1357 if constexpr(requires { Item::height(); })
1358 h = Item::height();
1359 else if constexpr(requires { item.height; })
1360 h = item.height;
1361 widg->setRect({0, 0, w, h});
1362 // A spacing that fills its row: static constexpr bool stretch() { return true; }
1363 if constexpr(requires { Item::stretch(); })
1364 if(Item::stretch())
1366 }
1367 else if constexpr(recursive_container_layout<Item>)
1368 {
1369 subLayout(item, initRecursiveLayout<Item>(), recursive_members...);
1370 }
1371 else if constexpr(avnd::control_layout<Item> && requires { Item::display_only; })
1372 {
1373 // Read-only view of a value, e.g. in a strip cell
1374 createDisplay(item, recursive_members..., item.model);
1375 }
1376 else if constexpr(avnd::control_layout<Item>)
1377 {
1378 // Widget with some metadata.. FIXME
1379 // Auto-generated item for a control
1380 if constexpr(
1381 sizeof...(recursive_members) > 0
1382 && !requires { ((inputs_type{}).*....*recursive_members).*item.model; }
1383 && requires { (inputs_type{}).*item.model; })
1384 {
1385 // In a recursive group, a control of the processor itself rather than
1386 // of the group, e.g. one added after all the groups so as to keep the
1387 // ids of their ports
1388 createWidget(item, item.model);
1389 }
1390 else
1391 {
1392 createWidget(item, recursive_members..., item.model);
1393 }
1394 }
1395 else if constexpr(avnd::custom_multi_control_layout<Item>)
1396 {
1397 // Custom-drawn item bound to several controls
1398 createCustomMultiControl(item, recursive_members...);
1399 }
1400 else if constexpr(avnd::custom_control_layout<Item>)
1401 {
1402 // Widget with some metadata.. FIXME
1403 // Custom-drawn item for a control
1404 createCustomControl(item, recursive_members..., item.model);
1405 }
1406 else if constexpr(avnd::custom_layout<Item>)
1407 {
1408 // Widget with some metadata.. FIXME
1409 // This is just a cosmetic item without behaviour or control attached
1410 createCustom(item);
1411 }
1412 else if constexpr(avnd::recursive_group_layout<Item>)
1413 {
1414 walkLayout(item.ui, recursive_members..., item.group);
1415 }
1416 else if constexpr(avnd::dynamic_controls<Item>)
1417 {
1418 walkLayout(item.ui, recursive_members..., item.group);
1419 }
1420 else if constexpr(avnd::has_layout<Item>)
1421 {
1422 // Treat it like group
1423 subLayout(item, initRecursiveLayout<Item>(), recursive_members...);
1424 }
1425 else
1426 {
1427 // Normal widget, e.g. just a const char*
1428 createWidget(item, item);
1429 }
1430 }
1431};
1432
1433template <typename Info>
1435{
1436public:
1437 virtual ~LayerFactory() { }
1438
1439private:
1440 std::optional<double> recommendedHeight() const noexcept override
1441 {
1442 if constexpr(requires { (double)Info::layout::height(); })
1443 {
1444 return Info::layout::height();
1445 }
1446 return Process::LayerFactory::recommendedHeight();
1447 }
1448
1449 UuidKey<Process::ProcessModel> concreteKey() const noexcept override
1450 {
1452 }
1453
1454 bool matches(const UuidKey<Process::ProcessModel>& p) const override
1455 {
1457 }
1458
1459 Process::LayerView* makeLayerView(
1460 const Process::ProcessModel& proc, const Process::Context& context,
1461 QGraphicsItem* parent) const final override
1462 {
1463 return nullptr;
1464 }
1465
1466 Process::LayerPresenter* makeLayerPresenter(
1468 const Process::Context& context, QObject* parent) const final override
1469 {
1470 return nullptr;
1471 }
1472
1473 template <typename Item>
1474 static void init_bus(ProcessModel<Info>& proc, Item& item)
1475 {
1476 auto ptr = &item;
1477 if constexpr(avnd::has_gui_to_processor_bus<Info>)
1478 {
1479 // ui -> engine
1480 ptr->bus.send_message = MessageBusSender{proc.from_ui};
1481 }
1482
1483 if constexpr(avnd::has_processor_to_gui_bus<Info>)
1484 {
1485 // engine -> ui: every view of the process listens.
1486 proc.add_to_ui_listener(ptr, [ptr = QPointer{ptr}](QByteArray mess) {
1487 // FIXME this is not enough as the message may be sent from another thread?
1488 if(!ptr)
1489 return;
1490 // What the processor tells the ui does not edit the document
1491 UiEdits::Reacting r{ptr->uiEdits};
1492
1493 if constexpr(requires { ptr->bus.process_message(); })
1494 {
1495 ptr->bus.process_message();
1496 }
1497 else if constexpr(requires { ptr->bus.process_message(ptr->ui); })
1498 {
1499 ptr->bus.process_message(ptr->ui);
1500 }
1501 else if constexpr(requires { ptr->bus.process_message(ptr->ui, {}); })
1502 {
1503 std::decay_t<avnd::second_argument<&Info::ui::bus::process_message>> arg;
1504 MessageBusReader b{mess};
1505 b(arg);
1506 ptr->bus.process_message(ptr->ui, std::move(arg));
1507 }
1508 else
1509 {
1510 ptr->bus.process_message(ptr->ui, {});
1511 }
1512 });
1513 }
1514
1515 if_possible(ptr->bus.init(ptr->ui));
1516 }
1517
1518 auto makeItemImpl(ProcessModel<Info>& proc, QGraphicsItem* parent) const noexcept
1519 {
1520 using ui_type = typename Info::ui;
1521 using root_layout_type
1522 = std::remove_cvref_t<decltype(*createRecursiveLayout<ui_type>(nullptr))>;
1523
1524 auto new_l = new RootItem<Info, root_layout_type>{parent};
1525 setupRecursiveLayout<ui_type>(new_l);
1526 if constexpr(requires { sizeof(typename Info::ui::bus); })
1527 init_bus(proc, *new_l);
1528
1529 if constexpr(requires { new_l->ui.start(); })
1530 {
1531 QObject::connect(&proc, &Process::ProcessModel::startExecution, new_l, [new_l] {
1532 UiEdits::Reacting r{new_l->uiEdits};
1533 new_l->ui.start();
1534 });
1535 }
1536 if constexpr(requires { new_l->ui.stop(); })
1537 {
1538 QObject::connect(&proc, &Process::ProcessModel::stopExecution, new_l, [new_l] {
1539 UiEdits::Reacting r{new_l->uiEdits};
1540 new_l->ui.stop();
1541 });
1542 }
1543 if constexpr(requires { new_l->ui.reset(); })
1544 {
1545 QObject::connect(&proc, &Process::ProcessModel::resetExecution, new_l, [new_l] {
1546 UiEdits::Reacting r{new_l->uiEdits};
1547 new_l->ui.reset();
1548 });
1549 }
1550 return new_l;
1551 }
1552
1553 score::ResizeableItem* makeItem(
1554 const Process::ProcessModel& proc, const Process::Context& ctx,
1555 QGraphicsItem* parent) const final override
1556 {
1557 using namespace score;
1558 auto& process = static_cast<const ProcessModel<Info>&>(proc);
1559
1560 auto rootItem = makeItemImpl(const_cast<ProcessModel<Info>&>(process), parent);
1561 rootItem->uiEdits = new UiEdits{ctx, rootItem};
1562
1563 auto recreate = [parent, &proc, &ctx, rootItem] {
1564 delete rootItem->uiConnections;
1565 rootItem->uiConnections = new QObject{rootItem};
1566 LayoutBuilder<Info> b{
1567 *rootItem, proc,
1568 ctx, ctx.app.interfaces<Process::PortFactoryList>(),
1569 proc.inlets(), proc.outlets(),
1570 };
1571 b.rootUi = &rootItem->ui;
1572 b.connections = rootItem->uiConnections;
1573 b.uiEdits = rootItem->uiEdits;
1574 b.layout = parent;
1575 // avnd UIs declare their own spacing
1576 b.marginOnNestedLayouts = false;
1577
1578 b.subLayout(rootItem->ui, rootItem);
1579
1580 b.finalizeLayout(rootItem);
1581
1582 rootItem->fitChildrenRect();
1583
1584 {
1585 UiEdits::Reacting r{rootItem->uiEdits};
1586 if_possible(b.rootUi->on_control_update());
1587 }
1588 };
1589
1590 // Delete the connections before the widgets: a widget losing focus while
1591 // being deleted still emits its edit.
1592 auto rebuild = [rootItem, recreate] {
1593 delete rootItem->uiConnections;
1594 rootItem->uiConnections = nullptr;
1595 auto cld = rootItem->childItems();
1596 for(auto item : cld)
1597 {
1598 delete item;
1599 }
1600 recreate();
1601 };
1602 QObject::connect(&proc, &Process::ProcessModel::inletsChanged, rootItem, rebuild);
1603 QObject::connect(&proc, &Process::ProcessModel::outletsChanged, rootItem, rebuild);
1604
1605 recreate();
1606 return rootItem;
1607 }
1608};
1609
1610}
The CommandDispatcher class.
Definition CommandDispatcher.hpp:13
Definition Port.hpp:247
Definition score-lib-process/Process/ProcessFactory.hpp:59
Definition LayerPresenter.hpp:34
Definition LayerView.hpp:21
Definition PortFactory.hpp:121
The Process class.
Definition score-lib-process/Process/Process.hpp:82
Definition SetControlValue.hpp:14
Definition UuidKey.hpp:345
Definition Painter.hpp:889
Definition Painter.hpp:425
void setDocument(const score::DocumentContext &doc) noexcept
The document the sound files drawn with draw_waveform() are looked up in.
Definition Painter.hpp:517
Definition ControlViews.hpp:105
Definition score-plugin-avnd/Crousti/Layer.hpp:1435
Definition score-plugin-avnd/Crousti/ProcessModel.hpp:123
The edits a custom ui makes to its process's controls.
Definition UiEdits.hpp:76
halp::display_style::bar
Definition ControlViews.hpp:30
Definition RectItem.hpp:78
Definition GraphicsGridLayout.hpp:13
Definition GraphicsGridLayout.hpp:27
Definition GraphicsBoxLayout.hpp:25
Definition GraphicsLayout.hpp:8
static void relayoutAround(QGraphicsItem *item)
Definition GraphicsLayout.cpp:93
Titled vbox padded on all sides. Sizes itself, independent of parent margins.
Definition GraphicsBoxLayout.hpp:68
Table row selected by a press on its background or controls.
Definition GraphicsBoxLayout.hpp:44
Definition GraphicsSplitLayout.hpp:9
Summary of a page, selects it on click.
Definition GraphicsStripDetailLayout.hpp:14
Definition GraphicsStripDetailLayout.hpp:55
Definition GraphicsTabLayout.hpp:12
Definition GraphicsGridLayout.hpp:43
Definition GraphicsBoxLayout.hpp:34
FactoryType * get(const key_type &k) const noexcept
Get a particular factory from its ConcreteKey.
Definition InterfaceList.hpp:128
Definition QGraphicsCombo.hpp:24
void valueEdited(const QString &text)
Text matching none of the entries; only sent when editable().
Definition QGraphicsEnum.hpp:17
Definition RectItem.hpp:12
Definition TextItem.hpp:28
Definition score-plugin-avnd/Crousti/Layer.hpp:53
Definition Controls.hpp:31
double normalizedValue(const Process::ControlInlet &port, const ossia::value &v)
A control's value, normalized over its range.
Definition ControlViews.hpp:72
void sendToTimeline(const Process::ProcessModel &proc, const score::DocumentContext &ctx, const halp::timeline_clip &clip)
Definition HostServices.cpp:64
Base toolkit upon which the software is built.
Definition Application.cpp:85
QString locateFilePath(const QString &filename) noexcept
Definition File.cpp:84
void setLayoutExcluded(QGraphicsItem &item, bool excluded)
Definition GraphicsBoxLayout.cpp:44
void setLayoutStretch(QGraphicsItem &item)
Definition GraphicsBoxLayout.cpp:27
QString relativizeFilePath(const QString &filename, const score::DocumentContext &ctx) noexcept
Definition File.cpp:122
STL namespace.
Static metadata implementation.
Definition lib/score/tools/Metadata.hpp:36
Definition ProcessContext.hpp:12
Definition PortFactory.hpp:54
Overrides of a control's default presentation in a layout.
Definition PortFactory.hpp:25
@ None
the port and its label only, for any control
std::optional< QString > label
Display only: the port name is unchanged.
Definition PortFactory.hpp:36
Definition plugins/score-lib-process/Control/Layout.hpp:16
Definition ObjectMatches.hpp:6
Definition score-plugin-avnd/Crousti/Layer.hpp:257
Definition score-plugin-avnd/Crousti/Layer.hpp:326
static QString displayText(const Process::ControlInlet &port, const ossia::value &v, const QFontMetricsF &metrics, qreal max_width=72.)
What a display shows for a value: an entry's name, or a number.
Definition score-plugin-avnd/Crousti/Layer.hpp:877
static consteval bool shownAsCombo()
Whether the native widget of Field, as Item presents it, is a QGraphicsCombo.
Definition score-plugin-avnd/Crousti/Layer.hpp:562
int tableDepth
Depth in titled tables: controls there hide their labels.
Definition score-plugin-avnd/Crousti/Layer.hpp:338
std::map< std::string, std::shared_ptr< score::LayoutSelection >, std::less<> > selections
Named selections, and the one each selecting table drives.
Definition score-plugin-avnd/Crousti/Layer.hpp:341
QObject * connections
Context object of the connections to this build's widgets.
Definition score-plugin-avnd/Crousti/Layer.hpp:334
void watchControl(Item &item, auto member)
Definition score-plugin-avnd/Crousti/Layer.hpp:1031
void setupControl(QGraphicsItem *parent, Port_T *port, const Process::ControlLayout &lay, Item &item)
Definition score-plugin-avnd/Crousti/Layer.hpp:368
void setupDependentControl(Process::ControlInlet *port, const Process::ControlLayout &lay, Item &item)
Definition score-plugin-avnd/Crousti/Layer.hpp:586
void acceptFileDrops(Process::FileChooserBase &port)
Files dropped on the enclosing strip cell set the port.
Definition score-plugin-avnd/Crousti/Layer.hpp:841
score::GraphicsStripCell * enclosingCell() const
The strip cell being built, if any.
Definition score-plugin-avnd/Crousti/Layer.hpp:832
void setupCondition(score::GraphicsLayout &lay, auto... recursive_members)
Definition score-plugin-avnd/Crousti/Layer.hpp:504
std::map< QGraphicsItem *, QTimer * > dependentRelayouts
One queued geometry refresh per native row, owned by this UI build.
Definition score-plugin-avnd/Crousti/Layer.hpp:346
Definition MessageBus.hpp:391
Definition MessageBus.hpp:238
Definition score-plugin-avnd/Crousti/Layer.hpp:60
Definition score-plugin-avnd/Crousti/Layer.hpp:73
~RootItem()
Definition score-plugin-avnd/Crousti/Layer.hpp:78
QObject * uiConnections
Definition score-plugin-avnd/Crousti/Layer.hpp:90
UiEdits * uiEdits
Definition score-plugin-avnd/Crousti/Layer.hpp:93
Definition score-plugin-avnd/Crousti/Layer.hpp:219
While one is alive, the ui is reacting to a change and set does nothing.
Definition UiEdits.hpp:97
Definition score-plugin-avnd/Crousti/Layer.hpp:207
Definition DocumentContext.hpp:18