ChangeElementName.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 ChangeElementName 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("ChangeElementName_") + 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 name").arg(Metadata<Description_k, T>::get());
31  }
32 
33  ChangeElementName() = default;
34 
35  ChangeElementName(const T& obj, QString newName)
36  : m_path{obj}
37  , m_newName{std::move(newName)}
38  {
39  m_oldName = obj.metadata().getName();
40  }
41 
42  void undo(const score::DocumentContext& ctx) const override
43  {
44  auto& obj = m_path.find(ctx);
45  obj.metadata().setName(m_oldName);
46  }
47 
48  void redo(const score::DocumentContext& ctx) const override
49  {
50  auto& obj = m_path.find(ctx);
51  obj.metadata().setName(m_newName);
52  }
53 
54 protected:
55  void serializeImpl(DataStreamInput& s) const override
56  {
57  s << m_path << m_oldName << m_newName;
58  }
59 
60  void deserializeImpl(DataStreamOutput& s) override
61  {
62  s >> m_path >> m_oldName >> m_newName;
63  }
64 
65 private:
66  Path<T> m_path;
67  QString m_newName;
68  QString m_oldName;
69 };
70 }
71 }
72 
73 SCORE_COMMAND_DECL_T(ChangeElementName<IntervalModel>)
74 SCORE_COMMAND_DECL_T(ChangeElementName<EventModel>)
75 SCORE_COMMAND_DECL_T(ChangeElementName<TimeSyncModel>)
76 SCORE_COMMAND_DECL_T(ChangeElementName<StateModel>)
77 SCORE_COMMAND_DECL_T(ChangeElementName<Process::ProcessModel>)
Definition: ChangeElementName.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