comparison data/model/EventCommands.h @ 1766:85b9b466a59f

Merge from branch by-id
author Chris Cannam
date Wed, 17 Jul 2019 14:24:51 +0100
parents 52705a328b34
children
comparison
equal deleted inserted replaced
1730:649ac57c5a2d 1766:85b9b466a59f
16 #ifndef SV_EVENT_COMMANDS_H 16 #ifndef SV_EVENT_COMMANDS_H
17 #define SV_EVENT_COMMANDS_H 17 #define SV_EVENT_COMMANDS_H
18 18
19 #include "base/Event.h" 19 #include "base/Event.h"
20 #include "base/Command.h" 20 #include "base/Command.h"
21 #include "base/ById.h"
21 22
22 /** 23 /**
23 * Interface for classes that can be modified through these commands 24 * Interface for classes that can be modified through these commands
24 */ 25 */
25 class EventEditable 26 class EventEditable
27 public: 28 public:
28 virtual void add(Event e) = 0; 29 virtual void add(Event e) = 0;
29 virtual void remove(Event e) = 0; 30 virtual void remove(Event e) = 0;
30 }; 31 };
31 32
33 class WithEditable
34 {
35 protected:
36 WithEditable(int editableId) : m_editableId(editableId) { }
37
38 std::shared_ptr<EventEditable> getEditable() {
39 auto editable = AnyById::getAs<EventEditable>(m_editableId);
40 if (!editable) {
41 SVCERR << "WARNING: Id passed to EventEditable command is not that of an EventEditable" << endl;
42 }
43 return editable;
44 }
45
46 private:
47 int m_editableId;
48 };
49
32 /** 50 /**
33 * Command to add an event to an editable containing events, with undo. 51 * Command to add an event to an editable containing events, with
52 * undo. The id must be that of a type that can be retrieved from the
53 * AnyById store and dynamic_cast to EventEditable.
34 */ 54 */
35 class AddEventCommand : public Command 55 class AddEventCommand : public Command,
56 public WithEditable
36 { 57 {
37 public: 58 public:
38 AddEventCommand(EventEditable *editable, const Event &e, QString name) : 59 AddEventCommand(int editableId, const Event &e, QString name) :
39 m_editable(editable), m_event(e), m_name(name) { } 60 WithEditable(editableId), m_event(e), m_name(name) { }
40 61
41 QString getName() const override { return m_name; } 62 QString getName() const override { return m_name; }
42 Event getEvent() const { return m_event; } 63 Event getEvent() const { return m_event; }
43 64
44 void execute() override { m_editable->add(m_event); } 65 void execute() override {
45 void unexecute() override { m_editable->remove(m_event); } 66 auto editable = getEditable();
67 if (editable) editable->add(m_event);
68 }
69 void unexecute() override {
70 auto editable = getEditable();
71 if (editable) editable->remove(m_event);
72 }
46 73
47 private: 74 private:
48 EventEditable *m_editable;
49 Event m_event; 75 Event m_event;
50 QString m_name; 76 QString m_name;
51 }; 77 };
52 78
53 /** 79 /**
54 * Command to remove an event from an editable containing events, with 80 * Command to remove an event from an editable containing events, with
55 * undo. 81 * undo. The id must be that of a type that can be retrieved from the
82 * AnyById store and dynamic_cast to EventEditable.
56 */ 83 */
57 class RemoveEventCommand : public Command 84 class RemoveEventCommand : public Command,
85 public WithEditable
58 { 86 {
59 public: 87 public:
60 RemoveEventCommand(EventEditable *editable, const Event &e, QString name) : 88 RemoveEventCommand(int editableId, const Event &e, QString name) :
61 m_editable(editable), m_event(e), m_name(name) { } 89 WithEditable(editableId), m_event(e), m_name(name) { }
62 90
63 QString getName() const override { return m_name; } 91 QString getName() const override { return m_name; }
64 Event getEvent() const { return m_event; } 92 Event getEvent() const { return m_event; }
65 93
66 void execute() override { m_editable->remove(m_event); } 94 void execute() override {
67 void unexecute() override { m_editable->add(m_event); } 95 auto editable = getEditable();
96 if (editable) editable->remove(m_event);
97 }
98 void unexecute() override {
99 auto editable = getEditable();
100 if (editable) editable->add(m_event);
101 }
68 102
69 private: 103 private:
70 EventEditable *m_editable;
71 Event m_event; 104 Event m_event;
72 QString m_name; 105 QString m_name;
73 }; 106 };
74 107
75 /** 108 /**
76 * Command to add or remove a series of events to or from an editable, 109 * Command to add or remove a series of events to or from an editable,
77 * with undo. Creates and immediately executes a sub-command for each 110 * with undo. Creates and immediately executes a sub-command for each
78 * add/remove requested. Consecutive add/remove pairs for the same 111 * add/remove requested. Consecutive add/remove pairs for the same
79 * point are collapsed. 112 * point are collapsed. The id must be that of a type that can be
113 * retrieved from the AnyById store and dynamic_cast to EventEditable.
80 */ 114 */
81 class ChangeEventsCommand : public MacroCommand 115 class ChangeEventsCommand : public MacroCommand
82 { 116 {
83 public: 117 public:
84 ChangeEventsCommand(EventEditable *editable, QString name) : 118 ChangeEventsCommand(int editableId, QString name) :
85 MacroCommand(name), m_editable(editable) { } 119 MacroCommand(name), m_editableId(editableId) { }
86 120
87 void add(Event e) { 121 void add(Event e) {
88 addCommand(new AddEventCommand(m_editable, e, getName()), true); 122 addCommand(new AddEventCommand(m_editableId, e, getName()), true);
89 } 123 }
90 void remove(Event e) { 124 void remove(Event e) {
91 addCommand(new RemoveEventCommand(m_editable, e, getName()), true); 125 addCommand(new RemoveEventCommand(m_editableId, e, getName()), true);
92 } 126 }
93 127
94 /** 128 /**
95 * Stack an arbitrary other command in the same sequence. 129 * Stack an arbitrary other command in the same sequence.
96 */ 130 */
132 } 166 }
133 167
134 MacroCommand::addCommand(command); 168 MacroCommand::addCommand(command);
135 } 169 }
136 170
137 EventEditable *m_editable; 171 int m_editableId;
138 }; 172 };
139 173
140 #endif 174 #endif