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.

603 lines
17 KiB

14 years ago
15 years ago
15 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2014 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file sch_field.cpp
  26. * @brief Implementation of the SCH_FIELD class.
  27. */
  28. /* Fields are texts attached to a component, having a special meaning
  29. * Fields 0 and 1 are very important: reference and value
  30. * Field 2 is used as default footprint name.
  31. * Field 3 is reserved (not currently used
  32. * Fields 4 and more are user fields.
  33. * They can be renamed and can appear in reports
  34. */
  35. #include <fctsys.h>
  36. #include <class_drawpanel.h>
  37. #include <base_struct.h>
  38. #include <gr_basic.h>
  39. #include <drawtxt.h>
  40. #include <macros.h>
  41. #include <schframe.h>
  42. #include <plot_common.h>
  43. #include <bitmaps.h>
  44. #include <general.h>
  45. #include <class_library.h>
  46. #include <sch_component.h>
  47. #include <sch_field.h>
  48. #include <kicad_string.h>
  49. SCH_FIELD::SCH_FIELD( const wxPoint& aPos, int aFieldId, SCH_COMPONENT* aParent, wxString aName ) :
  50. SCH_ITEM( aParent, SCH_FIELD_T ),
  51. EDA_TEXT()
  52. {
  53. SetTextPos( aPos );
  54. m_id = aFieldId;
  55. m_name = aName;
  56. SetVisible( false );
  57. SetLayer( LAYER_FIELDS );
  58. }
  59. SCH_FIELD::~SCH_FIELD()
  60. {
  61. }
  62. EDA_ITEM* SCH_FIELD::Clone() const
  63. {
  64. return new SCH_FIELD( *this );
  65. }
  66. const wxString SCH_FIELD::GetFullyQualifiedText() const
  67. {
  68. wxString text = m_Text;
  69. /* For more than one part per package, we must add the part selection
  70. * A, B, ... or 1, 2, .. to the reference. */
  71. if( m_id == REFERENCE )
  72. {
  73. SCH_COMPONENT* component = (SCH_COMPONENT*) m_Parent;
  74. wxCHECK_MSG( component != NULL, text,
  75. wxT( "No component associated with field" ) + text );
  76. if( component->GetUnitCount() > 1 )
  77. text << LIB_PART::SubReference( component->GetUnit() );
  78. }
  79. return text;
  80. }
  81. int SCH_FIELD::GetPenSize() const
  82. {
  83. int pensize = GetThickness();
  84. if( pensize == 0 ) // Use default values for pen size
  85. {
  86. if( IsBold() )
  87. pensize = GetPenSizeForBold( GetTextWidth() );
  88. else
  89. pensize = GetDefaultLineThickness();
  90. }
  91. // Clip pen size for small texts:
  92. pensize = Clamp_Text_PenSize( pensize, GetTextSize(), IsBold() );
  93. return pensize;
  94. }
  95. void SCH_FIELD::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  96. GR_DRAWMODE aDrawMode, COLOR4D aColor )
  97. {
  98. int orient;
  99. COLOR4D color;
  100. wxPoint textpos;
  101. SCH_COMPONENT* parentComponent = (SCH_COMPONENT*) m_Parent;
  102. int lineWidth = GetThickness();
  103. if( lineWidth == 0 ) // Use default values for pen size
  104. {
  105. if( IsBold() )
  106. lineWidth = GetPenSizeForBold( GetTextWidth() );
  107. else
  108. lineWidth = GetDefaultLineThickness();
  109. }
  110. // Clip pen size for small texts:
  111. lineWidth = Clamp_Text_PenSize( lineWidth, GetTextSize(), IsBold() );
  112. if( ( !IsVisible() && !m_forceVisible) || IsVoid() )
  113. return;
  114. GRSetDrawMode( aDC, aDrawMode );
  115. // Calculate the text orientation according to the component orientation.
  116. orient = GetTextAngle();
  117. if( parentComponent->GetTransform().y1 ) // Rotate component 90 degrees.
  118. {
  119. if( orient == TEXT_ANGLE_HORIZ )
  120. orient = TEXT_ANGLE_VERT;
  121. else
  122. orient = TEXT_ANGLE_HORIZ;
  123. }
  124. /* Calculate the text justification, according to the component
  125. * orientation/mirror this is a bit complicated due to cumulative
  126. * calculations:
  127. * - numerous cases (mirrored or not, rotation)
  128. * - the DrawGraphicText function recalculate also H and H justifications
  129. * according to the text orientation.
  130. * - When a component is mirrored, the text is not mirrored and
  131. * justifications are complicated to calculate
  132. * so the more easily way is to use no justifications ( Centered text )
  133. * and use GetBoundaryBox to know the text coordinate considered as centered
  134. */
  135. EDA_RECT boundaryBox = GetBoundingBox();
  136. textpos = boundaryBox.Centre() + aOffset;
  137. if( m_forceVisible )
  138. {
  139. color = COLOR4D( DARKGRAY );
  140. }
  141. else
  142. {
  143. if( m_id == REFERENCE )
  144. color = GetLayerColor( LAYER_REFERENCEPART );
  145. else if( m_id == VALUE )
  146. color = GetLayerColor( LAYER_VALUEPART );
  147. else
  148. color = GetLayerColor( LAYER_FIELDS );
  149. }
  150. EDA_RECT* clipbox = aPanel ? aPanel->GetClipBox() : NULL;
  151. DrawGraphicText( clipbox, aDC, textpos, color, GetFullyQualifiedText(), orient, GetTextSize(),
  152. GR_TEXT_HJUSTIFY_CENTER, GR_TEXT_VJUSTIFY_CENTER,
  153. lineWidth, IsItalic(), IsBold() );
  154. // While moving: don't loose visual contact to which component this label belongs.
  155. if ( IsWireImage() )
  156. {
  157. const wxPoint origin = parentComponent->GetPosition();
  158. textpos = GetTextPos() - origin;
  159. textpos = parentComponent->GetScreenCoord( textpos );
  160. textpos += parentComponent->GetPosition();
  161. GRLine( clipbox, aDC, origin, textpos, 2, DARKGRAY );
  162. }
  163. /* Enable this to draw the bounding box around the text field to validate
  164. * the bounding box calculations.
  165. */
  166. #if 0
  167. // Draw boundary box:
  168. GRRect( aPanel->GetClipBox(), aDC, boundaryBox, 0, BROWN );
  169. // Draw the text anchor point
  170. /* Calculate the text position, according to the component
  171. * orientation/mirror */
  172. textpos = m_Pos - parentComponent->GetPosition();
  173. textpos = parentComponent->GetScreenCoord( textpos );
  174. textpos += parentComponent->GetPosition();
  175. const int len = 10;
  176. GRLine( clipbox, aDC,
  177. textpos.x - len, textpos.y, textpos.x + len, textpos.y, 0, BLUE );
  178. GRLine( clipbox, aDC,
  179. textpos.x, textpos.y - len, textpos.x, textpos.y + len, 0, BLUE );
  180. #endif
  181. }
  182. void SCH_FIELD::ImportValues( const LIB_FIELD& aSource )
  183. {
  184. SetEffects( aSource );
  185. }
  186. void SCH_FIELD::ExportValues( LIB_FIELD& aDest ) const
  187. {
  188. aDest.SetId( GetId() );
  189. aDest.SetText( m_Text ); // Set field value
  190. aDest.SetName( GetName() );
  191. aDest.SetEffects( *this );
  192. }
  193. void SCH_FIELD::SwapData( SCH_ITEM* aItem )
  194. {
  195. wxCHECK_RET( (aItem != NULL) && (aItem->Type() == SCH_FIELD_T),
  196. wxT( "Cannot swap field data with invalid item." ) );
  197. SCH_FIELD* item = (SCH_FIELD*) aItem;
  198. std::swap( m_Text, item->m_Text );
  199. std::swap( m_Layer, item->m_Layer );
  200. SwapEffects( *item );
  201. }
  202. const EDA_RECT SCH_FIELD::GetBoundingBox() const
  203. {
  204. SCH_COMPONENT* parentComponent = (SCH_COMPONENT*) m_Parent;
  205. int linewidth = GetThickness() == 0 ? GetDefaultLineThickness() : GetThickness();
  206. // We must pass the effective text thickness to GetTextBox
  207. // when calculating the bounding box
  208. linewidth = Clamp_Text_PenSize( linewidth, GetTextSize(), IsBold() );
  209. // Calculate the text bounding box:
  210. EDA_RECT rect;
  211. if( m_id == REFERENCE ) // multi units have one letter or more added to reference
  212. {
  213. SCH_FIELD text( *this ); // Make a local copy to change text
  214. // because GetBoundingBox() is const
  215. text.SetText( GetFullyQualifiedText() );
  216. rect = text.GetTextBox( -1, linewidth );
  217. }
  218. else
  219. rect = GetTextBox( -1, linewidth );
  220. // Calculate the bounding box position relative to the component:
  221. wxPoint origin = parentComponent->GetPosition();
  222. wxPoint pos = GetTextPos() - origin;
  223. wxPoint begin = rect.GetOrigin() - origin;
  224. wxPoint end = rect.GetEnd() - origin;
  225. RotatePoint( &begin, pos, GetTextAngle() );
  226. RotatePoint( &end, pos, GetTextAngle() );
  227. // Due to the Y axis direction, we must mirror the bounding box,
  228. // relative to the text position:
  229. MIRROR( begin.y, pos.y );
  230. MIRROR( end.y, pos.y );
  231. // Now, apply the component transform (mirror/rot)
  232. begin = parentComponent->GetTransform().TransformCoordinate( begin );
  233. end = parentComponent->GetTransform().TransformCoordinate( end );
  234. rect.SetOrigin( begin);
  235. rect.SetEnd( end);
  236. rect.Move( origin );
  237. rect.Normalize();
  238. return rect;
  239. }
  240. bool SCH_FIELD::IsHorizJustifyFlipped() const
  241. {
  242. wxPoint render_center = GetBoundingBox().Centre();
  243. wxPoint pos = GetPosition();
  244. switch( GetHorizJustify() )
  245. {
  246. case GR_TEXT_HJUSTIFY_LEFT:
  247. return render_center.x < pos.x;
  248. case GR_TEXT_HJUSTIFY_RIGHT:
  249. return render_center.x > pos.x;
  250. default:
  251. return false;
  252. }
  253. }
  254. bool SCH_FIELD::Save( FILE* aFile ) const
  255. {
  256. char hjustify = 'C';
  257. if( GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
  258. hjustify = 'L';
  259. else if( GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
  260. hjustify = 'R';
  261. char vjustify = 'C';
  262. if( GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
  263. vjustify = 'B';
  264. else if( GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
  265. vjustify = 'T';
  266. if( fprintf( aFile, "F %d %s %c %-3d %-3d %-3d %4.4X %c %c%c%c",
  267. m_id,
  268. EscapedUTF8( m_Text ).c_str(), // wraps in quotes too
  269. GetTextAngle() == TEXT_ANGLE_HORIZ ? 'H' : 'V',
  270. GetTextPos().x, GetTextPos().y,
  271. GetTextWidth(),
  272. !IsVisible(),
  273. hjustify, vjustify,
  274. IsItalic() ? 'I' : 'N',
  275. IsBold() ? 'B' : 'N' ) == EOF )
  276. {
  277. return false;
  278. }
  279. // Save field name, if the name is user definable
  280. if( m_id >= FIELD1 )
  281. {
  282. if( fprintf( aFile, " %s", EscapedUTF8( m_name ).c_str() ) == EOF )
  283. {
  284. return false;
  285. }
  286. }
  287. if( fprintf( aFile, "\n" ) == EOF )
  288. {
  289. return false;
  290. }
  291. return true;
  292. }
  293. void SCH_FIELD::Place( SCH_EDIT_FRAME* frame, wxDC* DC )
  294. {
  295. frame->GetCanvas()->SetMouseCapture( NULL, NULL );
  296. SCH_COMPONENT* component = (SCH_COMPONENT*) GetParent();
  297. // save old cmp in undo list
  298. frame->SaveUndoItemInUndoList( component );
  299. Draw( frame->GetCanvas(), DC, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
  300. ClearFlags();
  301. frame->GetScreen()->SetCurItem( NULL );
  302. frame->OnModify();
  303. }
  304. bool SCH_FIELD::Matches( wxFindReplaceData& aSearchData, void* aAuxData, wxPoint* aFindLocation )
  305. {
  306. bool match;
  307. wxString text = GetFullyQualifiedText();
  308. // User defined fields have an ID of -1.
  309. if( ((m_id > VALUE || m_id < REFERENCE) && !(aSearchData.GetFlags() & FR_SEARCH_ALL_FIELDS))
  310. || ((m_id == REFERENCE) && !(aSearchData.GetFlags() & FR_REPLACE_REFERENCES)) )
  311. return false;
  312. wxLogTrace( traceFindItem, wxT( " child item " ) + GetSelectMenuText() );
  313. // Take sheet path into account which effects the reference field and the unit for
  314. // components with multiple parts.
  315. if( m_id == REFERENCE && aAuxData != NULL )
  316. {
  317. SCH_COMPONENT* component = (SCH_COMPONENT*) m_Parent;
  318. wxCHECK_MSG( component != NULL, false,
  319. wxT( "No component associated with field" ) + text );
  320. text = component->GetRef( (SCH_SHEET_PATH*) aAuxData );
  321. if( component->GetUnitCount() > 1 )
  322. text << LIB_PART::SubReference( component->GetUnit() );
  323. }
  324. match = SCH_ITEM::Matches( text, aSearchData );
  325. if( match )
  326. {
  327. if( aFindLocation )
  328. *aFindLocation = GetBoundingBox().Centre();
  329. return true;
  330. }
  331. return false;
  332. }
  333. bool SCH_FIELD::Replace( wxFindReplaceData& aSearchData, void* aAuxData )
  334. {
  335. bool isReplaced;
  336. wxString text = GetFullyQualifiedText();
  337. if( m_id == REFERENCE )
  338. {
  339. wxCHECK_MSG( aAuxData != NULL, false,
  340. wxT( "Cannot replace reference designator without valid sheet path." ) );
  341. wxCHECK_MSG( aSearchData.GetFlags() & FR_REPLACE_REFERENCES, false,
  342. wxT( "Invalid replace component reference field call." ) ) ;
  343. SCH_COMPONENT* component = (SCH_COMPONENT*) m_Parent;
  344. wxCHECK_MSG( component != NULL, false,
  345. wxT( "No component associated with field" ) + text );
  346. text = component->GetRef( (SCH_SHEET_PATH*) aAuxData );
  347. // if( component->GetUnitCount() > 1 )
  348. // text << LIB_PART::SubReference( component->GetUnit() );
  349. isReplaced = EDA_ITEM::Replace( aSearchData, text );
  350. if( isReplaced )
  351. component->SetRef( (SCH_SHEET_PATH*) aAuxData, text );
  352. }
  353. else
  354. {
  355. isReplaced = EDA_ITEM::Replace( aSearchData, m_Text );
  356. }
  357. return isReplaced;
  358. }
  359. void SCH_FIELD::Rotate( wxPoint aPosition )
  360. {
  361. wxPoint pt = GetTextPos();
  362. RotatePoint( &pt, aPosition, 900 );
  363. SetTextPos( pt );
  364. }
  365. wxString SCH_FIELD::GetSelectMenuText() const
  366. {
  367. wxString tmp;
  368. tmp.Printf( _( "Field %s" ), GetChars( GetName() ) );
  369. return tmp;
  370. }
  371. wxString SCH_FIELD::GetName( bool aUseDefaultName ) const
  372. {
  373. if( !m_name.IsEmpty() )
  374. return m_name;
  375. else if( aUseDefaultName )
  376. return TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id );
  377. return wxEmptyString;
  378. }
  379. BITMAP_DEF SCH_FIELD::GetMenuImage() const
  380. {
  381. if( m_id == REFERENCE )
  382. return edit_comp_ref_xpm;
  383. if( m_id == VALUE )
  384. return edit_comp_value_xpm;
  385. if( m_id == FOOTPRINT )
  386. return edit_comp_footprint_xpm;
  387. return edit_text_xpm;
  388. }
  389. bool SCH_FIELD::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  390. {
  391. // Do not hit test hidden or empty fields.
  392. if( !IsVisible() || IsVoid() )
  393. return false;
  394. EDA_RECT rect = GetBoundingBox();
  395. rect.Inflate( aAccuracy );
  396. return rect.Contains( aPosition );
  397. }
  398. bool SCH_FIELD::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  399. {
  400. // Do not hit test hidden fields.
  401. if( !IsVisible() || IsVoid() )
  402. return false;
  403. EDA_RECT rect = aRect;
  404. rect.Inflate( aAccuracy );
  405. if( aContained )
  406. return rect.Contains( GetBoundingBox() );
  407. return rect.Intersects( GetBoundingBox() );
  408. }
  409. void SCH_FIELD::Plot( PLOTTER* aPlotter )
  410. {
  411. SCH_COMPONENT* parent = ( SCH_COMPONENT* ) GetParent();
  412. wxCHECK_RET( parent != NULL && parent->Type() == SCH_COMPONENT_T,
  413. wxT( "Cannot plot field with invalid parent." ) );
  414. COLOR4D color = GetLayerColor( GetLayer() );
  415. if( !IsVisible() )
  416. return;
  417. if( IsVoid() )
  418. return;
  419. /* Calculate the text orientation, according to the component
  420. * orientation/mirror */
  421. int orient = GetTextAngle();
  422. if( parent->GetTransform().y1 ) // Rotate component 90 deg.
  423. {
  424. if( orient == TEXT_ANGLE_HORIZ )
  425. orient = TEXT_ANGLE_VERT;
  426. else
  427. orient = TEXT_ANGLE_HORIZ;
  428. }
  429. /* Calculate the text justification, according to the component
  430. * orientation/mirror
  431. * this is a bit complicated due to cumulative calculations:
  432. * - numerous cases (mirrored or not, rotation)
  433. * - the DrawGraphicText function recalculate also H and H justifications
  434. * according to the text orientation.
  435. * - When a component is mirrored, the text is not mirrored and
  436. * justifications are complicated to calculate
  437. * so the more easily way is to use no justifications ( Centered text )
  438. * and use GetBoundaryBox to know the text coordinate considered as centered
  439. */
  440. EDA_RECT BoundaryBox = GetBoundingBox();
  441. EDA_TEXT_HJUSTIFY_T hjustify = GR_TEXT_HJUSTIFY_CENTER;
  442. EDA_TEXT_VJUSTIFY_T vjustify = GR_TEXT_VJUSTIFY_CENTER;
  443. wxPoint textpos = BoundaryBox.Centre();
  444. int thickness = GetPenSize();
  445. aPlotter->Text( textpos, color, GetFullyQualifiedText(), orient, GetTextSize(),
  446. hjustify, vjustify,
  447. thickness, IsItalic(), IsBold() );
  448. }
  449. void SCH_FIELD::SetPosition( const wxPoint& aPosition )
  450. {
  451. SCH_COMPONENT* component = (SCH_COMPONENT*) GetParent();
  452. wxPoint pos = ( (SCH_COMPONENT*) GetParent() )->GetPosition();
  453. // Actual positions are calculated by the rotation/mirror transform of the
  454. // parent component of the field. The inverse transfrom is used to calculate
  455. // the position relative to the parent component.
  456. wxPoint pt = aPosition - pos;
  457. SetTextPos( pos + component->GetTransform().InverseTransform().TransformCoordinate( pt ) );
  458. }
  459. wxPoint SCH_FIELD::GetPosition() const
  460. {
  461. SCH_COMPONENT* component = (SCH_COMPONENT*) GetParent();
  462. wxPoint pos = GetTextPos() - component->GetPosition();
  463. return component->GetTransform().TransformCoordinate( pos ) + component->GetPosition();
  464. }