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

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 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_MANAGER_H
0021 #define TIKZ_UNDO_MANAGER_H
0022 
0023 #include <QAbstractItemModel>
0024 #include <QList>
0025 
0026 namespace tikz {
0027 namespace core {
0028 
0029 class Document;
0030 class UndoManagerPrivate;
0031 class UndoGroup;
0032 class UndoItem;
0033 
0034 /**
0035  * Base class for undo/redo items.
0036  */
0037 class UndoManager : public QAbstractItemModel
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     /**
0043      * Constructor. Sets the associated tikz::core::document to @p doc.
0044      */
0045     UndoManager(Document* doc);
0046 
0047     /**
0048      * Virtual destructor.
0049      */
0050     virtual ~UndoManager();
0051 
0052     /**
0053      * Accessor to the tikz Document.
0054      */
0055     Document* document();
0056 
0057 public Q_SLOTS:
0058     /**
0059      * Mark the current undo/redo state as clean.
0060      * Typically, the clean state matches the modified state of the doucment.
0061      */
0062     void setClean();
0063 
0064 public:
0065     /**
0066      * Returns whether undo can currently be invoked or not.
0067      */
0068     bool undoAvailable() const;
0069 
0070     /**
0071      * Returns whether redo can currently be invoked or not.
0072      */
0073     bool redoAvailable() const;
0074 
0075     /**
0076      * Returns the clean state.
0077      */
0078     bool isClean() const;
0079 
0080 Q_SIGNALS:
0081     /**
0082      * This signal is emitted whenever the clean state changes.
0083      */
0084     void cleanChanged(bool clean);
0085 
0086 public Q_SLOTS:
0087     /**
0088      * Undo one undo/redo group.
0089      * This applies all items in the previous undo group
0090      */
0091     void undo();
0092 
0093     /**
0094      * Redo one undo/redo group.
0095      * This applies all items in the next redo group
0096      */
0097     void redo();
0098 
0099     /**
0100      * Remove all undo/redo items.
0101      */
0102     void clear();
0103 
0104     /**
0105      * Add the undo item @p item to the current undo group.
0106      */
0107     void addUndoItem(UndoItem *item);
0108 
0109 public:
0110     /**
0111      * Returns a list of all undo groups.
0112      * You must not delete the pointers to the UndoGroup%s.
0113      */
0114     QList<UndoGroup*> undoGroups() const;
0115 
0116 public:
0117     /**
0118      * Start a new undo group.
0119      * All items added through addUndoItem() are grouped together into one
0120      * undo/redo action. To finish a transaction, call commitTransaction().
0121      * If a currently pending transaction should be aborted (e.g., the user
0122      * pressed ESC), just call cancelTransaction() before finally calling
0123      * commitTransaction().
0124      * @param text the text of the undo item.
0125      *
0126      * @note startTransaction() and commitTransaction() are ref-counted.
0127      *       Always make sure these two calls are balanced.
0128      */
0129     void startTransaction(const QString & text = QString());
0130 
0131     /**
0132      * Cancel the currently pending transaction.
0133      *
0134      * If you call cancelTransaction(), all the added items are un-done
0135      * again and the currently pending undo/redo group is deleted.
0136      * @note Make sure to call commitTransaction(), though.
0137      */
0138     void cancelTransaction();
0139 
0140     /**
0141      * Commits the currently pending transaction, if it was not canceled
0142      * through cancelTransaction().
0143      */
0144     void commitTransaction();
0145 
0146     /**
0147      * Returns whether there is a currently pending transaction.
0148      */
0149     bool transactionActive() const;
0150 
0151 //
0152 // Implementation of QAbstractItemModel
0153 //
0154 public:
0155     /**
0156      * Returns a QModelIndex for the requested position.
0157      */
0158     QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const override;
0159 
0160     /**
0161      * Returns a parent QModelIndex for @p index.
0162      */
0163     QModelIndex parent(const QModelIndex & index) const override;
0164 
0165     /**
0166      * Returns the number of child items (rows) for @p parent.
0167      */
0168     int rowCount(const QModelIndex & parent = QModelIndex()) const override;
0169 
0170     /**
0171      * Returns the number of columns for the index @p parent.
0172      */
0173     int columnCount(const QModelIndex & parent = QModelIndex()) const override;
0174 
0175     /**
0176      * Returns the data for @p index.
0177      */
0178     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
0179 
0180     /**
0181      * Inserts @p count rows starting at @p row.
0182      */
0183     bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex()) override;
0184 
0185     /**
0186      * Removes @p count rows starting at @p row.
0187      */
0188     bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()) override;
0189 
0190 public:
0191     // for debugging
0192     void printTree();
0193 
0194 private:
0195     /**
0196      * Pimpl pointer to the held data.
0197      */
0198     UndoManagerPrivate * const d;
0199 };
0200 
0201 }
0202 }
0203 
0204 #endif // TIKZ_UNDO_MANAGER_H
0205 
0206 // kate: indent-width 4; replace-tabs on;