File indexing completed on 2024-05-12 04:35:05

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013-2015 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef TIKZ_UNDO_ITEM_H
0021 #define TIKZ_UNDO_ITEM_H
0022 
0023 #include "tikz_export.h"
0024 
0025 #include <QString>
0026 #include <QJsonObject>
0027 
0028 namespace tikz {
0029 namespace core {
0030 
0031 class Document;
0032 class UndoGroup;
0033 class UndoItemPrivate;
0034 
0035 /**
0036  * Base class for undo/redo items.
0037  */
0038 class TIKZKITCORE_EXPORT UndoItem
0039 {
0040 public:
0041     /**
0042      * Constructor, setting the undo item's description to @p text,
0043      * and the associated tikz::core::document to @p doc.
0044      */
0045     UndoItem(const QString & text, Document* doc);
0046 
0047     /**
0048      * Virtual destructor.
0049      */
0050     virtual ~UndoItem();
0051 
0052     /**
0053      * Accessor to the tikz Document.
0054      */
0055     Document* document();
0056 
0057     /**
0058      * Set the undo item description to @p text.
0059      */
0060     void setText(const QString & text);
0061 
0062     /**
0063      * Returns the description of the undo item.
0064      */
0065     QString text() const;
0066 
0067     /**
0068      * Apply the redo operation.
0069      */
0070     virtual void redo() = 0;
0071 
0072     /**
0073      * Apply the undo operation.
0074      */
0075     virtual void undo() = 0;
0076 
0077     /**
0078      * Returns the uniq undo item identifier of this undo item.
0079      * Whenever two successive undo items have the same id, the function
0080      * mergeWith() is executed to fold the two undo items into a single
0081      * undo item.
0082      *
0083      * By default, -1 is returned. In this case (or any other negative id),
0084      * mergeWith() is not called.
0085      *
0086      * If you reimplement this function, reimplement it like this:
0087      * @code
0088      * int id() const override {
0089      *     return uniqId<decltype(this)>();
0090      * }
0091      * @endcode
0092      */
0093     virtual int id() const;
0094 
0095     /**
0096      * Merge @p item into this item. Afterwards, @p item is deleted.
0097      * Typically, the undo() operation is unchanged, and you only have to
0098      * copy the redo data of @p item into this undo item.
0099      */
0100     virtual bool mergeWith(const UndoItem * item);
0101 
0102 //
0103 // serialization
0104 //
0105 public:
0106     /**
0107      * Load the undo item state from the @p json object.
0108      */
0109     void load(const QJsonObject & json);
0110 
0111     /**
0112      * Serialize the undo item state to a JSON object.
0113      */
0114     QJsonObject save() const;
0115 
0116 protected:
0117     /**
0118      * Load the undo item state from the @p json object.
0119      */
0120     virtual void loadData(const QJsonObject & json) = 0;
0121 
0122     /**
0123      * Serialize the undo item state to a JSON object.
0124      */
0125     virtual QJsonObject saveData() const = 0;
0126 
0127 // group information
0128 public:
0129     /**
0130      * Retuns the UndoGroup this UndoItem belongs to.
0131      * As long as an UndoItem is not added to an undo group, a nullptr is
0132      * returned.
0133      */
0134     UndoGroup * group() const;
0135 
0136 protected:
0137     friend class UndoGroup;
0138     /**
0139      * Once an undo item is added to an UndoGroup, the undo group this item
0140      * belongs to is set to @p group. This function is called by the UndoGroup.
0141      */
0142     void setGroup(UndoGroup * group);
0143 
0144     /**
0145      * Internal id counter starting at 0.
0146      * Each time this function is called, the counter is increased by one.
0147      */
0148     static int nextFreeId();
0149 
0150     /**
0151      * Template function that always returns the same id for T.
0152      * This is a trick: Since the template funcion is generated for each class,
0153      * each undo item calling this function with its type gets an own uniq id.
0154      * Hence: We magically never get undo item id clashes at runtime.
0155      */
0156     template<typename T>
0157     int uniqId() const
0158     {
0159         static int s_id = nextFreeId();
0160         return s_id;
0161     }
0162 
0163 private:
0164     /**
0165      * Pimpl pointer to the held data.
0166      */
0167     UndoItemPrivate * const d;
0168 };
0169 
0170 }
0171 }
0172 
0173 #endif // TIKZ_UNDO_ITEM_H
0174 
0175 // kate: indent-width 4; replace-tabs on;