RemoveTrigger.hpp
1 #pragma once
2 
3 #include <State/Expression.hpp>
4 
5 #include <Scenario/Commands/Interval/SetRigidity.hpp>
6 #include <Scenario/Commands/ScenarioCommandFactory.hpp>
7 #include <Scenario/Document/TimeSync/TimeSyncModel.hpp>
8 
9 #include <score/command/Command.hpp>
10 #include <score/model/path/Path.hpp>
11 
12 namespace Scenario
13 {
14 class TimeSyncModel;
15 namespace Command
16 {
17 template <typename Scenario_T>
18 class RemoveTrigger final : public score::Command
19 {
20 public:
21  const CommandGroupKey& parentKey() const noexcept override
22  {
23  return ::CommandFactoryName<Scenario_T>();
24  }
25  const CommandKey& key() const noexcept override { return static_key(); }
26  QString description() const override { return QObject::tr("Remove a trigger"); }
27  static const CommandKey& static_key()
28  {
29  static const CommandKey kagi{
30  QString("RemoveTrigger_") + Metadata<ObjectKey_k, Scenario_T>::get()};
31  return kagi;
32  }
33 
34  RemoveTrigger() = default;
35 
36  RemoveTrigger(Path<TimeSyncModel>&& timeSyncPath)
37  : m_path{std::move(timeSyncPath)}
38  {
39  }
40 
41  void undo(const score::DocumentContext& ctx) const override
42  {
43  auto& tn = m_path.find(ctx);
44  tn.setActive(true);
45 
46  for(const auto& cmd : m_cmds)
47  {
48  cmd.undo(ctx);
49  }
50 
51  m_cmds.clear();
52  }
53  void redo(const score::DocumentContext& ctx) const override
54  {
55  auto& tn = m_path.find(ctx);
56  tn.setActive(false);
57 
58  auto scenar = safe_cast<Scenario_T*>(tn.parent());
59 
60  for(const auto& cstrId : intervalsBeforeTimeSync(*scenar, tn.id()))
61  {
62  m_cmds.emplace_back(scenar->interval(cstrId), true);
63  m_cmds.back().redo(ctx);
64  }
65  }
66 
67 protected:
68  void serializeImpl(DataStreamInput& s) const override
69  {
70  s << m_path;
71  s << (int32_t)m_cmds.size();
72 
73  for(const auto& cmd : m_cmds)
74  {
75  s << cmd.serialize();
76  }
77  }
78  void deserializeImpl(DataStreamOutput& s) override
79  {
80  int32_t n;
81  s >> m_path;
82  s >> n;
83  m_cmds.resize(n);
84  for(int i = 0; i < n; i++)
85  {
86  QByteArray a;
87  s >> a;
88  m_cmds[i].deserialize(a);
89  }
90  }
91 
92 private:
93  Path<TimeSyncModel> m_path;
94  mutable std::vector<SetRigidity> m_cmds;
95 };
96 }
97 }
98 
99 #include <Scenario/Process/ScenarioModel.hpp>
101 
102 #include <Scenario/Document/BaseScenario/BaseScenario.hpp>
The Path class is a typesafe wrapper around ObjectPath.
Definition: Path.hpp:52
Definition: RemoveTrigger.hpp:19
The Command class.
Definition: Command.hpp:34
Main plug-in of score.
Definition: score-plugin-dataflow/Dataflow/PortItem.hpp:14
Definition: DataStreamHelpers.hpp:99
Definition: DataStreamHelpers.hpp:103
Static metadata implementation.
Definition: lib/score/tools/Metadata.hpp:36
Definition: DocumentContext.hpp:18