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 /*******************************************************************************
21 * Includes
22 ******************************************************************************/
23 #include "undocommand.h"
24
25 #include <QtCore>
26
27 /*******************************************************************************
28 * Namespace
29 ******************************************************************************/
30 namespace librepcb {
31
32 /*******************************************************************************
33 * Constructors / Destructor
34 ******************************************************************************/
35
UndoCommand(const QString & text)36 UndoCommand::UndoCommand(const QString& text) noexcept
37 : mText(text), mIsExecuted(false), mRedoCount(0), mUndoCount(0) {
38 }
39
~UndoCommand()40 UndoCommand::~UndoCommand() noexcept {
41 Q_ASSERT(qAbs(mRedoCount - mUndoCount) <= 1);
42 }
43
44 /*******************************************************************************
45 * General Methods
46 ******************************************************************************/
47
execute()48 bool UndoCommand::execute() {
49 if (mIsExecuted) {
50 throw LogicError(__FILE__, __LINE__);
51 }
52
53 mIsExecuted = true; // set this flag BEFORE performing the execution!
54 bool retval = performExecute(); // can throw
55 mRedoCount++;
56
57 return retval;
58 }
59
undo()60 void UndoCommand::undo() {
61 if (!isCurrentlyExecuted()) {
62 throw LogicError(__FILE__, __LINE__);
63 }
64
65 performUndo(); // can throw
66 mUndoCount++;
67 }
68
redo()69 void UndoCommand::redo() {
70 if ((!wasEverExecuted()) || (isCurrentlyExecuted())) {
71 throw LogicError(__FILE__, __LINE__);
72 }
73
74 performRedo(); // can throw
75 mRedoCount++;
76 }
77
78 /*******************************************************************************
79 * End of File
80 ******************************************************************************/
81
82 } // namespace librepcb
83