| [65b6e0] | 1 | /*
 | 
|---|
| [56f73b] | 2 |  * Action.hpp
 | 
|---|
| [65b6e0] | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Dec 8, 2009
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [56f73b] | 8 | #ifndef ACTION_HPP_
 | 
|---|
 | 9 | #define ACTION_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
| [65b6e0] | 15 | 
 | 
|---|
| [cc04b7] | 16 | #include <string>
 | 
|---|
| [5b0b98] | 17 | #include <boost/shared_ptr.hpp>
 | 
|---|
| [cc04b7] | 18 | 
 | 
|---|
| [e4afb4] | 19 | /** Used in .def files in paramdefaults define to set that no default value exists.
 | 
|---|
 | 20 |  * We define NODEFAULT here, as it is used in .def files and needs to be present
 | 
|---|
 | 21 |  * before these are included.
 | 
|---|
 | 22 |  */
 | 
|---|
 | 23 | #define NODEFAULT std::string()
 | 
|---|
 | 24 | 
 | 
|---|
| [67e2b3] | 25 | // forward declaration
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | class ActionState;
 | 
|---|
 | 28 | class ActionSequence;
 | 
|---|
| [ba7418] | 29 | class Dialog;
 | 
|---|
| [67e2b3] | 30 | 
 | 
|---|
| [df32ee] | 31 | #include "Actions/ActionTraits.hpp"
 | 
|---|
 | 32 | 
 | 
