EventCommands.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_EVENT_COMMANDS_H
17 #define SV_EVENT_COMMANDS_H
18 
19 #include "base/Event.h"
20 #include "base/Command.h"
21 #include "base/ById.h"
22 
27 {
28 public:
29  virtual void add(Event e) = 0;
30  virtual void remove(Event e) = 0;
31 };
32 
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:
48 };
49 
55 class AddEventCommand : public Command,
56  public WithEditable
57 {
58 public:
59  AddEventCommand(int editableId, const Event &e, QString name) :
60  WithEditable(editableId), m_event(e), m_name(name) { }
61 
62  QString getName() const override { return m_name; }
63  Event getEvent() const { return m_event; }
64 
65  void execute() override {
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  }
73 
74 private:
76  QString m_name;
77 };
78 
84 class RemoveEventCommand : public Command,
85  public WithEditable
86 {
87 public:
88  RemoveEventCommand(int editableId, const Event &e, QString name) :
89  WithEditable(editableId), m_event(e), m_name(name) { }
90 
91  QString getName() const override { return m_name; }
92  Event getEvent() const { return m_event; }
93 
94  void execute() override {
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  }
102 
103 private:
105  QString m_name;
106 };
107 
116 {
117 public:
118  ChangeEventsCommand(int editableId, QString name) :
119  MacroCommand(name), m_editableId(editableId) { }
120 
121  void add(Event e) {
122  addCommand(new AddEventCommand(m_editableId, e, getName()), true);
123  }
124  void remove(Event e) {
125  addCommand(new RemoveEventCommand(m_editableId, e, getName()), true);
126  }
127 
131  void addCommand(Command *command) override { addCommand(command, true); }
132 
139  if (!m_commands.empty()) {
140  return this;
141  } else {
142  delete this;
143  return nullptr;
144  }
145  }
146 
147 protected:
148  virtual void addCommand(Command *command, bool executeFirst) {
149 
150  if (executeFirst) command->execute();
151 
152  if (m_commands.empty()) {
153  MacroCommand::addCommand(command);
154  return;
155  }
156 
157  RemoveEventCommand *r =
158  dynamic_cast<RemoveEventCommand *>(command);
159  AddEventCommand *a =
160  dynamic_cast<AddEventCommand *>(*m_commands.rbegin());
161  if (r && a) {
162  if (a->getEvent() == r->getEvent()) {
163  deleteCommand(a);
164  return;
165  }
166  }
167 
168  MacroCommand::addCommand(command);
169  }
170 
172 };
173 
174 #endif
void addCommand(Command *command) override
Stack an arbitrary other command in the same sequence.
virtual void add(Event e)=0
Command to add an event to an editable containing events, with undo.
Definition: EventCommands.h:55
QString getName() const override
Definition: EventCommands.h:62
ChangeEventsCommand(int editableId, QString name)
void add(Event e)
ChangeEventsCommand * finish()
If any points have been added or deleted, return this command (so the caller can add it to the comman...
Event getEvent() const
Definition: EventCommands.h:63
Event getEvent() const
Definition: EventCommands.h:92
WithEditable(int editableId)
Definition: EventCommands.h:36
void execute() override
Definition: EventCommands.h:65
virtual void addCommand(Command *command)
Definition: Command.cpp:32
void unexecute() override
Definition: EventCommands.h:98
AddEventCommand(int editableId, const Event &e, QString name)
Definition: EventCommands.h:59
void execute() override
Definition: EventCommands.h:94
void unexecute() override
Definition: EventCommands.h:69
An immutable(-ish) type used for point and event representation in sparse models, as well as for inte...
Definition: Event.h:55
QString getName() const override
Definition: EventCommands.h:91
#define SVCERR
Definition: Debug.h:109
std::shared_ptr< EventEditable > getEditable()
Definition: EventCommands.h:38
RemoveEventCommand(int editableId, const Event &e, QString name)
Definition: EventCommands.h:88
virtual void execute()=0
virtual void addCommand(Command *command, bool executeFirst)
Interface for classes that can be modified through these commands.
Definition: EventCommands.h:26
Command to remove an event from an editable containing events, with undo.
Definition: EventCommands.h:84
Command to add or remove a series of events to or from an editable, with undo.