PresetHelpers.hpp
1 #pragma once
2 #include <Process/Dataflow/Port.hpp>
3 #include <Process/Process.hpp>
4 
5 #include <ossia/detail/algorithms.hpp>
6 
7 namespace Process
8 {
9 
10 template <typename T = Process::ControlInlet>
11 void saveFixedControls(JSONReader& r, const Process::ProcessModel& proc)
12 {
13  r.stream.StartArray();
14  for(const auto& inlet : proc.inlets())
15  {
16  if(auto ctrl = qobject_cast<Process::ControlInlet*>(inlet))
17  {
18  r.stream.StartArray();
19  r.stream.Int(ctrl->id().val());
20  r.readFrom(ctrl->value());
21  r.stream.EndArray();
22  }
23  }
24  r.stream.EndArray();
25 }
26 
27 template <typename T = Process::ControlInlet>
28 void loadFixedControls(
29  const rapidjson::Document::ConstArray& ctrls, Process::ProcessModel& proc)
30 {
31  for(const auto& arr : ctrls)
32  {
33  const auto& id = arr[0].GetInt();
34  ossia::value val = JsonValue{arr[1]}.to<ossia::value>();
35 
36  auto it = ossia::find_if(
37  proc.inlets(), [&](const auto& inl) { return inl->id().val() == id; });
38  if(it != proc.inlets().end())
39  {
40  Process::Inlet& inlet = **it;
41  if(auto ctrl = qobject_cast<T*>(&inlet))
42  {
43  ctrl->setValue(val);
44  }
45  }
46  }
47 }
48 
49 template <typename T>
50 Process::Preset saveScriptProcessPreset(const T& process, const QString& data)
51 {
53  p.name = process.metadata().getName();
54  p.key.key = Metadata<ConcreteKey_k, T>::get();
55  p.key.effect = data;
56 
57  JSONReader r;
58  Process::saveFixedControls(r, process);
59 
60  p.data = r.toByteArray();
61  return p;
62 }
63 
64 template <typename ScriptProperty, typename T>
65 void loadScriptProcessPreset(T& process, const Process::Preset& preset)
66 {
67  const rapidjson::Document doc = readJson(preset.data);
68  if(!doc.IsArray())
69  return;
70  Process::loadFixedControls(doc.GetArray(), process);
71 }
72 }
Definition: JSONVisitor.hpp:52
void readFrom(const score::Entity< T > &obj)
Definition: EntitySerialization.hpp:7
Definition: Port.hpp:177
The Process class.
Definition: score-lib-process/Process/Process.hpp:61
Base classes and tools to implement processes and layers.
Definition: JSONVisitor.hpp:1324
Definition: JSONVisitor.hpp:372
Static metadata implementation.
Definition: lib/score/tools/Metadata.hpp:36
Definition: Preset.hpp:32