Subversion Repositories Scribus

Rev

Rev 1206 | Rev 1213 | 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:
62
        /**
63
         * @brief The only instance of UndoManager available.
64
         *
65
         * UndoManager is singleton and the instance can be queried with the method
66
         * instance().
67
         */
68
        static UndoManager* _instance;
69
 
70
        /** @brief Should undo states be stored or ignored */
71
        static bool _undoEnabled;
72
 
1197 tsoots 73
        PrefsContext *prefs;
74
 
1111 tsoots 75
        /** @brief Doc to which the currently active stack belongs */
76
        QString currentDoc;
77
 
78
        /**
79
         * @brief Id number of the object for what the object specific undo is shown
80
         * @brief or -1 if global undo is used.
81
         */
82
        int currentUndoObjectId;
83
 
84
        /**
85
         * @brief UndoGuis attached to this UndoManager
86
         * @sa UndoGui
87
         * @sa UndoWidget
88
         * @sa UndoPalette
89
         */
90
        std::vector<UndoGui*> undoGuis;
91
 
92
        /** @brief Undo stacks for all open documents */
93
        StackMap stacks;
94
 
95
        /** @brief Maximum length of the undo stack */
96
        int historyLength;
97
 
98
        /**
99
         * @brief Initializes the UndoGui.
100
         * @param gui UndoGui to be initialized
101
         * @param uid UndoObject's id if in object specific mode or -1 to tell
102
         * that global undo is used.
103
         */
104
        void setState(UndoGui* gui, int uid = -1);
105
 
106
        void connectGuis();
107
        void disconnectGuis();
1190 tsoots 108
        void initIcons();
1197 tsoots 109
        void checkStackLength();
1111 tsoots 110
 
111
public:
112
        /**
113
         * @brief Returns a pointer to the UndoManager instance
114
         * @return A pointer to the UndoManager instance
115
         */
116
        static UndoManager* instance();
117
 
118
        /**
119
         * @brief Deletes the UndoManager Instance
120
         *
121
         * Must be called when UndoManager is no more needed.
122
         */
123
        static void deleteInstance();
124
 
125
        /**
126
         * @brief Sets the undo action tracking enabled or disabled.
127
         * @param isEnabled If true undo stack is updated with the states sent
128
         * to the UndoManager else undo stack is not updated and attached UndoGuis
129
         * are not informed of the actions.
130
         */
131
        void setUndoEnabled(bool isEnabled);
132
 
133
        /**
134
         * @brief Returns true if undo actions are stored, if not will return false.
135
         * @return true if undo actions are stored, if not will return false
136
         */
137
        static bool undoEnabled();
138
 
139
        /**
140
         * @brief Register an UndoGui to the UndoManager.
141
         *
142
         * After registering a gui to the manager the gui will be updated with the
143
         * undo action information received by the UndoManager. Actions done with the
144
         * UndoGui to be registered are also handled after registering.
145
         * @param gui A pointer to the UndoGui that is wanted to be registered.
146
         */
147
        void registerGui(UndoGui* gui);
148
 
149
        /**
150
         * @brief Removes an UndoGui from UndoManager.
151
         * @param gui UndoGui to be removed from the UndoManager.
152
         */
153
        void removeGui(UndoGui* gui);
154
 
155
        /**
156
         * @brief Changes the active undo stack.
157
         *
158
         * Sets the stack connected to the name <code>stackName</code> active. Calling
159
         * this method will send clear() signal to the attached <code>UndoGui</code>s and
160
         * will update their undo stack representations.
161
         * @param stackName Name of the stack to be used
162
         */
163
        void switchStack(const QString& stackName);
164
 
165
        /**
166
         * @brief Rename the current stack
167
         * @param newName New name for the current stack.
168
         */
169
        void rename(const QString& newName);
170
 
171
        /**
172
         * @brief Remove the stack with the name <code>stackName</code>
173
         * @param stackName Name of the stack that is wanted to be removed
174
         */
175
        void remove(const QString& stackName);
176
 
177
        /**
178
         * @brief Returns true if there are actions that can be undone otherwise returns false.
179
         *
180
         * This is useful when undo/redo actions are handled with a gui that is not attached to
181
         * the UndoManager (f.e. menu items) and when those gui items are wanted to set enabled
182
         * or disabled depending on the status of the undo stack.
183
         * @return true if there are actions that can be undone otherwise returns false
184
         */
185
        bool hasUndoActions();
186
 
187
        /**
188
         * @brief Returns true if there are actions that can be redone otherwise returns false.
189
         * @return true if there are actions that can be redone otherwise returns false
190
         * @sa UndoManager::hasUndoActions()
191
         */
192
        bool hasRedoActions();
193
 
194
        /**
195
         * @brief Switches the state of UndoManager (global/object specific undo).
196
         *
197
         * If parameter uid is less than 0 global undo is used else the undo state
198
         * is object specific using the object whose uid is given as a parameter.
199
         * @param uid UndoObject's id for what the object specific undo is wanted or
200
         * -1 if global undo is wanted.
201
         */
202
        void showObject(int uid);
203
 
204
        /**
205
         * @brief Returns the maximum length of the undostack.
206
         * @return the maximum length of the undostack
207
         */
208
        int getHistoryLength();
209
 
1199 tsoots 210
        /**
1190 tsoots 211
        * @name Action strings
212
        * Strings describing undo actions
213
        */
214
        /*@{*/
215
        static const QString AddVGuide;
216
        static const QString AddHGuide;
217
        static const QString DelVGuide;
218
        static const QString DelHGuide;
219
        static const QString MoveVGuide;
220
        static const QString MoveHGuide;
221
        static const QString LockGuides;
