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.

787 lines
24 KiB

  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-2021 KiCad Developers, see AUTHORS.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. * Fields are texts attached to a symbol, some of which have a special meaning.
  26. * Fields 0 and 1 are very important: reference and value.
  27. * Field 2 is used as default footprint name.
  28. * Field 3 is used to point to a datasheet (usually a URL).
  29. * Fields 4+ are user fields. They can be renamed and can appear in reports.
  30. */
  31. #include <wx/log.h>
  32. #include <wx/menu.h>
  33. #include <common.h> // for ExpandTextVars
  34. #include <eda_item.h>
  35. #include <sch_edit_frame.h>
  36. #include <plotters/plotter.h>
  37. #include <bitmaps.h>
  38. #include <core/kicad_algo.h>
  39. #include <core/mirror.h>
  40. #include <kiway.h>
  41. #include <general.h>
  42. #include <symbol_library.h>
  43. #include <sch_symbol.h>
  44. #include <sch_field.h>
  45. #include <schematic.h>
  46. #include <settings/color_settings.h>
  47. #include <string_utils.h>
  48. #include <trace_helpers.h>
  49. #include <trigo.h>
  50. #include <eeschema_id.h>
  51. #include <tool/tool_manager.h>
  52. #include <tools/ee_actions.h>
  53. SCH_FIELD::SCH_FIELD( const wxPoint& aPos, int aFieldId, SCH_ITEM* aParent,
  54. const wxString& aName ) :
  55. SCH_ITEM( aParent, SCH_FIELD_T ),
  56. EDA_TEXT( wxEmptyString ),
  57. m_id( 0 ),
  58. m_name( aName )
  59. {
  60. SetTextPos( aPos );
  61. SetId( aFieldId ); // will also set the layer
  62. SetVisible( false );
  63. }
  64. SCH_FIELD::~SCH_FIELD()
  65. {
  66. }
  67. EDA_ITEM* SCH_FIELD::Clone() const
  68. {
  69. return new SCH_FIELD( *this );
  70. }
  71. void SCH_FIELD::SetId( int aId )
  72. {
  73. m_id = aId;
  74. if( m_parent && m_parent->Type() == SCH_SHEET_T )
  75. {
  76. switch( m_id )
  77. {
  78. case SHEETNAME: SetLayer( LAYER_SHEETNAME ); break;
  79. case SHEETFILENAME: SetLayer( LAYER_SHEETFILENAME ); break;
  80. default: SetLayer( LAYER_SHEETFIELDS ); break;
  81. }
  82. }
  83. else
  84. {
  85. switch( m_id )
  86. {
  87. case REFERENCE_FIELD: SetLayer( LAYER_REFERENCEPART ); break;
  88. case VALUE_FIELD: SetLayer( LAYER_VALUEPART ); break;
  89. default: SetLayer( LAYER_FIELDS ); break;
  90. }
  91. }
  92. }
  93. wxString SCH_FIELD::GetShownText( int aDepth ) const
  94. {
  95. std::function<bool( wxString* )> symbolResolver =
  96. [&]( wxString* token ) -> bool
  97. {
  98. if( token->Contains( ':' ) )
  99. {
  100. if( Schematic()->ResolveCrossReference( token, aDepth ) )
  101. return true;
  102. }
  103. else
  104. {
  105. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  106. if( parentSymbol->ResolveTextVar( token, aDepth + 1 ) )
  107. return true;
  108. SCHEMATIC* schematic = parentSymbol->Schematic();
  109. SCH_SHEET* sheet = schematic ? schematic->CurrentSheet().Last() : nullptr;
  110. if( sheet && sheet->ResolveTextVar( token, aDepth + 1 ) )
  111. return true;
  112. }
  113. return false;
  114. };
  115. std::function<bool( wxString* )> sheetResolver =
  116. [&]( wxString* token ) -> bool
  117. {
  118. SCH_SHEET* sheet = static_cast<SCH_SHEET*>( m_parent );
  119. return sheet->ResolveTextVar( token, aDepth + 1 );
  120. };
  121. std::function<bool( wxString* )> globalLabelResolver =
  122. [&]( wxString* token ) -> bool
  123. {
  124. SCH_GLOBALLABEL* globalLabel = static_cast<SCH_GLOBALLABEL*>( m_parent );
  125. return globalLabel->ResolveTextVar( token, aDepth + 1 );
  126. };
  127. PROJECT* project = nullptr;
  128. wxString text = EDA_TEXT::GetShownText();
  129. if( text == "~" ) // Legacy placeholder for empty string
  130. {
  131. text = "";
  132. }
  133. else if( HasTextVars() )
  134. {
  135. if( Schematic() )
  136. project = &Schematic()->Prj();
  137. if( aDepth < 10 )
  138. {
  139. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  140. text = ExpandTextVars( text, &symbolResolver, nullptr, project );
  141. else if( m_parent && m_parent->Type() == SCH_SHEET_T )
  142. text = ExpandTextVars( text, &sheetResolver, nullptr, project );
  143. else if( m_parent && m_parent->Type() == SCH_GLOBAL_LABEL_T )
  144. text = ExpandTextVars( text, &globalLabelResolver, nullptr, project );
  145. else
  146. text = ExpandTextVars( text, project );
  147. }
  148. }
  149. // WARNING: the IDs of FIELDS and SHEETS overlap, so one must check *both* the
  150. // id and the parent's type.
  151. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  152. {
  153. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  154. if( m_id == REFERENCE_FIELD )
  155. {
  156. // For more than one part per package, we must add the part selection
  157. // A, B, ... or 1, 2, .. to the reference.
  158. if( parentSymbol->GetUnitCount() > 1 )
  159. text << LIB_SYMBOL::SubReference( parentSymbol->GetUnit() );
  160. }
  161. }
  162. else if( m_parent && m_parent->Type() == SCH_SHEET_T )
  163. {
  164. if( m_id == SHEETFILENAME )
  165. text = _( "File:" ) + wxS( " " )+ text;
  166. }
  167. return text;
  168. }
  169. int SCH_FIELD::GetPenWidth() const
  170. {
  171. return GetEffectiveTextPenWidth();
  172. }
  173. void SCH_FIELD::Print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
  174. {
  175. wxDC* DC = aSettings->GetPrintDC();
  176. COLOR4D color = aSettings->GetLayerColor( IsForceVisible() ? LAYER_HIDDEN : m_layer );
  177. int orient;
  178. wxPoint textpos;
  179. int penWidth = GetEffectiveTextPenWidth( aSettings->GetDefaultPenWidth() );
  180. if( ( !IsVisible() && !IsForceVisible() ) || IsVoid() )
  181. return;
  182. // Calculate the text orientation according to the symbol orientation.
  183. orient = GetTextAngle();
  184. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  185. {
  186. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  187. if( parentSymbol && parentSymbol->GetTransform().y1 ) // Rotate symbol 90 degrees.
  188. {
  189. if( orient == TEXT_ANGLE_HORIZ )
  190. orient = TEXT_ANGLE_VERT;
  191. else
  192. orient = TEXT_ANGLE_HORIZ;
  193. }
  194. }
  195. /*
  196. * Calculate the text justification, according to the symbol orientation/mirror.
  197. * This is a bit complicated due to cumulative calculations:
  198. * - numerous cases (mirrored or not, rotation)
  199. * - the GRText function will also recalculate H and V justifications according to the text
  200. * orientation.
  201. * - When a symbol is mirrored, the text is not mirrored and justifications are complicated
  202. * to calculate so the more easily way is to use no justifications (centered text) and use
  203. * GetBoundingBox to know the text coordinate considered as centered
  204. */
  205. textpos = GetBoundingBox().Centre() + aOffset;
  206. GRText( DC, textpos, color, GetShownText(), orient, GetTextSize(), GR_TEXT_HJUSTIFY_CENTER,
  207. GR_TEXT_VJUSTIFY_CENTER, penWidth, IsItalic(), IsBold() );
  208. }
  209. void SCH_FIELD::ImportValues( const LIB_FIELD& aSource )
  210. {
  211. SetEffects( aSource );
  212. }
  213. void SCH_FIELD::SwapData( SCH_ITEM* aItem )
  214. {
  215. wxCHECK_RET( ( aItem != nullptr ) && ( aItem->Type() == SCH_FIELD_T ),
  216. wxT( "Cannot swap field data with invalid item." ) );
  217. SCH_FIELD* item = (SCH_FIELD*) aItem;
  218. std::swap( m_layer, item->m_layer );
  219. SwapText( *item );
  220. SwapEffects( *item );
  221. }
  222. const EDA_RECT SCH_FIELD::GetBoundingBox() const
  223. {
  224. // Calculate the text bounding box:
  225. EDA_RECT rect = GetTextBox();
  226. // Calculate the bounding box position relative to the parent:
  227. wxPoint origin = GetParentPosition();
  228. wxPoint pos = GetTextPos() - origin;
  229. wxPoint begin = rect.GetOrigin() - origin;
  230. wxPoint end = rect.GetEnd() - origin;
  231. RotatePoint( &begin, pos, GetTextAngle() );
  232. RotatePoint( &end, pos, GetTextAngle() );
  233. // Now, apply the symbol transform (mirror/rot)
  234. TRANSFORM transform;
  235. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  236. {
  237. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  238. // Due to the Y axis direction, we must mirror the bounding box,
  239. // relative to the text position:
  240. MIRROR( begin.y, pos.y );
  241. MIRROR( end.y, pos.y );
  242. transform = parentSymbol->GetTransform();
  243. }
  244. else
  245. {
  246. transform = TRANSFORM( 1, 0, 0, 1 ); // identity transform
  247. }
  248. rect.SetOrigin( transform.TransformCoordinate( begin ) );
  249. rect.SetEnd( transform.TransformCoordinate( end ) );
  250. rect.Move( origin );
  251. rect.Normalize();
  252. return rect;
  253. }
  254. bool SCH_FIELD::IsHorizJustifyFlipped() const
  255. {
  256. wxPoint render_center = GetBoundingBox().Centre();
  257. wxPoint pos = GetPosition();
  258. switch( GetHorizJustify() )
  259. {
  260. case GR_TEXT_HJUSTIFY_LEFT:
  261. return render_center.x < pos.x;
  262. case GR_TEXT_HJUSTIFY_RIGHT:
  263. return render_center.x > pos.x;
  264. default:
  265. return false;
  266. }
  267. }
  268. bool SCH_FIELD::IsVoid() const
  269. {
  270. return GetText().Len() == 0;
  271. }
  272. bool SCH_FIELD::Matches( const wxFindReplaceData& aSearchData, void* aAuxData ) const
  273. {
  274. wxString text = GetShownText();
  275. int flags = aSearchData.GetFlags();
  276. bool searchHiddenFields = flags & FR_SEARCH_ALL_FIELDS;
  277. bool searchAndReplace = flags & FR_SEARCH_REPLACE;
  278. bool replaceReferences = flags & FR_REPLACE_REFERENCES;
  279. wxLogTrace( traceFindItem, wxT( " child item " )
  280. + GetSelectMenuText( EDA_UNITS::MILLIMETRES ) );
  281. if( !IsVisible() && !searchHiddenFields )
  282. return false;
  283. if( m_parent && m_parent->Type() == SCH_SYMBOL_T && m_id == REFERENCE_FIELD )
  284. {
  285. if( searchAndReplace && !replaceReferences )
  286. return false;
  287. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  288. wxASSERT( aAuxData );
  289. // Take sheet path into account which effects the reference field and the unit for
  290. // symbols with multiple parts.
  291. if( aAuxData )
  292. {
  293. text = parentSymbol->GetRef((SCH_SHEET_PATH*) aAuxData );
  294. if( SCH_ITEM::Matches( text, aSearchData ) )
  295. return true;
  296. if( parentSymbol->GetUnitCount() > 1 )
  297. text << LIB_SYMBOL::SubReference( parentSymbol->GetUnit() );
  298. }
  299. }
  300. return SCH_ITEM::Matches( text, aSearchData );
  301. }
  302. bool SCH_FIELD::IsReplaceable() const
  303. {
  304. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  305. {
  306. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  307. if( m_id == VALUE_FIELD )
  308. {
  309. if( parentSymbol->GetLibSymbolRef() && parentSymbol->GetLibSymbolRef()->IsPower() )
  310. return false;
  311. }
  312. }
  313. else if( m_parent && m_parent->Type() == SCH_SHEET_T )
  314. {
  315. // See comments in SCH_FIELD::Replace(), below.
  316. if( m_id == SHEETFILENAME )
  317. return false;
  318. }
  319. else if( m_parent && m_parent->Type() == SCH_GLOBAL_LABEL_T )
  320. {
  321. return false;
  322. }
  323. return true;
  324. }
  325. bool SCH_FIELD::Replace( const wxFindReplaceData& aSearchData, void* aAuxData )
  326. {
  327. wxString text;
  328. bool resolve = false; // Replace in source text, not shown text
  329. bool isReplaced = false;
  330. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  331. {
  332. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  333. switch( m_id )
  334. {
  335. case REFERENCE_FIELD:
  336. wxCHECK_MSG( aAuxData, false, wxT( "Need sheetpath to replace in refdes." ) );
  337. if( !( aSearchData.GetFlags() & FR_REPLACE_REFERENCES ) )
  338. return false;
  339. text = parentSymbol->GetRef( (SCH_SHEET_PATH*) aAuxData );
  340. isReplaced = EDA_ITEM::Replace( aSearchData, text );
  341. if( isReplaced )
  342. parentSymbol->SetRef( (SCH_SHEET_PATH*) aAuxData, text );
  343. break;
  344. case VALUE_FIELD:
  345. wxCHECK_MSG( aAuxData, false, wxT( "Need sheetpath to replace in value field." ) );
  346. text = parentSymbol->GetValue((SCH_SHEET_PATH*) aAuxData, resolve );
  347. isReplaced = EDA_ITEM::Replace( aSearchData, text );
  348. if( isReplaced )
  349. parentSymbol->SetValue( (SCH_SHEET_PATH*) aAuxData, text );
  350. break;
  351. case FOOTPRINT_FIELD:
  352. wxCHECK_MSG( aAuxData, false, wxT( "Need sheetpath to replace in footprint field." ) );
  353. text = parentSymbol->GetFootprint((SCH_SHEET_PATH*) aAuxData, resolve );
  354. isReplaced = EDA_ITEM::Replace( aSearchData, text );
  355. if( isReplaced )
  356. parentSymbol->SetFootprint( (SCH_SHEET_PATH*) aAuxData, text );
  357. break;
  358. default:
  359. isReplaced = EDA_TEXT::Replace( aSearchData );
  360. }
  361. }
  362. else if( m_parent && m_parent->Type() == SCH_SHEET_T )
  363. {
  364. isReplaced = EDA_TEXT::Replace( aSearchData );
  365. if( m_id == SHEETFILENAME && isReplaced )
  366. {
  367. // If we allowed this we'd have a bunch of work to do here, including warning
  368. // about it not being undoable, checking for recursive hierarchies, reloading
  369. // sheets, etc. See DIALOG_SHEET_PROPERTIES::TransferDataFromWindow().
  370. }
  371. }
  372. return isReplaced;
  373. }
  374. void SCH_FIELD::Rotate( const wxPoint& aCenter )
  375. {
  376. wxPoint pt = GetPosition();
  377. RotatePoint( &pt, aCenter, 900 );
  378. SetPosition( pt );
  379. }
  380. wxString SCH_FIELD::GetSelectMenuText( EDA_UNITS aUnits ) const
  381. {
  382. return wxString::Format( "%s '%s'", GetName(), ShortenedShownText() );
  383. }
  384. void SCH_FIELD::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  385. {
  386. wxString msg;
  387. aList.emplace_back( _( "Symbol Field" ), GetName() );
  388. // Don't use GetShownText() here; we want to show the user the variable references
  389. aList.emplace_back( _( "Text" ), UnescapeString( GetText() ) );
  390. aList.emplace_back( _( "Visible" ), IsVisible() ? _( "Yes" ) : _( "No" ) );
  391. aList.emplace_back( _( "Style" ), GetTextStyleName() );
  392. aList.emplace_back( _( "Text Size" ), MessageTextFromValue( aFrame->GetUserUnits(),
  393. GetTextWidth() ) );
  394. switch ( GetHorizJustify() )
  395. {
  396. case GR_TEXT_HJUSTIFY_LEFT: msg = _( "Left" ); break;
  397. case GR_TEXT_HJUSTIFY_CENTER: msg = _( "Center" ); break;
  398. case GR_TEXT_HJUSTIFY_RIGHT: msg = _( "Right" ); break;
  399. }
  400. aList.emplace_back( _( "H Justification" ), msg );
  401. switch ( GetVertJustify() )
  402. {
  403. case GR_TEXT_VJUSTIFY_TOP: msg = _( "Top" ); break;
  404. case GR_TEXT_VJUSTIFY_CENTER: msg = _( "Center" ); break;
  405. case GR_TEXT_VJUSTIFY_BOTTOM: msg = _( "Bottom" ); break;
  406. }
  407. aList.emplace_back( _( "V Justification" ), msg );
  408. }
  409. void SCH_FIELD::DoHypertextMenu( EDA_DRAW_FRAME* aFrame )
  410. {
  411. constexpr int START_ID = 1;
  412. static wxString back = "HYPERTEXT_BACK";
  413. wxMenu menu;
  414. SCH_TEXT* label = dynamic_cast<SCH_TEXT*>( m_parent );
  415. if( label && Schematic() )
  416. {
  417. auto it = Schematic()->GetPageRefsMap().find( label->GetText() );
  418. if( it != Schematic()->GetPageRefsMap().end() )
  419. {
  420. std::map<wxString, wxString> sheetNames;
  421. std::vector<wxString> pageListCopy;
  422. pageListCopy.insert( pageListCopy.end(), it->second.begin(), it->second.end() );
  423. if( !Schematic()->Settings().m_IntersheetRefsListOwnPage )
  424. {
  425. wxString currentPage = Schematic()->CurrentSheet().GetPageNumber();
  426. alg::delete_matching( pageListCopy, currentPage );
  427. if( pageListCopy.empty() )
  428. return;
  429. }
  430. std::sort( pageListCopy.begin(), pageListCopy.end(),
  431. []( const wxString& a, const wxString& b ) -> bool
  432. {
  433. return StrNumCmp( a, b, true ) < 0;
  434. } );
  435. for( const SCH_SHEET_PATH& sheet : Schematic()->GetSheets() )
  436. {
  437. if( sheet.size() == 1 )
  438. sheetNames[ sheet.GetPageNumber() ] = _( "<root sheet>" );
  439. else
  440. sheetNames[ sheet.GetPageNumber() ] = sheet.Last()->GetName();
  441. }
  442. for( int i = 0; i < (int) pageListCopy.size(); ++i )
  443. {
  444. menu.Append( i + START_ID, wxString::Format( _( "Go to Page %s (%s)" ),
  445. pageListCopy[i],
  446. sheetNames[ pageListCopy[i] ] ) );
  447. }
  448. menu.AppendSeparator();
  449. menu.Append( 999, _( "Back to Previous Selected Sheet" ) );
  450. int sel = aFrame->GetPopupMenuSelectionFromUser( menu ) - START_ID;
  451. void* param = nullptr;
  452. if( sel >= 0 && sel < (int) pageListCopy.size() )
  453. param = (void*) &pageListCopy[ sel ];
  454. else if( sel == 999 )
  455. param = (void*) &back;
  456. if( param )
  457. aFrame->GetToolManager()->RunAction( EE_ACTIONS::hypertextCommand, true, param );
  458. }
  459. }
  460. }
  461. wxString SCH_FIELD::GetName( bool aUseDefaultName ) const
  462. {
  463. if( !m_name.IsEmpty() )
  464. return m_name;
  465. else if( aUseDefaultName )
  466. {
  467. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  468. return TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id );
  469. else if( m_parent && m_parent->Type() == SCH_SHEET_T )
  470. return SCH_SHEET::GetDefaultFieldName( m_id );
  471. else if( m_parent && m_parent->Type() == SCH_GLOBAL_LABEL_T )
  472. return _( "Intersheet References" );
  473. }
  474. return wxEmptyString;
  475. }
  476. wxString SCH_FIELD::GetCanonicalName() const
  477. {
  478. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  479. {
  480. switch( m_id )
  481. {
  482. case REFERENCE_FIELD: return wxT( "Reference" );
  483. case VALUE_FIELD: return wxT( "Value" );
  484. case FOOTPRINT_FIELD: return wxT( "Footprint" );
  485. case DATASHEET_FIELD: return wxT( "Datasheet" );
  486. }
  487. }
  488. else if( m_parent && m_parent->Type() == SCH_SHEET_T )
  489. {
  490. switch( m_id )
  491. {
  492. case SHEETNAME: return wxT( "Sheetname" );
  493. case SHEETFILENAME: return wxT( "Sheetfile" );
  494. }
  495. }
  496. else if( m_parent && m_parent->Type() == SCH_GLOBAL_LABEL_T )
  497. {
  498. return wxT( "Intersheet References" );
  499. }
  500. return m_name;
  501. }
  502. BITMAPS SCH_FIELD::GetMenuImage() const
  503. {
  504. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  505. {
  506. switch( m_id )
  507. {
  508. case REFERENCE_FIELD: return BITMAPS::edit_comp_ref;
  509. case VALUE_FIELD: return BITMAPS::edit_comp_value;
  510. case FOOTPRINT_FIELD: return BITMAPS::edit_comp_footprint;
  511. default: return BITMAPS::text;
  512. }
  513. }
  514. return BITMAPS::text;
  515. }
  516. bool SCH_FIELD::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  517. {
  518. // Do not hit test hidden or empty fields.
  519. if( !IsVisible() || IsVoid() )
  520. return false;
  521. EDA_RECT rect = GetBoundingBox();
  522. rect.Inflate( aAccuracy );
  523. return rect.Contains( aPosition );
  524. }
  525. bool SCH_FIELD::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  526. {
  527. // Do not hit test hidden fields.
  528. if( !IsVisible() || IsVoid() )
  529. return false;
  530. EDA_RECT rect = aRect;
  531. rect.Inflate( aAccuracy );
  532. if( aContained )
  533. return rect.Contains( GetBoundingBox() );
  534. return rect.Intersects( GetBoundingBox() );
  535. }
  536. void SCH_FIELD::Plot( PLOTTER* aPlotter ) const
  537. {
  538. RENDER_SETTINGS* settings = aPlotter->RenderSettings();
  539. COLOR4D color = settings->GetLayerColor( GetLayer() );
  540. int penWidth = GetEffectiveTextPenWidth( settings->GetDefaultPenWidth() );
  541. penWidth = std::max( penWidth, settings->GetMinPenWidth() );
  542. if( !IsVisible() )
  543. return;
  544. if( IsVoid() )
  545. return;
  546. // Calculate the text orientation, according to the symbol orientation/mirror
  547. int orient = GetTextAngle();
  548. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  549. {
  550. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  551. if( parentSymbol->GetTransform().y1 ) // Rotate symbol 90 deg.
  552. {
  553. if( orient == TEXT_ANGLE_HORIZ )
  554. orient = TEXT_ANGLE_VERT;
  555. else
  556. orient = TEXT_ANGLE_HORIZ;
  557. }
  558. }
  559. /*
  560. * Calculate the text justification, according to the symbol orientation/mirror.
  561. * This is a bit complicated due to cumulative calculations:
  562. * - numerous cases (mirrored or not, rotation)
  563. * - the plotter's Text function will also recalculate H and V justifications according to
  564. * the text orientation.
  565. * - When a symbol is mirrored, the text is not mirrored and justifications are complicated
  566. * to calculate so the easier way is to use no justifications (centered text) and use
  567. * GetBoundingBox to know the text coordinate considered as centered
  568. */
  569. EDA_TEXT_HJUSTIFY_T hjustify = GR_TEXT_HJUSTIFY_CENTER;
  570. EDA_TEXT_VJUSTIFY_T vjustify = GR_TEXT_VJUSTIFY_CENTER;
  571. wxPoint textpos = GetBoundingBox().Centre();
  572. aPlotter->Text( textpos, color, GetShownText(), orient, GetTextSize(), hjustify, vjustify,
  573. penWidth, IsItalic(), IsBold() );
  574. }
  575. void SCH_FIELD::SetPosition( const wxPoint& aPosition )
  576. {
  577. // Actual positions are calculated by the rotation/mirror transform of the parent symbol
  578. // of the field. The inverse transform is used to calculate the position relative to the
  579. // parent symbol.
  580. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  581. {
  582. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  583. wxPoint relPos = aPosition - parentSymbol->GetPosition();
  584. relPos = parentSymbol->GetTransform().InverseTransform().TransformCoordinate( relPos );
  585. SetTextPos( relPos + parentSymbol->GetPosition() );
  586. return;
  587. }
  588. SetTextPos( aPosition );
  589. }
  590. wxPoint SCH_FIELD::GetPosition() const
  591. {
  592. if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
  593. {
  594. SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
  595. wxPoint relativePos = GetTextPos() - parentSymbol->GetPosition();
  596. relativePos = parentSymbol->GetTransform().TransformCoordinate( relativePos );
  597. return relativePos + parentSymbol->GetPosition();
  598. }
  599. return GetTextPos();
  600. }
  601. wxPoint SCH_FIELD::GetParentPosition() const
  602. {
  603. return m_parent ? m_parent->GetPosition() : wxPoint( 0, 0 );
  604. }
  605. bool SCH_FIELD::operator <( const SCH_ITEM& aItem ) const
  606. {
  607. if( Type() != aItem.Type() )
  608. return Type() < aItem.Type();
  609. auto field = static_cast<const SCH_FIELD*>( &aItem );
  610. if( GetId() != field->GetId() )
  611. return GetId() < field->GetId();
  612. if( GetText() != field->GetText() )
  613. return GetText() < field->GetText();
  614. if( GetLibPosition().x != field->GetLibPosition().x )
  615. return GetLibPosition().x < field->GetLibPosition().x;
  616. if( GetLibPosition().y != field->GetLibPosition().y )
  617. return GetLibPosition().y < field->GetLibPosition().y;
  618. return GetName() < field->GetName();
  619. }