Rev 1211 | Rev 1226 | Go to most recent revision | Details | Compare with Previous | 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> |
||
1190 | tsoots | 28 | #include <qstring.h> |
1111 | tsoots | 29 | #include "undostate.h" |
30 | #include "undoobject.h" |
||
31 | |||
1142 | tsoots | 32 | class UndoGui; |
1197 | tsoots | 33 | class PrefsContext; |
1142 | tsoots | 34 | |
1111 | tsoots | 35 | typedef std::pair<UndoObject*, UndoState*> ActionPair; |
36 | typedef std::vector<ActionPair> ActionList; |
||
37 | typedef std::pair<ActionList::iterator, ActionList> StackPair; |
||
38 | typedef QMap<QString, StackPair> StackMap; |
||
39 | |||
40 | /** |
||
41 | * @brief UndoManager handles the undo stack. |
||
42 | * |
||
43 | * UndoManager is the engine of the undo/redo handling. When ever an undoable |
||
44 | * action happens an UndoState object will be sent to the UndoManager which then |
||
45 | * stores the state in the undo stack. When an undo is requested the state is |
||
46 | * send back to it's creator which is then responsible for doing the actual undo or |
||
47 | * redo. |
||
48 | * |
||
49 | * UndoManager also handles the UndoGuis by linking them together and reporting |
||
50 | * all undo/redo actions to the guis for them to update the visual representation. |
||
51 | * For this to work all UndoGuis need to be registered to the UndoManager with the |
||
52 | * registerGui() method. |
||
53 | * |
||
54 | * @author Riku Leino tsoots@gmail.com |
||
55 | * @date December 2004 |
||
56 | */ |
||
57 | class UndoManager : public QObject |
||
58 | { |
||
59 | Q_OBJECT |
||
60 | |||
61 | private: |
||
1213 | tsoots | 62 | |
63 | /*** UndoManager::TransactionState ***************************************************/ |
||
64 | |||
65 | /** |
||
66 | * @brief TransactionState provides a container where multiple actions can be stored |
||
67 | * @brief as a single action which then appears in the attached <code>UndoGui</code>s. |
||
68 | * @author Riku Leino tsoots@gmail.com |
||
69 | * @date January 2005 |
||
70 | */ |
||
71 | class TransactionState : public UndoState |
||
72 | { |
||
73 | private: |
||
74 | /** @brief Number of undo states stored in this transaction */ |
||
75 | uint _size; |
||
76 | /** @brief vector to keep the states in */ |
||
77 | std::vector<ActionPair*> states; |
||
78 | public: |
||
79 | /** @brief Creates a new TransactionState instance */ |
||
80 | TransactionState(); |
||
81 | /** @brief Destroys the TransactionState instance */ |
||
82 | ~TransactionState(); |
||
83 | /** |
||
84 | * @brief Add a new <code>UndoState</code> object to the transaction. |
||
85 | * @param state state to be added to the transaction |
||
86 | */ |
||
87 | void pushBack(UndoObject *target, UndoState *state); |
||
88 | /** |
||
89 | * @brief Returns the count of the <code>UndoState</code> objects in this transaction. |
||
90 | * @return count of the <code>UndoState</code> objects in this transaction |
||
91 | */ |
||
92 | uint sizet(); |
||
93 | /** |
||
94 | * @brief Returns an <code>UndoState</code> object at <code>index</code>. |
||
95 | * @param index index from where an <code>UndoState</code> object is returned. |
||
96 | * If <code>index</code> is out of scope <code>NULL</code> will be rerturned. |
||
97 | * @return <code>UndoState</code> object from <code>index</code> or <code>NULL</code> |
||
98 | * if <code>index</code> is out of scope. |
||
99 | */ |
||
100 | ActionPair* at(int index); |
||
101 | }; |
||
102 | |||
103 | /*** UndoManager::TransactionObject ***************************************************/ |
||
104 | |||
105 | /** |
||
106 | * @brief Dummy subclass of <code>UndoObject</code> which is used for holding the name |
||
107 | * @brief for the transaction's target object(group) (f.e "Selection", "Group", "Script"...). |
||
108 | * @author Riku Leino tsoots@gmail.com |
||
109 | * @date January 2005 |
||
110 | */ |
||
111 | class TransactionObject : public UndoObject |
||
112 | { |
||
113 | public: |
||
114 | TransactionObject() {}; |
||
115 | ~TransactionObject() {}; |
||
116 | void restore(UndoState*, bool) {}; |
||
117 | }; |
||
118 | |||
119 | /**************************************************************************************/ |
||
120 | |||
1111 | tsoots | 121 | /** |
122 | * @brief The only instance of UndoManager available. |
||
123 | * |
||
124 | * UndoManager is singleton and the instance can be queried with the method |
||
125 | * instance(). |
||
126 | */ |
||
127 | static UndoManager* _instance; |
||
128 | |||
129 | /** @brief Should undo states be stored or ignored */ |
||
130 | static bool _undoEnabled; |
||
131 | |||
1197 | tsoots | 132 | PrefsContext *prefs; |
133 | |||
1111 | tsoots | 134 | /** @brief Doc to which the currently active stack belongs */ |
135 | QString currentDoc; |
||
136 | |||
137 | /** |
||
138 | * @brief Id number of the object for what the object specific undo is shown |
||
139 | * @brief or -1 if global undo is used. |
||
140 | */ |
||
141 | int currentUndoObjectId; |
||
142 | |||
1213 | tsoots | 143 | /** |
144 | * @brief If in transaction mode this is the container for incoming <code>UndoState</code> |
||
145 | * @brief objects. |
||
146 | * |
||
147 | * It is also used to detect if we are in the transaction mode. When it is <code>NULL</code> |
||
148 | * normal mode is on. |
||
149 | */ |
||
150 | TransactionState *transaction; |
||
151 | |||
152 | /** @brief Dummy object for storing transaction target's name */ |
||
153 | TransactionObject *transactionTarget; |
||
154 | |||
1111 | tsoots | 155 | /** |
156 | * @brief UndoGuis attached to this UndoManager |
||
157 | * @sa UndoGui |
||
158 | * @sa UndoWidget |
||
159 | * @sa UndoPalette |
||
160 | */ |
||
161 | std::vector<UndoGui*> undoGuis; |
||
162 | |||
163 | /** @brief Undo stacks for all open documents */ |
||
164 | StackMap stacks; |
||
165 | |||
166 | /** @brief Maximum length of the undo stack */ |
||
167 | int historyLength; |
||
168 | |||
169 | /** |
||
170 | * @brief Initializes the UndoGui. |
||
171 | * @param gui UndoGui to be initialized |
||
172 | * @param uid UndoObject's id if in object specific mode or -1 to tell |
||
173 | * that global undo is used. |
||
174 | */ |
||
175 | void setState(UndoGui* gui, int uid = -1); |
||
176 | |||
177 | void connectGuis(); |
||
178 | void disconnectGuis(); |
||
1190 | tsoots | 179 | void initIcons(); |
1197 | tsoots | 180 | void checkStackLength(); |
1213 | tsoots | 181 | void doTransactionUndo(TransactionState *tstate); |
182 | void doTransactionRedo(TransactionState *tstate); |
||
1111 | tsoots | 183 | |
184 | public: |
||
185 | /** |
||
186 | * @brief Returns a pointer to the UndoManager instance |
||
187 | * @return A pointer to the UndoManager instance |
||
188 | */ |
||
189 | static UndoManager* instance(); |
||
190 | |||
191 | /** |
||
192 | * @brief Deletes the UndoManager Instance |
||
193 | * |
||
194 | * Must be called when UndoManager is no more needed. |
||
195 | */ |
||
196 | static void deleteInstance(); |
||
197 | |||
198 | /** |
||
199 | * @brief Sets the undo action tracking enabled or disabled. |
||
200 | * @param isEnabled If true undo stack is updated with the states sent |
||
201 | * to the UndoManager else undo stack is not updated and attached UndoGuis |
||
202 | * are not informed of the actions. |
||
203 | */ |
||
204 | void setUndoEnabled(bool isEnabled); |
||
205 | |||
206 | /** |
||
207 | * @brief Returns true if undo actions are stored, if not will return false. |
||
208 | * @return true if undo actions are stored, if not will return false |
||
209 | */ |
||
210 | static bool undoEnabled(); |
||
211 | |||
212 | /** |
||
1213 | tsoots | 213 | * @brief Start a transaction. |
214 | * |
||
215 | * After this method has been invoked <code>UndoManager</code> will switch to the |
||
216 | * transaction (silent) mode where it does not report actions to the attached |
||
217 | * <UndoGui> widgets but stores all incoming <code>UndoState</code> objects into |
||
218 | * the transaction container which after call to the method commit() will be sent |
||
219 | * to the guis as a single undo action. Transaction can be named when starting it or |
||
220 | * naming can be done when commiting it. |
||
221 | * @param targetName name for the target of this transaction (f.e. "Selection") |
||
222 | * @param name name for the transaction (f.e. "Move" would make with the above |
||
223 | * "Move Selection") |
||
224 | * @param description description for the transaction |
||
225 | * @param pixmap icon for the transaction |
||
226 | * @sa commit() |
||
227 | */ |
||
228 | void beginTransaction(const QString &targetName = "", |
||
229 | const QString &name = "", |
||
230 | const QString &description = "", |
||
231 | QPixmap *pixmap = 0); |
||
232 | |||
233 | /** |
||
234 | * @brief Cancels the current transaction and deletes groupped <code>UndoState</code>s. |
||
235 | * @brief Nothing from canceled transaction will be sent to the undo gui widgets. |
||
236 | */ |
||
237 | void cancelTransaction(); |
||
238 | |||
239 | /** |
||
240 | * @brief Commit the current transaction. |
||
241 | * |
||
242 | * Current transaction will be commited and <code>UndoManager</code> will be switched |
||
243 | * to the normal mode. Commited transaction will be sent to the attached undo gui |
||
244 | * widgets and it will show up there as a single undo action. Details used as a parameter |
||
245 | * will be details shown in the gui widgets. |
||
246 | * @param targetName name for the target of this transaction (f.e. "Selection") |
||
247 | * @param name name for the action |
||
248 | * @param description description for the action |
||
249 | * @param pixmap icon for the action |
||
250 | * @sa beginTransaction() |
||
251 | */ |
||
252 | void commit(const QString &targetName = "", |
||
253 | const QString &name = "", |
||
254 | const QString &description = "", |
||
255 | QPixmap *pixmap = 0); |
||
256 | |||
257 | /** |
||
258 | * @brief Returns true if in transaction mode if not will return false. |
||
259 | * @return true if in transaction mode if not will return false |
||
260 | */ |
||
261 | bool isTransactionMode(); |
||
262 | |||
263 | /** |
||
1111 | tsoots | 264 | * @brief Register an UndoGui to the UndoManager. |
265 | * |
||
266 | * After registering a gui to the manager the gui will be updated with the |
||
267 | * undo action information received by the UndoManager. Actions done with the |
||
268 | * UndoGui to be registered are also handled after registering. |
||
269 | * @param gui A pointer to the UndoGui that is wanted to be registered. |
||
270 | */ |
||
271 | void registerGui(UndoGui* gui); |
||
272 | |||
273 | /** |
||
274 | * @brief Removes an UndoGui from UndoManager. |
||
275 | * @param gui UndoGui to be removed from the UndoManager. |
||
276 | */ |
||
277 | void removeGui(UndoGui* gui); |
||
278 | |||
279 | /** |
||
280 | * @brief Changes the active undo stack. |
||
281 | * |
||
282 | * Sets the stack connected to the name <code>stackName</code> active. Calling |
||
283 | * this method will send clear() signal to the attached <code>UndoGui</code>s and |
||
284 | * will update their undo stack representations. |
||
285 | * @param stackName Name of the stack to be used |
||
286 | */ |
||
287 | void switchStack(const QString& stackName); |
||
288 | |||
289 | /** |
||
290 | * @brief Rename the current stack |
||
291 | * @param newName New name for the current stack. |
||
292 | */ |
||
293 | void rename(const QString& newName); |
||
294 | |||
295 | /** |
||
296 | * @brief Remove the stack with the name <code>stackName</code> |
||
297 | * @param stackName Name of the stack that is wanted to be removed |
||
298 | */ |
||
299 | void remove(const QString& stackName); |
||
300 | |||
301 | /** |
||
302 | * @brief Returns true if there are actions that can be undone otherwise returns false. |
||
303 | * |
||
304 | * This is useful when undo/redo actions are handled with a gui that is not attached to |
||
305 | * the UndoManager (f.e. menu items) and when those gui items are wanted to set enabled |
||
306 | * or disabled depending on the status of the undo stack. |
||
307 | * @return true if there are actions that can be undone otherwise returns false |
||
308 | */ |
||
309 | bool hasUndoActions(); |
||
310 | |||
311 | /** |
||
312 | * @brief Returns true if there are actions that can be redone otherwise returns false. |
||
313 | * @return true if there are actions that can be redone otherwise returns false |
||
314 | * @sa UndoManager::hasUndoActions() |
||
315 | */ |
||
316 | bool hasRedoActions(); |
||
317 | |||
318 | /** |
||
319 | * @brief Switches the state of UndoManager (global/object specific undo). |
||
320 | * |
||
321 | * If parameter uid is less than 0 global undo is used else the undo state |
||
322 | * is object specific using the object whose uid is given as a parameter. |
||
323 | * @param uid UndoObject's id for what the object specific undo is wanted or |
||
324 | * -1 if global undo is wanted. |
||
325 | */ |
||
326 | void showObject(int uid); |
||
327 | |||
328 | /** |
||
329 | * @brief Returns the maximum length of the undostack. |
||
330 | * @return the maximum length of the undostack |
||
331 | */ |
||
332 | int getHistoryLength(); |
||
333 | |||
1199 | tsoots | 334 | /** |
1190 | tsoots | 335 | * @name Action strings |
336 | * Strings describing undo actions |
||
337 | */ |
||
338 | /*@{*/ |
||
339 | static const QString AddVGuide; |
||
340 | static const QString AddHGuide; |
||
341 | static const QString DelVGuide; |
||
342 | static const QString DelHGuide; |
||
343 | static const QString MoveVGuide; |
||
344 | static const QString MoveHGuide; |
||
345 | static const QString LockGuides; |
||
346 | static const QString UnlockGuides; |
||
1204 | tsoots | 347 | static const QString Move; |
1206 | tsoots | 348 | static const QString Resize; |
1211 | tsoots | 349 | static const QString Rotate; |
350 | static const QString MoveFromTo; |
||
351 | static const QString ResizeFromTo; |
||
352 | static const QString RotateFromTo; |
||
1190 | tsoots | 353 | /*@}*/ |
1204 | tsoots | 354 | |
1190 | tsoots | 355 | /** |
356 | * @name Action icons |
||
357 | * Icons for undo actions |
||
358 | */ |
||
359 | /*@{*/ |
||
360 | static QPixmap *IGuides; |
||
361 | static QPixmap *ILockGuides; |
||
1204 | tsoots | 362 | static QPixmap *IMoveText; |
363 | static QPixmap *IMoveImage; |
||
364 | static QPixmap *IMoveLine; |
||
365 | static QPixmap *IMovePolygon; |
||
366 | static QPixmap *IMovePolyline; |
||
367 | static QPixmap *IMovePathText; |
||
1206 | tsoots | 368 | static QPixmap *IResizeText; |
369 | static QPixmap *IResizeImage; |
||
370 | static QPixmap *IResizeLine; |
||
371 | static QPixmap *IResizePolygon; |
||
372 | static QPixmap *IResizePolyline; |
||
373 | static QPixmap *IResizePathText; |
||
1211 | tsoots | 374 | static QPixmap *IRotateText; |
375 | static QPixmap *IRotateImage; |
||
376 | static QPixmap *IRotateLine; |
||
377 | static QPixmap *IRotatePolygon; |
||
378 | static QPixmap *IRotatePolyline; |
||
379 | static QPixmap *IRotatePathText; |
||
1190 | tsoots | 380 | /*@}*/ |
381 | |||
1111 | tsoots | 382 | protected: |
383 | /** @brief Creates a new UndoManager instance */ |
||
384 | UndoManager(); |
||
385 | |||
386 | /** @brief Destroys the UndoManager instance */ |
||
387 | ~UndoManager(); |
||
388 | |||
389 | private slots: |
||
390 | /** |
||
391 | * @brief Take undo steps the given amount |
||
392 | * |
||
393 | * When an UndoGui is registered to the UndoManager gui's undo() signal |
||
394 | * is connected to this slot. Emitting an undo() signal will make the |
||
395 | * UndoManager perform undo actions the given number of steps. |
||
396 | * @param steps Number of undo actions to make |
||
397 | */ |
||
398 | void doUndo(int steps); |
||
399 | |||
400 | /** |
||
401 | * @brief Take redo steps the given amount |
||
402 | * @param steps Number of redo actions to make |
||
403 | * @sa UndoManager::doUndo(int steps) |
||
404 | */ |
||
405 | void doRedo(int steps); |
||
406 | |||
407 | public slots: |
||
408 | /** |
||
409 | * @brief Adds a new action to the undo stack. |
||
410 | * |
||
411 | * If _unodEnabled is true the action will be stored otherwise it will |
||
412 | * be just ignored. When a new action is added redo items from the stack |
||
413 | * will be removed and the current action will be set to the one which was |
||
414 | * added. |
||
415 | * @param target Source of the action. When undoing/redoing this action |
||
416 | * restore() method of this UndoObject will be called. |
||
417 | * @param state UndoSate describing the state (action). |
||
418 | */ |
||
419 | void action(UndoObject* target, UndoState* state); |
||
420 | |||
421 | /** |
||
422 | * @brief Informs UndoManager to perform undo |
||
423 | * |
||
424 | * If an undo is wanted and the caller is not registered UndoGui this method |
||
425 | * can be used. Useful for example with the menu entry for undo. |
||
426 | * @param steps Number of undo steps to take |
||
427 | */ |
||
428 | void undo(int steps); |
||
429 | |||
430 | /** |
||
431 | * @brief Informs UndoManager to perform redo |
||
432 | * @param steps Number of redo steps to take |
||
433 | * @sa UndoManager::undo(int steps) |
||
434 | */ |
||
435 | void redo(int steps); |
||
436 | |||
1197 | tsoots | 437 | /** |
438 | * @brief Sets the length of the undo stack. |
||
439 | * |
||
440 | * Tells how many UndoStates are stored. |
||
441 | * @param steps number of UndoStates to store in the undo stack |
||
442 | */ |
||
443 | void setHistoryLength(int steps); |
||
444 | |||
1111 | tsoots | 445 | signals: |
446 | /** |
||
447 | * @brief Emitted when a new undo action is stored to the undo stack. |
||
448 | * |
||
449 | * This signal is connected to the registered UndoGuis for them to update the |
||
450 | * visual representation of the undo stack. |
||
451 | * @param target Source of the action |
||
452 | * @param state UndoState describing the action |
||
453 | */ |
||
454 | void newAction(UndoObject* target, UndoState* state); |
||
455 | |||
456 | /** |
||
457 | * @brief Emitted when an undo action has been done. |
||
458 | * |
||
459 | * It is connected to attached guis to inform them to update the visual |
||
1190 | tsoots | 460 | * representation of the undo stack. This signal will be also sent to the |
461 | * <code>UndoGui</code> where it came from. |
||
1111 | tsoots | 462 | * @param steps Number of steps to take |
463 | */ |
||
464 | void undoSignal(int steps); |
||
465 | |||
466 | /** |
||
467 | * @brief Emitted when a redo action has been done. |
||
468 | * @param steps Number of steps to take |
||
469 | * @sa UndoManager::undoSignal(int steps) |
||
470 | */ |
||
471 | void redoSignal(int steps); |
||
472 | |||
473 | /** |
||
474 | * @brief This signal is used to notify registered UndoGui instances that |
||
475 | * @brief history limit is reached and the last item from the stack |
||
476 | * @brief representation should be removed. |
||
477 | */ |
||
478 | void popBack(); |
||
479 | |||
480 | /** |
||
481 | * @brief This signal is emitted when all requested undo/redo actions have been done. |
||
482 | * |
||
483 | * It could be used in the application as a signal when the view can be rendered |
||
484 | * again after undo/redo. |
||
485 | */ |
||
486 | void undoRedoDone(); |
||
1190 | tsoots | 487 | |
1111 | tsoots | 488 | }; |
489 | |||
1190 | tsoots | 490 | typedef UndoManager Um; |
491 | |||
1111 | tsoots | 492 | #endif |