LibrePCB Developers Documentation
undostack.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_UNDOSTACK_H
21 #define LIBREPCB_UNDOSTACK_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "exceptions.h"
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 class UndoStack;
36 class UndoCommand;
37 class UndoCommandGroup;
38 
39 /*******************************************************************************
40  * Class UndoStackTransaction
41  ******************************************************************************/
42 
57 class UndoStackTransaction final {
58 public:
59  // Constructors / Destructor
60  UndoStackTransaction() = delete;
61  UndoStackTransaction(const UndoStackTransaction& other) = delete;
62  UndoStackTransaction(UndoStack& stack, const QString& text);
63  ~UndoStackTransaction() noexcept;
64 
65  // General Methods
66  void append(UndoCommand* cmd);
67  void abort();
68  void commit();
69 
70  // Operator Overloadings
72 
73 private:
75  bool mCmdActive;
76 };
77 
78 /*******************************************************************************
79  * Class UndoStack
80  ******************************************************************************/
81 
107 class UndoStack final : public QObject {
108  Q_OBJECT
109 
110 public:
111  // Constructors / Destructor
112  UndoStack(const UndoStack& other) = delete;
113  UndoStack& operator=(const UndoStack& rhs) = delete;
114 
118  UndoStack() noexcept;
119 
123  ~UndoStack() noexcept;
124 
125  // Getters
126 
131  QString getUndoText() const noexcept;
132 
137  QString getRedoText() const noexcept;
138 
143  bool canUndo() const noexcept;
144 
149  bool canRedo() const noexcept;
150 
160  bool isClean() const noexcept;
161 
168  bool isCommandGroupActive() const noexcept;
169 
170  // Setters
171 
175  void setClean() noexcept;
176 
177  // General Methods
178 
202  bool execCmd(UndoCommand* cmd, bool forceKeepCmd = false);
203 
215  void beginCmdGroup(const QString& text);
216 
232  bool appendToCmdGroup(UndoCommand* cmd);
233 
243  bool commitCmdGroup();
244 
251  void abortCmdGroup();
252 
264  void undo();
265 
273  void redo();
274 
281  void clear() noexcept;
282 
283 signals:
284  void undoTextChanged(const QString& text);
285  void redoTextChanged(const QString& text);
286  void canUndoChanged(bool canUndo);
287  void canRedoChanged(bool canRedo);
288  void cleanChanged(bool clean);
289  void commandGroupEnded();
290  void commandGroupAborted();
291  void stateModified();
292 
293 private:
300  QList<UndoCommand*> mCommands;
301 
311 
317 
327 };
328 
329 /*******************************************************************************
330  * End of File
331  ******************************************************************************/
332 
333 } // namespace librepcb
334 
335 #endif // LIBREPCB_UNDOSTACK_H
void commit()
Definition: undostack.cpp:66
UndoCommandGroup * mActiveCommandGroup
If a command group is active at the moment, this is the pointer to it.
Definition: undostack.h:326
Definition: airwiresbuilder.cpp:32
QList< UndoCommand * > mCommands
This list holds all commands of the undo stack.
Definition: undostack.h:300
The UndoCommandGroup class makes it possible to pack multiple undo commands together (it acts as a pa...
Definition: undocommandgroup.h:43
The UndoStack class holds UndoCommand objects and provides undo/redo commands.
Definition: undostack.h:107
The UndoStackTransaction class helps to execute transactions on an UndoStack.
Definition: undostack.h:57
bool mCmdActive
Definition: undostack.h:75
~UndoStackTransaction() noexcept
Definition: undostack.cpp:46
UndoStack & mStack
Definition: undostack.h:74
int mCurrentIndex
This attribute holds the current position in the undo stack mCommands.
Definition: undostack.h:310
The UndoCommand class represents a command which you can undo/redo.
Definition: undocommand.h:47
void abort()
Definition: undostack.cpp:60
int mCleanIndex
The index of the command list where the stack was cleaned the last time.
Definition: undostack.h:316
void append(UndoCommand *cmd)
Definition: undostack.cpp:55
UndoStackTransaction & operator=(const UndoStackTransaction &rhs)=delete