source: src/Actions/ActionQueue.hpp@ 33c97e

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 33c97e was c01fec, checked in by Frederik Heber <heber@…>, 10 years ago

Undo/Redobuttons are grayed out when respective Action not possible.

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * ActionQueue.hpp
3 *
4 * Created on: Aug 16, 2013
5 * Author: heber
6 */
7
8#ifndef ACTIONQUEUE_HPP_
9#define ACTIONQUEUE_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include "CodePatterns/Singleton.hpp"
17
18#include "CodePatterns/Observer/Channels.hpp"
19#include "CodePatterns/Observer/Observable.hpp"
20
21#ifdef HAVE_ACTION_THREAD
22#include <boost/thread.hpp>
23#endif
24#include <vector>
25
26#include "Actions/Action.hpp"
27#include "Actions/ActionState.hpp"
28#include "Actions/ActionStatusList.hpp"
29
30#ifdef HAVE_ACTION_THREAD
31void stopQueue();
32void waitQueue();
33#endif
34
35class CommandLineParser;
36
37namespace MoleCuilder {
38
39class ActionHistory;
40class ActionRegistry;
41class ActionTrait;
42
43namespace Queuedetail {
44 template <class T> const T* lastChanged()
45 {
46 ASSERT(0, "Queuedetail::lastChanged() - only specializations may be used.");
47 return NULL;
48 }
49}
50
51/** This class combines the whole handling of Actions into a single class.
52 *
53 * It spawns new Actions by its internal ActionRegistry. Spawned Actions are
54 * automatically queued and placed into a History after execution.
55 */
56class ActionQueue : public Singleton<ActionQueue>, public Observable
57{
58 friend class Singleton<ActionQueue>;
59public:
60 typedef std::vector<std::string> ActionTokens_t;
61 typedef std::vector< Action * > ActionQueue_t;
62
63 //!> channels for this observable
64 enum NotificationType {
65 ActionQueued, // new action was queued
66 NotificationType_MAX // denotes the maximum of available notification types
67 };
68
69 //>! access to last changed element (atom or molecule)
70 template <class T> const T* lastChanged() const
71 { return Queuedetail::lastChanged<T>(); }
72
73 /** Queues the Action with \a name to be called.
74 *
75 * \param name token of Action to actionqueue
76 * \param state whether Actions needs to be filled via a Dialog or not
77 */
78 void queueAction(const std::string &name, enum Action::QueryOptions state = Action::Interactive);
79
80 /** Queues the Action with \a name to be called.
81 *
82 * \param _action action to add
83 * \param state whether Actions needs to be filled via a Dialog or not
84 */
85 void queueAction(const Action * const _action, enum Action::QueryOptions state = Action::Interactive);
86
87 /** Returns the spawned action by token \a name.
88 *
89 * Action is checked into internal actionqueue.
90 *
91 * \return pointer to newly spawned action
92 */
93 Action* getActionByName(const std::string &name);
94
95 /** Checks whether the Action is known by the given \a name.
96 *
97 * \param name token of action to check
98 * \return true - token is known, false - else
99 */
100 bool isActionKnownByName(const std::string &name) const;
101
102 /** Register an Action with the ActionRegistry.
103 *
104 * \param _action action to add
105 */
106 void registerAction(Action *_action);
107
108 /** Returns the vector with the tokens of all currently known Actions.
109 *
110 * \return list of all tokens
111 */
112 const ActionTokens_t getListOfActions() const;
113
114 /** Returns the trait to an Action.
115 *
116 * \param name name of Action
117 * \return const ref to its default Trait.
118 */
119 const ActionTrait& getActionsTrait(const std::string &name) const;
120
121 /** Print the current contents of the actionqueue as CLI instantiated list of Actions.
122 *
123 * This is useful for storing the current session.
124 *
125 * \param output output stream to print to
126 */
127 void outputAsCLI(std::ostream &output) const;
128
129 /** Print the current contents of the actionqueue as Python instantiated list of Actions.
130 *
131 * This is useful for storing the current session.
132 *
133 * \param output output stream to print to
134 */
135 void outputAsPython(std::ostream &output) const;
136
137 /** Undoes last called Acfriend void ::cleanUp();tion.
138 *
139 */
140 void undoLast();
141
142 /** Redoes last undone Action.
143 *
144 */
145 void redoLast();
146
147 /** Checks whether there is one completed Action stored in ActionHistory in the past.
148 *
149 * @return true - at least one Action to undo present, false - else
150 */
151 bool canUndo() const;
152
153 /** Checks whether there is one completed Action stored in ActionHistory in the future.
154 *
155 * @return true - at least one Action to redo present, false - else
156 */
157 bool canRedo() const;
158
159 /** Return status of last executed action.
160 *
161 * \return true - action executed correctly, false - else
162 */
163 bool getLastActionOk() const
164 { return lastActionOk; }
165
166#ifdef HAVE_ACTION_THREAD
167 boost::thread &getRunThread()
168 { return run_thread; }
169#endif
170
171 /** Getter to ref to list of status messages.
172 *
173 * This is meant for UIs to registers as Observables.
174 *
175 * \return ref to StatusList variable
176 */
177 ActionStatusList& getStatusList()
178 { return StatusList; }
179
180private:
181 //!> grant Action access to internal history functions.
182 friend class Action;
183 //!> grant CommandLineParser access to stop and clearQueue()
184 friend class ::CommandLineParser;
185
186 /** Wrapper function to add state to ActionHistory.
187 *
188 * \param _Action Action whose state to add
189 * \param _state state to add
190 */
191 void addElement(Action* _Action, ActionState::ptr _state);
192
193 /** Advocate function to add status message to the list.
194 *
195 */
196 void pushStatus(const std::string &_msg)
197 { StatusList.pushMessage(_msg); }
198
199 /** Wrapper function to clear ActionHistory.
200 *
201 */
202 void clear();
203
204 /** Clears all actions currently present in the actionqueues.
205 *
206 */
207 void clearQueue();
208
209#ifdef HAVE_ACTION_THREAD
210 /** Runs the ActionQueue.
211 *
212 */
213 void run();
214
215 friend void ::stopQueue();
216
217 /** Stops the internal thread.
218 *
219 */
220 void stop();
221
222 friend void ::waitQueue();
223
224 /** Wait till all currently queued actions are processed.
225 *
226 */
227 void wait();
228#endif
229
230 /** Insert an action after CurrentAction.
231 *
232 * This is implemented only to allow action's COMMAND to work. If we
233 * were to use queueAction, actions would come after all other already
234 * present actions.
235 */
236 void insertAction(Action *_action, enum Action::QueryOptions state);
237
238 /** Moves all action from tempqueue into real queue.
239 *
240 */
241 void insertTempQueue();
242
243private:
244 /** Private cstor for ActionQueue.
245 *
246 * Must be private as is singleton.
247 *
248 */
249 ActionQueue();
250
251 /** Dstor for ActionQueue.
252 *
253 */
254 ~ActionQueue();
255
256private:
257 friend const Action *Queuedetail::lastChanged<Action>();
258 static const Action *_lastchangedaction;
259
260 //!> ActionRegistry to spawn new actions
261 ActionRegistry *AR;
262
263 //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
264 ActionHistory *history;
265
266 //!> internal actionqueue of actions
267 ActionQueue_t actionqueue;
268
269 //!> point to current action in actionqueue
270 size_t CurrentAction;
271
272 //!> internal temporary actionqueue of actions used by insertAction()
273 ActionQueue_t tempqueue;
274
275 //!> indicates that the last action has failed
276 bool lastActionOk;
277
278#ifdef HAVE_ACTION_THREAD
279 //!> internal thread to call Actions
280 boost::thread run_thread;
281
282 //!> internal mutex to synchronize access to queue
283 boost::mutex mtx_queue;
284
285 //!> conditional variable notifying when run_thread is idling
286 boost::condition_variable cond_idle;
287
288 //!> flag indicating whether run_thread is idle or not
289 bool run_thread_isIdle;
290
291 //!> internal mutex to synchronize access to run_thread_isIdle
292 boost::mutex mtx_idle;
293#endif
294
295 //!> internal list of status messages from Actions for UIs to display
296 ActionStatusList StatusList;
297};
298namespace Queuedetail {
299 template <> inline const Action* lastChanged<Action>() { return ActionQueue::_lastchangedaction; }
300}
301
302};
303
304#endif /* ACTIONQUEUE_HPP_ */
Note: See TracBrowser for help on using the repository browser.