ChangeElementLabel.hpp
1 #pragma once
2 #include <Scenario/Commands/ScenarioCommandFactory.hpp>
3 
4 #include <score/command/Command.hpp>
5 #include <score/model/path/Path.hpp>
6 #include <score/model/path/PathSerialization.hpp>
7 
8 namespace Scenario
9 {
10 namespace Command
11 {
12 template <class T>
13 class ChangeElementLabel final : public score::Command
14 {
15  // No SCORE_COMMAND here since it's a template.
16 public:
17  const CommandGroupKey& parentKey() const noexcept override
18  {
19  return CommandFactoryName();
20  }
21  static const CommandKey& static_key() noexcept
22  {
23  QString name = QString("ChangeElementLabel_") + Metadata<ObjectKey_k, T>::get();
24  static const CommandKey kagi{std::move(name)};
25  return kagi;
26  }
27  const CommandKey& key() const noexcept override { return static_key(); }
28  QString description() const override
29  {
30  return QObject::tr("Change %1 label").arg(Metadata<Description_k, T>::get());
31  }
32 
33  ChangeElementLabel() = default;
34 
35  ChangeElementLabel(const T& obj, QString newLabel)
36  : m_path{obj}
37  , m_newLabel{std::move(newLabel)}
38  {
39  m_oldLabel = obj.metadata().getLabel();
40  }
41 
42  void undo(const score::DocumentContext& ctx) const override
43  {
44  auto& obj = m_path.find(ctx);
45  obj.metadata().setLabel(m_oldLabel);
46  }
47 
48  void redo(const score::DocumentContext& ctx) const override
49  {
50  auto& obj = m_path.find(ctx);
51  obj.metadata().setLabel(m_newLabel);
52  }
53 
54 protected:
55  void serializeImpl(DataStreamInput& s) const override
56  {
57  s << m_path << m_oldLabel << m_newLabel;
58  }
59 
60  void deserializeImpl(DataStreamOutput& s) override
61  {
62  s >> m_path >> m_oldLabel >> m_newLabel;
63  }
64 
65 private:
66  Path<T> m_path;
67  QString m_newLabel;
68  QString m_oldLabel;
69 };
70 }
71 }
72 
73 SCORE_COMMAND_DECL_T(ChangeElementLabel<IntervalModel>)
74 SCORE_COMMAND_DECL_T(ChangeElementLabel<EventModel>)
75 SCORE_COMMAND_DECL_T(ChangeElementLabel<TimeSyncModel>)
76 SCORE_COMMAND_DECL_T(ChangeElementLabel<StateModel>)
77 SCORE_COMMAND_DECL_T(ChangeElementLabel<Process::ProcessModel>)
Definition: ChangeElementLabel.hpp:14
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