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 
159  uint getUniqueStateId() const noexcept;
160 
171  bool isClean() const noexcept;
172 
179  bool isCommandGroupActive() const noexcept;
180 
181  // Setters
182 
186  void setClean() noexcept;
187 
188  // General Methods
189 
213  bool execCmd(UndoCommand* cmd, bool forceKeepCmd = false);
214 
226  void beginCmdGroup(const QString& text);
227 
243  bool appendToCmdGroup(UndoCommand* cmd);
244 
254  bool commitCmdGroup();
255 
262  void abortCmdGroup();
263 
275  void undo();
276 
284  void redo();
285 
292  void clear() noexcept;
293 
294 signals:
295  void undoTextChanged(const QString& text);
296  void redoTextChanged(const QString& text);
297  void canUndoChanged(bool canUndo);
298  void canRedoChanged(bool canRedo);
299  void cleanChanged(bool clean);
300  void commandGroupEnded();
301  void commandGroupAborted();
302  void stateModified();
303 
304 private:
311  QList<UndoCommand*> mCommands;
312 
322 
328 
338 };
339 
340 /*******************************************************************************
341  * End of File
342  ******************************************************************************/
343 
344 } // namespace librepcb
345 
346 #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:337
Definition: airwiresbuilder.cpp:32
QList< UndoCommand * > mCommands
This list holds all commands of the undo stack.
Definition: undostack.h:311
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:321
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:327
void append(UndoCommand *cmd)
Definition: undostack.cpp:55
UndoStackTransaction & operator=(const UndoStackTransaction &rhs)=delete