|---|
| [2efa90] | 33 | /**
 | 
|---|
 | 34 |  * @file
 | 
|---|
 | 35 |  * <H1> Action Howto </H1>
 | 
|---|
 | 36 |  *
 | 
|---|
 | 37 |  * <H2> Introduction </H2>
 | 
|---|
 | 38 |  *
 | 
|---|
 | 39 |  * Actions are used in object oriented design as a replacement for callback functions.
 | 
|---|
 | 40 |  * In most ways Actions can be used in the same way that callbacks were used in non
 | 
|---|
 | 41 |  * OO-Systems, but can contain support for several extra mechanism such as undo/redo
 | 
|---|
 | 42 |  * or progress indicators.
 | 
|---|
 | 43 |  *
 | 
|---|
 | 44 |  * The main purpose of an action class is to contain small procedures, that can be repeatedly
 | 
|---|
 | 45 |  * called. These procedures can also be stored, passed around, so that the execution of an
 | 
|---|
 | 46 |  * action can happen quite far away from the place of creation. For a detailed description of
 | 
|---|
 | 47 |  * the Action pattern see GOF:1996.
 | 
|---|
 | 48 |  *
 | 
|---|
 | 49 |  * <H3> How to use an action </H3>
 | 
|---|
 | 50 |  *
 | 
|---|
 | 51 |  * The process of using an action is as easy as calling the call() method of the action. The
 | 
|---|
 | 52 |  * action will then do whatever it is supposed to do. If it is an action that can be undone, it
 | 
|---|
 | 53 |  * will also register itself in the history to make itself available for undo. To undo the last
 | 
|---|
 | 54 |  * action, you can either use the undoLast() method inside the ActionHistory class or call the
 | 
|---|
 | 55 |  * UndoAction also provided by the ActionHistory. If an action was undone it will be available for
 | 
|---|
 | 56 |  * redo, using the redoLast() method of the ActionHistory or the RedoAction also provided by this
 | 
|---|
 | 57 |  * class. To check whether undo/redo is available at any moment you can use the hasUndo() or
 | 
|---|
 | 58 |  * hasRedo() method respectively.
 | 
|---|
 | 59 |  *
 | 
|---|
| [0430e3] | 60 |  * Note that an Action always has two functions createDialog() and performCall(). The former
 | 
|---|
 | 61 |  * returns a Dialog filled with query...() functions for all information that we need from the
 | 
|---|
 | 62 |  * user. The latter must not contain any interaction but just uses these values (which are
 | 
|---|
 | 63 |  * temporarily stored by class ValueStorage) to perform the Action.
 | 
|---|
 | 64 |  *
 | 
|---|
 | 65 |  * Furthermore, there is a global action function that makes the action callable with already
 | 
|---|
 | 66 |  * present parameters (i.e. without user interaction and for internal use within the code only).
 | 
|---|
 | 67 |  * This function is basically just a macro, that puts the parameters into the ValueStorage and
 | 
|---|
 | 68 |  * calls Action::call(Action::NonInteractive).
 | 
|---|
 | 69 |  *
 | 
|---|
| [2efa90] | 70 |  * Actions can be set to be active or inactive. If an action is set to inactive it is signaling, that
 | 
|---|
 | 71 |  * some condition necessary for this action to be executed is not currently met. For example the
 | 
|---|
 | 72 |  * UndoAction will set itself to inactive, when there is no action at that time that can be undone.
 | 
|---|
 | 73 |  * Using call() on an inactive Action results in a no-op. You can query the state of an action using
 | 
|---|
 | 74 |  * the isActive() method.
 | 
|---|
 | 75 |  *
 | 
|---|
 | 76 |  * The undo capabilities of actions come in three types as signaled by two boolean flags (one
 | 
|---|
 | 77 |  * combination of these flags is left empty as can be seen later).
 | 
|---|
 | 78 |  * <ul>
 | 
|---|
 | 79 |  * <li/> The first flag indicates if the undo mechanism for this action should be considered at all, i.e.
 | 
|---|
 | 80 |  *   if the state of the application changes in a way that needs to be reverted. Actions that should
 | 
|---|
 | 81 |  *   consider the undo mechanism are for example adding a molecule, moving atoms, changing
 | 
|---|
 | 82 |  *   the name of a molecule etc. Changing the View-Area on the other hand should be an action that
 | 
|---|
 | 83 |  *   does not consider the undo mechanism. This flag can be queried using the shouldUndo() method.
 | 
|---|
 | 84 |  *
 | 
|---|
 | 85 |  * <li/> The second flag indicates whether the changes can be undo for this action. If this flag is true
 | 
|---|
 | 86 |  *   the action will be made available for undo using the ActionHistory class and the actions of this
 | 
|---|
 | 87 |  *   class. If this flag is false while the shoudlUndo() flag is true this means that this action
 | 
|---|
 | 88 |  *   changes the state of the application changes in a way that cannot be undone, but might cause
 | 
|---|
 | 89 |  *   the undo of previous actions to fail. In this case the whole History is cleared, as to keep
 | 
|---|
 | 90 |  *   the state of the application intact by avoiding dangerous undos. This flag can be queried
 | 
|---|
 | 91 |  *   using the canUndo() method.
 | 
|---|
 | 92 |  *</ul>
 | 
|---|
 | 93 |  *
 | 
|---|
 | 94 |  * Each action has a name, that can be used to identify it throughout the run of the application.
 | 
|---|
 | 95 |  * This name can be retrieved using the getName() method. Most actions also register themselves with
 | 
|---|
 | 96 |  * a global structure, called the ActionRegistry. Actions that register themselves need to have a
 | 
|---|
 | 97 |  * unique name for the whole application. If the name is known these actions can be retrieved from
 | 
|---|
 | 98 |  * the registry by their name and then be used as normal.
 | 
|---|
 | 99 |  *
 | 
|---|
 | 100 |  * <H2> Building your own actions </H2>
 | 
|---|
 | 101 |  *
 | 
|---|
| [10fa1d] | 102 |  * Building actions is easy. Each specific ...Action is derived from the base class Action.
 | 
|---|
 | 103 |  * In order to create all the reoccuring stuff, macros have been created which you can simply
 | 
|---|
 | 104 |  * include and then don't need to worry about it.
 | 
|---|
 | 105 |  * There are three major virtual functions: performCall(), performUndo(), performRedo() which
 | 
|---|
 | 106 |  * you have to write, to equip your action with some actual capabilities.
 | 
|---|
 | 107 |  * Each Action definition and implementation consists of of three files:
 | 
|---|
 | 108 |  * -# cpp: contains performX() which you have to write, also some boilerplate functions which are
 | 
|---|
 | 109 |  *         constructed automatically when including your def and "Actions/action_impl_pre.hpp"
 | 
|---|
 | 110 |  * -# hpp: boilerplate definitions created simply by including your def and
 | 
|---|
 | 111 |  *         "Actions/action_impl_header.hpp"
 | 
|---|
 | 112 |  * -# def: macro definitions of all your parameters and additional variables needed for the state,
 | 
|---|
 | 113 |  *         also name and category and token of your action.
 | 
|---|
 | 114 |  *
 | 
|---|
 | 115 |  * Best thing to do is look at one of the already present triples and you should soon understand
 | 
|---|
 | 116 |  * what you have to add:
 | 
|---|
 | 117 |  * -# pick the right category, i.e. the right folder in src/Actions
 | 
|---|
 | 118 |  * -# pick the right name
 | 
|---|
 | 119 |  * -# decide which parameters your actions need and what the type, the variable name and the token
 | 
|---|
 | 120 |  *    to reference it from the command-line should be. Check whether already present and fitting
 | 
|---|
 | 121 |  *    tokens exists, e.g. "position" as token for a Vector representing a position.
 | 
|---|
 | 122 |  * -# consider which additional information you need to undo your action
 | 
|---|
 | 123 |  * -# don't forget to include your .def file followed by "action_impl_pre.hpp" in .cpp or
 | 
|---|
 | 124 |  *    "action_impl_header.hpp" in the .hpp
 | 
|---|
 | 125 |  * -# continue to write the functionality of your action in performCall(), undo and redo in performUndo()
 | 
|---|
 | 126 |  *    and performRedo().
 | 
|---|
 | 127 |  * -# You should indicate whether the action supports undo by implementing the shouldUndo() and
 | 
|---|
 | 128 |  *    canUndo() methods to return the appropriate flags.
 | 
|---|
 | 129 |  *
 | 
|---|
 | 130 |  * <H3> Specific notes on the macros </H3>
 | 
|---|
 | 131 |  *
 | 
|---|
 | 132 |  * The following functions are created by the macros, i.e. you don't need to worry about it:
 | 
|---|
 | 133 |  *
 | 
|---|
 | 134 |  * Any user interaction should be placed into the dialog returned by fillDialog().
 | 
|---|
| [2efa90] | 135 |  *
 | 
|---|
| [0430e3] | 136 |  * Also, create the global function to allow for easy calling of your function internally (i.e.
 | 
|---|
 | 137 |  * without user interaction). It should have the name of the Action class without the suffix Action.
 | 
|---|
 | 138 |  *
 | 
|---|
| [2efa90] | 139 |  * The constructor of your derived class also needs to call the Base constructor, passing it the
 | 
|---|
 | 140 |  * name of the Action and a flag indicating whether this action should be made available in the
 | 
|---|
 | 141 |  * registry. WARNING: Do not use the virtual getName() method of the derived action to provide the
 | 
|---|
 | 142 |  * constructor with the name, even if you overloaded this method to return a constant. Doing this
 | 
|---|
 | 143 |  * will most likely not do what you think it does (see: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.5
 | 
|---|
 | 144 |  * if you want to know why this wont work)
 | 
|---|
 | 145 |  *
 | 
|---|
 | 146 |  * <H3> Interfacing your Action with the Undo mechanism </H3>
 | 
|---|
 | 147 |  *
 | 
|---|
 | 148 |  * The performX() methods need to comply to a simple standard to allow for undo and redo. The first
 | 
|---|
 | 149 |  * convention in this standard concerns the return type. All methods that handle calling, undoing
 | 
|---|
 | 150 |  * or redoing return an object of Action::state_ptr. This is a smart pointer to a State object, that
 | 
|---|
 | 151 |  * can be used to store state information that is needed by your action for later redo. A rename
 | 
|---|
 | 152 |  * Action for example would need to store which object has been renamed and what the old name was.
 | 
|---|
 | 153 |  * A move Action on the other hand would need to store the object that has been moved as well as the
 | 
|---|
 | 154 |  * old position. If your Action does not need to store any kind of information for redo you can
 | 
|---|
 | 155 |  * simply return Action::success and skip the rest of this paragraph. If your action has been
 | 
|---|
 | 156 |  * abborted you can return Action::failure, which indicates to the history mechanism that this
 | 
|---|
 | 157 |  * action should not be stored.
 | 
|---|
 | 158 |  *
 | 
|---|
 | 159 |  * If your Action needs any kind of information to undo its execution, you need to store this
 | 
|---|
 | 160 |  * information in the state that is returned by the performCall() method. Since no assumptions
 | 
|---|
 | 161 |  * can be made on the type or amount of information the ActionState base class is left empty.
 | 
|---|
 | 162 |  * To use this class you need to derive a YourActionState class from the ActionState base class
 | 
|---|
 | 163 |  * adding your data fields and accessor functions. Upon undo the ActionState object produced
 | 
|---|
 | 164 |  * by the corresponding performCall() is then passed to the performUndo() method which should
 | 
|---|
 | 165 |  * typecast the ActionState to the appropriate sub class, undo all the changes and produce
 | 
|---|
 | 166 |  * a State object that can be used to redo the action if neccessary. This new state object is
 | 
|---|
 | 167 |  * then used if the redo mechanism is invoked and passed to the performRedo() function, which
 | 
|---|
 | 168 |  * again produces a State that can be used for performUndo().
 | 
|---|
 | 169 |  *
 | 
|---|
 | 170 |  * <H3> Outline of the implementation of Actions </H3>
 | 
|---|
 | 171 |  *
 | 
|---|
 | 172 |  * To sum up the actions necessary to build actions here is a brief outline of things methioned
 | 
|---|
 | 173 |  * in the last paragraphs:
 | 
|---|
 | 174 |  *
 | 
|---|
 | 175 |  * <H4> Basics </H4>
 | 
|---|
 | 176 |  *
 | 
|---|
 | 177 |  * <ul>
 | 
|---|
| [10fa1d] | 178 |  *  <li/> create parameter tupels (type, token, reference), put into def. Access them later in
 | 
|---|
 | 179 |  *        the performX() via the structure params.###.
 | 
|---|
 | 180 |  *  <li/> think of name, category and token for your action, put into def
 | 
|---|
 | 181 |  *  <li/> create additional state variables tupels (type, reference) for storing extra information
 | 
|---|
 | 182 |  *        that you need for undo/redo in the ActionState. You can always access the parameters
 | 
|---|
 | 183 |  *        of your Action by state.params.### (i.e. they are copied to the state by default).
 | 
|---|
 | 184 |  *  <li/> implement performCall(), first line should be calling of getParametersfromValueStorage().
 | 
|---|
 | 185 |  *  <li/> performUndo(), performRedo()
 | 
|---|
 | 186 |  *  <li/> implement the functions that return the flags for the undo mechanism, i.e. true/false.
 | 
|---|
| [2efa90] | 187 |  * </ul>
 | 
|---|
 | 188 |  *
 | 
|---|
 | 189 |  * <H4> Implementing performX() methods </H4>
 | 
|---|
 | 190 |  *
 | 
|---|
 | 191 |  * <ul>
 | 
|---|
 | 192 |  *  <li/> performCall():
 | 
|---|
 | 193 |  *  <ul>
 | 
|---|
| [10fa1d] | 194 |  *   <li/> first line should be calling of getParametersfromValueStorage().
 | 
|---|
 | 195 |  *   <li/> Access your parameters by the structure params.### (where ### stands for the reference/
 | 
|---|
 | 196 |  *         variable name chosen in the tupel).
 | 
|---|
| [2efa90] | 197 |  *   <li/> do whatever is needed to make the action work
 | 
|---|
 | 198 |  *   <li/> if the action was abborted return Action::failure
 | 
|---|
 | 199 |  *   <li/> if the action needs to save a state return a custom state object
 | 
|---|
 | 200 |  *   <li/> otherwise return Action::success
 | 
|---|
 | 201 |  *  </ul>
 | 
|---|
 | 202 |  *  <li/> performUndo():
 | 
|---|
 | 203 |  *  <ul>
 | 
|---|
 | 204 |  *   <li/> typecast the ActionState pointer to a Pointer to YourActionState if necessary
 | 
|---|
| [10fa1d] | 205 |  *   <li/> undo the action using the extra information and the Action's parameters in the state
 | 
|---|
| [2efa90] | 206 |  *   <li/> produce a new state that can be used for redoing and return it
 | 
|---|
 | 207 |  *  </ul>
 | 
|---|
 | 208 |  *  <li/> performRedo():
 | 
|---|
 | 209 |  *  <ul>
 | 
|---|
 | 210 |  *   <li/> take the ActionState produced by performUndo and typecast it to a pointer to YourActionState if necessary
 | 
|---|
| [10fa1d] | 211 |  *   <li/> redo the undone action using the extra information and the Action's parameters in the state
 | 
|---|
| [2efa90] | 212 |  *   <li/> produce a new state that can be used by performUndo() and return it
 | 
|---|
 | 213 |  *  </ul>
 | 
|---|
 | 214 |  * </ul>
 | 
|---|
 | 215 |  *
 | 
|---|
 | 216 |  * <H2> Advanced techniques </H2>
 | 
|---|
 | 217 |  *
 | 
|---|
 | 218 |  * <H3> Predefined Actions </H3>
 | 
|---|
 | 219 |  *
 | 
|---|
 | 220 |  * To make construction of actions easy there are some predefined actions. Namely these are
 | 
|---|
 | 221 |  * the MethodAction and the ErrorAction.
 | 
|---|
 | 222 |  *
 | 
|---|
 | 223 |  * The method action can be used to turn any function with empty arguments and return type void
 | 
|---|
 | 224 |  * into an action (also works for functors with those types). Simply pass the constructor for the
 | 
|---|
 | 225 |  * MethodAction a name to use for this action, the function to call inside the performCall()
 | 
|---|
 | 226 |  * method and a flag indicating if this action should be made retrievable inside the registry
 | 
|---|
 | 227 |  * (default is true). MethodActions always report themselves as changing the state of the
 | 
|---|
 | 228 |  * application but cannot be undone. i.e. calling MethodActions will always cause the ActionHistory
 | 
|---|
 | 229 |  * to be cleared.
 | 
|---|
 | 230 |  *
 | 
|---|
 | 231 |  * ErrorActions can be used to produce a short message using the Log() << Verbose() mechanism of
 | 
|---|
 | 232 |  * the molecuilder. Simply pass the constructor a name for the action, the message to show upon
 | 
|---|
 | 233 |  * calling this action and the flag for the registry (default is again true). Error action
 | 
|---|
 | 234 |  * report that they do not change the state of the application and are therefore not considered
 | 
|---|
 | 235 |  * for undo.
 | 
|---|
 | 236 |  *
 | 
|---|
 | 237 |  * <H3> Sequences of Actions and MakroActions </H3>
 | 
|---|
 | 238 |  *
 | 
|---|
 | 239 |  * <H4> Building sequences of Actions </H4>
 | 
|---|
 | 240 |  *
 | 
|---|
 | 241 |  * Actions can be chained to sequences using the ActionSequence class. Once an ActionSequence is
 | 
|---|
 | 242 |  * constructed it will be initially empty. Any Actions can then be added to the sequence using the
 | 
|---|
 | 243 |  * addAction() method of the ActionSequence class. The last added action can be removed using the
 | 
|---|
 | 244 |  * removeLastAction() method. If the construction of the sequence is done, you can use the
 | 
|---|
 | 245 |  * callAll() method. Each action called this way will register itself with the History to allow
 | 
|---|
| [a295d1] | 246 |  * separate undo of all actions in the sequence.
 | 
|---|
| [2efa90] | 247 |  *
 | 
|---|
 | 248 |  * <H4> Building larger Actions from simple ones </H4>
 | 
|---|
 | 249 |  *
 | 
|---|
 | 250 |  * Using the pre-defined class MakroAction it is possible to construct bigger actions from a sequence
 | 
|---|
 | 251 |  * of smaller ones. For this you first have to build a sequence of the actions using the ActionSequence
 | 
|---|
 | 252 |  * as described above. Then you can construct a MakroAction passing it a name, the sequence to use
 | 
|---|
 | 253 |  * and as usual a flag for the registry. You can then simply call the complete action-sequence through
 | 
|---|
 | 254 |  * this makro action using the normal interface. Other than with the direct use of the action sequence
 | 
|---|
 | 255 |  * only the complete MakroAction is registered inside the history, i.e. the complete sequence can be
 | 
|---|
 | 256 |  * undone at once. Also there are a few caveats you have to take care of when using the MakroAction:
 | 
|---|
 | 257 |  * <ul>
 | 
|---|
 | 258 |  *  <li/> All Actions as well as the sequence should exclusively belong to the MakroAction. This
 | 
|---|
 | 259 |  *        especially means, that the destruction of these objects should be handled by the MakroAction.
 | 
|---|
 | 260 |  *  <li/> none of the Actions inside the MakroAction should be registered with the registry, since the
 | 
|---|
 | 261 |  *        registry also assumes sole ownership of the actions.
 | 
|---|
 | 262 |  *  <li/> Do not remove or add actions from the sequence once the MakroAction has been constructed, since this
 | 
|---|
 | 263 |  *        might brake important assumptions for the undo/redo mechanism
 | 
|---|
 | 264 |  * </ul>
 | 
|---|
| [a295d1] | 265 |  *
 | 
|---|
 | 266 |  * <H3> Special kinds of Actions </H3>
 | 
|---|
 | 267 |  *
 | 
|---|
 | 268 |  * To make the usage of Actions more versatile there are two special kinds of actions defined,
 | 
|---|
 | 269 |  * that contain special mechanisms. These are defined inside the class Process, for actions that
 | 
|---|
 | 270 |  * take some time and indicate their own progress, and in the class Calculations for actions that
 | 
|---|
 | 271 |  * have a retrievable result.
 | 
|---|
 | 272 |  *
 | 
|---|
 | 273 |  * <H4> Processes </H4>
 | 
|---|
 | 274 |  *
 | 
|---|
 | 275 |  * Processes are Actions that might take some time and therefore contain special mechanisms
 | 
|---|
 | 276 |  * to indicate their progress to the user. If you want to implement a process you can follow the
 | 
|---|
 | 277 |  * guidelines for implementing actions. In addition to the normal Action constructor parameters,
 | 
|---|
 | 278 |  * you also need to define the number of steps the process takes to finish (use 0 if that number is
 | 
|---|
 | 279 |  * not known upon construction). At the beginning of your process you then simply call start() to
 | 
|---|
 | 280 |  * indicate that the process is taking up its work. You might also want to set the number of steps it
 | 
|---|
 | 281 |  * needs to finish, if it has changed since the last invocation/construction. You can use the
 | 
|---|
 | 282 |  * setMaxSteps() method for this. Then after each finished step of calulation simply call step(),
 | 
|---|
 | 283 |  * to let the indicators know that it should update itself. If the number of steps is not known
 | 
|---|
 | 284 |  * at the time of calculation, you should make sure the maxSteps field is set to 0, either through
 | 
|---|
 | 285 |  * the constructor or by using setMaxSteps(0). Indicators are required to handle both processes that
 | 
|---|
 | 286 |  * know the number of steps needed as well as processes that cannot predict when they will be finished.
 | 
|---|
 | 287 |  * Once your calculation is done call stop() to let every indicator know that the process is done with
 | 
|---|
 | 288 |  * the work and to let the user know.
 | 
|---|
 | 289 |  *
 | 
|---|
 | 290 |  * Indicators that want to know about processes need to implement the Observer class with all the
 | 
|---|
 | 291 |  * methods defined there. They can then globally sign on to all processes using the static
 | 
|---|
 | 292 |  * Process::AddObserver() method and remove themselves using the Process::RemoveObserver()
 | 
|---|
 | 293 |  * methods. When a process starts it will take care that the notification for this process
 | 
|---|
 | 294 |  * is invoked at the right time. Indicators should not try to observe a single process, but rather
 | 
|---|
 | 295 |  * be ready to observe the status of any kind of process using the methods described here.
 | 
|---|
 | 296 |  *
 | 
|---|
 | 297 |  * <H4> Calculations </H4>
 | 
|---|
 | 298 |  *
 | 
|---|
 | 299 |  * Calculations are special Actions that also return a result when called. Calculations are
 | 
|---|
 | 300 |  * always derived from Process, so that the progress of a calculation can be shown. Also
 | 
|---|
 | 301 |  * Calculations should not contain side-effects and not consider the undo mechanism.
 | 
|---|
 | 302 |  * When a Calculation is called using the Action mechanism this will cause it to calculate
 | 
|---|
 | 303 |  * the result and make it available using the getResult() method. Another way to have a Calculation
 | 
|---|
 | 304 |  * produce a result is by using the function-call operator. When this operator is used, the Calculation
 | 
|---|
 | 305 |  * will try to return a previously calculated and cached result and only do any actuall calculations
 | 
|---|
 | 306 |  * when no such result is available. You can delete the cached result using the reset() method.
 | 
|---|
| [2efa90] | 307 |  */
 | 
