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 
60 class UndoStackTransaction final {
61 public:
62  // Constructors / Destructor
63  UndoStackTransaction() = delete;
64  UndoStackTransaction(const UndoStackTransaction& other) = delete;
65  UndoStackTransaction(UndoStack& stack, const QString& text);
66  ~UndoStackTransaction() noexcept;
67 
68  // General Methods
69  void append(UndoCommand* cmd);
70  void abort();
71  void commit();
72 
73  // Operator Overloadings
75 
76 private:
78  bool mCmdActive;
79 };
80 
81 /*******************************************************************************
82  * Class UndoStack
83  ******************************************************************************/
84 
120 class UndoStack final : public QObject {
121  Q_OBJECT
122 
123 public:
124  // Constructors / Destructor
125  UndoStack(const UndoStack& other) = delete;
126  UndoStack& operator=(const UndoStack& rhs) = delete;
127 
131  UndoStack() noexcept;
132 
136  ~UndoStack() noexcept;
137 
138  // Getters
139 
144  QString getUndoText() const noexcept;
145 
150  QString getRedoText() const noexcept;
151 
156  bool canUndo() const noexcept;
157 
162  bool canRedo() const noexcept;
163 
173  bool isClean() const noexcept;
174 
181  bool isCommandGroupActive() const noexcept;
182 
183  // Setters
184 
188  void setClean() noexcept;
189 
190  // General Methods
191 
212  void execCmd(UndoCommand* cmd, bool forceKeepCmd = false);
213 
225  void beginCmdGroup(const QString& text);
226 
239  void appendToCmdGroup(UndoCommand* cmd);
240 
247  void commitCmdGroup();
248 
255  void abortCmdGroup();
256 
268  void undo();
269 
277  void redo();
278 
285  void clear() noexcept;
286 
287 signals:
288  void undoTextChanged(const QString& text);
289  void redoTextChanged(const QString& text);
290  void canUndoChanged(bool canUndo);
291  void canRedoChanged(bool canRedo);
292  void cleanChanged(bool clean);
293  void commandGroupEnded();
294  void commandGroupAborted();
295  void stateModified();
296 
297 private:
304  QList<UndoCommand*> mCommands;
305 
315 
321 
331 };
332 
333 /*******************************************************************************
334  * End of File
335  ******************************************************************************/
336 
337 } // namespace librepcb
338 
339 #endif // LIBREPCB_UNDOSTACK_H
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:314
void commit()
Definition: undostack.cpp:66
void undo()
Undo the last command.
Definition: undostack.cpp:270
UndoCommandGroup * mActiveCommandGroup
If a command group is active at the moment, this is the pointer to it.
Definition: undostack.h:330
void execCmd(UndoCommand *cmd, bool forceKeepCmd=false)
Execute a command and push it to the stack (similar to QUndoStack::push())
Definition: undostack.cpp:137
QList< UndoCommand * > mCommands
This list holds all commands of the undo stack.
Definition: undostack.h:304
The UndoCommandGroup class makes it possible to pack multiple undo commands together (it acts as a pa...
Definition: undocommandgroup.h:46
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:120
The UndoStackTransaction class helps to execute transactions on an UndoStack.
Definition: undostack.h:60
void redoTextChanged(const QString &text)
bool mCmdActive
Definition: undostack.h:78
void abortCmdGroup()
End the currently active command group and revert the changes.
Definition: undostack.cpp:241
void appendToCmdGroup(UndoCommand *cmd)
Append a new command to the currently active command group.
Definition: undostack.cpp:200
bool canUndo() const noexcept
Check if undo is possible.
Definition: undostack.cpp:105
void redo()
Redo the last undoed command.
Definition: undostack.cpp:292
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:77
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:314
void canRedoChanged(bool canRedo)
The UndoCommand class represents a command which you can undo/redo.
Definition: undocommand.h:52
~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
void commitCmdGroup()
End the currently active command group and keep the changes.
Definition: undostack.cpp:219
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:320
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:183
UndoStackTransaction & operator=(const UndoStackTransaction &rhs)=delete