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 
52 class UndoCommand {
53  Q_DECLARE_TR_FUNCTIONS(UndoCommand)
54 
55 public:
56  // Constructors / Destructor
57  UndoCommand() = delete;
58  UndoCommand(const UndoCommand& other) = delete;
59  explicit UndoCommand(const QString& text) noexcept;
60  virtual ~UndoCommand() noexcept;
61 
62  // Getters
63  const QString& getText() const noexcept { return mText; }
64 
69  bool wasEverExecuted() const noexcept { return (mRedoCount > 0); }
70 
75  bool wasEverReverted() const noexcept { return (mUndoCount > 0); }
76 
81  bool isCurrentlyExecuted() const noexcept { return mRedoCount > mUndoCount; }
82 
83  // General Methods
84 
92  virtual bool execute() final;
93 
97  virtual void undo() final;
98 
102  virtual void redo() final;
103 
104  // Operator Overloadings
105  UndoCommand& operator=(const UndoCommand& rhs) = delete;
106 
107 protected:
119  virtual bool performExecute() = 0;
120 
126  virtual void performUndo() = 0;
127 
133  virtual void performRedo() = 0;
134 
135 private:
136  QString mText;
137  bool mIsExecuted;
140 };
141 
142 /*******************************************************************************
143  * End of File
144  ******************************************************************************/
145 
146 } // namespace librepcb
147 
148 #endif // LIBREPCB_UNDOCOMMAND_H
virtual void performRedo()=0
Redo the command.
const QString & getText() const noexcept
Definition: undocommand.h:63
bool wasEverExecuted() const noexcept
This method shows whether that command was ever executed (execute() called successfully) ...
Definition: undocommand.h:69
bool isCurrentlyExecuted() const noexcept
This method shows whether that command is currently executed (redo() called one time more than undo()...
Definition: undocommand.h:81
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:136
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:75
int mRedoCount
Counter of how often redo() was called.
Definition: undocommand.h:138
bool mIsExecuted
Shows whether execute() was called or not.
Definition: undocommand.h:137
int mUndoCount
Counter of how often undo() was called.
Definition: undocommand.h:139
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:52
virtual void performUndo()=0
Undo the command.
virtual void redo() final
Redo the command.
Definition: undocommand.cpp:69