Audacity  2.2.0
Command.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity - A Digital Audio Editor
4  Copyright 1999-2009 Audacity Team
5  File License: wxWidgets
6 
7  Dan Horgan
8 
9 ******************************************************************//*******************************************************************/
36 
37 #ifndef __COMMAND__
38 #define __COMMAND__
39 
40 #include <wx/app.h>
41 
42 #include "../Project.h"
43 
44 #include "CommandMisc.h"
45 #include "CommandSignature.h"
46 
47 class AudacityApp;
49 
51 {
52 public:
53  CommandExecutionContext(AudacityApp *WXUNUSED(app), AudacityProject *WXUNUSED(proj))
54  {
55  };
57  {
58  return (AudacityApp *) wxTheApp;
59  };
61  {
62  // TODO: Presumably, this would be different if running in a command context.
63  // So, if this command system is ever actually enabled, then this will need to
64  // be reviewed.
65  return GetActiveProject();
66  };
67 };
68 
69 // Abstract base class for command interface.
70 class Command /* not final */
71 {
72 public:
73  virtual void Progress(double completed) = 0;
74  virtual void Status(const wxString &message) = 0;
75  virtual void Error(const wxString &message) = 0;
76  virtual ~Command() { }
77  virtual wxString GetName() = 0;
78  virtual CommandSignature &GetSignature() = 0;
79  virtual bool SetParameter(const wxString &paramName, const wxVariant &paramValue);
80  virtual bool Apply(CommandExecutionContext context) = 0;
81 };
82 
83 using CommandHolder = std::shared_ptr<Command>;
84 
86 class DecoratedCommand /* not final */ : public Command
87 {
88 protected:
90 public:
91  void Progress(double completed) override;
92  void Status(const wxString &message) override;
93  void Error(const wxString &message) override;
94 
96  : mCommand(cmd)
97  {
98  wxASSERT(cmd != NULL);
99  }
100  virtual ~DecoratedCommand();
101  wxString GetName() override;
102  CommandSignature &GetSignature() override;
103  bool SetParameter(const wxString &paramName, const wxVariant &paramValue) override;
104 };
105 
106 // Decorator command that performs the given command and then outputs a status
107 // message according to the result
109 {
110 public:
112  : DecoratedCommand(cmd)
113  { }
114 
115  bool Apply(CommandExecutionContext context) override;
116 };
117 
118 class CommandImplementation /* not final */ : public Command
119 {
120 private:
123 
126  bool Valid(const wxString &paramName, const wxVariant &paramValue);
127 
128 protected:
129  std::unique_ptr<CommandOutputTarget> mOutput;
130 
131  // Convenience methods for allowing subclasses to access parameters
132  void TypeCheck(const wxString &typeName,
133  const wxString &paramName,
134  const wxVariant &param);
135  void CheckParam(const wxString &paramName);
136  bool GetBool(const wxString &paramName);
137  long GetLong(const wxString &paramName);
138  double GetDouble(const wxString &paramName);
139  wxString GetString(const wxString &paramName);
140 
141 public:
142  // Convenience methods for passing messages to the output target
143  void Progress(double completed);
144  void Status(const wxString &status) override;
145  void Error(const wxString &message) override;
146 
150  std::unique_ptr<CommandOutputTarget> &&output);
151 
152  virtual ~CommandImplementation();
153 
155  wxString GetName();
156 
159 
162  bool SetParameter(const wxString &paramName, const wxVariant &paramValue);
163 
164  // Subclasses should override the following:
165  // =========================================
166 
169  bool Apply(CommandExecutionContext context) override;
170 };
171 
172 #endif /* End of include guard: __COMMAND__ */
ParamValueMap mParams
Definition: Command.h:122
AudacityApp * GetApp() const
Definition: Command.h:56
virtual ~Command()
Definition: Command.h:76
void Status(const wxString &message) override
Definition: Command.cpp:43
std::map< wxString, wxVariant > ParamValueMap
Definition: CommandMisc.h:23
virtual ~CommandImplementation()
Definition: Command.cpp:103
virtual ~DecoratedCommand()
Definition: Command.cpp:53
std::shared_ptr< Command > CommandHolder
Definition: AppCommandEvent.h:26
wxString GetString(const wxString &paramName)
Definition: Command.cpp:155
wxString GetName() override
Definition: Command.cpp:57
CommandType & mType
Definition: Command.h:121
CommandSignature & GetSignature() override
Definition: Command.cpp:62
Abstract base class for command interface. It implements Command::SetParameter() and defers all other...
Definition: Command.h:70
virtual bool Apply(CommandExecutionContext context)=0
void TypeCheck(const wxString &typeName, const wxString &paramName, const wxVariant &param)
Definition: Command.cpp:107
CommandExecutionContext(AudacityApp *WXUNUSED(app), AudacityProject *WXUNUSED(proj))
Definition: Command.h:53
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:161
double GetDouble(const wxString &paramName)
Definition: Command.cpp:147
virtual void Progress(double completed)=0
std::unique_ptr< CommandOutputTarget > mOutput
Definition: Command.h:129
is derived from Command. It validates and applies the command. CommandImplementation::Apply() is over...
Definition: Command.h:118
Base class for containing data common to all commands of a given type. Also acts as a factory...
Definition: CommandType.h:29
long GetLong(const wxString &paramName)
Definition: Command.cpp:139
void CheckParam(const wxString &paramName)
Definition: Command.cpp:121
bool Valid(const wxString &paramName, const wxVariant &paramValue)
wxString GetName()
An instance method for getting the command name (for consistency)
Definition: Command.cpp:180
void Progress(double completed)
Definition: Command.cpp:164
bool Apply(CommandExecutionContext context) override
Definition: Command.cpp:73
void Progress(double completed) override
Definition: Command.cpp:38
DecoratedCommand(const CommandHolder &cmd)
Definition: Command.h:95
void Status(const wxString &status) override
Definition: Command.cpp:169
AudacityApp is the 'main' class for Audacity.
Definition: AudacityApp.h:59
ApplyAndSendResponse(const CommandHolder &cmd)
Definition: Command.h:111
bool SetParameter(const wxString &paramName, const wxVariant &paramValue) override
Definition: Command.cpp:67
void Error(const wxString &message) override
Definition: Command.cpp:48
Definition: CommandTargets.h:186
CommandSignature & GetSignature()
Get the signature of the command.
Definition: Command.cpp:186
virtual void Status(const wxString &message)=0
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:303
Command which wraps another command.
Definition: Command.h:86
Represents a context to which a command may be applied.
Definition: Command.h:50
virtual CommandSignature & GetSignature()=0
Decorator command that performs the given command and then outputs a status message according to the ...
Definition: Command.h:108
virtual wxString GetName()=0
virtual bool SetParameter(const wxString &paramName, const wxVariant &paramValue)
Definition: Command.cpp:31
CommandHolder mCommand
Definition: Command.h:89
CommandImplementation(CommandType &type, std::unique_ptr< CommandOutputTarget > &&output)
Definition: Command.cpp:94
bool SetParameter(const wxString &paramName, const wxVariant &paramValue)
Definition: Command.cpp:191
bool Apply(CommandExecutionContext context) override
Definition: Command.cpp:218
AudacityProject * GetProject() const
Definition: Command.h:60
bool GetBool(const wxString &paramName)
Definition: Command.cpp:131
Class that maps parameter names to default values and validators.
Definition: CommandSignature.h:28
Declaration of CommandSignature class.
void Error(const wxString &message) override
Definition: Command.cpp:174
virtual void Error(const wxString &message)=0