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.

355 lines
10 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2009-2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
  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 symbdraw.cpp
  27. * @brief Create, move .. graphic shapes used to build and draw a component (lines, arcs ..)
  28. */
  29. #include <fctsys.h>
  30. #include <gr_basic.h>
  31. #include <class_drawpanel.h>
  32. #include <confirm.h>
  33. #include <class_sch_screen.h>
  34. #include <eeschema_id.h>
  35. #include <general.h>
  36. #include <libeditframe.h>
  37. #include <class_libentry.h>
  38. #include <lib_arc.h>
  39. #include <lib_circle.h>
  40. #include <lib_polyline.h>
  41. #include <lib_rectangle.h>
  42. #include <lib_text.h>
  43. #include <dialogs/dialog_lib_edit_draw_item.h>
  44. static void SymbolDisplayDraw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  45. bool aErase );
  46. static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  47. bool aErase );
  48. /*
  49. * Show the dialog box for editing a graphical item properties
  50. */
  51. void LIB_EDIT_FRAME::EditGraphicSymbol( wxDC* DC, LIB_ITEM* DrawItem )
  52. {
  53. if( DrawItem == NULL )
  54. return;
  55. LIB_COMPONENT* component = DrawItem->GetParent();
  56. DIALOG_LIB_EDIT_DRAW_ITEM dialog( this, DrawItem->GetTypeName() );
  57. dialog.SetWidthUnits( ReturnUnitSymbol( g_UserUnit ) );
  58. wxString val = ReturnStringFromValue( g_UserUnit, m_drawLineWidth, m_internalUnits );
  59. dialog.SetWidth( val );
  60. dialog.SetApplyToAllUnits( !m_drawSpecificUnit );
  61. dialog.EnableApplyToAllUnits( component && component->GetPartCount() > 1 );
  62. dialog.SetApplyToAllConversions( !m_drawSpecificConvert );
  63. dialog.EnableApplyToAllConversions( component && component->HasConversion() );
  64. // dialog.SetFillStyle( m_drawFillStyle ); // could better to show the current setting
  65. dialog.SetFillStyle( DrawItem->GetFillMode() );
  66. dialog.EnableFillStyle( DrawItem->IsFillable() );
  67. if( dialog.ShowModal() == wxID_CANCEL )
  68. return;
  69. val = dialog.GetWidth();
  70. m_drawLineWidth = ReturnValueFromString( g_UserUnit, val, m_internalUnits );
  71. m_drawSpecificConvert = !dialog.GetApplyToAllConversions();
  72. m_drawSpecificUnit = !dialog.GetApplyToAllUnits();
  73. #if 0
  74. /* TODO: see if m_drawFillStyle must retain the last fill option or not.
  75. * if the last is Filled, having next new graphic items created
  76. * with filled body is often bad.
  77. * currently m_drawFillStyle is left with the default value (not filled)
  78. */
  79. if( DrawItem->IsFillable() )
  80. m_drawFillStyle = (FILL_T) dialog.GetFillStyle();
  81. #endif
  82. // Save copy for undo if not in edit (edit command already handle the save copy)
  83. if( !DrawItem->InEditMode() )
  84. SaveCopyInUndoList( DrawItem->GetParent() );
  85. if( m_drawSpecificUnit )
  86. DrawItem->SetUnit( GetUnit() );
  87. else
  88. DrawItem->SetUnit( 0 );
  89. if( m_drawSpecificConvert )
  90. DrawItem->SetConvert( GetConvert() );
  91. else
  92. DrawItem->SetConvert( 0 );
  93. if( DrawItem->IsFillable() )
  94. DrawItem->SetFillMode( (FILL_T) dialog.GetFillStyle() );
  95. DrawItem->SetWidth( m_drawLineWidth );
  96. if( component )
  97. component->GetDrawItemList().sort();
  98. OnModify( );
  99. DrawItem->DisplayInfo( this );
  100. m_canvas->Refresh();
  101. }
  102. static void AbortSymbolTraceOn( EDA_DRAW_PANEL* Panel, wxDC* DC )
  103. {
  104. LIB_EDIT_FRAME* parent = (LIB_EDIT_FRAME*) Panel->GetParent();
  105. LIB_ITEM* item = parent->GetDrawItem();
  106. if( item == NULL )
  107. return;
  108. bool newItem = item->IsNew();
  109. item->EndEdit( parent->GetScreen()->GetCrossHairPosition( true ), true );
  110. if( newItem )
  111. {
  112. delete item;
  113. }
  114. else
  115. parent->RestoreComponent();
  116. parent->SetDrawItem( NULL );
  117. Panel->Refresh();
  118. }
  119. LIB_ITEM* LIB_EDIT_FRAME::CreateGraphicItem( LIB_COMPONENT* LibEntry, wxDC* DC )
  120. {
  121. m_canvas->SetMouseCapture( SymbolDisplayDraw, AbortSymbolTraceOn );
  122. wxPoint drawPos = GetScreen()->GetCrossHairPosition( true );
  123. // no temp copy -> the current version of component will be used for Undo
  124. // This is normal when adding new items to the current component
  125. ClearTempCopyComponent();
  126. switch( GetToolId() )
  127. {
  128. case ID_LIBEDIT_BODY_ARC_BUTT:
  129. m_drawItem = new LIB_ARC( LibEntry );
  130. break;
  131. case ID_LIBEDIT_BODY_CIRCLE_BUTT:
  132. m_drawItem = new LIB_CIRCLE( LibEntry );
  133. break;
  134. case ID_LIBEDIT_BODY_RECT_BUTT:
  135. m_drawItem = new LIB_RECTANGLE( LibEntry );
  136. break;
  137. case ID_LIBEDIT_BODY_LINE_BUTT:
  138. m_drawItem = new LIB_POLYLINE( LibEntry );
  139. break;
  140. case ID_LIBEDIT_BODY_TEXT_BUTT:
  141. {
  142. LIB_TEXT* Text = new LIB_TEXT( LibEntry );
  143. Text->m_Size.x = Text->m_Size.y = m_textSize;
  144. Text->m_Orient = m_textOrientation;
  145. // Enter the graphic text info
  146. m_canvas->SetIgnoreMouseEvents( true );
  147. EditSymbolText( NULL, Text );
  148. m_canvas->SetIgnoreMouseEvents( false );
  149. m_canvas->MoveCursorToCrossHair();
  150. if( Text->m_Text.IsEmpty() )
  151. {
  152. delete Text;
  153. m_drawItem = NULL;
  154. }
  155. else
  156. {
  157. m_drawItem = Text;
  158. }
  159. break;
  160. }
  161. default:
  162. DisplayError( this, wxT( "LIB_EDIT_FRAME::CreateGraphicItem() error" ) );
  163. return NULL;
  164. }
  165. if( m_drawItem )
  166. {
  167. m_drawItem->BeginEdit( IS_NEW, drawPos );
  168. m_drawItem->SetWidth( m_drawLineWidth );
  169. m_drawItem->SetFillMode( m_drawFillStyle );
  170. if( m_drawSpecificUnit )
  171. m_drawItem->SetUnit( m_unit );
  172. if( m_drawSpecificConvert )
  173. m_drawItem->SetConvert( m_convert );
  174. // Draw initial symbol:
  175. m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
  176. }
  177. else
  178. {
  179. m_canvas->EndMouseCapture();
  180. return NULL;
  181. }
  182. m_canvas->MoveCursorToCrossHair();
  183. m_canvas->SetIgnoreMouseEvents( false );
  184. return m_drawItem;
  185. }
  186. /* Create new library component graphic object.
  187. */
  188. void LIB_EDIT_FRAME::GraphicItemBeginDraw( wxDC* DC )
  189. {
  190. if( m_drawItem == NULL )
  191. return;
  192. wxPoint pos = GetScreen()->GetCrossHairPosition( true );
  193. if( m_drawItem->ContinueEdit( pos ) )
  194. {
  195. m_drawItem->Draw( m_canvas, DC, pos, -1, g_XorMode, NULL, DefaultTransform );
  196. return;
  197. }
  198. EndDrawGraphicItem( DC );
  199. }
  200. /*
  201. * Redraw the graphic shape while moving
  202. */
  203. static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  204. bool aErase )
  205. {
  206. LIB_ITEM* item;
  207. item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
  208. if( item == NULL )
  209. return;
  210. BASE_SCREEN* Screen = aPanel->GetScreen();
  211. item->SetEraseLastDrawItem( aErase );
  212. // if item is the reference field, we must add the current unit id
  213. if( item->Type() == LIB_FIELD_T )
  214. {
  215. int unit = ((LIB_EDIT_FRAME*)aPanel->GetParent())->GetUnit();
  216. wxString text = ((LIB_FIELD*)item)->GetFullText( unit );
  217. item->Draw( aPanel, aDC, Screen->GetCrossHairPosition( true ), -1, g_XorMode, &text,
  218. DefaultTransform );
  219. }
  220. else
  221. item->Draw( aPanel, aDC, Screen->GetCrossHairPosition( true ), -1, g_XorMode, NULL,
  222. DefaultTransform );
  223. }
  224. void LIB_EDIT_FRAME::StartMoveDrawSymbol( wxDC* DC )
  225. {
  226. if( m_drawItem == NULL )
  227. return;
  228. SetCursor( wxCURSOR_HAND );
  229. TempCopyComponent();
  230. m_drawItem->BeginEdit( IS_MOVED, GetScreen()->GetCrossHairPosition( true ) );
  231. m_canvas->SetMouseCapture( RedrawWhileMovingCursor, AbortSymbolTraceOn );
  232. m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
  233. }
  234. // @brief Modify a graphic symbol (drag edges etc.)
  235. void LIB_EDIT_FRAME::StartModifyDrawSymbol( wxDC* DC )
  236. {
  237. if( m_drawItem == NULL )
  238. return;
  239. TempCopyComponent();
  240. m_drawItem->BeginEdit( IS_RESIZED, GetScreen()->GetCrossHairPosition( true ) );
  241. m_canvas->SetMouseCapture( SymbolDisplayDraw, AbortSymbolTraceOn );
  242. m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
  243. }
  244. //! @brief Manage mouse events when creating new graphic object or modifying an graphic object.
  245. static void SymbolDisplayDraw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
  246. bool aErase )
  247. {
  248. BASE_SCREEN* Screen = aPanel->GetScreen();
  249. LIB_ITEM* item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
  250. if( item == NULL )
  251. return;
  252. item->SetEraseLastDrawItem( aErase );
  253. item->Draw( aPanel, aDC, Screen->GetCrossHairPosition( true ), -1, g_XorMode, NULL,
  254. DefaultTransform );
  255. }
  256. /*
  257. * Place the new graphic object in the list of component drawing objects,
  258. * or terminate a draw item edition
  259. */
  260. void LIB_EDIT_FRAME::EndDrawGraphicItem( wxDC* DC )
  261. {
  262. if( m_component == NULL || m_drawItem == NULL )
  263. return;
  264. if( GetToolId() != ID_NO_TOOL_SELECTED )
  265. SetCursor( wxCURSOR_PENCIL );
  266. else
  267. SetCursor( m_canvas->GetDefaultCursor() );
  268. if( GetTempCopyComponent() ) // used when editing an existing item
  269. SaveCopyInUndoList( GetTempCopyComponent() );
  270. else // When creating a new item, there is still no change for the current component
  271. // So save it.
  272. SaveCopyInUndoList( m_component );
  273. if( m_drawItem->IsNew() )
  274. m_component->AddDrawItem( m_drawItem );
  275. m_drawItem->EndEdit( GetScreen()->GetCrossHairPosition( true ) );
  276. m_drawItem = NULL;
  277. OnModify();
  278. m_canvas->SetMouseCapture( NULL, NULL );
  279. m_canvas->Refresh();
  280. }