LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
undocommand.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_UNDOCOMMAND_H
21 #define LIBREPCB_UNDOCOMMAND_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 /*******************************************************************************
36  * Class UndoCommand
37  ******************************************************************************/
38 
47 class UndoCommand {
48  Q_DECLARE_TR_FUNCTIONS(UndoCommand)
49 
50 public:
51  // Constructors / Destructor
52  UndoCommand() = delete;
53  UndoCommand(const UndoCommand& other) = delete;
54  explicit UndoCommand(const QString& text) noexcept;
55  virtual ~UndoCommand() noexcept;
56 
57  // Getters
58  const QString& getText() const noexcept { return mText; }
59 
64  bool wasEverExecuted() const noexcept { return (mRedoCount > 0); }
65 
70  bool wasEverReverted() const noexcept { return (mUndoCount > 0); }
71 
76  bool isCurrentlyExecuted() const noexcept { return mRedoCount > mUndoCount; }
77 
78  // General Methods
79 
87  virtual bool execute() final;
88 
92  virtual void undo() final;
93 
97  virtual void redo() final;
98 
99  // Operator Overloadings
100  UndoCommand& operator=(const UndoCommand& rhs) = delete;
101 
102 protected:
114  virtual bool performExecute() = 0;
115 
121  virtual void performUndo() = 0;
122 
128  virtual void performRedo() = 0;
129 
130 private:
131  QString mText;
132  bool mIsExecuted;
135 };
136 
137 /*******************************************************************************
138  * End of File
139  ******************************************************************************/
140 
141 } // namespace librepcb
142 
143 #endif // LIBREPCB_UNDOCOMMAND_H
virtual void performRedo()=0
Redo the command.
const QString & getText() const noexcept
Definition: undocommand.h:58
bool wasEverExecuted() const noexcept
This method shows whether that command was ever executed (execute() called successfully) ...
Definition: undocommand.h:64
bool isCurrentlyExecuted() const noexcept
This method shows whether that command is currently executed (redo() called one time more than undo()...
Definition: undocommand.h:76
virtual bool execute() final
Execute the command (must only be called once)
Definition: undocommand.cpp:48
virtual bool performExecute()=0
Execute the command the first time.
QString mText
Definition: undocommand.h:131
virtual ~UndoCommand() noexcept
Definition: undocommand.cpp:40
bool wasEverReverted() const noexcept
This method shows whether that command was ever reverted (undo() called at least one time) ...
Definition: undocommand.h:70
int mRedoCount
Counter of how often redo() was called.
Definition: undocommand.h:133
bool mIsExecuted
Shows whether execute() was called or not.
Definition: undocommand.h:132
int mUndoCount
Counter of how often undo() was called.
Definition: undocommand.h:134
virtual void undo() final
Undo the command.
Definition: undocommand.cpp:60
The UndoCommand class represents a command which you can undo/redo.
Definition: undocommand.h:47
virtual void performUndo()=0
Undo the command.
virtual void redo() final
Redo the command.
Definition: undocommand.cpp:69