Audacity  2.2.0
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/msgdlg.h>
28 #include <wx/statusbr.h>
29 #include "../widgets/ProgressDialog.h"
30 #include "../commands/ResponseQueue.h"
31 #include "../src/Project.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  wxMessageBox(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
Definition: CommandTargets.h:138
static std::shared_ptr< CommandMessageTarget > MessageDefault()
Definition: CommandTargets.h:178
void Status(const wxString &status)
Definition: CommandTargets.h:206
ResponseQueueTarget(ResponseQueue &responseQueue)
Definition: CommandTargets.h:131
void AddResponse(Response response)
Definition: ResponseQueue.cpp:28
StatusBarTarget(wxStatusBar &sb)
Definition: CommandTargets.h:116
Displays messages from a command in a wxMessageBox.
Definition: CommandTargets.h:100
ProgressDialog Class.
Definition: ProgressDialog.h:56
virtual ~CommandProgressTarget()
Definition: CommandTargets.h:37
Definition: CommandTargets.h:74
Sends command progress information to a ProgressDialog.
Definition: CommandTargets.h:50
std::shared_ptr< CommandMessageTarget > mErrorTarget
Definition: CommandTargets.h:191
Adds messages to a response queue (to be sent back to a script)
Definition: CommandTargets.h:126
ProgressDialog & mProgress
Definition: CommandTargets.h:53
void Update(const wxString &message) override
Definition: CommandTargets.h:160
virtual ~ResponseQueueTarget()
Definition: CommandTargets.h:134
void Error(const wxString &message)
Definition: CommandTargets.h:211
Interface for objects that can receive command progress information.
Definition: CommandTargets.h:34
virtual ~GUIProgressTarget()
Definition: CommandTargets.h:58
CommandOutputTarget(std::unique_ptr< CommandProgressTarget > &&pt=TargetFactory::ProgressDefault(), std::shared_ptr< CommandMessageTarget > &&st=TargetFactory::MessageDefault(), std::shared_ptr< CommandMessageTarget > &&et=TargetFactory::MessageDefault())
Definition: CommandTargets.h:193
virtual ~NullProgressTarget()
Definition: CommandTargets.h:45
virtual ~NullMessageTarget()
Definition: CommandTargets.h:95
virtual ~ProgressToMessageTarget()
Definition: CommandTargets.h:82
std::unique_ptr< CommandMessageTarget > m2
Definition: CommandTargets.h:148
void Update(const wxString &) override
Definition: CommandTargets.h:96
void Update(const wxString &message) override
Definition: CommandTargets.h:119
std::unique_ptr< CommandMessageTarget > mTarget
Definition: CommandTargets.h:77
Definition: CommandTargets.h:170
ProgressToMessageTarget(std::unique_ptr< CommandMessageTarget > &&target)
Definition: CommandTargets.h:79
~CommandOutputTarget()
Definition: CommandTargets.h:198
Allow messages to be sent from the main thread to the script thread.
Definition: ResponseQueue.h:60
~CombinedMessageTarget()
Definition: CommandTargets.h:157
ProgressResult Update(int value, const wxString &message=wxEmptyString)
Definition: ProgressDialog.cpp:1271
virtual void Update(double completed)=0
void Progress(double completed)
Definition: CommandTargets.h:201
Interface for objects that can receive (string) messages from a command.
Definition: CommandTargets.h:66
void Update(const wxString &message) override
Definition: CommandTargets.h:104
Sends messages to two message targets at once.
Definition: CommandTargets.h:145
Used to ignore a command's progress updates.
Definition: CommandTargets.h:42
Definition: CommandTargets.h:186
ResponseQueue & mResponseQueue
Definition: CommandTargets.h:129
void Update(double completed) override
Definition: CommandTargets.h:59
std::unique_ptr< CommandProgressTarget > mProgressTarget
Definition: CommandTargets.h:189
void Update(double completed) override
Definition: CommandTargets.h:85
virtual void Update(const wxString &message)=0
std::unique_ptr< CommandMessageTarget > m1
Definition: CommandTargets.h:148
Used to ignore a command's message updates.
Definition: CommandTargets.h:92
Displays messages from a command in a wxStatusBar.
Definition: CommandTargets.h:111
std::shared_ptr< CommandMessageTarget > mStatusTarget
Definition: CommandTargets.h:190
static std::unique_ptr< CommandProgressTarget > ProgressDefault()
Definition: CommandTargets.h:173
CombinedMessageTarget(std::unique_ptr< CommandMessageTarget > &&t1, std::unique_ptr< CommandMessageTarget > &&t2)
Definition: CommandTargets.h:150
virtual ~MessageBoxTarget()
Definition: CommandTargets.h:103
virtual ~CommandMessageTarget()
Definition: CommandTargets.h:69
void Update(double WXUNUSED(completed)) override
Definition: CommandTargets.h:46
wxStatusBar & mStatus
Definition: CommandTargets.h:114
GUIProgressTarget(ProgressDialog &pd)
Definition: CommandTargets.h:55