Rev 1142 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1111 | tsoots | 1 | /*************************************************************************** |
2 | * Copyright (C) 2005 by Riku Leino * |
||
3 | * tsoots@gmail.com * |
||
4 | * * |
||
5 | * This program is free software; you can redistribute it and/or modify * |
||
6 | * it under the terms of the GNU General Public License as published by * |
||
7 | * the Free Software Foundation; either version 2 of the License, or * |
||
8 | * (at your option) any later version. * |
||
9 | * * |
||
10 | * This program is distributed in the hope that it will be useful, * |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
13 | * GNU General Public License for more details. * |
||
14 | * * |
||
15 | * You should have received a copy of the GNU General Public License * |
||
16 | * along with this program; if not, write to the * |
||
17 | * Free Software Foundation, Inc., * |
||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
19 | ***************************************************************************/ |
||
20 | |||
21 | #ifndef UNDOMANAGER_H |
||
22 | #define UNDOMANAGER_H |
||
23 | |||
24 | #include <vector> |
||
25 | #include <utility> |
||
26 | #include <qobject.h> |
||
27 | #include <qpixmap.h> |
||
28 | #include "undogui.h" |
||
29 | #include "undostate.h" |
||
30 | #include "undoobject.h" |
||
31 | |||
32 | typedef std::pair<UndoObject*, UndoState*> ActionPair; |
||
33 | typedef std::vector<ActionPair> ActionList; |
||
34 | typedef std::pair<ActionList::iterator, ActionList> StackPair; |
||
35 | typedef QMap<QString, StackPair> StackMap; |
||
36 | |||
37 | /** |
||
38 | * @brief UndoManager handles the undo stack. |
||
39 | * |
||
40 | * UndoManager is the engine of the undo/redo handling. When ever an undoable |
||
41 | * action happens an UndoState object will be sent to the UndoManager which then |
||
42 | * stores the state in the undo stack. When an undo is requested the state is |
||
43 | * send back to it's creator which is then responsible for doing the actual undo or |
||
44 | * redo. |
||
45 | * |
||
46 | * UndoManager also handles the UndoGuis by linking them together and reporting |
||
47 | * all undo/redo actions to the guis for them to update the visual representation. |
||
48 | * For this to work all UndoGuis need to be registered to the UndoManager with the |
||
49 | * registerGui() method. |
||
50 | * |
||
51 | * @author Riku Leino tsoots@gmail.com |
||
52 | * @date December 2004 |
||
53 | */ |
||
54 | class UndoManager : public QObject |
||
55 | { |
||
56 | Q_OBJECT |
||
57 | |||
58 | private: |
||
59 | /** |
||
60 | * @brief The only instance of UndoManager available. |
||
61 | * |
||
62 | * UndoManager is singleton and the instance can be queried with the method |
||
63 | * instance(). |
||
64 | */ |
||
65 | static UndoManager* _instance; |
||
66 | |||
67 | /** @brief Should undo states be stored or ignored */ |
||
68 | static bool _undoEnabled; |
||
69 | |||
70 | /** @brief Doc to which the currently active stack belongs */ |
||
71 | QString currentDoc; |
||
72 | |||
73 | /** |
||
74 | * @brief Id number of the object for what the object specific undo is shown |
||
75 | * @brief or -1 if global undo is used. |
||
76 | */ |
||
77 | int currentUndoObjectId; |
||
78 | |||
79 | /** |
||
80 | * @brief UndoGuis attached to this UndoManager |
||
81 | * @sa UndoGui |
||
82 | * @sa UndoWidget |
||
83 | * @sa UndoPalette |
||
84 | */ |
||
85 | std::vector<UndoGui*> undoGuis; |
||
86 | |||
87 | /** @brief Undo stacks for all open documents */ |
||
88 | StackMap stacks; |
||
89 | |||
90 | /** @brief Maximum length of the undo stack */ |
||
91 | int historyLength; |
||
92 | |||
93 | /** |
||
94 | * @brief Initializes the UndoGui. |
||
95 | * @param gui UndoGui to be initialized |
||
96 | * @param uid UndoObject's id if in object specific mode or -1 to tell |
||
97 | * that global undo is used. |
||
98 | */ |
||
99 | void setState(UndoGui* gui, int uid = -1); |
||
100 | |||
101 | void connectGuis(); |
||
102 | void disconnectGuis(); |
||
103 | |||
104 | public: |
||
105 | /** |
||
106 | * @brief Returns a pointer to the UndoManager instance |
||
107 | * @return A pointer to the UndoManager instance |
||
108 | */ |
||
109 | static UndoManager* instance(); |
||
110 | |||
111 | /** |
||
112 | * @brief Deletes the UndoManager Instance |
||
113 | * |
||
114 | * Must be called when UndoManager is no more needed. |
||
115 | */ |
||
116 | static void deleteInstance(); |
||
117 | |||
118 | /** |
||
119 | * @brief Sets the undo action tracking enabled or disabled. |
||
120 | * @param isEnabled If true undo stack is updated with the states sent |
||
121 | * to the UndoManager else undo stack is not updated and attached UndoGuis |
||
122 | * are not informed of the actions. |
||
123 | */ |
||
124 | void setUndoEnabled(bool isEnabled); |
||
125 | |||
126 | /** |
||
127 | * @brief Returns true if undo actions are stored, if not will return false. |
||
128 | * @return true if undo actions are stored, if not will return false |
||
129 | */ |
||
130 | static bool undoEnabled(); |
||
131 | |||
132 | /** |
||
133 | * @brief Register an UndoGui to the UndoManager. |
||
134 | * |
||
135 | * After registering a gui to the manager the gui will be updated with the |
||
136 | * undo action information received by the UndoManager. Actions done with the |
||
137 | * UndoGui to be registered are also handled after registering. |
||
138 | * @param gui A pointer to the UndoGui that is wanted to be registered. |
||
139 | */ |
||
140 | void registerGui(UndoGui* gui); |
||
141 | |||
142 | /** |
||
143 | * @brief Removes an UndoGui from UndoManager. |
||
144 | * @param gui UndoGui to be removed from the UndoManager. |
||
145 | */ |
||
146 | void removeGui(UndoGui* gui); |
||
147 | |||
148 | /** |
||
149 | * @brief Changes the active undo stack. |
||
150 | * |
||
151 | * Sets the stack connected to the name <code>stackName</code> active. Calling |
||
152 | * this method will send clear() signal to the attached <code>UndoGui</code>s and |
||
153 | * will update their undo stack representations. |
||
154 | * @param stackName Name of the stack to be used |
||
155 | */ |
||
156 | void switchStack(const QString& stackName); |
||
157 | |||
158 | /** |
||
159 | * @brief Rename the current stack |
||
160 | * @param newName New name for the current stack. |
||
161 | */ |
||
162 | void rename(const QString& newName); |
||
163 | |||
164 | /** |
||
165 | * @brief Remove the stack with the name <code>stackName</code> |
||
166 | * @param stackName Name of the stack that is wanted to be removed |
||
167 | */ |
||
168 | void remove(const QString& stackName); |
||
169 | |||
170 | /** |
||
171 | * @brief Returns true if there are actions that can be undone otherwise returns false. |
||
172 | * |
||
173 | * This is useful when undo/redo actions are handled with a gui that is not attached to |
||
174 | * the UndoManager (f.e. menu items) and when those gui items are wanted to set enabled |
||
175 | * or disabled depending on the status of the undo stack. |
||
176 | * @return true if there are actions that can be undone otherwise returns false |
||
177 | */ |
||
178 | bool hasUndoActions(); |
||
179 | |||
180 | /** |
||
181 | * @brief Returns true if there are actions that can be redone otherwise returns false. |
||
182 | * @return true if there are actions that can be redone otherwise returns false |
||
183 | * @sa UndoManager::hasUndoActions() |
||
184 | */ |
||
185 | bool hasRedoActions(); |
||
186 | |||
187 | /** |
||
188 | * @brief Switches the state of UndoManager (global/object specific undo). |
||
189 | * |
||
190 | * If parameter uid is less than 0 global undo is used else the undo state |
||
191 | * is object specific using the object whose uid is given as a parameter. |
||
192 | * @param uid UndoObject's id for what the object specific undo is wanted or |
||
193 | * -1 if global undo is wanted. |
||
194 | */ |
||
195 | void showObject(int uid); |
||
196 | |||
197 | /** |
||
198 | * @brief Sets the length of the undo stack. |
||
199 | * |
||
200 | * Tells how many UndoStates are stored. |
||
201 | * @param steps number of UndoStates to store in the undo stack |
||
202 | */ |
||
203 | void setHistoryLength(int steps); |
||
204 | |||
205 | /** |
||
206 | * @brief Returns the maximum length of the undostack. |
||
207 | * @return the maximum length of the undostack |
||
208 | */ |
||
209 | int getHistoryLength(); |
||
210 | |||
211 | protected: |
||
212 | /** @brief Creates a new UndoManager instance */ |
||
213 | UndoManager(); |
||
214 | |||
215 | /** @brief Destroys the UndoManager instance */ |
||
216 | ~UndoManager(); |
||
217 | |||
218 | private slots: |
||
219 | /** |
||
220 | * @brief Take undo steps the given amount |
||
221 | * |
||
222 | * When an UndoGui is registered to the UndoManager gui's undo() signal |
||
223 | * is connected to this slot. Emitting an undo() signal will make the |
||
224 | * UndoManager perform undo actions the given number of steps. |
||
225 | * @param steps Number of undo actions to make |
||
226 | */ |
||
227 | void doUndo(int steps); |
||
228 | |||
229 | /** |
||
230 | * @brief Take redo steps the given amount |
||
231 | * @param steps Number of redo actions to make |
||
232 | * @sa UndoManager::doUndo(int steps) |
||
233 | */ |
||
234 | void doRedo(int steps); |
||
235 | |||
236 | public slots: |
||
237 | /** |
||
238 | * @brief Adds a new action to the undo stack. |
||
239 | * |
||
240 | * If _unodEnabled is true the action will be stored otherwise it will |
||
241 | * be just ignored. When a new action is added redo items from the stack |
||
242 | * will be removed and the current action will be set to the one which was |
||
243 | * added. |
||
244 | * @param target Source of the action. When undoing/redoing this action |
||
245 | * restore() method of this UndoObject will be called. |
||
246 | * @param state UndoSate describing the state (action). |
||
247 | */ |
||
248 | void action(UndoObject* target, UndoState* state); |
||
249 | |||
250 | /** |
||
251 | * @brief Informs UndoManager to perform undo |
||
252 | * |
||
253 | * If an undo is wanted and the caller is not registered UndoGui this method |
||
254 | * can be used. Useful for example with the menu entry for undo. |
||
255 | * @param steps Number of undo steps to take |
||
256 | */ |
||
257 | void undo(int steps); |
||
258 | |||
259 | /** |
||
260 | * @brief Informs UndoManager to perform redo |
||
261 | * @param steps Number of redo steps to take |
||
262 | * @sa UndoManager::undo(int steps) |
||
263 | */ |
||
264 | void redo(int steps); |
||
265 | |||
266 | signals: |
||
267 | /** |
||
268 | * @brief Emitted when a new undo action is stored to the undo stack. |
||
269 | * |
||
270 | * This signal is connected to the registered UndoGuis for them to update the |
||
271 | * visual representation of the undo stack. |
||
272 | * @param target Source of the action |
||
273 | * @param state UndoState describing the action |
||
274 | */ |
||
275 | void newAction(UndoObject* target, UndoState* state); |
||
276 | |||
277 | /** |
||
278 | * @brief Emitted when an undo action has been done. |
||
279 | * |
||
280 | * It is connected to attached guis to inform them to update the visual |
||
281 | * representation of the undo stack. |
||
282 | * @param steps Number of steps to take |
||
283 | */ |
||
284 | void undoSignal(int steps); |
||
285 | |||
286 | /** |
||
287 | * @brief Emitted when a redo action has been done. |
||
288 | * @param steps Number of steps to take |
||
289 | * @sa UndoManager::undoSignal(int steps) |
||
290 | */ |
||
291 | void redoSignal(int steps); |
||
292 | |||
293 | /** |
||
294 | * @brief This signal is used to notify registered UndoGui instances that |
||
295 | * @brief history limit is reached and the last item from the stack |
||
296 | * @brief representation should be removed. |
||
297 | */ |
||
298 | void popBack(); |
||
299 | |||
300 | /** |
||
301 | * @brief This signal is emitted when all requested undo/redo actions have been done. |
||
302 | * |
||
303 | * It could be used in the application as a signal when the view can be rendered |
||
304 | * again after undo/redo. |
||
305 | */ |
||
306 | void undoRedoDone(); |
||
307 | }; |
||
308 | |||
309 | #endif |