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 ******************************************************************//*******************************************************************/
26 
27 #ifndef __COMMAND__
28 #define __COMMAND__
29 
30 #include <wx/app.h>
31 
32 #include "../Project.h"
33 
34 #include "CommandMisc.h"
35 #include "CommandSignature.h"
36 
37 class AudacityApp;
39 
41 {
42 public:
43  CommandExecutionContext(AudacityApp *WXUNUSED(app), AudacityProject *WXUNUSED(proj))
44  {
45  };
47  {
48  return (AudacityApp *) wxTheApp;
49  };
51  {
52  // TODO: Presumably, this would be different if running in a command context.
53  // So, if this command system is ever actually enabled, then this will need to
54  // be reviewed.
55  return GetActiveProject();
56  };
57 };
58 
59 // Interface
60 class Command /* not final */
61 {
62 public:
63  virtual void Progress(double completed) = 0;
64  virtual void Status(const wxString &message) = 0;
65  virtual void Error(const wxString &message) = 0;
66  virtual ~Command() { }
67  virtual wxString GetName() = 0;
68  virtual CommandSignature &GetSignature() = 0;
69  virtual bool SetParameter(const wxString &paramName, const wxVariant &paramValue);
70  virtual bool Apply(CommandExecutionContext context) = 0;
71 };
72 
73 using CommandHolder = std::shared_ptr<Command>;
74 
75 // Command which wraps another command
76 class DecoratedCommand /* not final */ : public Command
77 {
78 protected:
80 public:
81  void Progress(double completed) override;
82  void Status(const wxString &message) override;
83  void Error(const wxString &message) override;
84 
86  : mCommand(cmd)
87  {
88  wxASSERT(cmd != NULL);
89  }
90  virtual ~DecoratedCommand();
91  wxString GetName() override;
92  CommandSignature &GetSignature() override;
93  bool SetParameter(const wxString &paramName, const wxVariant &paramValue) override;
94 };
95 
96 // Decorator command that performs the given command and then outputs a status
97 // message according to the result
99 {
100 public:
102  : DecoratedCommand(cmd)
103  { }
104 
105  bool Apply(CommandExecutionContext context) override;
106 };
107 
108 class CommandImplementation /* not final */ : public Command
109 {
110 private:
113 
116  bool Valid(const wxString &paramName, const wxVariant &paramValue);
117 
118 protected:
119  std::unique_ptr<CommandOutputTarget> mOutput;
120 
121  // Convenience methods for allowing subclasses to access parameters
122  void TypeCheck(const wxString &typeName,
123  const wxString &paramName,
124  const wxVariant &param);
125  void CheckParam(const wxString &paramName);
126  bool GetBool(const wxString &paramName);
127  long GetLong(const wxString &paramName);
128  double GetDouble(const wxString &paramName);
129  wxString GetString(const wxString &paramName);
130 
131 public:
132  // Convenience methods for passing messages to the output target
133  void Progress(double completed);
134  void Status(const wxString &status) override;
135  void Error(const wxString &message) override;
136 
140  std::unique_ptr<CommandOutputTarget> &&output);
141 
142  virtual ~CommandImplementation();
143 
145  wxString GetName();
146 
149 
152  bool SetParameter(const wxString &paramName, const wxVariant &paramValue);
153 
154  // Subclasses should override the following:
155  // =========================================
156 
159  bool Apply(CommandExecutionContext context) override;
160 };
161 
162 #endif /* End of include guard: __COMMAND__ */
ParamValueMap mParams
Definition: Command.h:112
AudacityApp * GetApp() const
Definition: Command.h:46
virtual ~Command()
Definition: Command.h:66
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:111
CommandSignature & GetSignature() override
Definition: Command.cpp:62
Base class which encapsulates a process.
Definition: Command.h:60
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:43
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:119
Definition: Command.h:108
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:85
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:101
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
Definition: Command.h:76
Represents a context to which a command may be applied.
Definition: Command.h:40
virtual CommandSignature & GetSignature()=0
Definition: Command.h:98
virtual wxString GetName()=0
virtual bool SetParameter(const wxString &paramName, const wxVariant &paramValue)
Definition: Command.cpp:31
CommandHolder mCommand
Definition: Command.h:79
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:50
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