Debian Codegraph @

include/iundo.h

raw
1 #pragma once
2
3 /// \file
4 /// \brief The undo-system interface. Uses the 'memento' pattern.
5
6 #include "imodule.h"
7 #include "imap.h"
8 #include <cstddef>
9 #include <memory>
10 #include <sigc++/signal.h>
11
12 /**
13 * greebo: An UndoMemento has to be allocated on the heap
14 * and contains all the information that is needed to describe
15 * the status of an Undoable.
16 */
17 class IUndoMemento
18 {
19 public:
20 virtual ~IUndoMemento() {}
21 };
22 typedef std::shared_ptr<IUndoMemento> IUndoMementoPtr;
23
24 /* greebo: This is the abstract base class for an Undoable object.
25 * Derive from this class if your instance/object should be Undoable.
26 *
27 * The exportState method has to allocate and return a new UndoMemento
28 * with all the necessary data to restore the current state.
29 *
30 * The importState() method should re-import the values saved in the
31 * UndoMemento
32 */
33 class IUndoable
34 {
35 public:
36 virtual ~IUndoable() {}
37 virtual IUndoMementoPtr exportState() const = 0;
38 virtual void importState(const IUndoMementoPtr& state) = 0;
39
40 // Optional method that is invoked after the whole snapshot has been restored,
41 // applicable to both undo or redo operations.
42 // May be used by Undoable objects to perform a post-undo cleanup.
43 virtual void onOperationRestored()
44 {}
45 };
46
47 /**
48 * Undoables request their associated StateSaver to save their current state.
49 * The state saver might call the Undoable's exportState() method or not,
50 * depending on whether the Undoable has already been saved during
51 * the current operation's lifetime.
52 * To acquire an UndoStateSaver, use UndoSystem::getStateSaver().
53 */
54 class IUndoStateSaver
55 {
56 public:
57 virtual ~IUndoStateSaver() {}
58
59 // Request a state save of the associated IUndoable
60 virtual void saveState() = 0;
61
62 // Returns the undo system this saver is associated to
63 virtual IUndoSystem& getUndoSystem() = 0;
64 };
65
66 class IUndoSystem
67 {
68 public:
69 using Ptr = std::shared_ptr<IUndoSystem>;
70
71 // Undoable objects need to call this to get hold of a StateSaver instance
72 // which will take care of exporting and saving the state.
73 virtual IUndoStateSaver* getStateSaver(IUndoable& undoable) = 0;
74 virtual void releaseStateSaver(IUndoable& undoable) = 0;
75
76 virtual void start() = 0;
77 virtual void finish(const std::string& command) = 0;
78 virtual void undo() = 0;
79 virtual void redo() = 0;
80 virtual void clear() = 0;
81
82 // Returns true if an operation is already started
83 virtual bool operationStarted() const = 0;
84
85 // greebo: This finishes the current operation and removes
86 // it immediately from the stack, therefore it never existed.
87 virtual void cancel() = 0;
88
89 enum class EventType
90 {
91 OperationRecorded,
92 OperationUndone,
93 OperationRedone,
94 AllOperationsCleared,
95 };
96
97 /**
98 * Emitted on edit/undo/redo and clear events, passes the operation type and name
99 * as arguments. Except for AllOperationsCleared, which will have an empty name argument.
100 */
101 virtual sigc::signal<void(EventType, const std::string&)>& signal_undoEvent() = 0;
102 };
103
104 class IUndoSystemFactory :
105 public RegisterableModule
106 {
107 public:
108 virtual ~IUndoSystemFactory() {}
109
110 // Create a new UndoSystem instance for use in a map root node
111 virtual IUndoSystem::Ptr createUndoSystem() = 0;
112 };
113
114 constexpr const char* const MODULE_UNDOSYSTEM_FACTORY("UndoSystemFactory");
115
116 inline IUndoSystemFactory& GlobalUndoSystemFactory()
117 {
118 static module::InstanceReference<IUndoSystemFactory> _reference(MODULE_UNDOSYSTEM_FACTORY);
119 return _reference;
120 }
121
122 // The accessor function to the main map's undo system
123 inline IUndoSystem& GlobalUndoSystem()
124 {
125 return GlobalMapModule().getUndoSystem();
126 }
127
128 class UndoableCommand
129 {
130 const std::string _command;
131 bool _shouldFinish;
132 public:
133
134 UndoableCommand(const std::string& command) :
135 _command(command),
136 _shouldFinish(false)
137 {
138 // Avoid double-starting undo operations
139 if (!GlobalUndoSystem().operationStarted())
140 {
141 GlobalUndoSystem().start();
142 _shouldFinish = true;
143 }
144 }
145
146 ~UndoableCommand()
147 {
148 if (_shouldFinish)
149 {
150 GlobalUndoSystem().finish(_command);
151 }
152 }
153 };