Subversion Repositories Scribus

Rev

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