File indexing completed on 2024-05-12 15:56:14

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 
0071 
0072 /**
0073  * WARNING: In general, don't derive undo commands from QObject. And
0074  *          if you really need it, don't use QObject lifetime tracking
0075  *          for the commands:  KUndo2Command has its own, *nonvirtual*
0076  *          hierarchy, and don't make it a parent or a child of any
0077  *          QObject. Otherwise two different parents will try to track
0078  *          the lifetime of your command and, most probably, you'll
0079  *          get a crash.
0080  *
0081  *          As a general rule: an undo command should be derived
0082  *          from QObject only for the sake of signal/slots capabilities.
0083  *          Nothing else.
0084  */
0085 class KRITACOMMAND_EXPORT KUndo2Command
0086 {
0087     KUndo2CommandPrivate *d {0};
0088 
0089 public:
0090     explicit KUndo2Command(KUndo2Command *parent = 0);
0091     explicit KUndo2Command(const KUndo2MagicString &text, KUndo2Command *parent = 0);
0092     virtual ~KUndo2Command();
0093 
0094     virtual void undo();
0095     virtual void redo();
0096 
0097     QString actionText() const;
0098     KUndo2MagicString text() const;
0099     void setText(const KUndo2MagicString &text);
0100 
0101     virtual int id() const;
0102     virtual int timedId();
0103     virtual void setTimedID(int timedID);
0104     virtual bool mergeWith(const KUndo2Command *other);
0105     virtual bool timedMergeWith(KUndo2Command *other);
0106 
0107     virtual bool canAnnihilateWith(const KUndo2Command *other) const;
0108 
0109     int childCount() const;
0110     const KUndo2Command *child(int index) const;
0111 
0112     bool hasParent();
0113     virtual void setTime();
0114     virtual QTime time();
0115     virtual void setEndTime();
0116     virtual QTime endTime();
0117 
0118     virtual QVector<KUndo2Command*> mergeCommandsVector();
0119     virtual bool isMerged();
0120     virtual void undoMergedCommands();
0121     virtual void redoMergedCommands();
0122 
0123     /**
0124      * \return user-defined object associated with the command
0125      *
0126      * \see setExtraData()
0127      */
0128     KUndo2CommandExtraData* extraData() const;
0129 
0130     /**
0131      * The user can assign an arbitrary object associated with the
0132      * command. The \p data object is owned by the command. If you assign
0133      * the object twice, the first one will be destroyed.
0134      */
0135     void setExtraData(KUndo2CommandExtraData *data);
0136 
0137 private:
0138     Q_DISABLE_COPY(KUndo2Command)
0139     friend class KUndo2QStack;
0140 
0141 
0142     bool m_hasParent {false};
0143     int m_timedID {-1};
0144 
0145     QTime m_timeOfCreation;
0146     QTime m_endOfCommand;
0147     QVector<KUndo2Command*> m_mergeCommandsVector;
0148 };
0149 
0150 #endif // QT_NO_UNDOCOMMAND
0151 
0152 #ifndef QT_NO_UNDOSTACK
0153 
0154 class KRITACOMMAND_EXPORT KUndo2QStack : public QObject
0155 {
0156     Q_OBJECT
0157 //    Q_DECLARE_PRIVATE(KUndo2QStack)
0158     Q_PROPERTY(bool active READ isActive WRITE setActive)
0159     Q_PROPERTY(int undoLimit READ undoLimit WRITE setUndoLimit)
0160 
0161 public:
0162     explicit KUndo2QStack(QObject *parent = 0);
0163     ~KUndo2QStack() override;
0164     void clear();
0165 
0166     void push(KUndo2Command *cmd);
0167 
0168     bool canUndo() const;
0169     bool canRedo() const;
0170     QString undoText() const;
0171     QString redoText() const;
0172 
0173     int count() const;
0174     int index() const;
0175     QString actionText(int idx) const;
0176     QString text(int idx) const;
0177 
0178 #ifndef QT_NO_ACTION
0179     QAction *createUndoAction(QObject *parent) const;
0180     QAction *createRedoAction(QObject *parent) const;
0181 #endif // QT_NO_ACTION
0182 
0183     bool isActive() const;
0184     bool isClean() const;
0185     int cleanIndex() const;
0186 
0187     void beginMacro(const KUndo2MagicString &text);
0188     void endMacro();
0189 
0190     void setUndoLimit(int limit);
0191     int undoLimit() const;
0192 
0193     const KUndo2Command *command(int index) const;
0194 
0195     void setUseCumulativeUndoRedo(bool value);
0196     bool useCumulativeUndoRedo();
0197     void setTimeT1(double value);
0198     double timeT1();
0199     void setTimeT2(double value);
0200     double timeT2();
0201     int strokesN();
0202     void setStrokesN(int value);
0203 
0204 
0205 public Q_SLOTS:
0206     void setClean();
0207     virtual void setIndex(int idx);
0208     virtual void undo();
0209     virtual void redo();
0210     void setActive(bool active = true);
0211 
0212     void purgeRedoState();
0213 
0214 Q_SIGNALS:
0215     void indexChanged(int idx);
0216     void cleanChanged(bool clean);
0217     void canUndoChanged(bool canUndo);
0218     void canRedoChanged(bool canRedo);
0219     void undoTextChanged(const QString &undoActionText);
0220     void redoTextChanged(const QString &redoActionText);
0221 
0222 protected:
0223     virtual void notifySetIndexChangedOneCommand();
0224 
0225 private:
0226     // from QUndoStackPrivate
0227     QList<KUndo2Command*> m_command_list;
0228     QList<KUndo2Command*> m_macro_stack;
0229     int m_index;
0230     int m_clean_index;
0231     KUndo2Group *m_group;
0232     int m_undo_limit;
0233     bool m_useCumulativeUndoRedo;
0234     double m_timeT1;
0235     double m_timeT2;
0236     int m_strokesN;
0237     int m_lastMergedSetCount;
0238     int m_lastMergedIndex;
0239 
0240     // also from QUndoStackPrivate
0241     void setIndex(int idx, bool clean);
0242     bool checkUndoLimit();
0243 
0244     Q_DISABLE_COPY(KUndo2QStack)
0245     friend class KUndo2Group;
0246 };
0247 
0248 class KRITACOMMAND_EXPORT KUndo2Stack : public KUndo2QStack
0249 {
0250 public:
0251     explicit KUndo2Stack(QObject *parent = 0);
0252 
0253     // functions from KUndoStack
0254     QAction* createRedoAction(KisKActionCollection* actionCollection, const QString& actionName = QString());
0255     QAction* createUndoAction(KisKActionCollection* actionCollection, const QString& actionName = QString());
0256 };
0257 
0258 #endif // QT_NO_UNDOSTACK
0259 
0260 #endif // KUNDO2STACK_H