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 "cmdboardplaneedit.h"
24 
25 #include "../board.h"
26 
27 #include <librepcb/common/graphics/graphicslayer.h>
28 
29 #include <QtCore>
30 
31 /*******************************************************************************
32  *  Namespace
33  ******************************************************************************/
34 namespace librepcb {
35 namespace project {
36 
37 /*******************************************************************************
38  *  Constructors / Destructor
39  ******************************************************************************/
40 
CmdBoardPlaneEdit(BI_Plane & plane,bool rebuildOnChanges)41 CmdBoardPlaneEdit::CmdBoardPlaneEdit(BI_Plane& plane,
42                                      bool rebuildOnChanges) noexcept
43   : UndoCommand(tr("Edit plane")),
44     mPlane(plane),
45     mDoRebuildOnChanges(rebuildOnChanges),
46     mOldOutline(plane.getOutline()),
47     mNewOutline(mOldOutline),
48     mOldLayerName(plane.getLayerName()),
49     mNewLayerName(mOldLayerName),
50     mOldNetSignal(&plane.getNetSignal()),
51     mNewNetSignal(mOldNetSignal),
52     mOldMinWidth(plane.getMinWidth()),
53     mNewMinWidth(mOldMinWidth),
54     mOldMinClearance(plane.getMinClearance()),
55     mNewMinClearance(mOldMinClearance),
56     mOldConnectStyle(plane.getConnectStyle()),
57     mNewConnectStyle(mOldConnectStyle),
58     mOldPriority(plane.getPriority()),
59     mNewPriority(mOldPriority),
60     mOldKeepOrphans(plane.getKeepOrphans()),
61     mNewKeepOrphans(mOldKeepOrphans) {
62 }
63 
~CmdBoardPlaneEdit()64 CmdBoardPlaneEdit::~CmdBoardPlaneEdit() noexcept {
65   if (!wasEverExecuted()) {
66     mPlane.setOutline(mOldOutline);
67     mPlane.setLayerName(mOldLayerName);
68   }
69 }
70 
71 /*******************************************************************************
72  *  Setters
73  ******************************************************************************/
74 
translate(const Point & deltaPos,bool immediate)75 void CmdBoardPlaneEdit::translate(const Point& deltaPos,
76                                   bool immediate) noexcept {
77   Q_ASSERT(!wasEverExecuted());
78   mNewOutline.translate(deltaPos);
79   if (immediate) mPlane.setOutline(mNewOutline);
80 }
81 
rotate(const Angle & angle,const Point & center,bool immediate)82 void CmdBoardPlaneEdit::rotate(const Angle& angle, const Point& center,
83                                bool immediate) noexcept {
84   Q_ASSERT(!wasEverExecuted());
85   mNewOutline.rotate(angle, center);
86   if (immediate) mPlane.setOutline(mNewOutline);
87 }
88 
mirror(const Point & center,Qt::Orientation orientation,bool immediate)89 void CmdBoardPlaneEdit::mirror(const Point& center, Qt::Orientation orientation,
90                                bool immediate) noexcept {
91   setLayerName(
92       GraphicsLayerName(GraphicsLayer::getMirroredLayerName(*mNewLayerName)),
93       immediate);
94   setOutline(mNewOutline.mirrored(orientation, center), immediate);
95 }
96 
setOutline(const Path & outline,bool immediate)97 void CmdBoardPlaneEdit::setOutline(const Path& outline,
98                                    bool immediate) noexcept {
99   Q_ASSERT(!wasEverExecuted());
100   mNewOutline = outline;
101   if (immediate) mPlane.setOutline(mNewOutline);
102 }
103 
setLayerName(const GraphicsLayerName & layerName,bool immediate)104 void CmdBoardPlaneEdit::setLayerName(const GraphicsLayerName& layerName,
105                                      bool immediate) noexcept {
106   Q_ASSERT(!wasEverExecuted());
107   mNewLayerName = layerName;
108   if (immediate) mPlane.setLayerName(mNewLayerName);
109 }
110 
setNetSignal(NetSignal & netsignal)111 void CmdBoardPlaneEdit::setNetSignal(NetSignal& netsignal) noexcept {
112   Q_ASSERT(!wasEverExecuted());
113   mNewNetSignal = &netsignal;
114 }
115 
setMinWidth(const UnsignedLength & minWidth)116 void CmdBoardPlaneEdit::setMinWidth(const UnsignedLength& minWidth) noexcept {
117   Q_ASSERT(!wasEverExecuted());
118   mNewMinWidth = minWidth;
119 }
120 
setMinClearance(const UnsignedLength & minClearance)121 void CmdBoardPlaneEdit::setMinClearance(
122     const UnsignedLength& minClearance) noexcept {
123   Q_ASSERT(!wasEverExecuted());
124   mNewMinClearance = minClearance;
125 }
126 
setConnectStyle(BI_Plane::ConnectStyle style)127 void CmdBoardPlaneEdit::setConnectStyle(BI_Plane::ConnectStyle style) noexcept {
128   Q_ASSERT(!wasEverExecuted());
129   mNewConnectStyle = style;
130 }
131 
setPriority(int priority)132 void CmdBoardPlaneEdit::setPriority(int priority) noexcept {
133   Q_ASSERT(!wasEverExecuted());
134   mNewPriority = priority;
135 }
136 
setKeepOrphans(bool keepOrphans)137 void CmdBoardPlaneEdit::setKeepOrphans(bool keepOrphans) noexcept {
138   Q_ASSERT(!wasEverExecuted());
139   mNewKeepOrphans = keepOrphans;
140 }
141 
142 /*******************************************************************************
143  *  Inherited from UndoCommand
144  ******************************************************************************/
145 
performExecute()146 bool CmdBoardPlaneEdit::performExecute() {
147   performRedo();  // can throw
148 
149   if (mNewOutline != mOldOutline) return true;
150   if (mNewLayerName != mOldLayerName) return true;
151   if (mNewNetSignal != mOldNetSignal) return true;
152   if (mNewMinWidth != mOldMinWidth) return true;
153   if (mNewMinClearance != mOldMinClearance) return true;
154   if (mNewConnectStyle != mOldConnectStyle) return true;
155   if (mNewPriority != mOldPriority) return true;
156   if (mNewKeepOrphans != mOldKeepOrphans) return true;
157   return false;
158 }
159 
performUndo()160 void CmdBoardPlaneEdit::performUndo() {
161   mPlane.setNetSignal(*mOldNetSignal);  // can throw
162   mPlane.setOutline(mOldOutline);
163   mPlane.setLayerName(mOldLayerName);
164   mPlane.setMinWidth(mOldMinWidth);
165   mPlane.setMinClearance(mOldMinClearance);
166   mPlane.setConnectStyle(mOldConnectStyle);
167   mPlane.setPriority(mOldPriority);
168   mPlane.setKeepOrphans(mOldKeepOrphans);
169 
170   // rebuild all planes to see the changes
171   if (mDoRebuildOnChanges) mPlane.getBoard().rebuildAllPlanes();
172 }
173 
performRedo()174 void CmdBoardPlaneEdit::performRedo() {
175   mPlane.setNetSignal(*mNewNetSignal);  // can throw
176   mPlane.setOutline(mNewOutline);
177   mPlane.setLayerName(mNewLayerName);
178   mPlane.setMinWidth(mNewMinWidth);
179   mPlane.setMinClearance(mNewMinClearance);
180   mPlane.setConnectStyle(mNewConnectStyle);
181   mPlane.setPriority(mNewPriority);
182   mPlane.setKeepOrphans(mNewKeepOrphans);
183 
184   // rebuild all planes to see the changes
185   if (mDoRebuildOnChanges) mPlane.getBoard().rebuildAllPlanes();
186 }
187 
188 /*******************************************************************************
189  *  End of File
190  ******************************************************************************/
191 
192 }  // namespace project
193 }  // namespace librepcb
194