LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
bool commitCmdGroup()
End the currently active command group and keep the changes.
Definition: undostack.cpp:222
bool isCommandGroupActive() const noexcept
Check if a command group is active at the moment (see mActiveCommandGroup)
Definition: undostack.cpp:117
void setClean() noexcept
Set the current state as the clean state (see also isClean())
Definition: undostack.cpp:125
void clear() noexcept
Clear the whole stack (delete all UndoCommand objects)
Definition: undostack.cpp:318
void commit()
Definition: undostack.cpp:66
void undo()
Undo the last command.
Definition: undostack.cpp:274
UndoCommandGroup * mActiveCommandGroup
If a command group is active at the moment, this is the pointer to it.
Definition: undostack.h:326
bool appendToCmdGroup(UndoCommand *cmd)
Append a new command to the currently active command group.
Definition: undostack.cpp:201
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
QString getUndoText() const noexcept
Get the text for the undo action.
Definition: undostack.cpp:91
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
void redoTextChanged(const QString &text)
bool mCmdActive
Definition: undostack.h:75
void abortCmdGroup()
End the currently active command group and revert the changes.
Definition: undostack.cpp:245
bool canUndo() const noexcept
Check if undo is possible.
Definition: undostack.cpp:105
void redo()
Redo the last undoed command.
Definition: undostack.cpp:296
QString getRedoText() const noexcept
Get the text for the redo action.
Definition: undostack.cpp:98
~UndoStackTransaction() noexcept
Definition: undostack.cpp:46
UndoStack & mStack
Definition: undostack.h:74
bool canRedo() const noexcept
Check if redo is possible.
Definition: undostack.cpp:109
int mCurrentIndex
This attribute holds the current position in the undo stack mCommands.
Definition: undostack.h:310
void canRedoChanged(bool canRedo)
The UndoCommand class represents a command which you can undo/redo.
Definition: undocommand.h:47
~UndoStack() noexcept
The destructor (will also call clear())
Definition: undostack.cpp:83
void abort()
Definition: undostack.cpp:60
void canUndoChanged(bool canUndo)
void undoTextChanged(const QString &text)
UndoStack & operator=(const UndoStack &rhs)=delete
UndoStack() noexcept
The default constructor.
Definition: undostack.cpp:76
bool isClean() const noexcept
Check if the stack is in a clean state (the state of the last setClean())
Definition: undostack.cpp:113
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
void cleanChanged(bool clean)
void beginCmdGroup(const QString &text)
Begin building a new command group that consists of multiple commands step by step (over a "long" tim...
Definition: undostack.cpp:184
UndoStackTransaction & operator=(const UndoStackTransaction &rhs)=delete
bool execCmd(UndoCommand *cmd, bool forceKeepCmd=false)
Execute a command and push it to the stack (similar to QUndoStack::push())
Definition: undostack.cpp:137