File indexing completed on 2024-04-21 04:52:39

0001 /*
0002     SPDX-FileCopyrightText: 2017 Nicolas Carion
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #pragma once
0007 
0008 #include <functional>
0009 
0010 using Fun = std::function<bool(void)>;
0011 
0012 /** @brief this macro executes an operation after a given lambda
0013  */
0014 #define PUSH_LAMBDA(operation, lambda)                                                                                                                         \
0015     lambda = [lambda, operation]() {                                                                                                                           \
0016         bool v = lambda();                                                                                                                                     \
0017         return v && operation();                                                                                                                               \
0018     };
0019 
0020 /** @brief this macro executes an operation before a given lambda
0021  */
0022 #define PUSH_FRONT_LAMBDA(operation, lambda)                                                                                                                   \
0023     lambda = [lambda, operation]() {                                                                                                                           \
0024         bool v = operation();                                                                                                                                  \
0025         return v && lambda();                                                                                                                                  \
0026     };
0027 
0028 #include <QUndoCommand>
0029 
0030 /** @brief this is a generic class that takes fonctors as undo and redo actions. It just executes them when required by Qt
0031   Note that QUndoStack actually executes redo() when we push the undoCommand to the stack
0032   This is bad for us because we execute the command as we construct the undo Function. So to prevent it to be executed twice, there is a small hack in this
0033   command that prevent redoing if it has not been undone before.
0034  */
0035 class FunctionalUndoCommand : public QUndoCommand
0036 {
0037 public:
0038     FunctionalUndoCommand(Fun undo, Fun redo, const QString &text, QUndoCommand *parent = nullptr);
0039     void undo() override;
0040     void redo() override;
0041 
0042 private:
0043     Fun m_undo, m_redo;
0044     bool m_undone;
0045 };