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.

294 lines
8.3 KiB

  1. /**
  2. * @file dimension.cpp
  3. * @brief Dialog and code for editing a dimension object.
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
  9. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  10. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, you may find one here:
  24. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  25. * or you may search the http://www.gnu.org website for the version 2 license,
  26. * or you may write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  28. */
  29. #include <fctsys.h>
  30. #include <confirm.h>
  31. #include <gr_basic.h>
  32. #include <class_drawpanel.h>
  33. #include <pcb_edit_frame.h>
  34. #include <draw_graphic_text.h>
  35. #include <dialog_helpers.h>
  36. #include <macros.h>
  37. #include <base_units.h>
  38. #include <board_commit.h>
  39. #include <class_board.h>
  40. #include <class_pcb_text.h>
  41. #include <class_dimension.h>
  42. #include <pcbnew.h>
  43. #include <pcb_layer_box_selector.h>
  44. #include <dialogs/dialog_text_properties.h>
  45. /* Local functions */
  46. static void BuildDimension( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  47. const wxPoint& aPosition, bool aErase );
  48. static void MoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  49. const wxPoint& aPosition, bool aErase );
  50. static void AbortMoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC );
  51. /* Local variables : */
  52. static int status_dimension; /* Used in dimension creation:
  53. * = 0 : initial value: no dimension in progress
  54. * = 1 : First point created
  55. * = 2 : Second point created, the text must be placed */
  56. /*
  57. * A dimension has this shape:
  58. * It has 2 reference points, and a text
  59. * | |
  60. * | dist |
  61. * |<---------->|
  62. * | |
  63. *
  64. */
  65. static void AbortBuildDimension( EDA_DRAW_PANEL* Panel, wxDC* aDC )
  66. {
  67. DIMENSION* dimension = (DIMENSION*) Panel->GetScreen()->GetCurItem();
  68. if( dimension )
  69. {
  70. if( dimension->IsNew() )
  71. {
  72. dimension->Draw( Panel, aDC, GR_XOR );
  73. dimension->DeleteStructure();
  74. }
  75. else
  76. {
  77. dimension->Draw( Panel, aDC, GR_OR );
  78. }
  79. }
  80. status_dimension = 0;
  81. ((PCB_EDIT_FRAME*)Panel->GetParent())->SetCurItem( NULL );
  82. }
  83. DIMENSION* PCB_EDIT_FRAME::EditDimension( DIMENSION* aDimension, wxDC* aDC )
  84. {
  85. wxPoint pos;
  86. if( aDimension == NULL )
  87. {
  88. const BOARD_DESIGN_SETTINGS& boardSettings = GetBoard()->GetDesignSettings();
  89. status_dimension = 1;
  90. pos = GetCrossHairPosition();
  91. aDimension = new DIMENSION( GetBoard() );
  92. aDimension->SetFlags( IS_NEW );
  93. aDimension->SetLayer( GetActiveLayer() );
  94. aDimension->SetOrigin( pos );
  95. aDimension->SetEnd( pos );
  96. aDimension->Text().SetTextSize( boardSettings.GetTextSize( GetActiveLayer() ) );
  97. aDimension->Text().SetThickness( boardSettings.GetTextThickness( GetActiveLayer() ) );
  98. aDimension->Text().SetItalic( boardSettings.GetTextItalic( GetActiveLayer() ) );
  99. aDimension->SetWidth( boardSettings.GetLineThickness( GetActiveLayer() ) );
  100. aDimension->AdjustDimensionDetails();
  101. aDimension->Draw( m_canvas, aDC, GR_XOR );
  102. m_canvas->SetMouseCapture( BuildDimension, AbortBuildDimension );
  103. return aDimension;
  104. }
  105. // Dimension != NULL
  106. if( status_dimension == 1 )
  107. {
  108. status_dimension = 2;
  109. return aDimension;
  110. }
  111. aDimension->Draw( m_canvas, aDC, GR_OR );
  112. aDimension->ClearFlags();
  113. /* ADD this new item in list */
  114. GetBoard()->Add( aDimension );
  115. // Add store it in undo/redo list
  116. SaveCopyInUndoList( aDimension, UR_NEW );
  117. OnModify();
  118. m_canvas->SetMouseCapture( NULL, NULL );
  119. return NULL;
  120. }
  121. static void BuildDimension( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  122. const wxPoint& aPosition, bool aErase )
  123. {
  124. PCB_SCREEN* screen = (PCB_SCREEN*) aPanel->GetScreen();
  125. DIMENSION* Dimension = (DIMENSION*) screen->GetCurItem();
  126. wxPoint pos = aPanel->GetParent()->GetCrossHairPosition();
  127. if( Dimension == NULL )
  128. return;
  129. // Erase previous dimension.
  130. if( aErase )
  131. {
  132. Dimension->Draw( aPanel, aDC, GR_XOR );
  133. }
  134. Dimension->SetLayer( screen->m_Active_Layer );
  135. if( status_dimension == 1 )
  136. {
  137. Dimension->m_featureLineDO = pos;
  138. Dimension->m_crossBarF = Dimension->m_featureLineDO;
  139. Dimension->AdjustDimensionDetails();
  140. }
  141. else
  142. {
  143. /* Calculating the direction of travel perpendicular to the selected axis. */
  144. double angle = Dimension->GetAngle() + (M_PI / 2);
  145. wxPoint delta = pos - Dimension->m_featureLineDO;
  146. double depl = ( delta.x * cos( angle ) ) + ( delta.y * sin( angle ) );
  147. Dimension->SetHeight( depl );
  148. }
  149. Dimension->Draw( aPanel, aDC, GR_XOR );
  150. }
  151. void PCB_EDIT_FRAME::ShowDimensionPropertyDialog( DIMENSION* aDimension, wxDC* aDC )
  152. {
  153. if( aDimension == NULL )
  154. return;
  155. DIALOG_TEXT_PROPERTIES dlg( this, aDimension, aDC );
  156. dlg.ShowModal();
  157. }
  158. void PCB_EDIT_FRAME::DeleteDimension( DIMENSION* aDimension, wxDC* aDC )
  159. {
  160. if( aDimension == NULL )
  161. return;
  162. if( aDC )
  163. aDimension->Draw( m_canvas, aDC, GR_XOR );
  164. SaveCopyInUndoList( aDimension, UR_DELETED );
  165. aDimension->UnLink();
  166. OnModify();
  167. }
  168. /* Initialize parameters to move a pcb text
  169. */
  170. static wxPoint initialTextPosition;
  171. void PCB_EDIT_FRAME::BeginMoveDimensionText( DIMENSION* aItem, wxDC* DC )
  172. {
  173. if( aItem == NULL )
  174. return;
  175. // Store the initial position for undo/abort command
  176. initialTextPosition = aItem->Text().GetTextPos();
  177. aItem->Draw( m_canvas, DC, GR_XOR );
  178. aItem->SetFlags( IS_MOVED );
  179. SetMsgPanel( aItem );
  180. SetCrossHairPosition( aItem->Text().GetTextPos() );
  181. m_canvas->MoveCursorToCrossHair();
  182. m_canvas->SetMouseCapture( MoveDimensionText, AbortMoveDimensionText );
  183. SetCurItem( aItem );
  184. m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
  185. }
  186. /* Move dimension text following the cursor. */
  187. static void MoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  188. bool aErase )
  189. {
  190. DIMENSION* dimension = (DIMENSION*) aPanel->GetScreen()->GetCurItem();
  191. if( dimension == NULL )
  192. return;
  193. if( aErase )
  194. dimension->Draw( aPanel, aDC, GR_XOR );
  195. dimension->Text().SetTextPos( aPanel->GetParent()->GetCrossHairPosition() );
  196. dimension->Draw( aPanel, aDC, GR_XOR );
  197. }
  198. /*
  199. * Abort current text edit progress.
  200. *
  201. * If a text is selected, its initial coord are regenerated
  202. */
  203. void AbortMoveDimensionText( EDA_DRAW_PANEL* aPanel, wxDC* aDC )
  204. {
  205. DIMENSION* dimension = (DIMENSION*) aPanel->GetScreen()->GetCurItem();
  206. ( (PCB_EDIT_FRAME*) aPanel->GetParent() )->SetCurItem( NULL );
  207. aPanel->SetMouseCapture( NULL, NULL );
  208. if( dimension == NULL ) // Should not occur
  209. return;
  210. dimension->Draw( aPanel, aDC, GR_XOR );
  211. dimension->Text().SetTextPos( initialTextPosition );
  212. dimension->ClearFlags();
  213. dimension->Draw( aPanel, aDC, GR_OR );
  214. }
  215. /*
  216. * Place the current dimension text being moving
  217. */
  218. void PCB_EDIT_FRAME::PlaceDimensionText( DIMENSION* aItem, wxDC* DC )
  219. {
  220. m_canvas->SetMouseCapture( NULL, NULL );
  221. SetCurItem( NULL );
  222. if( aItem == NULL )
  223. return;
  224. aItem->Draw( m_canvas, DC, GR_OR );
  225. OnModify();
  226. wxPoint tmp = aItem->Text().GetTextPos();
  227. aItem->Text().SetTextPos( initialTextPosition );
  228. SaveCopyInUndoList( aItem, UR_CHANGED );
  229. aItem->Text().SetTextPos( tmp );
  230. aItem->ClearFlags();
  231. }