Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef UNDOMANAGER_HPP
10 : #define UNDOMANAGER_HPP
11 :
12 : #include "datacontainer.hpp"
13 : #include "treeviewmodel.hpp"
14 : #include <QApplication>
15 : #include <QClipboard>
16 : #include <QObject>
17 :
18 : class QUndoStack;
19 :
20 : /**
21 : * @brief The UndoManager class
22 : *
23 : * To add your own command, implement a class that inherits from QUndoCommand. This class will have an undo and redo method respectively.
24 : * ATTENTION: you have to put the code of THE FIRST ACTION into the redo method. Redo will be called automatically when the command is
25 : * pushed onto the QUndoStack. If you put the code of the initial action somewhere in your code and then create the new command, the action
26 : * will be performed twice. In the undo method you have to put the code that reverts the initial action. To execute the command, push your
27 : * new command on the stack.
28 : */
29 0 : class UndoManager : public QObject
30 : {
31 : Q_OBJECT
32 :
33 : Q_PROPERTY (bool canUndo READ canUndo () NOTIFY canUndoChanged ())
34 : Q_PROPERTY (bool canRedo READ canRedo () NOTIFY canRedoChanged ())
35 : Q_PROPERTY (bool canPaste READ canPaste () NOTIFY canPasteChanged ())
36 : Q_PROPERTY (QString redoText READ redoText () NOTIFY redoTextChanged ())
37 : Q_PROPERTY (QString undoText READ undoText () NOTIFY undoTextChanged ())
38 : Q_PROPERTY (QString clipboardType READ clipboardType () NOTIFY clipboardTypeChanged ())
39 :
40 : public:
41 : /**
42 : * @brief The default constructor.
43 : * @param parentManager
44 : */
45 : explicit UndoManager (QObject * parentManager = nullptr);
46 :
47 : /**
48 : * @brief The mandatory copy constructor.
49 : * @param other
50 : */
51 0 : UndoManager (UndoManager const & other) : QObject ()
52 : {
53 : Q_UNUSED (other)
54 : }
55 :
56 : /**
57 : * @brief Returns if a command can be undone.
58 : *
59 : * @return True if a command can be undone.
60 : */
61 : bool canUndo () const;
62 :
63 : /**
64 : * @brief Returns if a command can be redone.
65 : *
66 : * @return True if a command can be redone.
67 : */
68 : bool canRedo () const;
69 :
70 : /**
71 : * @brief Returns a textual description of the command on top of the UndoStack.
72 : *
73 : * @return A textual description of the command on top of the UndoStack.
74 : */
75 : QString redoText () const;
76 :
77 : /**
78 : * @brief Returns a textual description of the command on top of the UndoStack.
79 : *
80 : * @return A textual description of the command on top of the UndoStack.
81 : */
82 : QString undoText () const;
83 :
84 : /**
85 : * @brief Returns a textual description of the type of content currently in the clipboard.
86 : *
87 : * @return A textual description of the type of content currently in the clipboard.
88 : */
89 : QString clipboardType () const;
90 :
91 : /**
92 : * @brief Put some content into the clipboard.
93 : *
94 : * @param type A textual description of the type of content.
95 : * @param model The TreeViewModel to put in the clipboard.
96 : * @param idx The index of the ConfigNode.
97 : */
98 : Q_INVOKABLE void putToClipboard (const QString & type, TreeViewModel * model, int idx);
99 :
100 : /**
101 : * @brief Create a new EditKeyCommand.
102 : *
103 : * @param model The TreeViewModel of the edited ConfigNode.
104 : * @param idx The index of the edited ConfigNode.
105 : * @param data This container holds the data needed to undo and redo the edit.
106 : */
107 : Q_INVOKABLE void createEditKeyCommand (TreeViewModel * model, int idx, DataContainer * data);
108 :
109 : /**
110 : * @brief Create a new DeleteKeyCommand.
111 : *
112 : * @param type The type of the command ("deleteKey" or "deleteBranch").
113 : * @param model The TreeViewModel of the deleted ConfigNode.
114 : * @param idx The index of the deleted ConfigNode.
115 : */
116 : Q_INVOKABLE void createDeleteKeyCommand (const QString & type, TreeViewModel * model, int idx);
117 :
118 : /**
119 : * @brief Create a new NewKeyCommand.
120 : *
121 : * @param model The TreeViewModel of the new ConfigNode.
122 : * @param idx The index of the new ConfigNode.
123 : * @param data This container holds the data needed to undo and redo the edit.
124 : * @param isBelow Determines if the new key is a treebranch that requires the treeview to update.
125 : */
126 : Q_INVOKABLE void createNewKeyCommand (TreeViewModel * model, int idx, DataContainer * data, bool isBelow);
127 :
128 : /**
129 : * @brief Copy a ConfigNode into a different TreeViewModel.
130 : * @param model The target TreeViewModel of the copied ConfigNode.
131 : * @param idx The index of the copied ConfigNode.
132 : */
133 : Q_INVOKABLE void createCopyKeyCommand (TreeViewModel * model, int idx);
134 :
135 : /**
136 : * @brief Copy a ConfigNode into a different TreeViewModel and delete the source ConfigNode.
137 : * @param model The target TreeViewModel of the cut ConfigNode.
138 : * @param idx The index of the cut ConfigNode.
139 : */
140 : Q_INVOKABLE void createCutKeyCommand (TreeViewModel * model, int idx);
141 :
142 : /**
143 : * @brief Import a configuration from file.
144 : * @param model The TreeViewModel to paste the configuration into.
145 : * @param idx The index of the ConfigNode that is the root of the imported configuration.
146 : * @param data This container holds the data needed to undo and redo the import.
147 : */
148 : Q_INVOKABLE void createImportConfigurationCommand (TreeViewModel * model, int idx, DataContainer * data);
149 :
150 : /**
151 : * @brief This function is called when the configuration is saved; if the user closes the application
152 : * in a clean state, she will not be asked to save the configuration again.
153 : *
154 : */
155 : Q_INVOKABLE void setClean ();
156 :
157 : /**
158 : * @brief Returns if the UndoStack is in a clean state.
159 : *
160 : * @return True if the UndoStack is in a clean state.
161 : */
162 : Q_INVOKABLE bool isClean () const;
163 :
164 : /**
165 : * @brief Returns if the clipboard is filled.
166 : *
167 : * @return True if the clipboard is filled.
168 : */
169 : Q_INVOKABLE bool canPaste () const;
170 :
171 : /**
172 : * @brief Returns the current index of the UndoStack.
173 : *
174 : * @return The current index of the UndoStack.
175 : */
176 :
177 : Q_INVOKABLE int index () const;
178 :
179 : /**
180 : * @brief Returns the clean index of the UndoStack.
181 : *
182 : * @return The clean index of the UndoStack.
183 : */
184 : Q_INVOKABLE int cleanIndex () const;
185 :
186 : /**
187 : * @brief Returns the size of the UndoStack.
188 : *
189 : * @return The size of the UndoStack..
190 : */
191 : Q_INVOKABLE int count () const;
192 :
193 : /**
194 : * @brief setIndex
195 : * @param idx
196 : */
197 : Q_INVOKABLE void setIndex (int idx);
198 :
199 : Q_SIGNALS:
200 : void canUndoChanged ();
201 : void canRedoChanged ();
202 : void redoTextChanged ();
203 : void undoTextChanged ();
204 : void clipboardTypeChanged ();
205 : void canPasteChanged ();
206 :
207 : public Q_SLOTS:
208 : void undo ();
209 : void redo ();
210 :
211 : private:
212 : QUndoStack * m_undoStack;
213 : QClipboard * m_clipboard;
214 : QString m_clipboardType;
215 : bool m_clipboardEmpty;
216 : TreeViewModel * m_clipboardModel;
217 : };
218 :
219 0 : Q_DECLARE_METATYPE (UndoManager)
220 :
221 : #endif // UNDOMANAGER_HPP
|