1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2006 Chris Cannam.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#ifndef SV_EVENT_COMMANDS_H
#define SV_EVENT_COMMANDS_H
#include "base/Event.h"
#include "base/Command.h"
#include "base/ById.h"
namespace sv {
/**
* Interface for classes that can be modified through these commands
*/
class EventEditable
{
public:
virtual ~EventEditable() { }
virtual void add(Event e) = 0;
virtual void remove(Event e) = 0;
};
class WithEditable
{
protected:
WithEditable(int editableId) : m_editableId(editableId) { }
virtual ~WithEditable() { }
std::shared_ptr<EventEditable> getEditable() {
auto editable = AnyById::getAs<EventEditable>(m_editableId);
if (!editable) {
SVCERR << "WARNING: Id passed to EventEditable command is not that of an EventEditable" << endl;
}
return editable;
}
private:
int m_editableId;
};
/**
* Command to add an event to an editable containing events, with
* undo. The id must be that of a type that can be retrieved from the
* AnyById store and dynamic_cast to EventEditable.
*/
class AddEventCommand : public Command,
public WithEditable
{
public:
AddEventCommand(int editableId, const Event &e, QString name) :
WithEditable(editableId), m_event(e), m_name(name) { }
QString getName() const override { return m_name; }
Event getEvent() const { return m_event; }
void execute() override {
auto editable = getEditable();
if (editable) editable->add(m_event);
}
void unexecute() override {
auto editable = getEditable();
if (editable) editable->remove(m_event);
}
private:
Event m_event;
QString m_name;
};
/**
* Command to remove an event from an editable containing events, with
* undo. The id must be that of a type that can be retrieved from the
* AnyById store and dynamic_cast to EventEditable.
*/
class RemoveEventCommand : public Command,
public WithEditable
{
public:
RemoveEventCommand(int editableId, const Event &e, QString name) :
WithEditable(editableId), m_event(e), m_name(name) { }
QString getName() const override { return m_name; }
Event getEvent() const { return m_event; }
void execute() override {
auto editable = getEditable();
if (editable) editable->remove(m_event);
}
void unexecute() override {
auto editable = getEditable();
if (editable) editable->add(m_event);
}
private:
Event m_event;
QString m_name;
};
/**
* Command to add or remove a series of events to or from an editable,
* with undo. Creates and immediately executes a sub-command for each
* add/remove requested. Consecutive add/remove pairs for the same
* point are collapsed. The id must be that of a type that can be
* retrieved from the AnyById store and dynamic_cast to EventEditable.
*/
class ChangeEventsCommand : public MacroCommand
{
public:
ChangeEventsCommand(int editableId, QString name) :
MacroCommand(name), m_editableId(editableId) { }
void add(Event e) {
addCommand(new AddEventCommand(m_editableId, e, getName()), true);
}
void remove(Event e) {
addCommand(new RemoveEventCommand(m_editableId, e, getName()), true);
}
/**
* Stack an arbitrary other command in the same sequence.
*/
void addCommand(Command *command) override { addCommand(command, true); }
/**
* If any points have been added or deleted, return this
* command (so the caller can add it to the command history).
* Otherwise delete the command and return NULL.
*/
ChangeEventsCommand *finish() {
if (!m_commands.empty()) {
return this;
} else {
delete this;
return nullptr;
}
}
protected:
virtual void addCommand(Command *command, bool executeFirst) {
if (executeFirst) command->execute();
if (m_commands.empty()) {
MacroCommand::addCommand(command);
return;
}
RemoveEventCommand *r =
dynamic_cast<RemoveEventCommand *>(command);
AddEventCommand *a =
dynamic_cast<AddEventCommand *>(*m_commands.rbegin());
if (r && a) {
if (a->getEvent() == r->getEvent()) {
deleteCommand(a);
return;
}
}
MacroCommand::addCommand(command);
}
int m_editableId;
};
} // end namespace sv
#endif
|