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 "cmdboardviaedit.h"
24
25 #include "../items/bi_via.h"
26
27 #include <QtCore>
28
29 /*******************************************************************************
30 * Namespace
31 ******************************************************************************/
32 namespace librepcb {
33 namespace project {
34
35 /*******************************************************************************
36 * Constructors / Destructor
37 ******************************************************************************/
38
CmdBoardViaEdit(BI_Via & via)39 CmdBoardViaEdit::CmdBoardViaEdit(BI_Via& via) noexcept
40 : UndoCommand(tr("Edit via")),
41 mVia(via),
42 mOldPos(via.getPosition()),
43 mNewPos(mOldPos),
44 mOldShape(via.getShape()),
45 mNewShape(mOldShape),
46 mOldSize(via.getSize()),
47 mNewSize(mOldSize),
48 mOldDrillDiameter(via.getDrillDiameter()),
49 mNewDrillDiameter(mOldDrillDiameter) {
50 }
51
~CmdBoardViaEdit()52 CmdBoardViaEdit::~CmdBoardViaEdit() noexcept {
53 if (!wasEverExecuted()) {
54 mVia.setPosition(mOldPos);
55 mVia.setShape(mOldShape);
56 mVia.setSize(mOldSize);
57 mVia.setDrillDiameter(mOldDrillDiameter);
58 }
59 }
60
61 /*******************************************************************************
62 * Setters
63 ******************************************************************************/
64
setPosition(const Point & pos,bool immediate)65 void CmdBoardViaEdit::setPosition(const Point& pos, bool immediate) noexcept {
66 Q_ASSERT(!wasEverExecuted());
67 mNewPos = pos;
68 if (immediate) mVia.setPosition(mNewPos);
69 }
70
translate(const Point & deltaPos,bool immediate)71 void CmdBoardViaEdit::translate(const Point& deltaPos,
72 bool immediate) noexcept {
73 Q_ASSERT(!wasEverExecuted());
74 mNewPos += deltaPos;
75 if (immediate) mVia.setPosition(mNewPos);
76 }
77
rotate(const Angle & angle,const Point & center,bool immediate)78 void CmdBoardViaEdit::rotate(const Angle& angle, const Point& center,
79 bool immediate) noexcept {
80 Q_ASSERT(!wasEverExecuted());
81 mNewPos.rotate(angle, center);
82 if (immediate) mVia.setPosition(mNewPos);
83 }
84
setShape(Via::Shape shape,bool immediate)85 void CmdBoardViaEdit::setShape(Via::Shape shape, bool immediate) noexcept {
86 Q_ASSERT(!wasEverExecuted());
87 mNewShape = shape;
88 if (immediate) mVia.setShape(mNewShape);
89 }
90
setSize(const PositiveLength & size,bool immediate)91 void CmdBoardViaEdit::setSize(const PositiveLength& size,
92 bool immediate) noexcept {
93 Q_ASSERT(!wasEverExecuted());
94 mNewSize = size;
95 if (immediate) mVia.setSize(mNewSize);
96 }
97
setDrillDiameter(const PositiveLength & diameter,bool immediate)98 void CmdBoardViaEdit::setDrillDiameter(const PositiveLength& diameter,
99 bool immediate) noexcept {
100 Q_ASSERT(!wasEverExecuted());
101 mNewDrillDiameter = diameter;
102 if (immediate) mVia.setDrillDiameter(mNewDrillDiameter);
103 }
104
105 /*******************************************************************************
106 * Inherited from UndoCommand
107 ******************************************************************************/
108
performExecute()109 bool CmdBoardViaEdit::performExecute() {
110 performRedo(); // can throw
111
112 return true; // TODO: determine if the via was really modified
113 }
114
performUndo()115 void CmdBoardViaEdit::performUndo() {
116 mVia.setPosition(mOldPos);
117 mVia.setShape(mOldShape);
118 mVia.setSize(mOldSize);
119 mVia.setDrillDiameter(mOldDrillDiameter);
120 }
121
performRedo()122 void CmdBoardViaEdit::performRedo() {
123 mVia.setPosition(mNewPos);
124 mVia.setShape(mNewShape);
125 mVia.setSize(mNewSize);
126 mVia.setDrillDiameter(mNewDrillDiameter);
127 }
128
129 /*******************************************************************************
130 * End of File
131 ******************************************************************************/
132
133 } // namespace project
134 } // namespace librepcb
135