KiCad PCB EDA Suite
target_edit.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2013 KiCad Developers, see change_log.txt for contributors.
6  *
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #include <fctsys.h>
32 #include <class_drawpanel.h>
33 #include <pcb_edit_frame.h>
34 #include <dialog_helpers.h>
35 #include <base_units.h>
36 #include <gr_basic.h>
37 #include <board_commit.h>
38 
39 #include <class_board.h>
40 #include <class_pcb_target.h>
41 
42 #include <pcbnew.h>
44 #include <widgets/unit_binder.h>
45 
46 // Routines Locales
47 static void AbortMoveAndEditTarget( EDA_DRAW_PANEL* Panel, wxDC* DC );
49  wxDC* aDC,
50  const wxPoint& aPosition,
51  bool aErase );
52 
53 // Local variables :
54 static int MireDefaultSize = Millimeter2iu( 5 );
55 
56 static PCB_TARGET s_TargetCopy( NULL ); // Used to store "old" values of the current item
57  // parameters before editing for undo/redo/cancel
58 
59 /**********************************/
60 /* class DIALOG_TARGET_PROPERTIES */
61 /**********************************/
62 
64 {
65 private:
67  wxDC* m_DC;
69 
72 
73 public:
74  DIALOG_TARGET_PROPERTIES( PCB_EDIT_FRAME* aParent, PCB_TARGET* aTarget, wxDC* aDC );
76 
77 private:
78  bool TransferDataToWindow() override;
79  bool TransferDataFromWindow() override;
80 };
81 
82 
84 {
85  DIALOG_TARGET_PROPERTIES dialog( this, aTarget, DC );
86 
87  dialog.ShowModal();
88 }
89 
90 
92  wxDC* aDC ) :
94  m_Parent( aParent ),
95  m_DC( aDC ),
96  m_Target( aTarget ),
97  m_Size( aParent, m_sizeLabel, m_sizeCtrl, m_sizeUnits, true, 0 ),
99 {
100  m_sdbSizerButtsOK->SetDefault();
101 
103 
104  // Now all widgets have the size fixed, call FinishDialogSettings
106 }
107 
108 
110 {
113 
114  m_TargetShape->SetSelection( m_Target->GetShape() ? 1 : 0 );
115 
116  return true;
117 }
118 
119 
121 {
122  BOARD_COMMIT commit( m_Parent );
123  commit.Modify( m_Target );
124 
125  if( m_DC )
127 
128  // Save old item in undo list, if is is not currently edited (will be later if so)
129  bool pushCommit = ( m_Target->GetFlags() == 0 );
130 
131  if( m_Target->GetFlags() != 0 ) // other edit in progress (MOVE, NEW ..)
132  m_Target->SetFlags( IN_EDIT ); // set flag in edit to force
133  // undo/redo/abort proper operation
134 
137  m_Target->SetShape( m_TargetShape->GetSelection() ? 1 : 0 );
138 
139  if( m_DC )
141 
142  if( pushCommit )
143  commit.Push( _( "Modified alignment target" ) );
144 
145  return true;
146 }
147 
148 
149 void PCB_EDIT_FRAME::DeleteTarget( PCB_TARGET* aTarget, wxDC* DC )
150 {
151  if( aTarget == NULL )
152  return;
153 
154  aTarget->Draw( m_canvas, DC, GR_XOR );
155  SaveCopyInUndoList( aTarget, UR_DELETED );
156  aTarget->UnLink();
157 }
158 
159 
160 static void AbortMoveAndEditTarget( EDA_DRAW_PANEL* Panel, wxDC* DC )
161 {
162  BASE_SCREEN* screen = Panel->GetScreen();
163  PCB_TARGET* target = (PCB_TARGET*) screen->GetCurItem();
164 
165  ( (PCB_EDIT_FRAME*) Panel->GetParent() )->SetCurItem( NULL );
166 
167  Panel->SetMouseCapture( NULL, NULL );
168 
169  if( target == NULL )
170  return;
171 
172  target->Draw( Panel, DC, GR_XOR );
173 
174  if( target->IsNew() ) // If it is new, delete it
175  {
176  target->Draw( Panel, DC, GR_XOR );
177  target->DeleteStructure();
178  target = NULL;
179  }
180  else // it is an existing item: retrieve initial values of parameters
181  {
182  if( ( target->GetFlags() & (IN_EDIT | IS_MOVED) ) )
183  {
184  target->SetPosition( s_TargetCopy.GetPosition() );
185  target->SetWidth( s_TargetCopy.GetWidth() );
186  target->SetSize( s_TargetCopy.GetSize() );
187  target->SetShape( s_TargetCopy.GetShape() );
188  }
189 
190  target->ClearFlags();
191  target->Draw( Panel, DC, GR_OR );
192  }
193 }
194 
195 
197 {
198  PCB_TARGET* target = new PCB_TARGET( GetBoard() );
199 
200  target->SetFlags( IS_NEW );
201 
202  GetBoard()->Add( target );
203 
204  target->SetLayer( Edge_Cuts );
205  target->SetWidth( GetDesignSettings().GetLineThickness( Edge_Cuts ) );
206  target->SetSize( MireDefaultSize );
207  target->SetPosition( GetCrossHairPosition() );
208 
209  PlaceTarget( target, DC );
210 
211  return target;
212 }
213 
214 
215 void PCB_EDIT_FRAME::BeginMoveTarget( PCB_TARGET* aTarget, wxDC* DC )
216 {
217  if( aTarget == NULL )
218  return;
219 
220  s_TargetCopy = *aTarget;
221  aTarget->SetFlags( IS_MOVED );
222  m_canvas->SetMouseCapture( ShowTargetShapeWhileMovingMouse, AbortMoveAndEditTarget );
223  SetCurItem( aTarget );
224 }
225 
226 
227 void PCB_EDIT_FRAME::PlaceTarget( PCB_TARGET* aTarget, wxDC* DC )
228 {
229  if( aTarget == NULL )
230  return;
231 
232  aTarget->Draw( m_canvas, DC, GR_OR );
233  m_canvas->SetMouseCapture( NULL, NULL );
234  SetCurItem( NULL );
235  OnModify();
236 
237  if( aTarget->IsNew() )
238  {
239  SaveCopyInUndoList( aTarget, UR_NEW );
240  aTarget->ClearFlags();
241  return;
242  }
243 
244  if( aTarget->GetFlags() == IS_MOVED )
245  {
246  SaveCopyInUndoList( aTarget, UR_MOVED,
247  aTarget->GetPosition() - s_TargetCopy.GetPosition() );
248  aTarget->ClearFlags();
249  return;
250  }
251 
252  if( (aTarget->GetFlags() & IN_EDIT) )
253  {
254  aTarget->SwapData( &s_TargetCopy );
255  SaveCopyInUndoList( aTarget, UR_CHANGED );
256  aTarget->SwapData( &s_TargetCopy );
257  }
258 
259  aTarget->ClearFlags();
260 }
261 
262 
263 // Redraw the contour of the track while moving the mouse
264 static void ShowTargetShapeWhileMovingMouse( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
265  const wxPoint& aPosition, bool aErase )
266 {
267  BASE_SCREEN* screen = aPanel->GetScreen();
268  PCB_TARGET* target = (PCB_TARGET*) screen->GetCurItem();
269 
270  if( target == NULL )
271  return;
272 
273  if( aErase )
274  target->Draw( aPanel, aDC, GR_XOR );
275 
276  target->SetPosition( aPanel->GetParent()->GetCrossHairPosition() );
277 
278  target->Draw( aPanel, aDC, GR_XOR );
279 }
virtual BASE_SCREEN * GetScreen()=0
void BeginMoveTarget(PCB_TARGET *aTarget, wxDC *DC)
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:258
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
bool IsMoving() const
Definition: base_struct.h:221
void DeleteTarget(PCB_TARGET *aTarget, wxDC *DC)
#define IN_EDIT
Item currently edited.
Definition: base_struct.h:112
Class DIALOG_TARGET_PROPERTIES_BASE.
virtual EDA_DRAW_PANEL * GetCanvas() const
Definition: draw_frame.h:388
EDA_ITEM * GetCurItem() const
Definition: base_screen.h:233
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
Implementation of conversion functions that require both schematic and board internal units...
int GetWidth() const
bool IsNew() const
Definition: base_struct.h:219
void SetSize(int aSize)
Class BOARD to handle a board.
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void DeleteStructure()
Function DeleteStructure deletes this object after UnLink()ing it from its owner if it has one...
DIALOG_TARGET_PROPERTIES(PCB_EDIT_FRAME *aParent, PCB_TARGET *aTarget, wxDC *aDC)
Definition: target_edit.cpp:91
virtual void UnLink()
Function UnLink detaches this object from its owner.
bool TransferDataToWindow() override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
void PlaceTarget(PCB_TARGET *aTarget, wxDC *DC)
bool TransferDataFromWindow() override
virtual int GetValue()
Function GetValue Returns the current value in Internal Units.
#define IS_NEW
New item, just created.
Definition: base_struct.h:114
PCB_TARGET * CreateTarget(wxDC *DC)
virtual EDA_DRAW_FRAME * GetParent() const =0
static PCB_TARGET s_TargetCopy(NULL)
static void AbortMoveAndEditTarget(EDA_DRAW_PANEL *Panel, wxDC *DC)
int GetShape() const
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:256
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:76
virtual void SetMouseCapture(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback, END_MOUSE_CAPTURE_CALLBACK aEndMouseCaptureCallback)
Function SetMouseCapture sets the mouse capture and end mouse capture callbacks to aMouseCaptureCallb...
void SetPosition(const wxPoint &aPos) override
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
Helper dialog and control classes.
void SetShape(int aShape)
void Draw(EDA_DRAW_PANEL *panel, wxDC *DC, GR_DRAWMODE aDrawMode, const wxPoint &offset=ZeroOffset) override
Function Draw BOARD_ITEMs have their own color information.
BOARD * GetBoard()
Definition: gr_basic.h:38
void ShowTargetOptionsDialog(PCB_TARGET *aTarget, wxDC *DC)
Definition: target_edit.cpp:83
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
void SetWidth(int aWidth)
int GetSize() const
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:257
static void ShowTargetShapeWhileMovingMouse(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
PCB_TARGET class definition.
PCB_EDIT_FRAME * m_Parent
Definition: target_edit.cpp:66
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Return the current cross hair position in logical (drawing) coordinates.
const wxPoint GetPosition() const override
static int MireDefaultSize
Definition: target_edit.cpp:54
#define IS_MOVED
Item being moved.
Definition: base_struct.h:113