~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/shared/qtundo_p.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the designer application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
//
 
30
//  W A R N I N G
 
31
//  -------------
 
32
//
 
33
// This file is not part of the Qt API.  It exists for the convenience
 
34
// of Qt Designer.  This header
 
35
// file may change from version to version without notice, or even be removed.
 
36
//
 
37
// We mean it.
 
38
//
 
39
 
 
40
#ifndef QTUNDO_H
 
41
#define QTUNDO_H
 
42
 
 
43
#include "shared_global_p.h"
 
44
 
 
45
#include <QAbstractItemModel>
 
46
#include <QtCore/QMap>
 
47
#include <QtCore/QList>
 
48
#include <QStringList>
 
49
#include <QListView>
 
50
 
 
51
class QWidget;
 
52
class QAction;
 
53
class QtUndoStack;
 
54
 
 
55
class QT_SHARED_EXPORT QtCommand : public QObject
 
56
{
 
57
    Q_OBJECT
 
58
 
 
59
    friend class QtUndoStack;
 
60
 
 
61
    public:
 
62
        enum Type { Command, MacroBegin, MacroEnd };
 
63
 
 
64
        QtCommand(Type type, const QString &description = QString(),
 
65
                        bool canMerge = false);
 
66
        QtCommand(const QString &description = QString(),
 
67
                        bool canMerge = true);
 
68
 
 
69
        virtual void redo() {};
 
70
        virtual void undo() {};
 
71
 
 
72
        QString description() const
 
73
            { return m_description; }
 
74
        void setDescription(const QString &s)
 
75
            { m_description = s; }
 
76
        bool canMerge() const
 
77
            { return m_can_merge; }
 
78
        void setCanMerge(bool b)
 
79
            { m_can_merge = b; }
 
80
        Type type() const
 
81
            { return m_type; }
 
82
 
 
83
        bool isMacroBegin() const
 
84
            { return m_type == MacroBegin; }
 
85
        bool isMacroEnd() const
 
86
            { return m_type == MacroEnd; }
 
87
        bool isCommand() const
 
88
            { return m_type == Command; }
 
89
 
 
90
    protected:
 
91
        virtual bool mergeMeWith(QtCommand *other);
 
92
 
 
93
    private:
 
94
        void shortenStack();
 
95
 
 
96
        bool m_can_merge;
 
97
        QString m_description;
 
98
        Type m_type;
 
99
};
 
100
 
 
101
class QtMultiCommand : public QtCommand
 
102
{
 
103
public:
 
104
    QtMultiCommand(const QString &description = QString());
 
105
    QtMultiCommand(const QList<QtCommand*> &command_list,
 
106
                    const QString &description = QString());
 
107
    ~QtMultiCommand();
 
108
    virtual void redo();
 
109
    virtual void undo();
 
110
 
 
111
    void append(QtCommand *command);
 
112
    int count() const;
 
113
    QtCommand *command(int i) const;
 
114
 
 
115
private:
 
116
    QList<QtCommand*> m_command_list;
 
117
};
 
118
 
 
119
class QtUndoState;
 
120
 
 
121
class QT_SHARED_EXPORT QtUndoStack : public QObject, private QList<QtCommand*>
 
122
{
 
123
    Q_OBJECT
 
124
 
 
125
    friend class QtUndoManager;
 
126
 
 
127
    public:
 
128
        QtUndoStack(QObject *parent = 0);
 
129
        void push(QtCommand *command);
 
130
        bool canUndo() const;
 
131
        bool canRedo() const;
 
132
        QString undoDescription() const;
 
133
        QString redoDescription() const;
 
134
        QStringList undoList() const;
 
135
        QStringList redoList() const;
 
136
        bool isClean() const;
 
137
 
 
138
        void setCurrent();
 
139
 
 
140
        QAction *createUndoAction(QObject *parent) const;
 
141
        QAction *createRedoAction(QObject *parent) const;
 
142
 
 
143
        inline int currentIndex() const { return m_current_iter; }
 
144
 
 
145
    public slots:
 
146
        void undo(int count = 1);
 
147
        void redo(int count = 1);
 
148
        void clear();
 
149
 
 
150
        void setClean();
 
151
 
 
152
    signals:
 
153
            void cleanChanged(bool clean);
 
154
        void commandExecuted();
 
155
 
 
156
        void undoDescriptionChanged(const QString &newDescription);
 
157
        void redoDescriptionChanged(const QString &newDescription);
 
158
        void canUndoChanged(bool enabled);
 
159
        void canRedoChanged(bool enabled);
 
160
 
 
161
    private:
 
162
        typedef int CommandIter;
 
163
 
 
164
        void undoMacro();
 
165
        void redoMacro();
 
166
        CommandIter findMacroBegin(CommandIter it) const;
 
167
        CommandIter findMacroEnd(CommandIter it) const;
 
168
 
 
169
        void beforeChange(QtUndoState &state);
 
170
        void afterChange(const QtUndoState &state);
 
171
 
 
172
        // *m_current_iter == 0 means "one-before-first"
 
173
        CommandIter m_current_iter;
 
174
        uint m_num_commands;
 
175
        int m_macro_nest;
 
176
 
 
177
        bool m_have_clean_command;
 
178
        const QtCommand *m_clean_command;
 
179
 
 
180
        QtCommand *commandAt(CommandIter it) const;
 
181
};
 