222
        static const QString UnlockGuides;
1204 tsoots 223
        static const QString Move;
1206 tsoots 224
        static const QString Resize;
1211 tsoots 225
        static const QString Rotate;
226
        static const QString MoveFromTo;
227
        static const QString ResizeFromTo;
228
        static const QString RotateFromTo;
1204 tsoots 229
        static const QString From;
230
        static const QString To;
231
        static const QString X;
232
        static const QString Y;
233
        static const QString Text;
234
        static const QString Image;
235
        static const QString Line;
236
        static const QString Polygon;
237
        static const QString Polyline;
238
        static const QString PathText;
1190 tsoots 239
        /*@}*/
1204 tsoots 240
 
1190 tsoots 241
        /**
242
         * @name Action icons
243
         * Icons for undo actions
244
         */
245
        /*@{*/
246
        static QPixmap *IGuides;
247
        static QPixmap *ILockGuides;
1204 tsoots 248
        static QPixmap *IMoveText;
249
        static QPixmap *IMoveImage;
250
        static QPixmap *IMoveLine;
251
        static QPixmap *IMovePolygon;
252
        static QPixmap *IMovePolyline;
253
        static QPixmap *IMovePathText;
1206 tsoots 254
        static QPixmap *IResizeText;
255
        static QPixmap *IResizeImage;
256
        static QPixmap *IResizeLine;
257
        static QPixmap *IResizePolygon;
258
        static QPixmap *IResizePolyline;
259
        static QPixmap *IResizePathText;
1211 tsoots 260
        static QPixmap *IRotateText;
261
        static QPixmap *IRotateImage;
262
        static QPixmap *IRotateLine;
263
        static QPixmap *IRotatePolygon;
264
        static QPixmap *IRotatePolyline;
265
        static QPixmap *IRotatePathText;
1190 tsoots 266
        /*@}*/
267
 
1111 tsoots 268
protected:
269
        /** @brief Creates a new UndoManager instance */
270
        UndoManager();
271
 
272
        /** @brief Destroys the UndoManager instance */
273
        ~UndoManager();
274
 
275
private slots:
276
        /**
277
         * @brief Take undo steps the given amount
278
         *
279
         * When an UndoGui is registered to the UndoManager gui's undo() signal
280
         * is connected to this slot. Emitting an undo() signal will make the
281
         * UndoManager perform undo actions the given number of steps.
282
         * @param steps Number of undo actions to make
283
         */
284
        void doUndo(int steps);
285
 
286
        /**
287
         * @brief Take redo steps the given amount
288
         * @param steps Number of redo actions to make
289
         * @sa UndoManager::doUndo(int steps)
290
         */
291
        void doRedo(int steps);
292
 
293
public slots:
294
        /**
295
         * @brief Adds a new action to the undo stack.
296
         *
297
         * If _unodEnabled is true the action will be stored otherwise it will
298
         * be just ignored. When a new action is added redo items from the stack
299
         * will be removed and the current action will be set to the one which was
300
         * added.
301
         * @param target Source of the action. When undoing/redoing this action
302
         * restore() method of this UndoObject will be called.
303
         * @param state UndoSate describing the state (action).
304
         */
305
        void action(UndoObject* target, UndoState* state);
306
 
307
        /**
308
         * @brief Informs UndoManager to perform undo
309
         *
310
         * If an undo is wanted and the caller is not registered UndoGui this method
311
         * can be used. Useful for example with the menu entry for undo.
312
         * @param steps Number of undo steps to take
313
         */
314
        void undo(int steps);
315
 
316
        /**
317
         * @brief Informs UndoManager to perform redo
318
         * @param steps Number of redo steps to take
319
         * @sa UndoManager::undo(int steps)
320
         */
321
        void redo(int steps);
322
 
1197 tsoots 323
        /**
324
         * @brief Sets the length of the undo stack.
325
         *
326
         * Tells how many UndoStates are stored.
327
         * @param steps number of UndoStates to store in the undo stack
328
         */
329
        void setHistoryLength(int steps);
330
 
1111 tsoots 331
signals:
332
        /**
333
         * @brief Emitted when a new undo action is stored to the undo stack.
334
         *
335
         * This signal is connected to the registered UndoGuis for them to update the
336
         * visual representation of the undo stack.
337
         * @param target Source of the action
338
         * @param state UndoState describing the action
339
         */
340
        void newAction(UndoObject* target, UndoState* state);
341
 
342
        /**
343
         * @brief Emitted when an undo action has been done.
344
         *
345
         * It is connected to attached guis to inform them to update the visual
1190 tsoots 346
         * representation of the undo stack. This signal will be also sent to the
347
         * <code>UndoGui</code> where it came from.
1111 tsoots 348
         * @param steps Number of steps to take
349
         */
350
        void undoSignal(int steps);
351
 
352
        /**
353
         * @brief Emitted when a redo action has been done.
354
         * @param steps Number of steps to take
355
         * @sa UndoManager::undoSignal(int steps)
356
         */
357
        void redoSignal(int steps);
358
 
359
        /**
360
         * @brief This signal is used to notify registered UndoGui instances that
361
         * @brief history limit is reached and the last item from the stack
362
         * @brief representation should be removed.
363
         */
364
        void popBack();
365
 
366
        /**
367
         * @brief This signal is emitted when all requested undo/redo actions have been done.
368
         *
369
         * It could be used in the application as a signal when the view can be rendered
370
         * again after undo/redo.
371
         */
372
        void undoRedoDone();
1190 tsoots 373
 
1111 tsoots 374
};
375
 
1190 tsoots 376
typedef UndoManager Um;
377
 
1111 tsoots 378
#endif