Audacity  2.3.1
CommandTargets.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity - A Digital Audio Editor
4  Copyright 1999-2009 Audacity Team
5  License: GPL v2 - see LICENSE.txt
6 
7  Dan Horgan
8 
9 ******************************************************************//*******************************************************************/
54 
55 #ifndef __COMMANDTARGETS__
56 #define __COMMANDTARGETS__
57 
58 #include "../MemoryX.h"
59 #include <wx/string.h>
60 #include <wx/statusbr.h>
61 //#include "../src/Project.h"
62 #include "../widgets/ProgressDialog.h"
63 #include "../commands/ResponseQueue.h"
64 #include "../widgets/ErrorDialog.h"
65 
67 class CommandProgressTarget /* not final */
68 {
69 public:
71  virtual void Update(double completed) = 0;
72 };
73 
75 class CommandMessageTarget /* not final */
76 {
77 public:
78  CommandMessageTarget() {mCounts.push_back(0);}
79  virtual ~CommandMessageTarget() { Flush();}
80  virtual void Update(const wxString &message) = 0;
81  virtual void StartArray();
82  virtual void EndArray();
83  virtual void StartStruct();
84  virtual void EndStruct();
85  virtual void AddItem(const wxString &value , const wxString &name="" );
86  virtual void AddBool(const bool value , const wxString &name="" );
87  virtual void AddItem(const double value , const wxString &name="" );
88  virtual void StartField( const wxString &name="" );
89  virtual void EndField( );
90  virtual void Flush();
91  wxString Escaped( const wxString & str);
92  wxArrayInt mCounts;
93 };
94 
96 {
97 public:
100  void Update(const wxString &message) override { mTarget.Update( message );}
101  void StartArray() override { mTarget.StartArray();}
102  void EndArray() override { mTarget.EndArray();}
103  void StartStruct() override { mTarget.StartStruct();}
104  void EndStruct() override { mTarget.EndStruct();}
105  void AddItem(const wxString &value , const wxString &name="" ) override
106  { mTarget.AddItem(value,name);}
107  void AddBool(const bool value , const wxString &name="" ) override
108  { mTarget.AddBool(value,name);}
109  void AddItem(const double value , const wxString &name="" ) override
110  { mTarget.AddItem(value,name);}
111  void StartField( const wxString &name="" ) override
112  { mTarget.StartField(name);}
113  void EndField( ) override
114  { mTarget.EndField();}
115  void Flush() override
116  { mTarget.Flush();}
118 };
119 
121 {
122 public:
124  virtual void StartArray() override;
125  virtual void EndArray() override;
126  virtual void StartStruct() override;
127  virtual void EndStruct() override;
128  virtual void AddItem(const wxString &value , const wxString &name="" )override;
129  virtual void AddBool(const bool value , const wxString &name="" )override;
130  virtual void AddItem(const double value , const wxString &name="" )override;
131  virtual void StartField( const wxString &name="" )override;
132  virtual void EndField( ) override;
133 };
134 
136 {
137 public:
139  virtual void StartArray() override;
140  virtual void EndArray() override;
141  virtual void StartStruct() override;
142  virtual void EndStruct() override;
143  virtual void AddItem(const wxString &value , const wxString &name="" )override;
144  virtual void AddBool(const bool value , const wxString &name="" )override;
145  virtual void AddItem(const double value , const wxString &name="" )override;
146  virtual void StartField( const wxString &name="" )override;
147  virtual void EndField( ) override;
148 };
149 
152 {
153 public:
154  virtual ~NullProgressTarget() {}
155  void Update(double WXUNUSED(completed)) override {}
156 };
157 
160 {
161 private:
163 public:
165  : mProgress(pd)
166  {}
167  virtual ~GUIProgressTarget() {}
168  void Update(double completed) override
169  {
170  mProgress.Update(completed);
171  }
172 };
173 
174 
177 {
178 private:
179  std::unique_ptr<CommandMessageTarget> mTarget;
180 public:
181  ProgressToMessageTarget(std::unique_ptr<CommandMessageTarget> &&target)
182  : mTarget(std::move(target))
183  { }
185  {
186  }
187  void Update(double completed) override
188  {
189  mTarget->Update(wxString::Format(wxT("%.2f%%"), completed*100));
190  }
191 };
192 
195 {
196 public:
197  virtual ~NullMessageTarget() {}
198  void Update(const wxString &) override {}
199 };
200 
203 {
204 public:
205  virtual ~MessageBoxTarget() {}
206  void Update(const wxString &message) override
207  {
208  AudacityMessageBox(message);
209  }
210 };
211 
214 {
215 private:
216  wxStatusBar &mStatus;
217 public:
218  StatusBarTarget(wxStatusBar &sb)
219  : mStatus(sb)
220  {}
221  void Update(const wxString &message) override
222  {
223  mStatus.SetStatusText(message, 0);
224  }
225 };
226 
229 {
230 private:
232  wxString mBuffer;
233 public:
235  : mResponseQueue(responseQueue),
236  mBuffer( wxEmptyString )
237  { }
239  {
240  if( mBuffer.StartsWith("\n" ) )
241  mBuffer = mBuffer.Mid( 1 );
243  mResponseQueue.AddResponse(wxString(wxT("\n")));
244  }
245  void Update(const wxString &message) override
246  {
247  mBuffer += message;
248 #if 0
249  mResponseQueue.AddResponse(message);
250 #endif
251  }
252 };
253 
256 {
257 private:
258  std::unique_ptr<CommandMessageTarget> m1, m2;
259 public:
260  CombinedMessageTarget(std::unique_ptr<CommandMessageTarget> &&t1,
261  std::unique_ptr<CommandMessageTarget> &&t2)
262  : m1(std::move(t1)), m2(std::move(t2))
263  {
264  wxASSERT(m1);
265  wxASSERT(m2);
266  }
268  {
269  }
270  void Update(const wxString &message) override
271  {
272  m1->Update(message);
273  m2->Update(message);
274  }
275 };
276 
277 
284 {
285 public:
286  static std::unique_ptr<CommandProgressTarget> ProgressDefault()
287  {
288  return std::make_unique<NullProgressTarget>();
289  }
290 
291  static std::shared_ptr<CommandMessageTarget> MessageDefault()
292  {
293  return std::make_shared<MessageBoxTarget>();
294  }
295 };
296 
302 class CommandOutputTargets /* not final */
303 {
304 public:
305  std::unique_ptr<CommandProgressTarget> mProgressTarget;
306  std::shared_ptr<CommandMessageTarget> mStatusTarget;
307  std::shared_ptr<CommandMessageTarget> mErrorTarget;
308 public:
309  // && is not a reference to a reference, but rather a way to allow reference to a temporary
310  // that will be gone or transfered after we have taken it. It's a reference to an xvalue,
311  // or 'expiring value'.
312  CommandOutputTargets(std::unique_ptr<CommandProgressTarget> &&pt = TargetFactory::ProgressDefault(),
313  std::shared_ptr<CommandMessageTarget> &&st = TargetFactory::MessageDefault(),
314  std::shared_ptr<CommandMessageTarget> &&et = TargetFactory::MessageDefault())
315  : mProgressTarget(std::move(pt)), mStatusTarget(st), mErrorTarget(et)
316  { }
318  {
319  }
320  // Lots of forwarding...
321  void Progress(double completed)
322  {
323  if (mProgressTarget)
324  mProgressTarget->Update(completed);
325  }
326  void Status(const wxString &status, bool bFlush=false)
327  {
328  if (mStatusTarget){
329  mStatusTarget->Update(status);
330  if( bFlush )
331  mStatusTarget->Flush();
332  }
333  }
334  void Error(const wxString &message)
335  {
336  if (mErrorTarget)
337  mErrorTarget->Update(message);
338  }
339  void StartArray()
340  {
341  if (mStatusTarget)
342  mStatusTarget->StartArray();
343  }
344  void EndArray()
345  {
346  if (mStatusTarget)
347  mStatusTarget->EndArray();
348  }
349  void StartStruct()
350  {
351  if (mStatusTarget)
352  mStatusTarget->StartStruct();
353  }
354  void EndStruct()
355  {
356  if (mStatusTarget)
357  mStatusTarget->EndStruct();
358  }
359  void StartField(const wxString &name)
360  {
361  if (mStatusTarget)
362  mStatusTarget->StartField(name);
363  }
364  void EndField()
365  {
366  if (mStatusTarget)
367  mStatusTarget->EndField();
368  }
369  void AddItem(const wxString &value , const wxString &name="" )
370  {
371  if (mStatusTarget)
372  mStatusTarget->AddItem( value, name );
373  }
374  void AddBool(const bool value , const wxString &name="" )
375  {
376  if (mStatusTarget)
377  mStatusTarget->AddItem( value, name );
378  }
379  void AddItem(const double value , const wxString &name="" )
380  {
381  if (mStatusTarget)
382  mStatusTarget->AddItem( value, name );
383  }
384 };
385 
387 {
388 public :
391 private:
393 };
394 
396 {
397 public :
400 private:
402 };
403 
405 {
406 public:
408 
409 };
410 
411 #endif /* End of include guard: __COMMANDTARGETS__ */
virtual void AddBool(const bool value, const wxString &name="") override
void Update(const wxString &message) override
static std::shared_ptr< CommandMessageTarget > MessageDefault()
wxString Escaped(const wxString &str)
ResponseQueueTarget(ResponseQueue &responseQueue)
void AddResponse(Response response)
CommandOutputTargets a mix of three output classes to output progress indication, status messages and...
StatusBarTarget(wxStatusBar &sb)
Displays messages from a command in an AudacityMessageBox.
ProgressDialog Class.
InteractiveOutputTargets is an output target that pops up a dialog, if necessary. ...
virtual ~CommandProgressTarget()
ProgressToMessageTarget formats the percentage complete text as a message and sends it to that messag...
BriefCommandMessageTarget is a CommandOutputTarget that provides status in a briefer listing...
virtual void EndStruct()
virtual void EndStruct() override
Sends command progress information to a ProgressDialog.
void AddItem(const double value, const wxString &name="")
BriefCommandMessageTarget(CommandMessageTarget &target)
void AddBool(const bool value, const wxString &name="") override
CommandMessageTarget & mTarget
CommandMessageTargetDecorator is a CommandOutputTarget that forwards its work on to another one...
Adds messages to a response queue (to be sent back to a script)
ProgressDialog & mProgress
void Update(const wxString &message) override
virtual void Flush()
std::shared_ptr< CommandMessageTarget > mErrorTarget
LispyCommandMessageTarget(CommandMessageTarget &target)
virtual void StartStruct() override
virtual ~ResponseQueueTarget()
std::unique_ptr< CommandProgressTarget > mProgressTarget
int AudacityMessageBox(const wxString &message, const wxString &caption=AudacityMessageBoxCaptionStr(), long style=wxOK|wxCENTRE, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord)
Definition: ErrorDialog.h:92
CommandOutputTargets * pToRestore
Interface for objects that can receive command progress information.
virtual ~GUIProgressTarget()
virtual void StartStruct()
LispifiedCommandOutputTargets is a CommandOutputTargets that replaces the status message target with ...
virtual ~NullProgressTarget()
virtual void StartField(const wxString &name="") override
virtual ~NullMessageTarget()
virtual void AddItem(const wxString &value, const wxString &name="") override
virtual void EndArray() override
virtual void EndField()
virtual void EndStruct() override
void StartField(const wxString &name)
virtual ~ProgressToMessageTarget()
void Error(const wxString &message)
std::unique_ptr< CommandMessageTarget > m2
void Update(const wxString &) override
virtual void StartArray() override
std::shared_ptr< CommandMessageTarget > mStatusTarget
void Update(const wxString &message) override
void AddBool(const bool value, const wxString &name="")
std::unique_ptr< CommandMessageTarget > mTarget
TargetFactory makes Command output targets. By default, we ignore progress updates but display all ot...
ProgressToMessageTarget(std::unique_ptr< CommandMessageTarget > &&target)
virtual void AddItem(const wxString &value, const wxString &name="")
BriefCommandOutputTargets(CommandOutputTargets &target)
CommandOutputTargets * pToRestore
virtual void StartField(const wxString &name="")
CommandMessageTargetDecorator(CommandMessageTarget &target)
Allow messages to be sent from the main thread to the script thread.
Definition: ResponseQueue.h:60
void Progress(double completed)
void Status(const wxString &status, bool bFlush=false)
virtual void StartArray() override
ProgressResult Update(int value, const wxString &message=wxEmptyString)
virtual void AddItem(const wxString &value, const wxString &name="") override
virtual void AddBool(const bool value, const wxString &name="") override
virtual void Update(double completed)=0
void AddItem(const double value, const wxString &name="") override
Interface for objects that can receive (string) messages from a command.
void Update(const wxString &message) override
void AddItem(const wxString &value, const wxString &name="")
void StartField(const wxString &name="") override
virtual void StartStruct() override
void AddItem(const wxString &value, const wxString &name="") override
Sends messages to two message targets at once.
Used to ignore a command's progress updates.
BriefCommandOutputTargets is a CommandOutputTargets that replaces the status message target with the ...
virtual void EndArray() override
CommandOutputTargets(std::unique_ptr< CommandProgressTarget > &&pt=TargetFactory::ProgressDefault(), std::shared_ptr< CommandMessageTarget > &&st=TargetFactory::MessageDefault(), std::shared_ptr< CommandMessageTarget > &&et=TargetFactory::MessageDefault())
ResponseQueue & mResponseQueue
void Update(double completed) override
const wxChar * name
Definition: Distortion.cpp:94
virtual void EndArray()
LispifiedCommandOutputTargets(CommandOutputTargets &target)
virtual void AddBool(const bool value, const wxString &name="")
virtual void EndField() override
void Update(double completed) override
LispyCommandMessageTarget is a CommandOutputTarget that provides status in a lispy style...
virtual void StartField(const wxString &name="") override
virtual void Update(const wxString &message)=0
std::unique_ptr< CommandMessageTarget > m1
Used to ignore a command's message updates.
Displays messages from a command in a wxStatusBar.
virtual void EndField() override
static std::unique_ptr< CommandProgressTarget > ProgressDefault()
CombinedMessageTarget(std::unique_ptr< CommandMessageTarget > &&t1, std::unique_ptr< CommandMessageTarget > &&t2)
void Update(const wxString &message) override
virtual ~MessageBoxTarget()
virtual ~CommandMessageTarget()
void Update(double WXUNUSED(completed)) override
wxStatusBar & mStatus
virtual void StartArray()
GUIProgressTarget(ProgressDialog &pd)