1 /*
2  *  Copyright (c) 2014 Dmitry Kazakov <dimula73@gmail.com>
3  *  Copyright (c) 2014 Mohit Goyal <mohit.bits2011@gmail.com>
4  *
5  *  This library is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU Lesser General Public License as published by
7  *  the Free Software Foundation; either version 2.1 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This library 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 Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 /****************************************************************************
20 **
21 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
22 ** All rights reserved.
23 ** Contact: Nokia Corporation (qt-info@nokia.com)
24 **
25 ** This file is part of the QtGui module of the Qt Toolkit.
26 **
27 ** $QT_BEGIN_LICENSE:LGPL$
28 ** No Commercial Usage
29 ** This file contains pre-release code and may not be distributed.
30 ** You may use this file in accordance with the terms and conditions
31 ** contained in the Technology Preview License Agreement accompanying
32 ** this package.
33 **
34 ** GNU Lesser General Public License Usage
35 ** Alternatively, this file may be used under the terms of the GNU Lesser
36 ** General Public License version 2.1 as published by the Free Software
37 ** Foundation and appearing in the file LICENSE.LGPL included in the
38 ** packaging of this file.  Please review the following information to
39 ** ensure the GNU Lesser General Public License version 2.1 requirements
40 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
41 **
42 ** In addition, as a special exception, Nokia gives you certain additional
43 ** rights.  These rights are described in the Nokia Qt LGPL Exception
44 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
45 **
46 ** If you have questions regarding the use of this file, please contact
47 ** Nokia at qt-info@nokia.com.
48 **
49 **
50 **
51 **
52 **
53 **
54 **
55 **
56 ** $QT_END_LICENSE$
57 **
58 ****************************************************************************/
59 
60 #ifndef KUNDO2STACK_H
61 #define KUNDO2STACK_H
62 
63 #include <QObject>
64 #include <QString>
65 #include <QList>
66 #include <QAction>
67 #include <QTime>
68 #include <QVector>
69 
70 
71 #include "kexiundo_export.h"
72 
73 class QAction;
74 class KUndo2CommandPrivate;
75 class KUndo2Group;
76 class KActionCollection;
77 
78 #ifndef QT_NO_UNDOCOMMAND
79 
80 #include "kundo2magicstring.h"
81 #include "kundo2commandextradata.h"
82 
83 
84 class KEXIUNDO_EXPORT KUndo2Command
85 {
86     KUndo2CommandPrivate *d;
87 
88 public:
89     explicit KUndo2Command(KUndo2Command *parent = 0);
90     explicit KUndo2Command(const KUndo2MagicString &text, KUndo2Command *parent = 0);
91     virtual ~KUndo2Command();
92 
93     virtual void undo();
94     virtual void redo();
95 
96     QString actionText() const;
97     KUndo2MagicString text() const;
98     void setText(const KUndo2MagicString &text);
99 
100     virtual int id() const;
101     virtual int timedId();
102     virtual void setTimedID(int timedID);
103     virtual bool mergeWith(const KUndo2Command *other);
104     virtual bool timedMergeWith(KUndo2Command *other);
105 
106     int childCount() const;
107     const KUndo2Command *child(int index) const;
108 
109     bool hasParent();
110     virtual void setTime();
111     virtual QTime time();
112     virtual void setEndTime();
113     virtual QTime endTime();
114 
115     virtual QVector<KUndo2Command*> mergeCommandsVector();
116     virtual bool isMerged();
117     virtual void undoMergedCommands();
118     virtual void redoMergedCommands();
119 
120     /**
121      * \return user-defined object associated with the command
122      *
123      * \see setExtraData()
124      */
125     KUndo2CommandExtraData* extraData() const;
126 
127     /**
128      * The user can assign an arbitrary object associated with the
129      * command. The \p data object is owned by the command. If you assign
130      * the object twice, the first one will be destroyed.
131      */
132     void setExtraData(KUndo2CommandExtraData *data);
133 
134 private:
135     Q_DISABLE_COPY(KUndo2Command)
136     friend class KUndo2QStack;
137 
138 
139     bool m_hasParent;
140     int m_timedID;
141 
142     QTime m_timeOfCreation;
143     QTime m_endOfCommand;
144     QVector<KUndo2Command*> m_mergeCommandsVector;
145 };
146 
147 #endif // QT_NO_UNDOCOMMAND
148 
149 #ifndef QT_NO_UNDOSTACK
150 
151 class KEXIUNDO_EXPORT KUndo2QStack : public QObject
152 {
153     Q_OBJECT
154 //    Q_DECLARE_PRIVATE(KUndo2QStack)
155     Q_PROPERTY(bool active READ isActive WRITE setActive)
156     Q_PROPERTY(int undoLimit READ undoLimit WRITE setUndoLimit)
157 
158 public:
159     explicit KUndo2QStack(QObject *parent = 0);
160     virtual ~KUndo2QStack();
161     void clear();
162 
163     bool push(KUndo2Command *cmd);
164 
165     bool canUndo() const;
166     bool canRedo() const;
167     QString undoText() const;
168     QString redoText() const;
169 
170     int count() const;
171     int index() const;
172     QString actionText(int idx) const;
173     QString text(int idx) const;
174 
175 #ifndef QT_NO_ACTION
176     QAction *createUndoAction(QObject *parent) const;
177     QAction *createRedoAction(QObject *parent) const;
178 #endif // QT_NO_ACTION
179 
180     bool isActive() const;
181     bool isClean() const;
182     int cleanIndex() const;
183 
184     void beginMacro(const KUndo2MagicString &text);
185     void endMacro();
186 
187     void setUndoLimit(int limit);
188     int undoLimit() const;
189 
190     const KUndo2Command *command(int index) const;
191 
192     void setUseCumulativeUndoRedo(bool value);
193     bool useCumulativeUndoRedo();
194     void setTimeT1(double value);
195     double timeT1();
196     void setTimeT2(double value);
197     double timeT2();
198     int strokesN();
199     void setStrokesN(int value);
200 
201 
202 public Q_SLOTS:
203     void setClean();
204     virtual void setIndex(int idx);
205     virtual void undo();
206     virtual void redo();
207     void setActive(bool active = true);
208 
209     void purgeRedoState();
210 
211 Q_SIGNALS:
212     void indexChanged(int idx);
213     void cleanChanged(bool clean);
214     void canUndoChanged(bool canUndo);
215     void canRedoChanged(bool canRedo);
216     void undoTextChanged(const QString &undoActionText);
217     void redoTextChanged(const QString &redoActionText);
218 
219 protected:
220     virtual void notifySetIndexChangedOneCommand();
221 
222 private:
223     // from QUndoStackPrivate
224     QList<KUndo2Command*> m_command_list;
225     QList<KUndo2Command*> m_macro_stack;
226     int m_index;
227     int m_clean_index;
228     KUndo2Group *m_group;
229     int m_undo_limit;
230     bool m_useCumulativeUndoRedo;
231     double m_timeT1;
232     double m_timeT2;
233     int m_strokesN;
234     int m_lastMergedSetCount;
235     int m_lastMergedIndex;
236 
237     // also from QUndoStackPrivate
238     void setIndex(int idx, bool clean);
239     bool checkUndoLimit();
240 
241     Q_DISABLE_COPY(KUndo2QStack)
242     friend class KUndo2Group;
243 };
244 
245 class KEXIUNDO_EXPORT KUndo2Stack : public KUndo2QStack
246 {
247     Q_OBJECT
248 public:
249     explicit KUndo2Stack(QObject *parent = 0);
250 
251     // functions from KUndoStack
252     QAction* createRedoAction(KActionCollection* actionCollection, const QString& actionName = QString());
253     QAction* createUndoAction(KActionCollection* actionCollection, const QString& actionName = QString());
254 };
255 
256 #endif // QT_NO_UNDOSTACK
257 
258 #endif // KUNDO2STACK_H
259