File indexing completed on 2024-05-19 04:24:19

0001 /*
0002  *  SPDX-FileCopyrightText: 2014 Dmitry Kazakov <dimula73@gmail.com>
0003  *  SPDX-FileCopyrightText: 2014 Mohit Goyal <mohit.bits2011@gmail.com>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.1-or-later
0006  */
0007 /****************************************************************************
0008 **
0009 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
0010 ** All rights reserved.
0011 ** Contact: Nokia Corporation (qt-info@nokia.com)
0012 **
0013 ** This file is part of the QtGui module of the Qt Toolkit.
0014 **
0015 ** $QT_BEGIN_LICENSE:LGPL$
0016 ** No Commercial Usage
0017 ** This file contains pre-release code and may not be distributed.
0018 ** You may use this file in accordance with the terms and conditions
0019 ** contained in the Technology Preview License Agreement accompanying
0020 ** this package.
0021 **
0022 ** GNU Lesser General Public License Usage
0023 ** Alternatively, this file may be used under the terms of the GNU Lesser
0024 ** General Public License version 2.1 as published by the Free Software
0025 ** Foundation and appearing in the file LICENSE.LGPL included in the
0026 ** packaging of this file.  Please review the following information to
0027 ** ensure the GNU Lesser General Public License version 2.1 requirements
0028 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
0029 **
0030 ** In addition, as a special exception, Nokia gives you certain additional
0031 ** rights.  These rights are described in the Nokia Qt LGPL Exception
0032 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
0033 **
0034 ** If you have questions regarding the use of this file, please contact
0035 ** Nokia at qt-info@nokia.com.
0036 **
0037 **
0038 **
0039 **
0040 **
0041 **
0042 **
0043 **
0044 ** $QT_END_LICENSE$
0045 **
0046 ****************************************************************************/
0047 
0048 #ifndef KUNDO2STACK_H
0049 #define KUNDO2STACK_H
0050 
0051 #include <QObject>
0052 #include <QString>
0053 #include <QList>
0054 #include <QAction>
0055 #include <QTime>
0056 #include <QVector>
0057 
0058 
0059 #include "kritacommand_export.h"
0060 
0061 class QAction;
0062 class KUndo2CommandPrivate;
0063 class KUndo2Group;
0064 class KisKActionCollection;
0065 
0066 #ifndef QT_NO_UNDOCOMMAND
0067 
0068 #include "kundo2magicstring.h"
0069 #include "kundo2commandextradata.h"
0070 #include "KisCumulativeUndoData.h"
0071 
0072 
0073 /**
0074  * WARNING: In general, don't derive undo commands from QObject. And
0075  *          if you really need it, don't use QObject lifetime tracking
0076  *          for the commands:  KUndo2Command has its own, *nonvirtual*
0077  *          hierarchy, and don't make it a parent or a child of any
0078  *          QObject. Otherwise two different parents will try to track
0079  *          the lifetime of your command and, most probably, you'll
0080  *          get a crash.
0081  *
0082  *          As a general rule: an undo command should be derived
0083  *          from QObject only for the sake of signal/slots capabilities.
0084  *          Nothing else.
0085  */
0086 class KRITACOMMAND_EXPORT KUndo2Command
0087 {
0088     KUndo2CommandPrivate *d {0};
0089 
0090 public:
0091     explicit KUndo2Command(KUndo2Command *parent = 0);
0092     explicit KUndo2Command(const KUndo2MagicString &text, KUndo2Command *parent = 0);
0093     virtual ~KUndo2Command();
0094 
0095     virtual void undo();
0096     virtual void redo();
0097 
0098     QString actionText() const;
0099     KUndo2MagicString text() const;
0100     void setText(const KUndo2MagicString &text);
0101 
0102     virtual int id() const;
0103     virtual int timedId() const;
0104     virtual void setTimedID(int timedID);
0105     virtual bool mergeWith(const KUndo2Command *other);
0106     virtual bool timedMergeWith(KUndo2Command *other);
0107 
0108     virtual bool canAnnihilateWith(const KUndo2Command *other) const;
0109 
0110     int childCount() const;
0111     const KUndo2Command *child(int index) const;
0112 
0113     bool hasParent() const;
0114     void setTime();
0115     virtual void setTime(const QTime &time);
0116     virtual QTime time() const;
0117     void setEndTime();
0118     virtual void setEndTime(const QTime &time);
0119     virtual QTime endTime() const;
0120 
0121     virtual QVector<KUndo2Command*> mergeCommandsVector() const;
0122     virtual bool isMerged() const;
0123     virtual void undoMergedCommands();
0124     virtual void redoMergedCommands();
0125 
0126     /**
0127      * \return user-defined object associated with the command
0128      *
0129      * \see setExtraData()
0130      */
0131     KUndo2CommandExtraData* extraData() const;
0132 
0133     /**
0134      * The user can assign an arbitrary object associated with the
0135      * command. The \p data object is owned by the command. If you assign
0136      * the object twice, the first one will be destroyed.
0137      */
0138     void setExtraData(KUndo2CommandExtraData *data);
0139 
0140 private:
0141     Q_DISABLE_COPY(KUndo2Command)
0142     friend class KUndo2QStack;
0143 
0144 
0145     bool m_hasParent {false};
0146     int m_timedID {-1};
0147 
0148     QTime m_timeOfCreation;
0149     QTime m_endOfCommand;
0150     QVector<KUndo2Command*> m_mergeCommandsVector;
0151 };
0152 
0153 #endif // QT_NO_UNDOCOMMAND
0154 
0155 #ifndef QT_NO_UNDOSTACK
0156 
0157 class KRITACOMMAND_EXPORT KUndo2QStack : public QObject
0158 {
0159     Q_OBJECT
0160 //    Q_DECLARE_PRIVATE(KUndo2QStack)
0161     Q_PROPERTY(bool active READ isActive WRITE setActive)
0162     Q_PROPERTY(int undoLimit READ undoLimit WRITE setUndoLimit)
0163 
0164 public:
0165     explicit KUndo2QStack(QObject *parent = 0);
0166     ~KUndo2QStack() override;
0167     void clear();
0168 
0169     void push(KUndo2Command *cmd);
0170 
0171     bool canUndo() const;
0172     bool canRedo() const;
0173     QString undoText() const;
0174     QString redoText() const;
0175 
0176     int count() const;
0177     int index() const;
0178     QString actionText(int idx) const;
0179     QString text(int idx) const;
0180 
0181 #ifndef QT_NO_ACTION
0182     QAction *createUndoAction(QObject *parent) const;
0183     QAction *createRedoAction(QObject *parent) const;
0184 #endif // QT_NO_ACTION
0185 
0186     bool isActive() const;
0187     bool isClean() const;
0188     int cleanIndex() const;
0189 
0190     void beginMacro(const KUndo2MagicString &text);
0191     void endMacro();
0192 
0193     void setUndoLimit(int limit);
0194     int undoLimit() const;
0195 
0196     const KUndo2Command *command(int index) const;
0197 
0198     void setUseCumulativeUndoRedo(bool value);
0199     bool useCumulativeUndoRedo() const;
0200 
0201     void setCumulativeUndoData(const KisCumulativeUndoData &data);
0202     KisCumulativeUndoData cumulativeUndoData();
0203 
0204 public Q_SLOTS:
0205     void setClean();
0206     virtual void setIndex(int idx);
0207     virtual void undo();
0208     virtual void redo();
0209     void setActive(bool active = true);
0210 
0211     void purgeRedoState();
0212 
0213 Q_SIGNALS:
0214     void indexChanged(int idx);
0215     void cleanChanged(bool clean);
0216     void canUndoChanged(bool canUndo);
0217     void canRedoChanged(bool canRedo);
0218     void undoTextChanged(const QString &undoActionText);
0219     void redoTextChanged(const QString &redoActionText);
0220 
0221 protected:
0222     virtual void notifySetIndexChangedOneCommand();
0223 
0224 private:
0225     // from QUndoStackPrivate
0226     QList<KUndo2Command*> m_command_list;
0227     QList<KUndo2Command*> m_macro_stack;
0228     int m_index;
0229     int m_clean_index;
0230     KUndo2Group *m_group;
0231     int m_undo_limit;
0232     bool m_useCumulativeUndoRedo;
0233     KisCumulativeUndoData m_cumulativeUndoData;
0234 
0235     // also from QUndoStackPrivate
0236     void setIndex(int idx, bool clean);
0237     bool checkUndoLimit();
0238 
0239     Q_DISABLE_COPY(KUndo2QStack)
0240     friend class KUndo2Group;
0241 };
0242 
0243 class KRITACOMMAND_EXPORT KUndo2Stack : public KUndo2QStack
0244 {
0245 public:
0246     explicit KUndo2Stack(QObject *parent = 0);
0247 
0248     // functions from KUndoStack
0249     QAction* createRedoAction(KisKActionCollection* actionCollection, const QString& actionName = QString());
0250     QAction* createUndoAction(KisKActionCollection* actionCollection, const QString& actionName = QString());
0251 };
0252 
0253 #endif // QT_NO_UNDOSTACK
0254 
0255 #endif // KUNDO2STACK_H