Subversion Repositories Scribus

Rev

Rev 1197 | Rev 1204 | 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;
223
        /*@}*/
224
 
225
        /**
226
         * @name Action icons
227
         * Icons for undo actions
228
         */
229
        /*@{*/
230
        static QPixmap *IGuides;
231
        static QPixmap *ILockGuides;
232
        /*@}*/
233
 
1111 tsoots 234
protected:
235
        /** @brief Creates a new UndoManager instance */
236
        UndoManager();
237
 
238
        /** @brief Destroys the UndoManager instance */
239
        ~UndoManager();
240
 
241
private slots:
242
        /**
243
         * @brief Take undo steps the given amount
244
         *
245
         * When an UndoGui is registered to the UndoManager gui's undo() signal
246
         * is connected to this slot. Emitting an undo() signal will make the
247
         * UndoManager perform undo actions the given number of steps.
248
         * @param steps Number of undo actions to make
249
         */
250
        void doUndo(int steps);
251
 
252
        /**
253
         * @brief Take redo steps the given amount
254
         * @param steps Number of redo actions to make
255
         * @sa UndoManager::doUndo(int steps)
256
         */
257
        void doRedo(int steps);
258
 
259
public slots:
260
        /**
261
         * @brief Adds a new action to the undo stack.
262
         *
263
         * If _unodEnabled is true the action will be stored otherwise it will
264
         * be just ignored. When a new action is added redo items from the stack
265
         * will be removed and the current action will be set to the one which was
266
         * added.
267
         * @param target Source of the action. When undoing/redoing this action
268
         * restore() method of this UndoObject will be called.
269
         * @param state UndoSate describing the state (action).
270
         */
271
        void action(UndoObject* target, UndoState* state);
272
 
273
        /**
274
         * @brief Informs UndoManager to perform undo
275
         *
276
         * If an undo is wanted and the caller is not registered UndoGui this method
277
         * can be used. Useful for example with the menu entry for undo.
278
         * @param steps Number of undo steps to take
279
         */
280
        void undo(int steps);
281
 
282
        /**
283
         * @brief Informs UndoManager to perform redo
284
         * @param steps Number of redo steps to take
285
         * @sa UndoManager::undo(int steps)
286
         */
287
        void redo(int steps);
288
 
1197 tsoots 289
        /**
290
         * @brief Sets the length of the undo stack.
291
         *
292
         * Tells how many UndoStates are stored.
293
         * @param steps number of UndoStates to store in the undo stack
294
         */
295
        void setHistoryLength(int steps);
296
 
1111 tsoots 297
signals:
298
        /**
299
         * @brief Emitted when a new undo action is stored to the undo stack.
300
         *
301
         * This signal is connected to the registered UndoGuis for them to update the
302
         * visual representation of the undo stack.
303
         * @param target Source of the action
304
         * @param state UndoState describing the action
305
         */
306
        void newAction(UndoObject* target, UndoState* state);
307
 
308
        /**
309
         * @brief Emitted when an undo action has been done.
310
         *
311
         * It is connected to attached guis to inform them to update the visual
1190 tsoots 312
         * representation of the undo stack. This signal will be also sent to the
313
         * <code>UndoGui</code> where it came from.
1111 tsoots 314
         * @param steps Number of steps to take
315
         */
316
        void undoSignal(int steps);
317
 
318
        /**
319
         * @brief Emitted when a redo action has been done.
320
         * @param steps Number of steps to take
321
         * @sa UndoManager::undoSignal(int steps)
322
         */
323
        void redoSignal(int steps);
324
 
325
        /**
326
         * @brief This signal is used to notify registered UndoGui instances that
327
         * @brief history limit is reached and the last item from the stack
328
         * @brief representation should be removed.
329
         */
330
        void popBack();
331
 
332
        /**
333
         * @brief This signal is emitted when all requested undo/redo actions have been done.
334
         *
335
         * It could be used in the application as a signal when the view can be rendered
336
         * again after undo/redo.
337
         */
338
        void undoRedoDone();
1190 tsoots 339
 
1111 tsoots 340
};
341
 
1190 tsoots 342
typedef UndoManager Um;
343
 
1111 tsoots 344
#endif