annotate base/Command.h @ 1886:f803d3c33f76 tip

Switch off copious debug in soft synth driving
author Chris Cannam
date Fri, 14 Aug 2020 10:44:44 +0100
parents 498ed1e86f92
children
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@16 2
Chris@16 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@52 7 This file copyright 2006 Chris Cannam.
Chris@16 8
Chris@52 9 This program is free software; you can redistribute it and/or
Chris@52 10 modify it under the terms of the GNU General Public License as
Chris@52 11 published by the Free Software Foundation; either version 2 of the
Chris@52 12 License, or (at your option) any later version. See the file
Chris@52 13 COPYING included with this distribution for more information.
Chris@16 14 */
Chris@16 15
Chris@1580 16 #ifndef SV_COMMAND_H
Chris@1580 17 #define SV_COMMAND_H
Chris@16 18
Chris@423 19 #include <QObject>
Chris@17 20 #include <QString>
Chris@17 21 #include <vector>
Chris@1831 22 #include <functional>
Chris@17 23
Chris@686 24 #include "Debug.h"
Chris@686 25
Chris@16 26 class Command
Chris@16 27 {
Chris@16 28 public:
Chris@17 29 virtual ~Command() { }
Chris@17 30
Chris@16 31 virtual void execute() = 0;
Chris@16 32 virtual void unexecute() = 0;
Chris@17 33 virtual QString getName() const = 0;
Chris@17 34 };
Chris@17 35
Chris@1831 36 /**
Chris@1831 37 * GenericCommand is a Command that can be constructed directly using
Chris@1831 38 * lambdas, without having to create a subclass. Best for commands
Chris@1831 39 * invoked only in a single place, and where little state is involved.
Chris@1831 40 */
Chris@1831 41 class GenericCommand : public Command
Chris@1831 42 {
Chris@1831 43 public:
Chris@1831 44 GenericCommand(QString name,
Chris@1831 45 std::function<void()> execute,
Chris@1831 46 std::function<void()> unexecute) :
Chris@1831 47 m_name(name),
Chris@1831 48 m_execute(execute),
Chris@1831 49 m_unexecute(unexecute),
Chris@1831 50 m_onDelete([] {}) {
Chris@1831 51 }
Chris@1831 52
Chris@1831 53 GenericCommand(QString name,
Chris@1831 54 std::function<void()> execute,
Chris@1831 55 std::function<void()> unexecute,
Chris@1831 56 std::function<void()> onDelete) :
Chris@1831 57 m_name(name),
Chris@1831 58 m_execute(execute),
Chris@1831 59 m_unexecute(unexecute),
Chris@1831 60 m_onDelete(onDelete) {
Chris@1831 61 }
Chris@1831 62
Chris@1831 63 virtual ~GenericCommand() {
Chris@1831 64 m_onDelete();
Chris@1831 65 }
Chris@1831 66
Chris@1831 67 QString getName() const override {
Chris@1831 68 return m_name;
Chris@1831 69 }
Chris@1831 70
Chris@1831 71 void execute() override {
Chris@1831 72 m_execute();
Chris@1831 73 }
Chris@1831 74
Chris@1831 75 void unexecute() override {
Chris@1831 76 m_unexecute();
Chris@1831 77 }
Chris@1831 78
Chris@1831 79 private:
Chris@1831 80 QString m_name;
Chris@1831 81 std::function<void()> m_execute;
Chris@1831 82 std::function<void()> m_unexecute;
Chris@1831 83 std::function<void()> m_onDelete;
Chris@1831 84 };
Chris@1831 85
Chris@17 86 class MacroCommand : public Command
Chris@17 87 {
Chris@17 88 public:
Chris@17 89 MacroCommand(QString name);
Chris@17 90 virtual ~MacroCommand();
Chris@17 91
Chris@17 92 virtual void addCommand(Command *command);
Chris@17 93 virtual void deleteCommand(Command *command);
Chris@47 94 virtual bool haveCommands() const;
Chris@17 95
Chris@1580 96 void execute() override;
Chris@1580 97 void unexecute() override;
Chris@17 98
Chris@1580 99 QString getName() const override;
Chris@47 100 virtual void setName(QString name);
Chris@17 101
Chris@17 102 protected:
Chris@17 103 QString m_name;
Chris@17 104 std::vector<Command *> m_commands;
Chris@16 105 };
Chris@16 106
Chris@423 107 /**
Chris@423 108 * BundleCommand is a MacroCommand whose name includes a note of how
Chris@423 109 * many commands it contains. It is a QObject with Q_OBJECT macro so
Chris@423 110 * that it can do plural-sensitive translations.
Chris@423 111 */
Chris@423 112 class BundleCommand : public QObject, public MacroCommand
Chris@423 113 {
Chris@423 114 Q_OBJECT
Chris@423 115
Chris@423 116 public:
Chris@423 117 BundleCommand(QString name);
Chris@423 118 virtual ~BundleCommand();
Chris@423 119
Chris@1580 120 QString getName() const override;
Chris@423 121 };
Chris@423 122
Chris@16 123 #endif
Chris@16 124