| [750cff] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | /** | 
|---|
|  | 9 | * \file action.dox | 
|---|
|  | 10 | * | 
|---|
|  | 11 | * Created on: Oct 31, 2011 | 
|---|
|  | 12 | *    Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
|  | 15 | /** | 
|---|
|  | 16 | * \page howto-action Action Howto | 
|---|
|  | 17 | * | 
|---|
|  | 18 | * \section howto-action-introduction Introduction | 
|---|
|  | 19 | * | 
|---|
|  | 20 | * Actions are used in object oriented design as a replacement for callback functions. | 
|---|
|  | 21 | * In most ways Actions can be used in the same way that callbacks were used in non | 
|---|
|  | 22 | * OO-Systems, but can contain support for several extra mechanism such as undo/redo | 
|---|
|  | 23 | * or progress indicators. | 
|---|
|  | 24 | * | 
|---|
|  | 25 | * The main purpose of an action class is to contain small procedures, that can be repeatedly | 
|---|
|  | 26 | * called. These procedures can also be stored, passed around, so that the execution of an | 
|---|
|  | 27 | * action can happen quite far away from the place of creation. For a detailed description of | 
|---|
|  | 28 | * the Action pattern see GOF:1996. | 
|---|
|  | 29 | * | 
|---|
|  | 30 | * \subsection howto-action-usage How to use an action | 
|---|
|  | 31 | * | 
|---|
|  | 32 | * The process of using an action is as easy as calling the call() method of the action. The | 
|---|
|  | 33 | * action will then do whatever it is supposed to do. If it is an action that can be undone, it | 
|---|
|  | 34 | * will also register itself in the history to make itself available for undo. To undo the last | 
|---|
|  | 35 | * action, you can either use the undoLast() method inside the ActionHistory class or call the | 
|---|
|  | 36 | * UndoAction also provided by the ActionHistory. If an action was undone it will be available for | 
|---|
|  | 37 | * redo, using the redoLast() method of the ActionHistory or the RedoAction also provided by this | 
|---|
|  | 38 | * class. To check whether undo/redo is available at any moment you can use the hasUndo() or | 
|---|
|  | 39 | * hasRedo() method respectively. | 
|---|
|  | 40 | * | 
|---|
|  | 41 | * Note that an Action always has two functions createDialog() and performCall(). The former | 
|---|
|  | 42 | * returns a Dialog filled with query...() functions for all information that we need from the | 
|---|
|  | 43 | * user. The latter must not contain any interaction but just uses these values (which are | 
|---|
|  | 44 | * temporarily stored by class ValueStorage) to perform the Action. | 
|---|
|  | 45 | * | 
|---|
|  | 46 | * Furthermore, there is a global action function that makes the action callable with already | 
|---|
|  | 47 | * present parameters (i.e. without user interaction and for internal use within the code only). | 
|---|
|  | 48 | * This function is basically just a macro, that puts the parameters into the ValueStorage and | 
|---|
|  | 49 | * calls Action::call(Action::NonInteractive). | 
|---|
|  | 50 | * | 
|---|
|  | 51 | * Actions can be set to be active or inactive. If an action is set to inactive it is signaling, that | 
|---|
|  | 52 | * some condition necessary for this action to be executed is not currently met. For example the | 
|---|
|  | 53 | * UndoAction will set itself to inactive, when there is no action at that time that can be undone. | 
|---|
|  | 54 | * Using call() on an inactive Action results in a no-op. You can query the state of an action using | 
|---|
|  | 55 | * the isActive() method. | 
|---|
|  | 56 | * | 
|---|
|  | 57 | * The undo capabilities of actions come in three types as signaled by two boolean flags (one | 
|---|
|  | 58 | * combination of these flags is left empty as can be seen later). | 
|---|
|  | 59 | * \li The first flag indicates if the undo mechanism for this action should be considered at all, i.e. | 
|---|
|  | 60 | *   if the state of the application changes in a way that needs to be reverted. Actions that should | 
|---|
|  | 61 | *   consider the undo mechanism are for example adding a molecule, moving atoms, changing | 
|---|
|  | 62 | *   the name of a molecule etc. Changing the View-Area on the other hand should be an action that | 
|---|
|  | 63 | *   does not consider the undo mechanism. This flag can be queried using the shouldUndo() method. | 
|---|
|  | 64 | * | 
|---|
|  | 65 | * \li The second flag indicates whether the changes can be undo for this action. If this flag is true | 
|---|
|  | 66 | *   the action will be made available for undo using the ActionHistory class and the actions of this | 
|---|
|  | 67 | *   class. If this flag is false while the shoudlUndo() flag is true this means that this action | 
|---|
|  | 68 | *   changes the state of the application changes in a way that cannot be undone, but might cause | 
|---|
|  | 69 | *   the undo of previous actions to fail. In this case the whole History is cleared, as to keep | 
|---|
|  | 70 | *   the state of the application intact by avoiding dangerous undos. This flag can be queried | 
|---|
|  | 71 | *   using the canUndo() method. | 
|---|
|  | 72 | * | 
|---|
|  | 73 | * Each action has a name, that can be used to identify it throughout the run of the application. | 
|---|
|  | 74 | * This name can be retrieved using the getName() method. Most actions also register themselves with | 
|---|
|  | 75 | * a global structure, called the ActionRegistry. Actions that register themselves need to have a | 
|---|
|  | 76 | * unique name for the whole application. If the name is known these actions can be retrieved from | 
|---|
|  | 77 | * the registry by their name and then be used as normal. | 
|---|
|  | 78 | * | 
|---|
|  | 79 | * \section howto-action-add Building your own actions | 
|---|
|  | 80 | * | 
|---|
|  | 81 | * Building actions is easy. Each specific ...Action is derived from the base class Action. | 
|---|
|  | 82 | * In order to create all the reoccuring stuff, macros have been created which you can simply | 
|---|
|  | 83 | * include and then don't need to worry about it. | 
|---|
|  | 84 | * There are three major virtual functions: performCall(), performUndo(), performRedo() which | 
|---|
|  | 85 | * you have to write, to equip your action with some actual capabilities. | 
|---|
|  | 86 | * Each Action definition and implementation consists of of three files: | 
|---|
|  | 87 | * -# cpp: contains performX() which you have to write, also some boilerplate functions which are | 
|---|
|  | 88 | *         constructed automatically when including your def and "Actions/action_impl_pre.hpp" | 
|---|
|  | 89 | * -# hpp: boilerplate definitions created simply by including your def and | 
|---|
|  | 90 | *         "Actions/action_impl_header.hpp" | 
|---|
|  | 91 | * -# def: macro definitions of all your parameters and additional variables needed for the state, | 
|---|
|  | 92 | *         also name and category and token of your action. | 
|---|
|  | 93 | * | 
|---|
|  | 94 | * Best thing to do is look at one of the already present triples and you should soon understand | 
|---|
|  | 95 | * what you have to add: | 
|---|
|  | 96 | * -# pick the right category, i.e. the right folder in src/Actions | 
|---|
|  | 97 | * -# pick the right name | 
|---|
|  | 98 | * -# decide which parameters your actions need and what the type, the variable name and the token | 
|---|
|  | 99 | *    to reference it from the command-line should be. Check whether already present and fitting | 
|---|
|  | 100 | *    tokens exists, e.g. "position" as token for a Vector representing a position. | 
|---|
|  | 101 | * -# consider which additional information you need to undo your action | 
|---|
|  | 102 | * -# don't forget to include your .def file followed by "action_impl_pre.hpp" in .cpp or | 
|---|
|  | 103 | *    "action_impl_header.hpp" in the .hpp | 
|---|
|  | 104 | * -# continue to write the functionality of your action in performCall(), undo and redo in performUndo() | 
|---|
|  | 105 | *    and performRedo(). | 
|---|
|  | 106 | * -# You should indicate whether the action supports undo by implementing the shouldUndo() and | 
|---|
|  | 107 | *    canUndo() methods to return the appropriate flags. | 
|---|
|  | 108 | * | 
|---|
|  | 109 | * \subsection howto-action-add-notes Specific notes on the macros | 
|---|
|  | 110 | * | 
|---|
|  | 111 | * The following functions are created by the macros, i.e. you don't need to worry about it: | 
|---|
|  | 112 | * | 
|---|
|  | 113 | * Any user interaction should be placed into the dialog returned by fillDialog(). | 
|---|
|  | 114 | * | 
|---|
|  | 115 | * Also, create the global function to allow for easy calling of your function internally (i.e. | 
|---|
|  | 116 | * without user interaction). It should have the name of the Action class without the suffix Action. | 
|---|
|  | 117 | * | 
|---|
|  | 118 | * The constructor of your derived class also needs to call the Base constructor, passing it the | 
|---|
|  | 119 | * name of the Action and a flag indicating whether this action should be made available in the | 
|---|
|  | 120 | * registry. WARNING: Do not use the virtual getName() method of the derived action to provide the | 
|---|
|  | 121 | * constructor with the name, even if you overloaded this method to return a constant. Doing this | 
|---|
|  | 122 | * will most likely not do what you think it does (see: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.5 | 
|---|
|  | 123 | * if you want to know why this wont work) | 
|---|
|  | 124 | * | 
|---|
|  | 125 | * \subsection howto-action-add-undo Interfacing your Action with the Undo mechanism | 
|---|
|  | 126 | * | 
|---|
|  | 127 | * The performX() methods need to comply to a simple standard to allow for undo and redo. The first | 
|---|
|  | 128 | * convention in this standard concerns the return type. All methods that handle calling, undoing | 
|---|
|  | 129 | * or redoing return an object of Action::state_ptr. This is a smart pointer to a State object, that | 
|---|
|  | 130 | * can be used to store state information that is needed by your action for later redo. A rename | 
|---|
|  | 131 | * Action for example would need to store which object has been renamed and what the old name was. | 
|---|
|  | 132 | * A move Action on the other hand would need to store the object that has been moved as well as the | 
|---|
|  | 133 | * old position. If your Action does not need to store any kind of information for redo you can | 
|---|
|  | 134 | * simply return Action::success and skip the rest of this paragraph. If your action has been | 
|---|
|  | 135 | * abborted you can return Action::failure, which indicates to the history mechanism that this | 
|---|
|  | 136 | * action should not be stored. | 
|---|
|  | 137 | * | 
|---|
|  | 138 | * If your Action needs any kind of information to undo its execution, you need to store this | 
|---|
|  | 139 | * information in the state that is returned by the performCall() method. Since no assumptions | 
|---|
|  | 140 | * can be made on the type or amount of information the ActionState base class is left empty. | 
|---|
|  | 141 | * To use this class you need to derive a YourActionState class from the ActionState base class | 
|---|
|  | 142 | * adding your data fields and accessor functions. Upon undo the ActionState object produced | 
|---|
|  | 143 | * by the corresponding performCall() is then passed to the performUndo() method which should | 
|---|
|  | 144 | * typecast the ActionState to the appropriate sub class, undo all the changes and produce | 
|---|
|  | 145 | * a State object that can be used to redo the action if neccessary. This new state object is | 
|---|
|  | 146 | * then used if the redo mechanism is invoked and passed to the performRedo() function, which | 
|---|
|  | 147 | * again produces a State that can be used for performUndo(). | 
|---|
|  | 148 | * | 
|---|
|  | 149 | * \section howto-action-implementation Outline of the implementation of Actions | 
|---|
|  | 150 | * | 
|---|
|  | 151 | * To sum up the actions necessary to build actions here is a brief outline of things methioned | 
|---|
|  | 152 | * in the last paragraphs: | 
|---|
|  | 153 | * | 
|---|
|  | 154 | * \subsection howto-action-implementation-notes Specific notes on the macros | 
|---|
|  | 155 | * | 
|---|
|  | 156 | *  \li create parameter tupels (type, token, reference), put into def. Access them later in | 
|---|
|  | 157 | *        the performX() via the structure params.###. | 
|---|
|  | 158 | *  \li think of name, category and token for your action, put into def | 
|---|
|  | 159 | *  \li create additional state variables tupels (type, reference) for storing extra information | 
|---|
|  | 160 | *        that you need for undo/redo in the ActionState. You can always access the parameters | 
|---|
|  | 161 | *        of your Action by state.params.### (i.e. they are copied to the state by default). | 
|---|
|  | 162 | *  \li implement performCall(), first line should be calling of getParametersfromValueStorage(). | 
|---|
|  | 163 | *  \li performUndo(), performRedo() | 
|---|
|  | 164 | *  \li implement the functions that return the flags for the undo mechanism, i.e. true/false. | 
|---|
|  | 165 | * | 
|---|
|  | 166 | * \subsection howto-action-implementation-perform-x Implementing performX() methods | 
|---|
|  | 167 | * | 
|---|
|  | 168 | *  \li performCall(): | 
|---|
|  | 169 | *   -# first line should be calling of getParametersfromValueStorage(). | 
|---|
|  | 170 | *   -# Access your parameters by the structure params.### (where ### stands for the reference/ | 
|---|
|  | 171 | *         variable name chosen in the tupel). | 
|---|
|  | 172 | *   -# do whatever is needed to make the action work | 
|---|
|  | 173 | *   -# if the action was abborted return Action::failure | 
|---|
|  | 174 | *   -# if the action needs to save a state return a custom state object | 
|---|
|  | 175 | *   -# otherwise return Action::success | 
|---|
|  | 176 | *  \li performUndo(): | 
|---|
|  | 177 | *   -# typecast the ActionState pointer to a Pointer to YourActionState if necessary | 
|---|
|  | 178 | *   -# undo the action using the extra information and the Action's parameters in the state | 
|---|
|  | 179 | *   -# produce a new state that can be used for redoing and return it | 
|---|
|  | 180 | *  \li performRedo(): | 
|---|
|  | 181 | *   -# take the ActionState produced by performUndo and typecast it to a pointer to YourActionState if necessary | 
|---|
|  | 182 | *   -# redo the undone action using the extra information and the Action's parameters in the state | 
|---|
|  | 183 | *   -# produce a new state that can be used by performUndo() and return it | 
|---|
|  | 184 | * | 
|---|
|  | 185 | * \section howto-action-advanced Advanced techniques | 
|---|
|  | 186 | * | 
|---|
|  | 187 | * \subsection howto-action-advanced-predefined Predefined Actions | 
|---|
|  | 188 | * | 
|---|
|  | 189 | * To make construction of actions easy there are some predefined actions. Namely these are | 
|---|
|  | 190 | * the MethodAction and the ErrorAction. | 
|---|
|  | 191 | * | 
|---|
|  | 192 | * The method action can be used to turn any function with empty arguments and return type void | 
|---|
|  | 193 | * into an action (also works for functors with those types). Simply pass the constructor for the | 
|---|
|  | 194 | * MethodAction a name to use for this action, the function to call inside the performCall() | 
|---|
|  | 195 | * method and a flag indicating if this action should be made retrievable inside the registry | 
|---|
|  | 196 | * (default is true). MethodActions always report themselves as changing the state of the | 
|---|
|  | 197 | * application but cannot be undone. i.e. calling MethodActions will always cause the ActionHistory | 
|---|
|  | 198 | * to be cleared. | 
|---|
|  | 199 | * | 
|---|
|  | 200 | * ErrorActions can be used to produce a short message using the Log() << Verbose() mechanism of | 
|---|
|  | 201 | * the molecuilder. Simply pass the constructor a name for the action, the message to show upon | 
|---|
|  | 202 | * calling this action and the flag for the registry (default is again true). Error action | 
|---|
|  | 203 | * report that they do not change the state of the application and are therefore not considered | 
|---|
|  | 204 | * for undo. | 
|---|
|  | 205 | * | 
|---|
|  | 206 | * \subsection howto-action-advanced-sequences Sequences of Actions and MakroActions | 
|---|
|  | 207 | * | 
|---|
|  | 208 | * \paragraph howto-action-advanced-sequences-add Building sequences of Actions | 
|---|
|  | 209 | * | 
|---|
|  | 210 | * Actions can be chained to sequences using the ActionSequence class. Once an ActionSequence is | 
|---|
|  | 211 | * constructed it will be initially empty. Any Actions can then be added to the sequence using the | 
|---|
|  | 212 | * addAction() method of the ActionSequence class. The last added action can be removed using the | 
|---|
|  | 213 | * removeLastAction() method. If the construction of the sequence is done, you can use the | 
|---|
|  | 214 | * callAll() method. Each action called this way will register itself with the History to allow | 
|---|
|  | 215 | * separate undo of all actions in the sequence. | 
|---|
|  | 216 | * | 
|---|
|  | 217 | * \paragraph howto-action-advanced-sequences-build-larger Building larger Actions from simple ones | 
|---|
|  | 218 | * | 
|---|
|  | 219 | * Using the pre-defined class MakroAction it is possible to construct bigger actions from a sequence | 
|---|
|  | 220 | * of smaller ones. For this you first have to build a sequence of the actions using the ActionSequence | 
|---|
|  | 221 | * as described above. Then you can construct a MakroAction passing it a name, the sequence to use | 
|---|
|  | 222 | * and as usual a flag for the registry. You can then simply call the complete action-sequence through | 
|---|
|  | 223 | * this makro action using the normal interface. Other than with the direct use of the action sequence | 
|---|
|  | 224 | * only the complete MakroAction is registered inside the history, i.e. the complete sequence can be | 
|---|
|  | 225 | * undone at once. Also there are a few caveats you have to take care of when using the MakroAction: | 
|---|
|  | 226 | *  -# All Actions as well as the sequence should exclusively belong to the MakroAction. This | 
|---|
|  | 227 | *        especially means, that the destruction of these objects should be handled by the MakroAction. | 
|---|
|  | 228 | *  -# none of the Actions inside the MakroAction should be registered with the registry, since the | 
|---|
|  | 229 | *        registry also assumes sole ownership of the actions. | 
|---|
|  | 230 | *  -# Do not remove or add actions from the sequence once the MakroAction has been constructed, since this | 
|---|
|  | 231 | *        might brake important assumptions for the undo/redo mechanism | 
|---|
|  | 232 | * | 
|---|
|  | 233 | * \section howto-action-special Special kinds of Actions | 
|---|
|  | 234 | * | 
|---|
|  | 235 | * To make the usage of Actions more versatile there are two special kinds of actions defined, | 
|---|
|  | 236 | * that contain special mechanisms. These are defined inside the class Process, for actions that | 
|---|
|  | 237 | * take some time and indicate their own progress, and in the class Calculations for actions that | 
|---|
|  | 238 | * have a retrievable result. | 
|---|
|  | 239 | * | 
|---|
|  | 240 | * \subsection howto-action-special-process Processes | 
|---|
|  | 241 | * | 
|---|
|  | 242 | * Processes are Actions that might take some time and therefore contain special mechanisms | 
|---|
|  | 243 | * to indicate their progress to the user. If you want to implement a process you can follow the | 
|---|
|  | 244 | * guidelines for implementing actions. In addition to the normal Action constructor parameters, | 
|---|
|  | 245 | * you also need to define the number of steps the process takes to finish (use 0 if that number is | 
|---|
|  | 246 | * not known upon construction). At the beginning of your process you then simply call start() to | 
|---|
|  | 247 | * indicate that the process is taking up its work. You might also want to set the number of steps it | 
|---|
|  | 248 | * needs to finish, if it has changed since the last invocation/construction. You can use the | 
|---|
|  | 249 | * setMaxSteps() method for this. Then after each finished step of calulation simply call step(), | 
|---|
|  | 250 | * to let the indicators know that it should update itself. If the number of steps is not known | 
|---|
|  | 251 | * at the time of calculation, you should make sure the maxSteps field is set to 0, either through | 
|---|
|  | 252 | * the constructor or by using setMaxSteps(0). Indicators are required to handle both processes that | 
|---|
|  | 253 | * know the number of steps needed as well as processes that cannot predict when they will be finished. | 
|---|
|  | 254 | * Once your calculation is done call stop() to let every indicator know that the process is done with | 
|---|
|  | 255 | * the work and to let the user know. | 
|---|
|  | 256 | * | 
|---|
|  | 257 | * Indicators that want to know about processes need to implement the Observer class with all the | 
|---|
|  | 258 | * methods defined there. They can then globally sign on to all processes using the static | 
|---|
|  | 259 | * Process::AddObserver() method and remove themselves using the Process::RemoveObserver() | 
|---|
|  | 260 | * methods. When a process starts it will take care that the notification for this process | 
|---|
|  | 261 | * is invoked at the right time. Indicators should not try to observe a single process, but rather | 
|---|
|  | 262 | * be ready to observe the status of any kind of process using the methods described here. | 
|---|
|  | 263 | * | 
|---|
|  | 264 | * \subsection howto-action-special-calculation Calculations | 
|---|
|  | 265 | * | 
|---|
|  | 266 | * Calculations are special Actions that also return a result when called. Calculations are | 
|---|
|  | 267 | * always derived from Process, so that the progress of a calculation can be shown. Also | 
|---|
|  | 268 | * Calculations should not contain side-effects and not consider the undo mechanism. | 
|---|
|  | 269 | * When a Calculation is called using the Action mechanism this will cause it to calculate | 
|---|
|  | 270 | * the result and make it available using the getResult() method. Another way to have a Calculation | 
|---|
|  | 271 | * produce a result is by using the function-call operator. When this operator is used, the Calculation | 
|---|
|  | 272 | * will try to return a previously calculated and cached result and only do any actuall calculations | 
|---|
|  | 273 | * when no such result is available. You can delete the cached result using the reset() method. | 
|---|
|  | 274 | * | 
|---|
|  | 275 | * | 
|---|
|  | 276 | * \date 2011-10-31 | 
|---|
|  | 277 | */ | 
|---|