00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef UNDOACTION_H 00011 #define UNDOACTION_H 00012 00013 #include <QtCore/QObject> 00014 #include <QtGui/QUndoCommand> 00015 00016 class SessionItem; 00017 00018 /** 00019 * Base class for all undo actions added to the undo stack in a view. 00020 * 00021 * This class can be subclassed to create custom undo actions that will be 00022 * executed when the user clicks the Undo or Redo toolbar buttons. Subclasses 00023 * must override the executeUndo() and executeRedo() methods to perform the 00024 * undo or redo. The inherited QUndoCommand::undo() and QUndoCommand::redo() 00025 * method obligations are implemented in this class to emit signals before and 00026 * after the undo or redo is performed and to change the mouse cursor to a 00027 * wait cursor while the undo or redo is performed. 00028 * 00029 * Since undo actions typically perform operations on a SessionItem, an 00030 * UndoAction optionally stores a session item by its ID and provides the 00031 * getSessionItem() accessor method as a convenience to retrieve the 00032 * SessionItem pointer when performing the undo or redo. Since the stored 00033 * session item may be deleted when performing the undo or redo, the 00034 * sessionItemChanged() signal is provided to notify the application that the 00035 * session item ID of other actions may need to be updated. 00036 */ 00037 class UndoAction : public QObject, public QUndoCommand 00038 { 00039 Q_OBJECT 00040 00041 public: 00042 /** 00043 * Creates the undo action. 00044 * 00045 * @param bRedoOnAdd 00046 * By default the redo() method is called when the action is added 00047 * to an undo stack in a view. If this value is \c false the 00048 * executeRedo() method is not called when the action is added to 00049 * the undo stack. 00050 */ 00051 UndoAction(bool bRedoOnAdd = false); 00052 00053 /** 00054 * Creates the undo action. 00055 * 00056 * @param pItem 00057 * The session item to store by its ID. 00058 * @param bRedoOnAdd 00059 * By default the redo() method is called when the action is added 00060 * to an undo stack in a view. If this value is \c false the 00061 * executeRedo() method is not called when the action is added to 00062 * the undo stack. 00063 * 00064 * @see setSessionItem() 00065 */ 00066 UndoAction(SessionItem* pItem, bool bRedoOnAdd = false); 00067 00068 /** 00069 * Destroys the undo action. 00070 */ 00071 virtual ~UndoAction(); 00072 00073 /** 00074 * Stores the ID for a given session item. 00075 * 00076 * @param pItem 00077 * The session item for which to store its ID. 00078 * 00079 * @see SessionItem::getId() 00080 */ 00081 void setSessionItem(SessionItem* pItem); 00082 00083 /** 00084 * Returns a SessionItem pointer for the stored ID. 00085 * 00086 * This method retrieves the session item for the stored ID value by 00087 * calling SessionManager::getSessionItem(). 00088 * 00089 * @warning The session manager does not currently store graphic objects or 00090 * the graphic layers contained in a product view. Subclasses 00091 * that need to access these objects should additionally store the 00092 * ID for the parent graphic layer in a spatial data view or the 00093 * ID for the product view and the graphic layer contained in the 00094 * product view. This method should be overridden to obtain the 00095 * parent session item from the session manager and used to access 00096 * the desired session item not contained in the session manager. 00097 * 00098 * @return The SessionItem pointer for the session item with the stored 00099 * ID. Returns \c NULL if no session item can be found in the 00100 * session manager with the stored ID. 00101 * 00102 * @see getSessionItemId() 00103 */ 00104 virtual SessionItem* getSessionItem() const; 00105 00106 /** 00107 * Returns a stored session item ID. 00108 * 00109 * @return The ID for the session item that was passed into the 00110 * constructor or in setSessionItem(). 00111 * 00112 * @see getSessionItem() 00113 */ 00114 const std::string& getSessionItemId() const; 00115 00116 /** 00117 * Updates the stored session item ID. 00118 * 00119 * This method is called by the application when the sessionItemChanged() 00120 * signal is emitted by another undo action. This is typically emitted 00121 * when another action recreates a session item during undo or redo 00122 * execution. 00123 * 00124 * This implementation compares the old ID with the stored member and 00125 * updates the member to the new ID if the old ID matches the stored 00126 * member. This method should be overridden in a subclass if the subclass 00127 * needs to store an additional session item ID. 00128 * 00129 * @param oldId 00130 * The previous ID for the session item that was deleted. 00131 * @param newId 00132 * The ID for the recreated session item. 00133 */ 00134 virtual void updateSessionItem(const std::string& oldId, const std::string& newId); 00135 00136 /** 00137 * Performs the undo to restore the application to its previous state. 00138 * 00139 * This method is called by the application when the user clicks the Undo 00140 * button on the toolbar. It provides a default implementation of the 00141 * virtual QUndoCommand::undo() method. It emits the aboutToUndo() signal, 00142 * changes the mouse cursor to a wait cursor, calls executeUndo(), restores 00143 * the mouse cursor, and emits the undoComplete() signal. 00144 */ 00145 void undo(); 00146 00147 /** 00148 * Performs the redo to return the application to its modified state. 00149 * 00150 * This method is called by the application when the user clicks the Redo 00151 * button on the toolbar. It provides a default implementation of the 00152 * virtual QUndoCommand::redo() method. It emits the aboutToRedo() signal, 00153 * changes the mouse cursor to a wait cursor, calls executeRedo(), restores 00154 * the mouse cursor, and emits the redoComplete() signal. 00155 * 00156 * If this method is called in response to the undo action being added to 00157 * the undo stack and the \em bRedoOnAdd parameter passed into the 00158 * constructor is \c false, this method does nothing. 00159 */ 00160 void redo(); 00161 00162 signals: 00163 /** 00164 * Emitted just before the undo is performed. 00165 * 00166 * This signal is emitted in the default implementation of undo() before 00167 * the executeUndo() method is called. 00168 * 00169 * @see undoComplete() 00170 */ 00171 void aboutToUndo(); 00172 00173 /** 00174 * Emitted after the undo is performed. 00175 * 00176 * This signal is emitted in the default implementation of undo() after 00177 * the executeUndo() method is called. 00178 * 00179 * @see aboutToUndo() 00180 */ 00181 void undoComplete(); 00182 00183 /** 00184 * Emitted just before the redo is performed. 00185 * 00186 * This signal is emitted in the default implementation of redo() before 00187 * the executeRedo() method is called. 00188 * 00189 * @see redoComplete() 00190 */ 00191 void aboutToRedo(); 00192 00193 /** 00194 * Emitted after the redo is performed. 00195 * 00196 * This signal is emitted in the default implementation of redo() after 00197 * the executeRedo() method is called. 00198 * 00199 * @see aboutToRedo() 00200 */ 00201 void redoComplete(); 00202 00203 /** 00204 * Emitted when a session item is deleted and recreated, thereby changing 00205 * its ID. 00206 * 00207 * This signal is emitted in subclasses that destroy and recreate session 00208 * items during undo and redo execution. After destroying the session item 00209 * the action should maintain the old ID string to use when emitting this 00210 * signal after recreating the session item. 00211 * 00212 * @param oldId 00213 * The ID of the deleted session item. 00214 * @param newId 00215 * The ID of the recreated session item, obtained with 00216 * SessionItem::getId(). 00217 * 00218 * @see updateSessionItem() 00219 */ 00220 void sessionItemChanged(const std::string& oldId, const std::string& newId); 00221 00222 protected: 00223 /** 00224 * Performs the undo to restore the application to its previous state. 00225 * 00226 * This method must be overridden in subclasses to perform specific changes 00227 * to the application. It is called from the default implementation of the 00228 * undo() method. 00229 */ 00230 virtual void executeUndo() = 0; 00231 00232 /** 00233 * Performs the redo to return the application to its modified state. 00234 * 00235 * This method must be overridden in subclasses to perform specific changes 00236 * to the application. It is called from the default implementation of the 00237 * redo() method. 00238 */ 00239 virtual void executeRedo() = 0; 00240 00241 private: 00242 UndoAction(const UndoAction& rhs); 00243 UndoAction& operator=(const UndoAction& rhs); 00244 std::string mSessionItemId; 00245 bool mRedo; 00246 }; 00247 00248 #endif