MoveBaseEvent.hpp
1 #pragma once
2 #include <Process/ExpandMode.hpp>
3 #include <Process/TimeValue.hpp>
4 
5 #include <Scenario/Commands/ScenarioCommandFactory.hpp>
6 #include <Scenario/Document/BaseScenario/BaseScenario.hpp>
7 #include <Scenario/Document/Event/EventModel.hpp>
8 #include <Scenario/Document/TimeSync/TimeSyncModel.hpp>
9 #include <Scenario/Process/Algorithms/ProcessPolicy.hpp>
10 #include <Scenario/Process/Algorithms/StandardDisplacementPolicy.hpp>
11 #include <Scenario/Tools/dataStructures.hpp>
12 
13 #include <score/command/Command.hpp>
14 #include <score/model/Identifier.hpp>
15 #include <score/model/path/Path.hpp>
16 #include <score/model/path/PathSerialization.hpp>
17 #include <score/tools/Unused.hpp>
18 
19 /*
20  * Command to change a interval duration by moving event. Vertical move is
21  * not allowed.
22  */
23 namespace Scenario
24 {
25 class BaseScenario;
26 namespace Command
27 {
28 template <typename SimpleScenario_T>
29 class MoveBaseEvent final : public score::Command
30 {
31 private:
32  template <typename ScaleFun>
33  static void updateDuration(
34  SimpleScenario_T& scenar, const TimeVal& newDuration, ScaleFun&& scaleMethod)
35  {
36  scenar.endEvent().setDate(newDuration);
37  scenar.endTimeSync().setDate(newDuration);
38 
39  auto& interval = scenar.interval();
40  IntervalDurations::Algorithms::changeAllDurations(interval, newDuration);
41  for(auto& process : interval.processes)
42  {
43  scaleMethod(process, newDuration);
44  }
45  }
46 
47 public:
48  const CommandGroupKey& parentKey() const noexcept override
49  {
50  return ::CommandFactoryName<SimpleScenario_T>();
51  }
52  const CommandKey& key() const noexcept override { return static_key(); }
53  QString description() const override
54  {
55  return QObject::tr("Move a %1 event")
57  }
58  static const CommandKey& static_key() noexcept
59  {
60  static const CommandKey kagi{
61  QString("MoveBaseEvent_") + Metadata<ObjectKey_k, SimpleScenario_T>::get()};
62  return kagi;
63  }
64 
65  MoveBaseEvent() = default;
66 
68  const SimpleScenario_T& scenar, const Id<EventModel>& event, const TimeVal& date,
69  double y, ExpandMode mode, LockMode)
70  : m_path{scenar}
71  , m_newDate{date}
72  , m_mode{mode}
73  {
74  const Scenario::IntervalModel& interval = scenar.interval();
75  m_oldDate = interval.duration.defaultDuration();
76  m_saveData = IntervalSaveData{interval, true}; // TODO fix the "clear" under this
77  }
78 
80  const SimpleScenario_T& scenar, const Id<EventModel>& event, const TimeVal& date,
81  double y, ExpandMode mode, LockMode lm, Id<StateModel>)
82  : MoveBaseEvent{scenar, event, date, y, mode, lm}
83  {
84  }
85 
86  void undo(const score::DocumentContext& ctx) const override
87  {
88  auto& scenar = m_path.find(ctx);
89 
90  updateDuration(scenar, m_oldDate, [&](Process::ProcessModel& p, const TimeVal& v) {
91  // Nothing is needed since the processes will be replaced anyway.
92  });
93 
94  // TODO do this only if we shrink.
95 
96  // Now we have to restore the state of each interval that might have been
97  // modified
98  // during this command.
99 
100  // 1. Clear the interval
101  ClearInterval clearCmd{scenar.interval()};
102  clearCmd.redo(ctx);
103 
104  // 2. Restore
105  auto& interval = scenar.interval();
106  m_saveData.reload(interval);
107  }
108 
109  void redo(const score::DocumentContext& ctx) const override
110  {
111  auto& scenar = m_path.find(ctx);
112 
113  updateDuration(scenar, m_newDate, [&](Process::ProcessModel& p, const TimeVal& v) {
114  p.setParentDuration(m_mode, v);
115  });
116  }
117 
118  void update(unused_t, unused_t, const TimeVal& date, double, ExpandMode, LockMode)
119  {
120  m_newDate = date;
121  }
122  void
123  update(unused_t, unused_t, const TimeVal& date, double, ExpandMode, LockMode, unused_t)
124  {
125  m_newDate = date;
126  }
127 
128  const Path<SimpleScenario_T>& path() const { return m_path; }
129 
130 protected:
131  void serializeImpl(DataStreamInput& s) const override
132  {
133  s << m_path << m_oldDate << m_newDate << (int)m_mode << m_saveData;
134  }
135  void deserializeImpl(DataStreamOutput& s) override
136  {
137  int mode;
138  s >> m_path >> m_oldDate >> m_newDate >> mode >> m_saveData;
139  m_mode = static_cast<ExpandMode>(mode);
140  }
141 
142 private:
143  Path<SimpleScenario_T> m_path;
144 
145  TimeVal m_oldDate{};
146  TimeVal m_newDate{};
147 
148  ExpandMode m_mode{ExpandMode::Scale};
149 
150  IntervalSaveData m_saveData;
151 };
152 }
153 }
154 
155 SCORE_COMMAND_DECL_T(MoveBaseEvent<Scenario::BaseScenario>)
The Process class.
Definition: score-lib-process/Process/Process.hpp:61
void setParentDuration(ExpandMode mode, const TimeVal &t) noexcept
Definition: score-lib-process/Process/Process.cpp:123
The ClearInterval class.
Definition: ClearInterval.hpp:27
Definition: MoveBaseEvent.hpp:30
Definition: IntervalModel.hpp:50
The id_base_t class.
Definition: Identifier.hpp:57
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: dataStructures.hpp:40
Definition: TimeValue.hpp:21
Definition: DocumentContext.hpp:18
Definition: Unused.hpp:3