Subversion Repositories Scribus

Rev

Rev 1200 | Rev 1255 | 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 UNDOGUI_H
22
#define UNDOGUI_H
23
 
24
#include "undoobject.h"
25
#include "undostate.h"
26
#include <vector>
27
#include <qwidget.h>
28
#include <qdialog.h>
29
#include <qstring.h>
30
#include <qlistbox.h>
31
#include <qtoolbutton.h>
1142 tsoots 32
#include <qpainter.h>
1111 tsoots 33
#include <qpixmap.h>
34
#include <qpopupmenu.h>
35
 
1148 tsoots 36
class PrefsContext;
37
 
1111 tsoots 38
/**
39
 * @brief UndoGui is a virtual superclass for undo/redo widgets.
40
 *
41
 * UndoGui is a virtual superclass for all the classes that wants to represent
42
 * undo/redo data. Subclasses must be registered to the UndoManager instance.
43
 * Subclasses are expected to keep the whole stack with them and they must be
44
 * able to work undo/redo situations by themselves. Only new undo actions and
45
 * steps (undo or redo) are reported back.
46
 *
47
 * @sa UndoWidget
48
 * @sa UndoPalette
49
 *
50
 * @author Riku Leino  tsoots@gmail.com
51
 * @date December 2004
52
 */
53
class UndoGui : public QWidget
54
{
55
        Q_OBJECT
56
 
57
public:
58
        /**
59
         * @brief Creates a new UndoGui instance.
60
         * @param parent Parent object for UndoGui
61
         * @param name Name of the object
62
         * @param f widget flags
63
         */
64
        UndoGui(QWidget* parent = 0, const char* name = 0, WFlags f = 0);
65
 
66
        /** @brief Destroys the widget */
67
        virtual ~UndoGui() {};
68
 
69
        /** @brief Empties the undo stack representation. */
70
        virtual void clear() = 0;
71
 
72
public slots:
73
        /**
74
         * @brief Insert a new undo item.
75
         *
76
         * Insert a new undo item. After an undoable action has happened and
77
         * if this UndoGui is registered to the UndoManager this slot will
78
         * be used to register the action to the widget.
79
         * @param target Target of the undo action
80
         * @param state State describing the action
81
         */
82
        virtual void insertUndoItem(UndoObject* target, UndoState* state) = 0;
83
 
84
        /**
85
         * @brief Insert a new redo item.
86
         * @param target Target of the redo action
87
         * @param state State describing the action
88
         */
89
        virtual void insertRedoItem(UndoObject* target, UndoState* state) = 0;
90
 
91
        /**
92
         * @brief Update undo stack representation with number of steps.
93
         *
94
         * When some other UndoGui subclass has received an undo request this
95
         * slot is used to inform this UndoGui to update it's UndoStack
96
         * representation.
97
         * @param steps Number of steps to take
98
         */
99
        virtual void updateUndo(int steps) = 0;
100
 
101
        /**
102
         * @brief Update redo stack representation with number of steps.
103
         *
104
         * When some other registered UndoGui subclass has received a redo
105
         * request this slot is used to inform this UndoGui to update it's
106
         * undo stack representation.
107
         * @param steps Number of steps to take
108
         */
109
        virtual void updateRedo(int steps) = 0;
110
 
111
        /** @brief Remove the last (oldest) item from the undo stack representation. */
112
        virtual void popBack() = 0;
113
/* signals: do not implement these but emit when action happens
114
        virtual void undo(int steps) = 0;
115
        virtual void redo(int steps) = 0;
116
*/
117
};
118
 
119
/*** UndoGui implementations **************************************************/
120
/*** UndoWidget ***************************************************************/
121
 
122
/**
123
 * @brief Tool bar buttons with pop up menus for undo and redo.
124
 *
125
 * Provides tool bar buttons for undo and redo. There is also a pop up menu
126
 * attached to these buttons for taking more than a single undo/redo step.
127
 *
128
 * @author Riku Leino tsoots@gmail.com
129
 * @date December 2004
130
 */