|---|
 | 308 | 
 | 
|---|
| [df32ee] | 309 | 
 | 
|---|
| [ef81b0] | 310 | /**
 | 
|---|
 | 311 |  * Base class for all actions.
 | 
|---|
 | 312 |  *
 | 
|---|
 | 313 |  * Actions describe something that has to be done.
 | 
|---|
 | 314 |  * Actions can be passed around, stored, performed and undone (Command-Pattern).
 | 
|---|
 | 315 |  */
 | 
|---|
| [65b6e0] | 316 | class Action
 | 
|---|
 | 317 | {
 | 
|---|
| [67e2b3] | 318 | friend class ActionSequence;
 | 
|---|
| [2efa90] | 319 | friend class ActionHistory;
 | 
|---|
| [1fa107] | 320 | public:
 | 
|---|
| [5b0b98] | 321 | 
 | 
|---|
| [4e145c] | 322 |   enum QueryOptions {Interactive, NonInteractive};
 | 
|---|
 | 323 | 
 | 
|---|
| [8a34392] | 324 |   /**
 | 
|---|
 | 325 |    * This type is used to store pointers to ActionStates while allowing multiple ownership
 | 
|---|
 | 326 |    */
 | 
|---|
| [5b0b98] | 327 |   typedef boost::shared_ptr<ActionState> state_ptr;
 | 
|---|
 | 328 | 
 | 
|---|
| [8a34392] | 329 |   /**
 | 
|---|
 | 330 |    * Standard constructor of Action Base class
 | 
|---|
 | 331 |    *
 | 
|---|
 | 332 |    * All Actions need to have a name. The second flag indicates, whether the action should
 | 
|---|
 | 333 |    * be registered with the ActionRegistry. If the Action is registered the name of the
 | 
|---|
 | 334 |    * Action needs to be unique for all Actions that are registered.
 | 
|---|
| [e4afb4] | 335 |    *
 | 
|---|
 | 336 |    * \note NO reference for \a _Traits as we do have to copy it, otherwise _Traits would have
 | 
|---|
 | 337 |    * to be present throughout the program's run.
 | 
|---|
 | 338 |    *
 | 
|---|
 | 339 |    * \param Traits information class to this action
 | 
|---|
 | 340 |    * \param _doRegister whether to register with ActionRegistry
 | 
|---|
| [8a34392] | 341 |    */
 | 
|---|
| [e4afb4] | 342 |   Action(const ActionTraits &_Traits, bool _doRegister=true);
 | 
|---|
| [65b6e0] | 343 |   virtual ~Action();
 | 
|---|
 | 344 | 
 | 
|---|
| [8a34392] | 345 |   /**
 | 
|---|
 | 346 |    * This method is used to call an action. The basic operations for the Action
 | 
|---|
 | 347 |    * are carried out and if necessary/possible the Action is added to the History
 | 
|---|
 | 348 |    * to allow for undo of this action.
 | 
|---|
 | 349 |    *
 | 
|---|
 | 350 |    * If the call needs to undone you have to use the History, to avoid destroying
 | 
|---|
 | 351 |    * invariants used by the History.
 | 
|---|
| [4e145c] | 352 |    *
 | 
|---|
 | 353 |    * Note that this call can be Interactive (i.e. a dialog will ask the user for
 | 
|---|
 | 354 |    * necessary information) and NonInteractive (i.e. the information will have to
 | 
|---|
 | 355 |    * be present already within the ValueStorage class or else a MissingArgumentException
 | 
|---|
 | 356 |    * is thrown)
 | 
|---|
| [8a34392] | 357 |    */
 | 
|---|
| [6bf52f] | 358 |   void call(enum QueryOptions state = Interactive);
 | 
|---|
| [67e2b3] | 359 | 
 | 
|---|
| [8a34392] | 360 |   /**
 | 
|---|
 | 361 |    * This method provides a flag that indicates if an undo mechanism is implemented
 | 
|---|
 | 362 |    * for this Action. If this is true, and this action was called last, you can
 | 
|---|
 | 363 |    * use the History to undo this action.
 | 
|---|
 | 364 |    */
 | 
|---|
| [65b6e0] | 365 |   virtual bool canUndo()=0;
 | 
|---|
| [8a34392] | 366 | 
 | 
|---|
 | 367 |   /**
 | 
|---|
 | 368 |    * This method provides a flag, that indicates if the Action changes the state of
 | 
|---|
 | 369 |    * the application in a way that needs to be undone for the History to work.
 | 
|---|
 | 370 |    *
 | 
|---|
 | 371 |    * If this is false the Action will not be added to the History upon calling. However
 | 
|---|
 | 372 |    * Actions called before this one will still be available for undo.
 | 
|---|
 | 373 |    */
 | 
|---|
| [67e2b3] | 374 |   virtual bool shouldUndo()=0;
 | 
|---|
| [65b6e0] | 375 | 
 | 
|---|
| [8a34392] | 376 |   /**
 | 
|---|
 | 377 |    * Indicates whether the Action can do it's work at the moment. If this
 | 
|---|
 | 378 |    * is false calling the action will result in a no-op.
 | 
|---|
 | 379 |    */
 | 
|---|
| [f9352d] | 380 |   virtual bool isActive();
 | 
|---|
 | 381 | 
 | 
|---|
| [8a34392] | 382 |   /**
 | 
|---|
 | 383 |    * Returns the name of the Action.
 | 
|---|
 | 384 |    */
 | 
|---|
| [e4afb4] | 385 |   const std::string getName();
 | 
|---|
| [cc04b7] | 386 | 
 | 
|---|
| [e4afb4] | 387 |   /**
 | 
|---|
 | 388 |    * Traits resemble all necessary information that "surrounds" an action, such as
 | 
|---|
 | 389 |    * its name (for ActionRegistry and as ref from string to instance and vice versa),
 | 
|---|
 | 390 |    * which menu, which position, what parameters, their types, if it is itself a
 | 
|---|
 | 391 |    * parameter and so on ...
 | 
|---|
 | 392 |    *
 | 
|---|
 | 393 |    * Note that is important that we do not use a reference here. We want to copy the
 | 
|---|
 | 394 |    * information in the Action's constructor and have it contained herein. Hence, we
 | 
|---|
 | 395 |    * also have our own copy constructor for ActionTraits. Information should be
 | 
|---|
 | 396 |    * encapsulated in the Action, no more references to the outside than absolutely
 | 
|---|
 | 397 |    * necessary.
 | 
|---|
 | 398 |    */
 | 
|---|
 | 399 |   const ActionTraits Traits;
 | 
|---|
| [df32ee] | 400 | 
 | 
|---|
| [41449c] | 401 |   /** Removes the static entities Action::success and Action::failure.
 | 
|---|
 | 402 |    * This is only to be called on the program's exit, i.e. in cleanUp(),
 | 
|---|
 | 403 |    * as these static entities are used throughout all Actions.
 | 
|---|
 | 404 |    */
 | 
|---|
 | 405 |   static void removeStaticStateEntities();
 | 
|---|
 | 406 | 
 | 
|---|
| [67e2b3] | 407 | protected:
 | 
|---|
| [8a34392] | 408 |   /**
 | 
|---|
 | 409 |    * This method is called by the History, when an undo is performed. It is
 | 
|---|
 | 410 |    * provided with the corresponding state produced by the performCall or
 | 
|---|
 | 411 |    * performRedo method and needs to provide a state that can be used for redo.
 | 
|---|
 | 412 |    */
 | 
|---|
| [2efa90] | 413 |   state_ptr undo(state_ptr);
 | 
|---|
| [8a34392] | 414 | 
 | 
|---|
 | 415 |   /**
 | 
|---|
 | 416 |    * This method is called by the Histor, when a redo is performed. It is
 | 
|---|
 | 417 |    * provided with the corresponding state produced by the undo method and
 | 
|---|
 | 418 |    * needs to produce a State that can then be used for another undo.
 | 
|---|
 | 419 |    */
 | 
|---|
| [2efa90] | 420 |   state_ptr redo(state_ptr);
 | 
|---|
 | 421 | 
 | 
|---|
| [8a34392] | 422 |   /**
 | 
|---|
 | 423 |    * This special state can be used to indicate that the Action was successfull
 | 
|---|
 | 424 |    * without providing a special state. Use this if your Action does not need
 | 
|---|
 | 425 |    * a speciallized state.
 | 
|---|
 | 426 |    */
 | 
|---|
| [5b0b98] | 427 |   static state_ptr success;
 | 
|---|
| [8a34392] | 428 | 
 | 
|---|
 | 429 |   /**
 | 
|---|
 | 430 |    * This special state can be returned, to indicate that the action could not do it's
 | 
|---|
 | 431 |    * work, was abborted by the user etc. If you return this state make sure to transactionize
 | 
|---|
 | 432 |    * your Actions and unroll the complete transaction before this is returned.
 | 
|---|
 | 433 |    */
 | 
|---|
| [5b0b98] | 434 |   static state_ptr failure;
 | 
|---|
| [67e2b3] | 435 | 
 | 
|---|
| [8a34392] | 436 |   /**
 | 
|---|
| [ba7418] | 437 |    * This creates the dialog requesting the information needed for this action from the user
 | 
|---|
 | 438 |    * via means of the user interface.
 | 
|---|
 | 439 |    */
 | 
|---|
| [047878] | 440 |   Dialog * createDialog();
 | 
|---|
 | 441 | 
 | 
|---|
 | 442 | private:
 | 
|---|
 | 443 | 
 | 
|---|
| [0b2ce9] | 444 |   /**
 | 
|---|
 | 445 |    * This is called internally before the Action::performCall(). It initializes the
 | 
|---|
 | 446 |    * necessary ActionParameters by retrieving the values from ValueStorage.
 | 
|---|
 | 447 |    */
 | 
|---|
 | 448 |   virtual void getParametersfromValueStorage()=0;
 | 
|---|
 | 449 | 
 | 
|---|
 | 450 |   /**
 | 
|---|
 | 451 |    * This is called internally before the action is processed. This adds necessary queries
 | 
|---|
 | 452 |    * to a given dialog to obtain parameters for the user for processing the action accordingly.
 | 
|---|
 | 453 |    * The dialog will be given to the user before Action::performCall() is initiated, values
 | 
|---|
 | 454 |    * are transfered via ValueStorage.
 | 
|---|
 | 455 |    */
 | 
|---|
| [047878] | 456 |   virtual Dialog * fillDialog(Dialog*)=0;
 | 
|---|
| [ba7418] | 457 | 
 | 
|---|
 | 458 |   /**
 | 
|---|
 | 459 |    * This is called internally when the call is being done. Implement this method to do the actual
 | 
|---|
| [8a34392] | 460 |    * work of the Action. Implement this in your Derived classes. Needs to return a state that can be
 | 
|---|
 | 461 |    * used to undo the action.
 | 
|---|
 | 462 |    */
 | 
|---|
| [5b0b98] | 463 |   virtual state_ptr performCall()=0;
 | 
|---|
| [8a34392] | 464 | 
 | 
|---|
 | 465 |   /**
 | 
|---|
 | 466 |    * This is called internally when the undo process is chosen. This Method should use the state
 | 
|---|
 | 467 |    * produced by the performCall method to return the state of the application to the state
 | 
|---|
 | 468 |    * it had before the Action.
 | 
|---|
 | 469 |    */
 | 
|---|
| [5b0b98] | 470 |   virtual state_ptr performUndo(state_ptr)=0;
 | 
|---|
| [8a34392] | 471 | 
 | 
|---|
 | 472 |   /**
 | 
|---|
 | 473 |    * This is called internally when the redo process is chosen. This method shoudl use the state
 | 
|---|
 | 474 |    * produced by the performUndo method to return the application to the state it should have after
 | 
|---|
 | 475 |    * the action.
 | 
|---|
 | 476 |    *
 | 
|---|
 | 477 |    * Often this method can be implement to re-use the performCall method. However if user interaction
 | 
|---|
 | 478 |    * or further parameters are needed, those should be taken from the state and not query the user
 | 
|---|
 | 479 |    * again.
 | 
|---|
 | 480 |    */
 | 
|---|
| [5b0b98] | 481 |   virtual state_ptr performRedo(state_ptr)=0;
 | 
|---|
| [65b6e0] | 482 | };
 | 
