Audacity  2.2.2
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 ******************************************************************//*******************************************************************/
21 
22 #ifndef __COMMANDTARGETS__
23 #define __COMMANDTARGETS__
24 
25 #include "../MemoryX.h"
26 #include <wx/string.h>
27 #include <wx/statusbr.h>
28 #include "../widgets/ProgressDialog.h"
29 #include "../commands/ResponseQueue.h"
30 #include "../src/Project.h"
31 #include "../widgets/ErrorDialog.h"
32 
34 class CommandProgressTarget /* not final */
35 {
36 public:
38  virtual void Update(double completed) = 0;
39 };
40 
43 {
44 public:
45  virtual ~NullProgressTarget() {}
46  void Update(double WXUNUSED(completed)) override {}
47 };
48 
51 {
52 private:
54 public:
56  : mProgress(pd)
57  {}
58  virtual ~GUIProgressTarget() {}
59  void Update(double completed) override
60  {
61  mProgress.Update(completed);
62  }
63 };
64 
66 class CommandMessageTarget /* not final */
67 {
68 public:
69  virtual ~CommandMessageTarget() {}
70  virtual void Update(const wxString &message) = 0;
71 };
72 
75 {
76 private:
77  std::unique_ptr<CommandMessageTarget> mTarget;
78 public:
79  ProgressToMessageTarget(std::unique_ptr<CommandMessageTarget> &&target)
80  : mTarget(std::move(target))
81  { }
83  {
84  }
85  void Update(double completed) override
86  {
87  mTarget->Update(wxString::Format(wxT("%.2f%%"), completed*100));
88  }
89 };
90 
93 {
94 public:
95  virtual ~NullMessageTarget() {}
96  void Update(const wxString &) override {}
97 };
98 
101 {
102 public:
103  virtual ~MessageBoxTarget() {}
104  void Update(const wxString &message) override
105  {
106  AudacityMessageBox(message);
107  }
108 };
109 
112 {
113 private:
114  wxStatusBar &mStatus;
115 public:
116  StatusBarTarget(wxStatusBar &sb)
117  : mStatus(sb)
118  {}
119  void Update(const wxString &message) override
120  {
121  mStatus.SetStatusText(message, 0);
122  }
123 };
124 
127 {
128 private:
130 public:
132  : mResponseQueue(responseQueue)
133  { }
135  {
136  mResponseQueue.AddResponse(wxString(wxT("\n")));
137  }
138  void Update(const wxString &message) override
139  {
140  mResponseQueue.AddResponse(message);
141  }
142 };
143 
146 {
147 private:
148  std::unique_ptr<CommandMessageTarget> m1, m2;
149 public:
150  CombinedMessageTarget(std::unique_ptr<CommandMessageTarget> &&t1,
151  std::unique_ptr<CommandMessageTarget> &&t2)
152  : m1(std::move(t1)), m2(std::move(t2))
153  {
154  wxASSERT(m1);
155  wxASSERT(m2);
156  }
158  {
159  }
160  void Update(const wxString &message) override
161  {
162  m1->Update(message);
163  m2->Update(message);
164  }
165 };
166 
167 
168 // By default, we ignore progress updates but display all other messages
169 // directly
171 {
172 public:
173  static std::unique_ptr<CommandProgressTarget> ProgressDefault()
174  {
175  return std::make_unique<NullProgressTarget>();
176  }
177 
178  static std::shared_ptr<CommandMessageTarget> MessageDefault()
179  {
180  return std::make_shared<MessageBoxTarget>();
181  }
182 };
183 
187 {
188 private:
189  std::unique_ptr<CommandProgressTarget> mProgressTarget;
190  std::shared_ptr<CommandMessageTarget> mStatusTarget;
191  std::shared_ptr<CommandMessageTarget> mErrorTarget;
192 public:
193  CommandOutputTarget(std::unique_ptr<CommandProgressTarget> &&pt = TargetFactory::ProgressDefault(),
194  std::shared_ptr<CommandMessageTarget> &&st = TargetFactory::MessageDefault(),
195  std::shared_ptr<CommandMessageTarget> &&et = TargetFactory::MessageDefault())
196  : mProgressTarget(std::move(pt)), mStatusTarget(st), mErrorTarget(et)
197  { }
199  {
200  }
201  void Progress(double completed)
202  {
203  if (mProgressTarget)
204  mProgressTarget->Update(completed);
205  }
206  void Status(const wxString &status)
207  {
208  if (mStatusTarget)
209  mStatusTarget->Update(status);
210  }
211  void Error(const wxString &message)
212  {
213  if (mErrorTarget)
214  mErrorTarget->Update(message);
215  }
216 };
217 
218 #endif /* End of include guard: __COMMANDTARGETS__ */
void Update(const wxString &message) override
static std::shared_ptr< CommandMessageTarget > MessageDefault()
void Status(const wxString &status)
ResponseQueueTarget(ResponseQueue &responseQueue)
void AddResponse(Response response)
StatusBarTarget(wxStatusBar &sb)
Displays messages from a command in an AudacityMessageBox.
ProgressDialog Class.
virtual ~CommandProgressTarget()
Sends command progress information to a ProgressDialog.
std::shared_ptr< CommandMessageTarget > mErrorTarget
Adds messages to a response queue (to be sent back to a script)
ProgressDialog & mProgress
void Update(const wxString &message) override
virtual ~ResponseQueueTarget()
void Error(const wxString &message)
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
Interface for objects that can receive command progress information.
virtual ~GUIProgressTarget()
CommandOutputTarget(std::unique_ptr< CommandProgressTarget > &&pt=TargetFactory::ProgressDefault(), std::shared_ptr< CommandMessageTarget > &&st=TargetFactory::MessageDefault(), std::shared_ptr< CommandMessageTarget > &&et=TargetFactory::MessageDefault())
virtual ~NullProgressTarget()
virtual ~NullMessageTarget()
virtual ~ProgressToMessageTarget()
std::unique_ptr< CommandMessageTarget > m2
void Update(const wxString &) override
void Update(const wxString &message) override
std::unique_ptr< CommandMessageTarget > mTarget
ProgressToMessageTarget(std::unique_ptr< CommandMessageTarget > &&target)
Allow messages to be sent from the main thread to the script thread.
Definition: ResponseQueue.h:60
ProgressResult Update(int value, const wxString &message=wxEmptyString)
virtual void Update(double completed)=0
void Progress(double completed)
Interface for objects that can receive (string) messages from a command.
void Update(const wxString &message) override
Sends messages to two message targets at once.
Used to ignore a command's progress updates.
ResponseQueue & mResponseQueue
void Update(double completed) override
std::unique_ptr< CommandProgressTarget > mProgressTarget
void Update(double completed) 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.
std::shared_ptr< CommandMessageTarget > mStatusTarget
static std::unique_ptr< CommandProgressTarget > ProgressDefault()
CombinedMessageTarget(std::unique_ptr< CommandMessageTarget > &&t1, std::unique_ptr< CommandMessageTarget > &&t2)
virtual ~MessageBoxTarget()
virtual ~CommandMessageTarget()
void Update(double WXUNUSED(completed)) override
wxStatusBar & mStatus
GUIProgressTarget(ProgressDialog &pd)