1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QUNDOSTACK_H |
5 | #define QUNDOSTACK_H |
6 | |
7 | #include <QtGui/qtguiglobal.h> |
8 | #include <QtCore/qobject.h> |
9 | #include <QtCore/qstring.h> |
10 | |
11 | QT_REQUIRE_CONFIG(undocommand); |
12 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | class QAction; |
16 | class QUndoCommandPrivate; |
17 | class QUndoStackPrivate; |
18 | |
19 | class Q_GUI_EXPORT QUndoCommand |
20 | { |
21 | QUndoCommandPrivate *d; |
22 | |
23 | public: |
24 | explicit QUndoCommand(QUndoCommand *parent = nullptr); |
25 | explicit QUndoCommand(const QString &text, QUndoCommand *parent = nullptr); |
26 | virtual ~QUndoCommand(); |
27 | |
28 | virtual void undo(); |
29 | virtual void redo(); |
30 | |
31 | QString text() const; |
32 | QString actionText() const; |
33 | void setText(const QString &text); |
34 | |
35 | bool isObsolete() const; |
36 | void setObsolete(bool obsolete); |
37 | |
38 | virtual int id() const; |
39 | virtual bool mergeWith(const QUndoCommand *other); |
40 | |
41 | int childCount() const; |
42 | const QUndoCommand *child(int index) const; |
43 | |
44 | private: |
45 | Q_DISABLE_COPY(QUndoCommand) |
46 | friend class QUndoStack; |
47 | }; |
48 | |
49 | #if QT_CONFIG(undostack) |
50 | |
51 | class Q_GUI_EXPORT QUndoStack : public QObject |
52 | { |
53 | Q_OBJECT |
54 | Q_DECLARE_PRIVATE(QUndoStack) |
55 | Q_PROPERTY(bool active READ isActive WRITE setActive) |
56 | Q_PROPERTY(int undoLimit READ undoLimit WRITE setUndoLimit) |
57 | Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged) |
58 | Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged) |
59 | Q_PROPERTY(QString undoText READ undoText NOTIFY undoTextChanged) |
60 | Q_PROPERTY(QString redoText READ redoText NOTIFY redoTextChanged) |
61 | Q_PROPERTY(bool clean READ isClean NOTIFY cleanChanged) |
62 | |
63 | public: |
64 | explicit QUndoStack(QObject *parent = nullptr); |
65 | ~QUndoStack(); |
66 | void clear(); |
67 | |
68 | void push(QUndoCommand *cmd); |
69 | |
70 | bool canUndo() const; |
71 | bool canRedo() const; |
72 | QString undoText() const; |
73 | QString redoText() const; |
74 | |
75 | int count() const; |
76 | int index() const; |
77 | QString text(int idx) const; |
78 | |
79 | #ifndef QT_NO_ACTION |
80 | QAction *createUndoAction(QObject *parent, const QString &prefix = QString()) const; |
81 | QAction *createRedoAction(QObject *parent, const QString &prefix = QString()) const; |
82 | #endif // QT_NO_ACTION |
83 | |
84 | bool isActive() const; |
85 | bool isClean() const; |
86 | int cleanIndex() const; |
87 | |
88 | void beginMacro(const QString &text); |
89 | void endMacro(); |
90 | |
91 | void setUndoLimit(int limit); |
92 | int undoLimit() const; |
93 | |
94 | const QUndoCommand *command(int index) const; |
95 | |
96 | public Q_SLOTS: |
97 | void setClean(); |
98 | void resetClean(); |
99 | void setIndex(int idx); |
100 | void undo(); |
101 | void redo(); |
102 | void setActive(bool active = true); |
103 | |
104 | Q_SIGNALS: |
105 | void indexChanged(int idx); |
106 | void cleanChanged(bool clean); |
107 | void canUndoChanged(bool canUndo); |
108 | void canRedoChanged(bool canRedo); |
109 | void undoTextChanged(const QString &undoText); |
110 | void redoTextChanged(const QString &redoText); |
111 | |
112 | private: |
113 | Q_DISABLE_COPY(QUndoStack) |
114 | friend class QUndoGroup; |
115 | }; |
116 | |
117 | #endif // QT_CONFIG(undostack) |
118 | |
119 | QT_END_NAMESPACE |
120 | |
121 | #endif // QUNDOSTACK_H |
122 | |