Subversion Repositories Scribus

Rev

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