Subversion Repositories Scribus

Rev

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