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 <wxPcbStruct.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_mire.h>
41 
42 #include <pcbnew.h>
44 
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
57  * current item parameters before
58  * edition (used in undo/redo or
59  * cancel operations)
60  */
61 
62 /*****************************************/
63 /* class TARGET_PROPERTIES_DIALOG_EDITOR */
64 /*****************************************/
65 
67 {
68 private:
70  wxDC* m_DC;
72 
73 public:
74  TARGET_PROPERTIES_DIALOG_EDITOR( PCB_EDIT_FRAME* parent, PCB_TARGET* Mire, wxDC* DC );
76 
77 private:
78  void OnOkClick( wxCommandEvent& event ) override;
79  void OnCancelClick( wxCommandEvent& event ) override;
80 };
81 
82 
84 {
86  new TARGET_PROPERTIES_DIALOG_EDITOR( this, aTarget, DC );
87 
88  frame->ShowModal();
89  frame->Destroy();
90 }
91 
92 
94  PCB_TARGET* aTarget, wxDC* DC ) :
96 {
97  m_Parent = parent;
98  m_DC = DC;
99  m_Target = aTarget;
100 
101  // Size:
104 
105  // Thickness:
108 
109  // Shape
110  m_TargetShape->SetSelection( m_Target->GetShape() ? 1 : 0 );
111 
112  // OK button on return key.
113  SetDefaultItem( m_sdbSizerButtsOK );
114 
115  // Now all widgets have the size fixed, call FinishDialogSettings
117 }
118 
119 
121 {
122  EndModal( -1 );
123 }
124 
125 
126 /* Updates the different parameters for the component being edited
127  */
128 void TARGET_PROPERTIES_DIALOG_EDITOR::OnOkClick( wxCommandEvent& event )
129 {
130  BOARD_COMMIT commit( m_Parent );
131  commit.Modify( m_Target );
132 
133  if( m_DC )
135 
136  // Save old item in undo list, if is is not currently edited (will be later if so)
137  bool pushCommit = ( m_Target->GetFlags() == 0 );
138 
139  if( m_Target->GetFlags() != 0 ) // other edition in progress (MOVE, NEW ..)
140  m_Target->SetFlags( IN_EDIT ); // set flag in edit to force
141  // undo/redo/abort proper operation
142 
143  int tmp = ValueFromString( g_UserUnit, m_TargetThicknessCtrl->GetValue() );
144  m_Target->SetWidth( tmp );
145 
148 
149  m_Target->SetShape( m_TargetShape->GetSelection() ? 1 : 0 );
150 
151  if( m_DC )
153 
154  if( pushCommit )
155  commit.Push( _( "Modified alignment target" ) );
156 
157  EndModal( 1 );
158 }
159 
160 
161 void PCB_EDIT_FRAME::DeleteTarget( PCB_TARGET* aTarget, wxDC* DC )
162 {
163  if( aTarget == NULL )
164  return;
165 
166  aTarget->Draw( m_canvas, DC, GR_XOR );
167  SaveCopyInUndoList( aTarget, UR_DELETED );
168  aTarget->UnLink();
169 }
170 
171 
172 static void AbortMoveAndEditTarget( EDA_DRAW_PANEL* Panel, wxDC* DC )
173 {
174  BASE_SCREEN* screen = Panel->GetScreen();
175  PCB_TARGET* target = (PCB_TARGET*) screen->GetCurItem();
176 
177  ( (PCB_EDIT_FRAME*) Panel->GetParent() )->SetCurItem( NULL );
178 
179  Panel->SetMouseCapture( NULL, NULL );
180 
181  if( target == NULL )
182  return;
183 
184  target->Draw( Panel, DC, GR_XOR );
185 
186  if( target->IsNew() ) // If it is new, delete it
187  {
188  target->Draw( Panel, DC, GR_XOR );
189  target->DeleteStructure();
190  target = NULL;
191  }
192  else // it is an existing item: retrieve initial values of parameters
193  {
194  if( ( target->GetFlags() & (IN_EDIT | IS_MOVED) ) )
195  {
196  target->SetPosition( s_TargetCopy.GetPosition() );
197  target->SetWidth( s_TargetCopy.GetWidth() );
198  target->SetSize( s_TargetCopy.GetSize() );
199  target->SetShape( s_TargetCopy.GetShape() );
200  }
201 
202  target->ClearFlags();
203  target->Draw( Panel, DC, GR_OR );
204  }
205 }
206 
207 
209 {
210  PCB_TARGET* target = new PCB_TARGET( GetBoard() );
211 
212  target->SetFlags( IS_NEW );
213 
214  GetBoard()->Add( target );
215 
216  target->SetLayer( Edge_Cuts );
217  target->SetWidth( GetDesignSettings().m_EdgeSegmentWidth );
218  target->SetSize( MireDefaultSize );
219  target->SetPosition( GetCrossHairPosition() );
220 
221  PlaceTarget( target, DC );
222 
223  return target;
224 }
225 
226 
227 void PCB_EDIT_FRAME::BeginMoveTarget( PCB_TARGET* aTarget, wxDC* DC )
228 {
229  if( aTarget == NULL )
230  return;
231 
232  s_TargetCopy = *aTarget;
233  aTarget->SetFlags( IS_MOVED );
235  SetCurItem( aTarget );
236 }
237 
238 
239 void PCB_EDIT_FRAME::PlaceTarget( PCB_TARGET* aTarget, wxDC* DC )
240 {
241  if( aTarget == NULL )
242  return;
243 
244  aTarget->Draw( m_canvas, DC, GR_OR );
245  m_canvas->SetMouseCapture( NULL, NULL );
246  SetCurItem( NULL );
247  OnModify();
248 
249  if( aTarget->IsNew() )
250  {
251  SaveCopyInUndoList( aTarget, UR_NEW );
252  aTarget->ClearFlags();
253  return;
254  }
255 
256  if( aTarget->GetFlags() == IS_MOVED )
257  {
258  SaveCopyInUndoList( aTarget, UR_MOVED,
259  aTarget->GetPosition() - s_TargetCopy.GetPosition() );
260  aTarget->ClearFlags();
261  return;
262  }
263 
264  if( (aTarget->GetFlags() & IN_EDIT) )
265  {
266  aTarget->SwapData( &s_TargetCopy );
267  SaveCopyInUndoList( aTarget, UR_CHANGED );
268  aTarget->SwapData( &s_TargetCopy );
269  }
270 
271  aTarget->ClearFlags();
272 }
273 
274 
275 // Redraw the contour of the track while moving the mouse
276 static void ShowTargetShapeWhileMovingMouse( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
277  const wxPoint& aPosition, bool aErase )
278 {
279  BASE_SCREEN* screen = aPanel->GetScreen();
280  PCB_TARGET* target = (PCB_TARGET*) screen->GetCurItem();
281 
282  if( target == NULL )
283  return;
284 
285  if( aErase )
286  target->Draw( aPanel, aDC, GR_XOR );
287 
288  target->SetPosition( aPanel->GetParent()->GetCrossHairPosition() );
289 
290  target->Draw( aPanel, aDC, GR_XOR );
291 }
void BeginMoveTarget(PCB_TARGET *aTarget, wxDC *DC)
TARGET_PROPERTIES_DIALOG_EDITOR(PCB_EDIT_FRAME *parent, PCB_TARGET *Mire, wxDC *DC)
Definition: target_edit.cpp:93
virtual void OnModify() override
Function OnModify must be called after a board change to set the modified flag.
Definition: pcbframe.cpp:999
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:269
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
void OnCancelClick(wxCommandEvent &event) override
bool IsMoving() const
Definition: base_struct.h:232
void DeleteTarget(PCB_TARGET *aTarget, wxDC *DC)
#define IN_EDIT
Item currently edited.
Definition: base_struct.h:125
PCB_TARGET class definition.
EDA_ITEM * GetCurItem() const
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
Definition: class_mire.h:72
bool IsNew() const
Definition: base_struct.h:230
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:304
void SetSize(int aSize)
Definition: class_mire.h:68
Class BOARD to handle a board.
wxString GetUnitsLabel(EDA_UNITS_T aUnit)
Get a human readable units string.
Definition: base_units.cpp:452
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:203
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void SetCurItem(BOARD_ITEM *aItem, bool aDisplayInfo=true)
Function SetCurItem sets the currently selected item and displays it in the MsgPanel.
void DeleteStructure()
Function DeleteStructure deletes this object after UnLink()ing it from its owner if it has one...
BOARD * GetBoard() const
virtual void UnLink()
Function UnLink detaches this object from its owner.
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true) override
Executes the changes.
void PlaceTarget(PCB_TARGET *aTarget, wxDC *DC)
#define IS_NEW
New item, just created.
Definition: base_struct.h:127
PCB_TARGET * CreateTarget(wxDC *DC)
void SwapData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Definition: undo_redo.cpp:567
static PCB_TARGET s_TargetCopy(NULL)
static void AbortMoveAndEditTarget(EDA_DRAW_PANEL *Panel, wxDC *DC)
int GetShape() const
Definition: class_mire.h:66
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:267
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
void SetPosition(const wxPoint &aPos) override
Definition: class_mire.h:62
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:368
Helper dialog and control classes.
void SetShape(int aShape)
Definition: class_mire.h:65
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame...
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:174
void SaveCopyInUndoList(BOARD_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0)) override
Function SaveCopyInUndoList Creates a new entry in undo list of commands.
Definition: undo_redo.cpp:172
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.
Definition: class_mire.cpp:78
Class TARGET_PROPERTIES_DIALOG_EDITOR_BASE.
void OnOkClick(wxCommandEvent &event) override
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
Definition: gr_basic.h:42
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
void ShowTargetOptionsDialog(PCB_TARGET *aTarget, wxDC *DC)
Definition: target_edit.cpp:83
const wxPoint & GetPosition() const override
Definition: class_mire.h:63
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:187
void SetWidth(int aWidth)
Definition: class_mire.h:71
int GetSize() const
Definition: class_mire.h:69
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:268
static void ShowTargetShapeWhileMovingMouse(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
void SetMouseCapture(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback, END_MOUSE_CAPTURE_CALLBACK aEndMouseCaptureCallback)
Function SetMouseCapture sets the mouse capture and end mouse capture callbacks to aMouseCaptureCallb...
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
static int MireDefaultSize
Definition: target_edit.cpp:54
#define IS_MOVED
Item being moved.
Definition: base_struct.h:126