Audacity  2.2.2
Command.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity - A Digital Audio Editor
4  Copyright 1999-2009 Audacity Team
5  License: wxwidgets
6 
7  Dan Horgan
8 
9 ******************************************************************//*******************************************************************/
77 
78 #include "../Audacity.h"
79 #include "Command.h"
80 #include <map>
81 #include <wx/string.h>
82 #include <wx/variant.h>
83 #include <wx/arrstr.h>
84 
85 #include "../AudacityException.h"
86 #include "Validators.h"
87 #include "CommandType.h"
88 #include "CommandMisc.h"
89 #include "CommandBuilder.h"
90 #include "CommandTargets.h"
91 #include "CommandDirectory.h"
92 
93 #include "CommandContext.h"
94 #include "../Project.h"
95 
96 
97 
98 bool OldStyleCommand::SetParameter(const wxString & WXUNUSED(paramName),
99  const wxVariant & WXUNUSED(paramValue))
100 {
101  wxASSERT_MSG(false, wxT("Tried to set parameter for command which doesn't support parameters!"));
102  return false;
103 }
104 
106 {
107 }
108 
110 {
111  return mCommand->GetSymbol();
112 }
113 
115 {
116  return mCommand->GetSignature();
117 }
118 
119 bool DecoratedCommand::SetParameter(const wxString &paramName,
120  const wxVariant &paramValue)
121 {
122  return mCommand->SetParameter(paramName, paramValue);
123 }
124 
125 ApplyAndSendResponse::ApplyAndSendResponse(const OldStyleCommandPointer &cmd, std::unique_ptr<CommandOutputTargets> &target)
126  : DecoratedCommand(cmd),
127  mCtx( std::make_unique<CommandContext>( *GetActiveProject(), std::move(target) ) )
128 {
129 }
130 
131 
132 bool ApplyAndSendResponse::Apply(const CommandContext &WXUNUSED(context))
133 {
134  wxLogMessage( "Context was passed in, but was ignored. ApplyAndSendResponse has its own one");
135  return Apply();
136 }
137 
138 
140 {
141  // ApplyAndSendResponse IS a command.
142  // It also HOLDS a command.
143 
144  // Mostly its functions forward to the recipient.
145  // However it uses its OWN context, not the one of
146  // the command it holds.
147  auto result = GuardedCall<bool>(
148  [&] {
149  bool bResult = mCommand->Apply(*( mCtx.get()));
150  return bResult; }
151  );
152  wxString response = wxT( "\n" );
153 
154  // PRL: it's all right to send untranslated strings to this channel
155  // I don't see _("") used with literal strings.
156  response += GetSymbol().Internal();
157 
158  // These three strings are deliberately not localised.
159  // They are used in script responses and always happen in English.
160  response += wxT(" finished: ");
161  if (result)
162  {
163  response += wxT("OK");
164  }
165  else
166  {
167  response += wxT("Failed!");
168  }
169  mCtx->Status(response);
170  return result;
171 }
172 
174 : mType(type),
175  mParams(type.GetSignature().GetDefaults()),
176  mSetParams()
177 {
178 }
179 
181 {
182 }
183 
184 void CommandImplementation::TypeCheck(const wxString &typeName,
185  const wxString &paramName,
186  const wxVariant &param)
187 {
188  // this macro is empty if wxWidgets is not compiled in debug mode
189  wxASSERT_MSG(param.IsType(typeName),
190  GetSymbol().Internal()
191  + wxT("command tried to get '")
192  + paramName
193  + wxT("' parameter as a ")
194  + typeName
195  + wxT(", but that wasn't enforced by the command signature."));
196 }
197 
198 void CommandImplementation::CheckParam(const wxString &paramName)
199 {
200  // this macro is empty if wxWidgets is not compiled in debug mode
201  wxASSERT_MSG(mParams.find(paramName) != mParams.end(),
202  GetSymbol().Internal()
203  + wxT("command tried to get '")
204  + paramName
205  + wxT("' parameter, but that parameter doesn't exist in the command signature!"));
206 }
207 
208 bool CommandImplementation::HasParam( const wxString &paramName)
209 {
210  // Test for not even in map...
211  if( mParams.count(paramName) < 1)
212  return false;
213  return mSetParams[paramName];
214 }
215 
216 bool CommandImplementation::GetBool(const wxString &paramName)
217 {
218  CheckParam(paramName);
219  const wxVariant &v = mParams[paramName];
220  TypeCheck(wxT("bool"), paramName, v);
221  return v.GetBool();
222 }
223 
224 long CommandImplementation::GetLong(const wxString &paramName)
225 {
226  CheckParam(paramName);
227  const wxVariant &v = mParams[paramName];
228  TypeCheck(wxT("double"), paramName, v);
229  return (long)v.GetDouble();
230 }
231 
232 double CommandImplementation::GetDouble(const wxString &paramName)
233 {
234  CheckParam(paramName);
235  const wxVariant &v = mParams[paramName];
236  TypeCheck(wxT("double"), paramName, v);
237  return v.GetDouble();
238 }
239 
240 wxString CommandImplementation::GetString(const wxString &paramName)
241 {
242  CheckParam(paramName);
243  const wxVariant &v = mParams[paramName];
244  TypeCheck(wxT("string"), paramName, v);
245  return v.GetString();
246 }
247 
250 {
251  return mType.GetSymbol();
252 }
253 
256 {
257  return mType.GetSignature();
258 }
259 
260 bool CommandImplementation::SetParameter(const wxString &paramName, const wxVariant &paramValue)
261 {
262  wxASSERT(!paramValue.IsType(wxT("null")));
263  CommandContext context( * GetActiveProject());
264  ParamValueMap::iterator iter = mParams.find(paramName);
265  if (iter == mParams.end())
266  {
267  // Translated format, but untranslated command name substituted into it?
268  // Perhaps these formats that don't need translating.
269  context.Error( wxString::Format(
270  _("%s is not a parameter accepted by %s"),
271  paramName, GetSymbol().Internal() ) );
272  return false;
273  }
274 
275  Validator &validator = mType.GetSignature().GetValidator(iter->first);
276  if (!validator.Validate(paramValue))
277  {
278  context.Error( wxString::Format(
279  _("Invalid value for parameter '%s': should be %s"),
280  paramName, validator.GetDescription() ) );
281  return false;
282  }
283  mParams[paramName] = validator.GetConverted();
284  mSetParams[ paramName ] = true;
285 
286  // (debug)
287  // context.Status(wxT("Set parameter ") + paramName + wxT(" to type ") + mParams[paramName].GetType() + wxT(", value ") + mParams[paramName].MakeString());
288 
289  return true;
290 }
291 
292 bool CommandImplementation::Apply(const CommandContext & WXUNUSED(context))
293 {
294  return true;
295 }
296 
297 
ParamValueMap mParams
Definition: Command.h:81
const wxVariant & GetConverted()
Definition: Validators.h:60
bool SetParameter(const wxString &paramName, const wxVariant &paramValue) override
Definition: Command.cpp:260
virtual ~CommandImplementation()
Definition: Command.cpp:180
virtual ~DecoratedCommand()
Definition: Command.cpp:105
IdentInterfaceSymbol GetSymbol() override
An instance method for getting the command name (for consistency)
Definition: Command.cpp:249
ApplyAndSendResponse(const OldStyleCommandPointer &cmd, std::unique_ptr< CommandOutputTargets > &target)
Definition: Command.cpp:125
Contains declarations for CommandType class.
Base class for containing data common to all commands of a given type. Also acts as a factory...
Definition: CommandType.h:44
wxString GetString(const wxString &paramName)
Definition: Command.cpp:240
Contains declaration of Command base class.
bool Apply() override
Definition: Command.cpp:139
CommandContext provides addiitonal information to an 'Apply()' command. It provides the project...
CommandSignature & GetSignature() override
Definition: Command.cpp:114
Contains declarations for CommandDirectory class.
bool Apply() override
Definition: Command.h:122
void TypeCheck(const wxString &typeName, const wxString &paramName, const wxVariant &param)
Definition: Command.cpp:184
Contains declarations and definitions for Validator, OptionValidator, BoolValidator, DoubleValidator, RangeValidator, IntValidator and AndValidator classes.
ParamBoolMap mSetParams
Definition: Command.h:82
virtual wxString GetDescription() const
Definition: Validators.h:74
double GetDouble(const wxString &paramName)
Definition: Command.cpp:232
bool HasParam(const wxString &paramName)
Definition: Command.cpp:208
long GetLong(const wxString &paramName)
Definition: Command.cpp:224
CommandSignature & GetSignature()
Definition: CommandType.cpp:41
void CheckParam(const wxString &paramName)
Definition: Command.cpp:198
IdentInterfaceSymbol GetSymbol() override
Definition: Command.cpp:109
OldStyleCommandPointer mCommand
Definition: Command.h:52
IdentInterfaceSymbol pairs a persistent string identifier used internally with an optional...
bool SetParameter(const wxString &paramName, const wxVariant &paramValue) override
Definition: Command.cpp:119
OldStyleCommandType & mType
Definition: Command.h:80
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
std::unique_ptr< CommandContext > mCtx
Definition: Command.h:73
virtual bool Validate(const wxVariant &v)
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:66
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:308
const wxString & Internal() const
CommandSignature & GetSignature() override
Get the signature of the command.
Definition: Command.cpp:255
DecoratedCommand is a decorator for command. It forwards functions to the mCommand it holds...
Definition: Command.h:49
CommandImplementation(OldStyleCommandType &type)
Definition: Command.cpp:173
OldStyleCommandPointer is a unique_ptr to an OldStyleCommand.
virtual bool SetParameter(const wxString &paramName, const wxVariant &paramValue)
Definition: Command.cpp:98
Contains declaration of CommandBuilder class.
Validator & GetValidator(const wxString &paramName)
bool GetBool(const wxString &paramName)
Definition: Command.cpp:216
Class that maps parameter names to default values and validators.
IdentInterfaceSymbol GetSymbol() override
Definition: CommandType.cpp:34
A Validator is an object which checks whether a wxVariant satisfies a certain criterion. This is a base validator which allows anything.
Definition: Validators.h:48