MoveEvent.hpp
1 #pragma once
2 #include <Process/ExpandMode.hpp>
3 #include <Process/TimeValue.hpp>
4 
5 #include <Scenario/Application/ScenarioValidity.hpp>
6 #include <Scenario/Commands/Scenario/Displacement/SerializableMoveEvent.hpp>
7 #include <Scenario/Commands/ScenarioCommandFactory.hpp>
8 #include <Scenario/Process/Algorithms/StandardDisplacementPolicy.hpp>
9 #include <Scenario/Process/ScenarioModel.hpp>
10 #include <Scenario/Tools/dataStructures.hpp>
11 #include <Scenario/Tools/elementFindingHelper.hpp>
12 
13 #include <score/model/Identifier.hpp>
14 #include <score/model/path/Path.hpp>
15 #include <score/model/path/PathSerialization.hpp>
16 
17 struct ElementsProperties;
18 
19 #include <tests/helpers/ForwardDeclaration.hpp>
20 
21 namespace Scenario
22 {
23 class EventModel;
24 class TimeSyncModel;
25 class IntervalModel;
26 class ProcessModel;
27 namespace Command
28 {
32 template <class DisplacementPolicy>
33 class MoveEvent final : public SerializableMoveEvent
34 {
35  // No SCORE_COMMAND here since it's a template.
36 
37 public:
38  const CommandGroupKey& parentKey() const noexcept override
39  {
40  return CommandFactoryName();
41  }
42  const CommandKey& key() const noexcept override
43  {
44  static const QByteArray name
45  = QString{"MoveEvent_%1"}.arg(DisplacementPolicy::name()).toLatin1();
46  static const CommandKey kagi{name.constData()};
47  return kagi;
48  }
49  QString description() const override
50  {
51  return QObject::tr("Move an event with %1").arg(DisplacementPolicy::name());
52  }
53 
54  MoveEvent()
56  {
57  }
68  const Scenario::ProcessModel& scenario, const Id<EventModel>& eventId,
69  const TimeVal& newDate, ExpandMode mode, LockMode lock)
71  , m_path{scenario}
72  , m_mode{mode}
73  , m_lock{lock}
74  {
75  auto& s = const_cast<Scenario::ProcessModel&>(scenario);
76  DisplacementPolicy::init(s, {scenario.event(eventId).timeSync()});
77  // we need to compute the new time delta and store this initial event id
78  // for recalculate the delta on updates
79  // NOTE: in the future in would be better to give directly the delta value
80  // to this method ?,
81  // in that way we wouldn't need to keep the initial event and recalculate
82  // the delta
83  m_eventId = eventId;
84  m_initialDate = getDate(scenario, eventId);
85 
86  update(s, eventId, newDate, 0, m_mode, lock);
87  }
88 
89  void update(
90  Scenario::ProcessModel& scenario, const Id<EventModel>& eventId,
91  const TimeVal& newDate, double, ExpandMode, LockMode) override
92  {
93  // we need to compute the new time delta
94  // NOTE: in the future in would be better to give directly the delta value
95  // to this method
96  TimeVal deltaDate = newDate - m_initialDate;
97 
98  // NOTICE: multiple event displacement functionality already available,
99  // this is "retro" compatibility
100  QVector<Id<TimeSyncModel>> draggedElements;
101  draggedElements.push_back(
102  scenario.events.at(eventId).timeSync()); // retrieve corresponding
103  // timesync and store it in
104  // array
105 
106  // the displacement is computed here and we don't need to know how.
107  DisplacementPolicy::computeDisplacement(
108  scenario, draggedElements, deltaDate, m_savedElementsProperties);
109  }
110 
111  void undo(const score::DocumentContext& ctx) const override
112  {
113  auto& scenario = m_path.find(ctx);
114 
115  // update positions using old stored dates
116  DisplacementPolicy::revertPositions(
117  ctx, scenario,
118  [&](Process::ProcessModel& p, const TimeVal& t) {
119  p.setParentDuration(m_mode, t);
120  },
121  m_savedElementsProperties);
122 
123  // Dataflow::restoreCables(m_savedElementsProperties.cables, ctx);
124  }
125 
126  void redo(const score::DocumentContext& ctx) const override
127  {
128  auto& scenario = m_path.find(ctx);
129 
130  // update positions using new stored dates
131  DisplacementPolicy::updatePositions(
132  scenario,
133  [&](Process::ProcessModel& p, const TimeVal& t) {
134  p.setParentDuration(m_mode, t);
135  },
136  m_savedElementsProperties);
137  }
138 
139  const Path<Scenario::ProcessModel>& path() const override { return m_path; }
140 
141 protected:
142  void serializeImpl(DataStreamInput& s) const override
143  {
144  s << m_savedElementsProperties << m_path << m_eventId << m_initialDate
145  << (int)m_mode;
146  }
147 
148  void deserializeImpl(DataStreamOutput& s) override
149  {
150  int mode;
151  s >> m_savedElementsProperties >> m_path >> m_eventId >> m_initialDate >> mode;
152 
153  m_mode = static_cast<ExpandMode>(mode);
154  }
155 
156 private:
157  ElementsProperties m_savedElementsProperties;
159 
160  ExpandMode m_mode{ExpandMode::Scale};
161  LockMode m_lock{LockMode::Free};
162 
163  Id<EventModel> m_eventId;
168  TimeVal m_initialDate; // used to compute the deltaTime and respect undo behavior
169 };
170 }
171 }
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
Definition: MoveEvent.hpp:34
MoveEvent(const Scenario::ProcessModel &scenario, const Id< EventModel > &eventId, const TimeVal &newDate, ExpandMode mode, LockMode lock)
MoveEvent.
Definition: MoveEvent.hpp:67
Definition: SerializableMoveEvent.hpp:20
The core hierarchical and temporal process of score.
Definition: ScenarioModel.hpp:37
The id_base_t class.
Definition: Identifier.hpp:57
Main plug-in of score.
Definition: score-plugin-dataflow/Dataflow/PortItem.hpp:14
Definition: DataStreamHelpers.hpp:99
Definition: DataStreamHelpers.hpp:103
Definition: TimeValue.hpp:21
Definition: DocumentContext.hpp:18