131
class UndoWidget : public UndoGui
132
{
133
        Q_OBJECT
134
 
135
private:
136
        /** @brief Items visible in the pop up menus */
137
        static const uint MENU_HEIGHT = 5;
138
        std::vector<QString> undoItems;
139
        std::vector<QString> redoItems;
140
        QToolButton* undoButton;
141
        QToolButton* redoButton;
142
        QPopupMenu* undoMenu;
143
        QPopupMenu* redoMenu;
144
        void updateUndoMenu();
145
        void updateRedoMenu();
146
        void clearRedoMenu();
147
public:
148
        /** @brief Creates a new UndoWidget instance. */
149
        UndoWidget(QWidget* parent = 0, const char* name = 0);
150
 
151
        /** @brief Destroys the widget */
152
        virtual ~UndoWidget();
153
 
154
        /** @brief Empties the undo stack for this widget. */
155
        void clear();
156
private slots:
157
        void undoClicked();
158
        void redoClicked();
159
        void undoMenuClicked(int id);
160
        void redoMenuClicked(int id);
161
public slots:
162
        /**
163
         * @brief Insert a new undo item.
164
         *
165
         * After an undoable action has happened and
166
         * if this UndoGui is registered to the UndoManager this slot will
167
         * be used to register the action to the widget.
168
         * @param target Target of the undo action
169
         * @param state State describing the action
170
         */
1142 tsoots 171
        void insertUndoItem(UndoObject*, UndoState* state);
1111 tsoots 172
 
173
        /**
174
         * @brief Insert a new redo item.
175
         * @param target Target of the redo action
176
         * @param state State describing the action
177
         */
1142 tsoots 178
        void insertRedoItem(UndoObject*, UndoState* state);
1111 tsoots 179
 
180
        /**
181
         * @brief Update undo stack representation with number of steps.
182
         *
183
         * When some other UndoGui subclass has received an undo request this
184
         * slot is used to inform this UndoGui to update it's UndoStack
185
         * representation.
186
         * @param steps Number of steps to take
187
         */
188
        void updateUndo(int steps);
189
 
190
        /**
191
         * @brief Update redo stack representation with number of steps.
192
         *
193
         * When some other UndoGui subclass has received a redo request this
194
         * slot is used to inform this UndoGui to update it's UndoStack
195
         * representation.
196
         * @param steps Number of steps to take
197
         */
198
        void updateRedo(int steps);
199
 
200
        /** @brief Remove the last (oldest) item from the undo stack representation. */
201
        void popBack();
202
signals:
203
        /**
204
         * @brief Emitted when undo is requested.
205
         *
206
         * When undo action is done by this class this signal is emitted.
207
         * @param steps How many undo steps to take
208
         */
209
        void undo(int steps);
210
 
211
        /**
212
         * @brief Emitted when redo is requested.
213
         *
214
         * When redo action is done by this class this signal is emitted.
215
         * @param steps How many redo steps to take
216
         */
217
        void redo(int steps);
218
};
219
 
220
/*** UndoPalette **************************************************************/
221
 
222
/**
223
 * @brief UndoGui subclass which creates an undo history window
224
 *
225
 * Provides a regular undo history window where a user can click on any action
226
 * and related undo/redo steps are taken. Undo steps are represented as a
227
 * <code>QListBox</code> where there can be an image to describe the action
228
 * visually, the name of the object and the name of the action per row.
229
 *
230
 * @author Riku Leino tsoots@gmail.com
231
 * @date December 2004
232
 */
