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