File indexing completed on 2024-05-19 05:42:24
0001 // ct_lvtqtw_undo_manager.h -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #ifndef DEFINED_CT_LVTQTW_UNDO_MANAGER_H 0021 #define DEFINED_CT_LVTQTW_UNDO_MANAGER_H 0022 0023 #include <lvtqtc_export.h> 0024 0025 #include <QObject> 0026 #include <memory> 0027 0028 class QUndoCommand; 0029 class QUndoStack; 0030 class QMainWindow; 0031 0032 namespace Codethink::lvtqtc { 0033 0034 class LVTQTC_EXPORT UndoManager : public QObject { 0035 // manages a collection of views and their undo / redo stack. 0036 Q_OBJECT 0037 public: 0038 class LVTQTC_EXPORT IgnoreFirstRunMixin { 0039 // Mixin to be used with the UndoManager with helper method to ignore the first run. 0040 // Qt calls redo() when pushing the UndoCommand for the first time, but we do not want it to be run, as the 0041 // first run can lead to failures. To use this class, simply extend the undoCommand with IgnoreFirstRunMixin 0042 // and write 'IGNORE_FIRST_CALL' as the first command of your redo() method. 0043 0044 public: 0045 #define IGNORE_FIRST_CALL \ 0046 if (isFirstRun) { \ 0047 isFirstRun = false; \ 0048 return; \ 0049 } 0050 0051 protected: 0052 bool isFirstRun = true; 0053 }; 0054 0055 UndoManager(); 0056 ~UndoManager() override; 0057 0058 void undoGroupRequested(const QString& groupName); 0059 void addUndoCommand(QUndoCommand *command); 0060 void undoGroupFinished(); 0061 void undo(); 0062 void redo(); 0063 void clear(); 0064 0065 void createDock(QMainWindow *mainWindow); 0066 0067 Q_SIGNAL void onBeforeUndo(const QUndoCommand *); 0068 Q_SIGNAL void onBeforeRedo(const QUndoCommand *); 0069 0070 private: 0071 struct Private; 0072 std::unique_ptr<Private> d; 0073 }; 0074 0075 } // namespace Codethink::lvtqtc 0076 0077 #endif