You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
7.9 KiB

14 years ago
14 years ago
14 years ago
  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. /**
  26. * @file target_edit.cpp
  27. * @brief Functions to edit targets (class #PCB_TARGET).
  28. */
  29. #include <fctsys.h>
  30. #include <class_drawpanel.h>
  31. #include <pcb_edit_frame.h>
  32. #include <dialog_helpers.h>
  33. #include <base_units.h>
  34. #include <gr_basic.h>
  35. #include <board_commit.h>
  36. #include <class_board.h>
  37. #include <class_pcb_target.h>
  38. #include <pcbnew.h>
  39. #include <dialog_target_properties_base.h>
  40. #include <widgets/unit_binder.h>
  41. // Routines Locales
  42. static void AbortMoveAndEditTarget( EDA_DRAW_PANEL* Panel, wxDC* DC );
  43. static void ShowTargetShapeWhileMovingMouse( EDA_DRAW_PANEL* aPanel,
  44. wxDC* aDC,
  45. const wxPoint& aPosition,
  46. bool aErase );
  47. // Local variables :
  48. static int MireDefaultSize = Millimeter2iu( 5 );
  49. static PCB_TARGET s_TargetCopy( NULL ); // Used to store "old" values of the current item
  50. // parameters before editing for undo/redo/cancel
  51. /**********************************/
  52. /* class DIALOG_TARGET_PROPERTIES */
  53. /**********************************/
  54. class DIALOG_TARGET_PROPERTIES : public DIALOG_TARGET_PROPERTIES_BASE
  55. {
  56. private:
  57. PCB_EDIT_FRAME* m_Parent;
  58. wxDC* m_DC;
  59. PCB_TARGET* m_Target;
  60. UNIT_BINDER m_Size;
  61. UNIT_BINDER m_Thickness;
  62. public:
  63. DIALOG_TARGET_PROPERTIES( PCB_EDIT_FRAME* aParent, PCB_TARGET* aTarget, wxDC* aDC );
  64. ~DIALOG_TARGET_PROPERTIES() { }
  65. private:
  66. bool TransferDataToWindow() override;
  67. bool TransferDataFromWindow() override;
  68. };
  69. void PCB_EDIT_FRAME::ShowTargetOptionsDialog( PCB_TARGET* aTarget, wxDC* DC )
  70. {
  71. DIALOG_TARGET_PROPERTIES dialog( this, aTarget, DC );
  72. dialog.ShowModal();
  73. }
  74. DIALOG_TARGET_PROPERTIES::DIALOG_TARGET_PROPERTIES( PCB_EDIT_FRAME* aParent, PCB_TARGET* aTarget,
  75. wxDC* aDC ) :
  76. DIALOG_TARGET_PROPERTIES_BASE( aParent ),
  77. m_Parent( aParent ),
  78. m_DC( aDC ),
  79. m_Target( aTarget ),
  80. m_Size( aParent, m_sizeLabel, m_sizeCtrl, m_sizeUnits, true, 0 ),
  81. m_Thickness( aParent, m_thicknessLabel, m_thicknessCtrl, m_thicknessUnits, true, 0 )
  82. {
  83. m_sdbSizerButtsOK->SetDefault();
  84. SetInitialFocus( m_sizeCtrl );
  85. // Now all widgets have the size fixed, call FinishDialogSettings
  86. FinishDialogSettings();
  87. }
  88. bool DIALOG_TARGET_PROPERTIES::TransferDataToWindow()
  89. {
  90. m_Size.SetValue( m_Target->GetSize() );
  91. m_Thickness.SetValue( m_Target->GetWidth() );
  92. m_TargetShape->SetSelection( m_Target->GetShape() ? 1 : 0 );
  93. return true;
  94. }
  95. bool DIALOG_TARGET_PROPERTIES::TransferDataFromWindow()
  96. {
  97. BOARD_COMMIT commit( m_Parent );
  98. commit.Modify( m_Target );
  99. if( m_DC )
  100. m_Target->Draw( m_Parent->GetCanvas(), m_DC, GR_XOR );
  101. // Save old item in undo list, if is is not currently edited (will be later if so)
  102. bool pushCommit = ( m_Target->GetFlags() == 0 );
  103. if( m_Target->GetFlags() != 0 ) // other edit in progress (MOVE, NEW ..)
  104. m_Target->SetFlags( IN_EDIT ); // set flag in edit to force
  105. // undo/redo/abort proper operation
  106. m_Target->SetWidth( m_Thickness.GetValue() );
  107. m_Target->SetSize( m_Size.GetValue() );
  108. m_Target->SetShape( m_TargetShape->GetSelection() ? 1 : 0 );
  109. if( m_DC )
  110. m_Target->Draw( m_Parent->GetCanvas(), m_DC, ( m_Target->IsMoving() ) ? GR_XOR : GR_OR );
  111. if( pushCommit )
  112. commit.Push( _( "Modified alignment target" ) );
  113. return true;
  114. }
  115. void PCB_EDIT_FRAME::DeleteTarget( PCB_TARGET* aTarget, wxDC* DC )
  116. {
  117. if( aTarget == NULL )
  118. return;
  119. aTarget->Draw( m_canvas, DC, GR_XOR );
  120. SaveCopyInUndoList( aTarget, UR_DELETED );
  121. aTarget->UnLink();
  122. }
  123. static void AbortMoveAndEditTarget( EDA_DRAW_PANEL* Panel, wxDC* DC )
  124. {
  125. BASE_SCREEN* screen = Panel->GetScreen();
  126. PCB_TARGET* target = (PCB_TARGET*) screen->GetCurItem();
  127. ( (PCB_EDIT_FRAME*) Panel->GetParent() )->SetCurItem( NULL );
  128. Panel->SetMouseCapture( NULL, NULL );
  129. if( target == NULL )
  130. return;
  131. target->Draw( Panel, DC, GR_XOR );
  132. if( target->IsNew() ) // If it is new, delete it
  133. {
  134. target->Draw( Panel, DC, GR_XOR );
  135. target->DeleteStructure();
  136. target = NULL;
  137. }
  138. else // it is an existing item: retrieve initial values of parameters
  139. {
  140. if( ( target->GetFlags() & (IN_EDIT | IS_MOVED) ) )
  141. {
  142. target->SetPosition( s_TargetCopy.GetPosition() );
  143. target->SetWidth( s_TargetCopy.GetWidth() );
  144. target->SetSize( s_TargetCopy.GetSize() );
  145. target->SetShape( s_TargetCopy.GetShape() );
  146. }
  147. target->ClearFlags();
  148. target->Draw( Panel, DC, GR_OR );
  149. }
  150. }
  151. PCB_TARGET* PCB_EDIT_FRAME::CreateTarget( wxDC* DC )
  152. {
  153. PCB_TARGET* target = new PCB_TARGET( GetBoard() );
  154. target->SetFlags( IS_NEW );
  155. GetBoard()->Add( target );
  156. target->SetLayer( Edge_Cuts );
  157. target->SetWidth( GetDesignSettings().GetLineThickness( Edge_Cuts ) );
  158. target->SetSize( MireDefaultSize );
  159. target->SetPosition( GetCrossHairPosition() );
  160. PlaceTarget( target, DC );
  161. return target;
  162. }
  163. void PCB_EDIT_FRAME::BeginMoveTarget( PCB_TARGET* aTarget, wxDC* DC )
  164. {
  165. if( aTarget == NULL )
  166. return;
  167. s_TargetCopy = *aTarget;
  168. aTarget->SetFlags( IS_MOVED );
  169. m_canvas->SetMouseCapture( ShowTargetShapeWhileMovingMouse, AbortMoveAndEditTarget );
  170. SetCurItem( aTarget );
  171. }
  172. void PCB_EDIT_FRAME::PlaceTarget( PCB_TARGET* aTarget, wxDC* DC )
  173. {
  174. if( aTarget == NULL )
  175. return;
  176. aTarget->Draw( m_canvas, DC, GR_OR );
  177. m_canvas->SetMouseCapture( NULL, NULL );
  178. SetCurItem( NULL );
  179. OnModify();
  180. if( aTarget->IsNew() )
  181. {
  182. SaveCopyInUndoList( aTarget, UR_NEW );
  183. aTarget->ClearFlags();
  184. return;
  185. }
  186. if( aTarget->GetFlags() == IS_MOVED )
  187. {
  188. SaveCopyInUndoList( aTarget, UR_MOVED,
  189. aTarget->GetPosition() - s_TargetCopy.GetPosition() );
  190. aTarget->ClearFlags();
  191. return;
  192. }
  193. if( (aTarget->GetFlags() & IN_EDIT) )
  194. {
  195. aTarget->SwapData( &s_TargetCopy );
  196. SaveCopyInUndoList( aTarget, UR_CHANGED );
  197. aTarget->SwapData( &s_TargetCopy );
  198. }
  199. aTarget->ClearFlags();
  200. }
  201. // Redraw the contour of the track while moving the mouse
  202. static void ShowTargetShapeWhileMovingMouse( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  203. const wxPoint& aPosition, bool aErase )
  204. {
  205. BASE_SCREEN* screen = aPanel->GetScreen();
  206. PCB_TARGET* target = (PCB_TARGET*) screen->GetCurItem();
  207. if( target == NULL )
  208. return;
  209. if( aErase )
  210. target->Draw( aPanel, aDC, GR_XOR );
  211. target->SetPosition( aPanel->GetParent()->GetCrossHairPosition() );
  212. target->Draw( aPanel, aDC, GR_XOR );
  213. }