233
class UndoPalette : public UndoGui
234
{
235
        Q_OBJECT
236
 
237
private:
238
        int currentSelection;
239
        int redoItems;
240
        QListBox* undoList;
241
        QPushButton* undoButton;
242
        QPushButton* redoButton;
1148 tsoots 243
        PrefsContext* undoPrefs;
1111 tsoots 244
        void updateList();
245
        void removeRedoItems();
1142 tsoots 246
 
247
/*** UndoPalette::UndoItem ****************************************************/
248
 
249
        /** @brief UndoItem provides a custom QListBoxItem for the undo history view. */
250
        class UndoItem : public QListBoxItem
251
        {
252
        private:
1250 tsoots 253
                /** @brief An icon for the undo target */
254
                                                   QPixmap *targetpixmap;
1142 tsoots 255
                /** @brief An icon for the undo state (action) */
1250 tsoots 256
                QPixmap *actionpixmap;
1142 tsoots 257
                /** @brief Name of the target of the state (action) */
258
                QString target;
259
                /** @brief Undo action's name */
260
                QString action;
1200 tsoots 261
                /** @brief Description of the action */
262
                QString description;
1142 tsoots 263
        public:
264
                /** @brief Create an empty UndoItem object */
265
                UndoItem();
266
                /**
267
                 * @brief Create a copy of <code>another</code> UndoItem instance.
268
                 * @param another UndoItem instance to copy
269
                 */
270
                UndoItem(const UndoItem &another);
271
                /**
272
                 * @brief Create an UndoItem instance.
273
                 * @param targetName Name of the target. Will appear on the first row of
274
                 * the item.
275
                 * @param actionName Name of the state (action). Will appear on the
276
                 * second row of the item.
1200 tsoots 277
                 * @param actionDescription This description of the action will be used as a
278
                 * tooltip when the mouse cursor is over the item.
1142 tsoots 279
                 * @param actionPixmap Icon for the state (action). Will appear on front
280
                 * of the text parts.
281
                 */
1200 tsoots 282
                UndoItem(const QString &targetName,
1142 tsoots 283
                 const QString &actionName,
1200 tsoots 284
                 const QString &actionDescription,
1250 tsoots 285
                 QPixmap *targetPixmap,
1142 tsoots 286
                 QPixmap *actionPixmap);
287
                ~UndoItem();
1167 tsoots 288
                void paint(QPainter *painter);
289
                int height(const QListBox*) const;
290
                int width(const QListBox*) const;
1200 tsoots 291
                QString getDescription();
1142 tsoots 292
        };
293
 
294
/******************************************************************************/
1111 tsoots 295
 
296
private slots:
297
        void undoClicked();
298
        void redoClicked();
299
        void undoListClicked(int i);
1200 tsoots 300
        void showToolTip(QListBoxItem *i);
301
        void removeToolTip();
1111 tsoots 302
 
303
protected:
304
        /**
305
         * @brief Captures the close event and changes it to hide.
306
         *
307
         * UndoPalette should not be closed but hidden instead. Method closeEvent()
308
         * will catch the close attempt and will transfer it to hide.
309
         */
310
        void closeEvent(QCloseEvent* e);
311
 
1148 tsoots 312
        /** @brief Stores the geometry of the window when hiding. */
313
        void hideEvent(QHideEvent*);
314
 
1111 tsoots 315
public:
316
        /**
317
         * @brief Creates a new UndoPalette instance.
318
         *
319
         * Creates a new UndoPalette instance. After creation of an UndoPalette it must
320
         * be registered to the UndoManager with UndoManager's registerGui() method.
321
         */
322
        UndoPalette(QWidget* parent = 0, const char* name = 0);
323
 
324
        /** @brief Destroys the widget */
325
        ~UndoPalette();
326
 
327
        /** @brief Empties the undo stack for this widget. */
328
        void clear();
329
 
330
public slots:
331
        /**
332
         * @brief Insert a new undo item.
333
         *
334
         * Insert a new undo item. After an undoable action has happened and
335
         * if this UndoGui is registered to the UndoManager this slot will
336
         * be used to register the action to the widget.
337
         * @param target Target of the undo action
338
         * @param state State describing the action
339
         */
340
        void insertUndoItem(UndoObject* target, UndoState* state);
341
 
342
        /**
343
         * @brief Insert a new redo item.
344
         * @param target Target of the redo action
345
         * @param state State describing the action
346
         */
347
        void insertRedoItem(UndoObject* target, UndoState* state);
348
 
349
        /**
350
         * @brief Update undo stack representation with number of steps.
351
         *
352
         * When some other UndoGui subclass has received an undo request this
353
         * slot is used to inform this UndoGui to update it's UndoStack
354
         * representation.
355
         * @param steps Number of steps to take
356
         */
357
        void updateUndo(int steps);
358
 
359
        /**
360
         * @brief Update redo stack representation with number of steps.
361
         *
362
         * When some other UndoGui subclass has received a redo request this
363
         * slot is used to inform this UndoGui to update it's UndoStack
364
         * representation.
365
         * @param steps Number of steps to take
366
         */
367
        void updateRedo(int steps);
368
 
369
        /** @brief Remove the last (oldest) item from the undo stack representation. */
370
        void popBack();
371
 
1148 tsoots 372
        /** @brief Restore the size and position of the window when shown. */
373
        void show();
374
 
1111 tsoots 375
signals:
376
        /**
377
         * @brief Emitted when undo is requested.
378
         *
379
         * When undo action is done by this class this signal is emitted.
380
         * @param steps How many undo steps to take
381
         */
382
        void undo(int steps);
383
 
384
        /**
385
         * @brief Emitted when redo is requested.
386
         *
387
         * When redo action is done by this class this signal is emitted.
388
         * @param steps How many redo steps to take
389
         */
390
        void redo(int steps);
391
};
392
 
393
#endif // UNDOGUI_H