|---|
 | 483 | 
 | 
|---|
| [67e2b3] | 484 | /**
 | 
|---|
 | 485 |  * This class can be used by actions to save the state.
 | 
|---|
 | 486 |  *
 | 
|---|
 | 487 |  * It is implementing a memento pattern. The base class is completely empty,
 | 
|---|
 | 488 |  * since no general state internals can be given. The Action performing
 | 
|---|
 | 489 |  * the Undo should downcast to the apropriate type.
 | 
|---|
 | 490 |  */
 | 
|---|
 | 491 | class ActionState{
 | 
|---|
 | 492 | public:
 | 
|---|
 | 493 |   ActionState(){}
 | 
|---|
 | 494 |   virtual ~ActionState(){}
 | 
|---|
 | 495 | };
 | 
|---|
 | 496 | 
 | 
|---|
| [0b2ce9] | 497 | /**
 | 
|---|
 | 498 |  * This class can be used by actions to contain parameters.
 | 
|---|
 | 499 |  *
 | 
|---|
 | 500 |  * The base class is completely empty, since no general parameters can be given. The
 | 
|---|
 | 501 |  * Action performing the function should construct its own parameter class derived
 | 
|---|
 | 502 |  * from it.
 | 
|---|
 | 503 |  */
 | 
|---|
 | 504 | class ActionParameters{
 | 
|---|
 | 505 | public:
 | 
|---|
 | 506 |   ActionParameters(){}
 | 
|---|
 | 507 |   virtual ~ActionParameters(){}
 | 
|---|
 | 508 | };
 | 
|---|
 | 509 | 
 | 
|---|
| [56f73b] | 510 | #endif /* ACTION_HPP_ */
 | 
|---|