Subversion Repositories Scribus

Rev

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