182
 
 
183
class QT_SHARED_EXPORT QtUndoManager : public QObject
 
184
{
 
185
    Q_OBJECT
 
186
 
 
187
    public:
 
188
        QtUndoManager();
 
189
 
 
190
        QAction *createUndoAction(QObject *parent) const;
 
191
        QAction *createRedoAction(QObject *parent) const;
 
192
 
 
193
        void associateView(QObject *obj, QtUndoStack *stack);
 
194
        void disassociateView(QObject *obj);
 
195
 
 
196
        bool canUndo() const;
 
197
        bool canRedo() const;
 
198
        QString undoDescription() const;
 
199
        QString redoDescription() const;
 
200
        void setUndoLimit(uint i);
 
201
        uint undoLimit() const;
 
202
        QStringList undoList() const;
 
203
        QStringList redoList() const;
 
204
        QtUndoStack *currentStack() const;
 
205
        void setCurrentStack(QtUndoStack *stack);
 
206
 
 
207
        static QtUndoManager *manager();
 
208
 
 
209
    public slots:
 
210
        void undo(int count = 1);
 
211
        void redo(int count = 1);
 
212
 
 
213
        void updateActions();
 
214
 
 
215
    signals:
 
216
        void changed();
 
217
 
 
218
        void undoDescriptionChanged(const QString &newDescription);
 
219
        void redoDescriptionChanged(const QString &newDescription);
 
220
        void canUndoChanged(bool enabled);
 
221
        void canRedoChanged(bool enabled);
 
222
 
 
223
    private slots:
 
224
        void stackDestroyed(QObject *stack);
 
225
        void viewDestroyed(QObject *view);
 
226
 
 
227
    private:
 
228
        typedef QMap<QObject*, QtUndoStack*> StackMap;
 
229
 
 
230
        StackMap m_stack_map;
 
231
        QtUndoStack *m_current_stack;
 
232
 
 
233
        static QtUndoManager *m_manager; // singleton
 
234
        static uint m_undo_limit;
 
235
 
 
236
        bool m_can_undo, m_can_redo;
 
237
        QString m_undo_description, m_redo_description;
 
238
};
 
239
 
 
240
class QT_SHARED_EXPORT QtUndoListModel: public QAbstractItemModel
 
241
{
 
242
    Q_OBJECT
 
243
public:
 
244
    QtUndoListModel(QObject *parent = 0);
 
245
    virtual ~QtUndoListModel();
 
246
 
 
247
    inline int undoIndex() const { return m_undoIndex; }
 
248
 
 
249
    virtual int rowCount(const QModelIndex &parent) const;
 
250
    virtual int columnCount(const QModelIndex &parent) const;
 
251
    virtual bool hasChildren(const QModelIndex &parent) const
 
252
    { return rowCount(parent) > 0; }
 
253
 
 
254
    virtual QModelIndex parent(const QModelIndex &index) const;
 
255
    virtual QModelIndex index(int row, int column, const QModelIndex &parent) const;
 
256
    virtual QVariant data(const QModelIndex &index, int role) const;
 
257
 
 
258
private slots:
 
259
    void updateItems();
 
260
 
 
261
private:
 
262
    QStringList m_items;
 
263
    int m_undoIndex;
 
264
};
 
265
 
 
266
class QT_SHARED_EXPORT QtUndoListView: public QListView
 
267
{
 
268
    Q_OBJECT
 
269
public:
 
270
    QtUndoListView(QWidget *parent = 0);
 
271
    virtual ~QtUndoListView();
 
272
    virtual void reset();
 
273
private slots:
 
274
    void undoOrRedo();
 
275
};
 
276
 
 
277
 
 
278
 
 
279
#endif