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