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

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2014-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_TRANSACTION_H
0021 #define TIKZ_TRANSACTION_H
0022 
0023 #include "tikz_export.h"
0024 
0025 #include <QString>
0026 
0027 namespace tikz {
0028 namespace core {
0029 
0030 class Document;
0031 class TransactionPrivate;
0032 
0033 /**
0034  * Undo/redo transaction support.
0035  *
0036  * Document modifications may consist of a sequence of changes, such as moving a
0037  * node and changing its text. The Transaction class allows to bunch together such
0038  * a sequence to a single undo/redo step.
0039  *
0040  * Using this class typically looks as follows:
0041  * @code
0042  * void foo()
0043  * {
0044  *     tikz::core::Transaction transaction(document, "Modify Node");
0045  *     // now call editing functions
0046  *     node->setPos(...);
0047  *     node->setText(...);
0048  * }
0049  * @endcode
0050  *
0051  * Although usually not required, a Transaction additionally allows to manually
0052  * call finish(). This way, the transaction is finished before the destructor,
0053  * and the destructor does nothing.
0054  *
0055  * Further, if a running transaction should be aborted, just call cancel().
0056  * This is handy whenever the user hits Escape during a modification.
0057  */
0058 class TIKZKITCORE_EXPORT Transaction
0059 {
0060     public:
0061         /**
0062          * Constructor. Will automatically start an editing transaction.
0063          *
0064          * @param document document for the transaction
0065          */
0066         explicit Transaction(Document * document);
0067 
0068         /**
0069          * Constructs the object and starts an editing transaction.
0070          *
0071          * @param document document for the transaction
0072          * @param name the transaction name
0073          */
0074         explicit Transaction(Document * document, const QString & name);
0075 
0076         /**
0077          * Destructs the object and, if needed, finishes a running editing
0078          * transaction by calling finish().
0079          *
0080          * @see finish()
0081          */
0082         ~Transaction();
0083 
0084         /**
0085          * Tells the history transaction to roolback on finish().
0086          */
0087         void cancel();
0088 
0089         /**
0090          * By calling finish(), the editing transaction can be finished
0091          * already before destruction of this instance.
0092          *
0093          * @see cancel()
0094          */
0095         void finish();
0096 
0097         /**
0098          * Check whether this transaction currently is active.
0099          */
0100         bool isRunning() const;
0101 
0102     private:
0103         // disallow copy constructor
0104         Transaction(const Transaction &) = delete;
0105         Transaction() = delete;
0106 
0107         /**
0108          * Document pointer
0109          */
0110         Document * m_document = nullptr;
0111 };
0112 
0113 }
0114 }
0115 
0116 #endif // TIKZ_TRANSACTION_H
0117 
0118 // kate: indent-width 4; replace-tabs on;