Choreonoid  1.5
Task.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_TASK_H
7 #define CNOID_UTIL_TASK_H
8 
9 #include <cnoid/Referenced>
10 #include <cnoid/Signal>
11 #include <boost/function.hpp>
12 #include <boost/bind.hpp>
13 #include <vector>
14 #include "exportdecl.h"
15 
16 namespace cnoid {
17 
18 class Mapping;
19 class AbstractTaskSequencer;
20 
22 {
23 public:
24  virtual ~TaskProc();
25  virtual int currentPhaseIndex() const = 0;
26  virtual bool isAutoMode() const = 0;
27  virtual void breakSequence() = 0;
28  virtual void setNextCommand(int commandIndex) = 0;
29  virtual void setNextPhase(int phaseIndex) = 0;
30  virtual void setCommandLinkAutomatic() = 0;
31  virtual bool executeCommand(int index) = 0;
32  virtual bool wait(double sec) = 0;
33  virtual bool waitForCommandToFinish(double timeout = 0.0) = 0;
34  virtual bool waitForCommandToFinish(Connection connectionToDisconnect, double timeout) = 0;
35 
36  template<class Signature> bool waitForSignal(SignalProxy<Signature> signalProxy, double timeout = 0.0){
37  return waitForCommandToFinish(signalProxy.connect(boost::bind(&TaskProc::notifyCommandFinish, this, true)), timeout);
38  }
39 
40  template<class Signature> bool waitForBooleanSignal(SignalProxy<Signature> signalProxy, double timeout = 0.0){
41  return waitForCommandToFinish(signalProxy.connect(boost::bind(&TaskProc::notifyCommandFinish, this, _1)), timeout);
42  }
43 
44  virtual void notifyCommandFinish(bool isCompleted = true) = 0;
45 };
46 
47 
48 typedef boost::function<void(TaskProc* proc)> TaskFunc;
49 
50 
52 {
53 public:
54  bool isChecked() const { return isChecked_; }
55  void setChecked(bool on);
56 
57  SignalProxy<void(bool on)> sigToggled() { return sigToggled_; }
58 
59 private:
60  bool isChecked_;
61  Signal<void(bool on)> sigToggled_;
62 };
63 
65 
66 
68 {
69 public:
70  TaskCommand();
71  TaskCommand(const std::string& caption);
72  ~TaskCommand();
73 
74  const std::string& caption() const { return caption_; }
75  TaskCommand* setCaption(const std::string& caption){
76  caption_ = caption;
77  return this;
78  }
79 
80  const std::string& description() const { return description_; }
81  TaskCommand* setDescription(const std::string& description) {
82  description_ = description; return this; }
83 
84  TaskFunc function() const { return function_; }
85  TaskCommand* setFunction(TaskFunc func) { function_ = func; return this; }
86 
87  TaskCommand* setDefault(bool on = true) { isDefault_ = on; return this; }
88  bool isDefault() const { return isDefault_; }
89 
90  TaskCommand* setCheckable(bool on = true);
91  TaskCommand* setToggleState(TaskToggleState* state);
92  TaskToggleState* toggleState();
93  TaskCommand* setChecked(bool on);
94  bool isChecked() const;
95 
96  int nextPhaseIndex(int currentPhaseIndex) const;
97  TaskCommand* setPhaseLink(int phaseIndex);
98  TaskCommand* setPhaseLinkStep(int phaseIndexStep);
99  TaskCommand* linkToNextPhase() { setPhaseLinkStep(1); return this; }
100 
101  int nextCommandIndex(int currentCommandIndex) const;
102  TaskCommand* setCommandLink(int commandIndex);
103  TaskCommand* setCommandLinkStep(int commandIndexStep);
104  TaskCommand* linkToNextCommand() { setCommandLinkStep(1); return this; }
105  bool isCommandLinkAutomatic() const { return isCommandLinkAutomatic_; }
106  TaskCommand* setCommandLinkAutomatic(bool on = true) { isCommandLinkAutomatic_ = on; return this; }
107 
108  TaskCommand* setLevel(int level) { level_ = level; return this; }
109  int level() const { return level_; }
110 
111 private:
112  std::string caption_;
113  std::string description_;
114  TaskFunc function_;
115  int nextPhaseIndex_;
116  int nextCommandIndex_;
117  int level_;
118  TaskToggleStatePtr toggleState_;
119  bool isNextPhaseRelative_;
120  bool isNextCommandRelative_;
121  bool isCommandLinkAutomatic_;
122  bool isDefault_;
123 
124  void initialize();
125 };
126 
128 
129 
132 
133 
135 {
136 public:
137  TaskPhase(const std::string& caption);
138  TaskPhase(const TaskPhase& org, bool doDeepCopy = true);
139  ~TaskPhase();
140 
141  virtual TaskPhase* clone(bool doDeepCopy = true);
142 
143  const std::string& caption() const { return caption_; }
144  void setCaption(const std::string& str);
145 
146  bool isSkipped() const { return isSkipped_; }
147  void setSkipped(bool on) { isSkipped_ = on; }
148 
149  void setPreCommand(TaskFunc func);
150  TaskFunc preCommand() const { return preCommand_; }
151 
152  TaskCommand* addCommand();
153  TaskCommand* addCommand(const std::string& caption);
154  TaskCommand* addToggleCommand();
155  TaskCommand* addToggleCommand(const std::string& caption);
156  int numCommands() const { return commands.size(); }
157  TaskCommand* command(int index) const;
158  int lastCommandIndex() const { return commands.size() - 1; }
159  TaskCommand* lastCommand() const { return command(commands.size() - 1); }
160 
161  TaskPhaseProxyPtr commandLevel(int level);
162  int maxCommandLevel() const;
163 
164 private:
165  std::string caption_;
166  TaskFunc preCommand_;
167  std::vector<TaskCommandPtr> commands;
168  bool isSkipped_;
169 };
170 
172 
173 
175 {
176 public:
177  TaskPhaseProxy(TaskPhase* phase);
178 
179  void setCommandLevel(int level);
180  int commandLevel() const { return commandLevel_; }
181 
182  TaskCommand* addCommand();
183  TaskCommand* addCommand(const std::string& caption);
184  TaskCommand* addToggleCommand();
185  TaskCommand* addToggleCommand(const std::string& caption);
186 
187 private:
188  TaskPhasePtr phase;
189  int commandLevel_;
190 };
191 
192 
197 {
198 public:
199  virtual ~TaskMenu();
200  virtual void addMenuItem(const std::string& caption, boost::function<void()> func) = 0;
201  virtual void addCheckMenuItem(const std::string& caption, bool isChecked, boost::function<void(bool on)> func) = 0;
202  virtual void addMenuSeparator() = 0;
203 };
204 
205 
207 {
208 public:
209  Task();
210  Task(const std::string& name, const std::string& caption);
211  Task(const Task& org, bool doDeepCopy = true);
212  ~Task();
213 
214  const std::string& name() const { return name_; }
215  void setName(const std::string& str);
216  const std::string& caption() const { return caption_; }
217  void setCaption(const std::string& str);
218 
219  int numPhases() const { return phases_.size(); }
220  TaskPhase* phase(int index);
221 
222  TaskPhase* addPhase(TaskPhase* phase);
223  TaskPhase* addPhase(const std::string& caption);
224  TaskPhase* lastPhase();
225 
226  // The following functions do operations to the last added phase
227  void setPreCommand(TaskFunc func);
228  TaskCommand* addCommand();
229  TaskCommand* addCommand(const std::string& caption);
230  TaskCommand* addToggleCommand();
231  TaskCommand* addToggleCommand(const std::string& caption);
232  TaskCommand* lastCommand();
233  int lastCommandIndex();
234  TaskPhaseProxyPtr commandLevel(int level);
235  int maxCommandLevel() const;
236 
237  TaskFunc funcToSetCommandLink(int commandIndex) const;
238 
239  virtual void onActivated(AbstractTaskSequencer* sequencer);
240  virtual void onDeactivated(AbstractTaskSequencer* sequencer);
241  virtual void storeState(AbstractTaskSequencer* sequencer, Mapping& archive);
242  virtual void restoreState(AbstractTaskSequencer* sequencer, const Mapping& archive);
243 
245  virtual void onMenuRequest(TaskMenu& menu);
246 
247 private:
248  std::string name_;
249  std::string caption_;
250  std::vector<TaskPhasePtr> phases_;
251 };
252 
254 
255 }
256 
257 #endif
TaskCommand * setDescription(const std::string &description)
Definition: Task.h:81
bool isCommandLinkAutomatic() const
Definition: Task.h:105
connection_type connect(typename SignalType::slot_function_type f)
Definition: Signal.h:393
const std::string & caption() const
Definition: Task.h:74
const std::string & name() const
Definition: Task.h:214
Definition: ValueTree.h:224
int lastCommandIndex() const
Definition: Task.h:158
bool waitForBooleanSignal(SignalProxy< Signature > signalProxy, double timeout=0.0)
Definition: Task.h:40
bool isChecked() const
Definition: Task.h:54
const std::string & caption() const
Definition: Task.h:143
int numPhases() const
Definition: Task.h:219
Definition: Referenced.h:67
TaskCommand * setCaption(const std::string &caption)
Definition: Task.h:75
bool waitForSignal(SignalProxy< Signature > signalProxy, double timeout=0.0)
Definition: Task.h:36
boost::function< void(TaskProc *proc)> TaskFunc
Definition: Task.h:48
void setSkipped(bool on)
Definition: Task.h:147
bool isDefault() const
Definition: Task.h:88
TaskCommand * setLevel(int level)
Definition: Task.h:108
TaskCommand * linkToNextPhase()
Definition: Task.h:99
TaskCommand * linkToNextCommand()
Definition: Task.h:104
ref_ptr< Task > TaskPtr
Definition: Task.h:253
TaskCommand * lastCommand() const
Definition: Task.h:159
Definition: Task.h:21
std::string str(const Vector3 &v)
Definition: EigenUtil.cpp:90
int numCommands() const
Definition: Task.h:156
const std::string & caption() const
Definition: Task.h:216
Definition: AbstractTaskSequencer.h:14
ref_ptr< TaskPhase > TaskPhasePtr
Definition: Task.h:171
Definition: Task.h:174
TaskFunc preCommand() const
Definition: Task.h:150
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
Definition: Signal.h:107
Definition: Task.h:196
const std::string & description() const
Definition: Task.h:80
TaskCommand * setCommandLinkAutomatic(bool on=true)
Definition: Task.h:106
Definition: Task.h:134
int commandLevel() const
Definition: Task.h:180
Definition: Task.h:206
TaskCommand * setFunction(TaskFunc func)
Definition: Task.h:85
#define CNOID_EXPORT
Definition: Util/exportdecl.h:37
SignalProxy< void(bool on)> sigToggled()
Definition: Task.h:57
ref_ptr< TaskToggleState > TaskToggleStatePtr
Definition: Task.h:64
bool isSkipped() const
Definition: Task.h:146
Definition: Task.h:51
Definition: Signal.h:380
virtual void notifyCommandFinish(bool isCompleted=true)=0
int level() const
Definition: Task.h:109
Definition: Task.h:67
TaskCommand * setDefault(bool on=true)
Definition: Task.h:87
ref_ptr< TaskCommand > TaskCommandPtr
Definition: Task.h:127
ref_ptr< TaskPhaseProxy > TaskPhaseProxyPtr
Definition: Task.h:130