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.

260 lines
7.5 KiB

6 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) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.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. #include <fctsys.h>
  26. #include <base_struct.h>
  27. #include <pcb_edit_frame.h>
  28. #include <base_units.h>
  29. #include <bitmaps.h>
  30. #include <class_board.h>
  31. #include <class_pcb_text.h>
  32. #include <pcb_painter.h>
  33. using KIGFX::PCB_RENDER_SETTINGS;
  34. TEXTE_PCB::TEXTE_PCB( BOARD_ITEM* parent ) :
  35. BOARD_ITEM( parent, PCB_TEXT_T ),
  36. EDA_TEXT()
  37. {
  38. SetMultilineAllowed( true );
  39. }
  40. TEXTE_PCB::~TEXTE_PCB()
  41. {
  42. }
  43. wxString TEXTE_PCB::GetShownText( int aDepth ) const
  44. {
  45. BOARD* board = dynamic_cast<BOARD*>( GetParent() );
  46. std::function<bool( wxString* )> pcbTextResolver =
  47. [&]( wxString* token ) -> bool
  48. {
  49. if( token->IsSameAs( wxT( "LAYER" ) ) )
  50. {
  51. *token = GetLayerName();
  52. return true;
  53. }
  54. if( token->Contains( ':' ) )
  55. {
  56. wxString remainder;
  57. wxString ref = token->BeforeFirst( ':', &remainder );
  58. BOARD_ITEM* refItem = board->GetItem( KIID( ref ) );
  59. if( refItem && refItem->Type() == PCB_MODULE_T )
  60. {
  61. MODULE* refModule = static_cast<MODULE*>( refItem );
  62. if( refModule->ResolveTextVar( &remainder, aDepth + 1 ) )
  63. {
  64. *token = remainder;
  65. return true;
  66. }
  67. }
  68. }
  69. return false;
  70. };
  71. std::function<bool( wxString* )> boardTextResolver =
  72. [&]( wxString* token ) -> bool
  73. {
  74. return board->ResolveTextVar( token, aDepth + 1 );
  75. };
  76. bool processTextVars = false;
  77. wxString text = EDA_TEXT::GetShownText( &processTextVars );
  78. if( board && processTextVars && aDepth < 10 )
  79. text = ExpandTextVars( text, &pcbTextResolver, board->GetProject(), &boardTextResolver );
  80. return text;
  81. }
  82. void TEXTE_PCB::SetTextAngle( double aAngle )
  83. {
  84. EDA_TEXT::SetTextAngle( NormalizeAngle360Min( aAngle ) );
  85. }
  86. void TEXTE_PCB::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  87. {
  88. wxString msg;
  89. wxCHECK_RET( m_Parent != NULL, wxT( "TEXTE_PCB::GetMsgPanelInfo() m_Parent is NULL." ) );
  90. if( m_Parent->Type() == PCB_DIMENSION_T )
  91. aList.emplace_back( _( "Dimension" ), GetShownText(), DARKGREEN );
  92. else
  93. aList.emplace_back( _( "PCB Text" ), GetShownText(), DARKGREEN );
  94. aList.emplace_back( _( "Layer" ), GetLayerName(), BLUE );
  95. if( !IsMirrored() )
  96. aList.emplace_back( _( "Mirror" ), _( "No" ), DARKGREEN );
  97. else
  98. aList.emplace_back( _( "Mirror" ), _( "Yes" ), DARKGREEN );
  99. msg.Printf( wxT( "%.1f" ), GetTextAngle() / 10.0 );
  100. aList.emplace_back( _( "Angle" ), msg, DARKGREEN );
  101. msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextThickness() );
  102. aList.emplace_back( _( "Thickness" ), msg, MAGENTA );
  103. msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextWidth() );
  104. aList.emplace_back( _( "Width" ), msg, RED );
  105. msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextHeight() );
  106. aList.emplace_back( _( "Height" ), msg, RED );
  107. }
  108. const EDA_RECT TEXTE_PCB::GetBoundingBox() const
  109. {
  110. EDA_RECT rect = GetTextBox();
  111. if( GetTextAngle() )
  112. rect = rect.GetBoundingBoxRotated( GetTextPos(), GetTextAngle() );
  113. return rect;
  114. }
  115. void TEXTE_PCB::Rotate( const wxPoint& aRotCentre, double aAngle )
  116. {
  117. wxPoint pt = GetTextPos();
  118. RotatePoint( &pt, aRotCentre, aAngle );
  119. SetTextPos( pt );
  120. SetTextAngle( GetTextAngle() + aAngle );
  121. }
  122. void TEXTE_PCB::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
  123. {
  124. double angle = GetTextAngle();
  125. bool vertical = KiROUND( angle ) % 1800 == 900;
  126. if( KiROUND( angle ) != 0 )
  127. {
  128. Rotate( aCentre, -angle );
  129. if( vertical )
  130. aFlipLeftRight = !aFlipLeftRight;
  131. }
  132. // Flip the bounding box
  133. EDA_RECT box = GetTextBox();
  134. int left = box.GetLeft();
  135. int right = box.GetRight();
  136. int top = box.GetTop();
  137. int bottom = box.GetBottom();
  138. if( aFlipLeftRight )
  139. {
  140. MIRROR( left, aCentre.x );
  141. MIRROR( right, aCentre.x );
  142. std::swap( left, right );
  143. }
  144. else
  145. {
  146. MIRROR( top, aCentre.y );
  147. MIRROR( bottom, aCentre.y );
  148. std::swap( top, bottom );
  149. }
  150. // Now put the text back in it (these look backwards but remember that out text will
  151. // be mirrored when all is said and done)
  152. switch( GetHorizJustify() )
  153. {
  154. case GR_TEXT_HJUSTIFY_LEFT: SetTextX( right ); break;
  155. case GR_TEXT_HJUSTIFY_CENTER: SetTextX( ( left + right ) / 2 ); break;
  156. case GR_TEXT_HJUSTIFY_RIGHT: SetTextX( left ); break;
  157. }
  158. switch( GetVertJustify() )
  159. {
  160. case GR_TEXT_VJUSTIFY_TOP: SetTextY( bottom ); break;
  161. case GR_TEXT_VJUSTIFY_CENTER: SetTextY( ( top + bottom ) / 2 ); break;
  162. case GR_TEXT_VJUSTIFY_BOTTOM: SetTextY( top ); break;
  163. }
  164. // And restore orientation
  165. if( KiROUND( angle ) != 0 )
  166. Rotate( aCentre, angle );
  167. SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
  168. SetMirrored( !IsMirrored() );
  169. }
  170. wxString TEXTE_PCB::GetSelectMenuText( EDA_UNITS aUnits ) const
  171. {
  172. return wxString::Format( _( "Pcb Text \"%s\" on %s"), ShortenedShownText(), GetLayerName() );
  173. }
  174. BITMAP_DEF TEXTE_PCB::GetMenuImage() const
  175. {
  176. return text_xpm;
  177. }
  178. EDA_ITEM* TEXTE_PCB::Clone() const
  179. {
  180. return new TEXTE_PCB( *this );
  181. }
  182. void TEXTE_PCB::SwapData( BOARD_ITEM* aImage )
  183. {
  184. assert( aImage->Type() == PCB_TEXT_T );
  185. std::swap( *((TEXTE_PCB*) this), *((TEXTE_PCB*) aImage) );
  186. }
  187. std::shared_ptr<SHAPE> TEXTE_PCB::GetEffectiveShape( PCB_LAYER_ID aLayer ) const
  188. {
  189. return GetEffectiveTextShape();
  190. }
  191. static struct TEXTE_PCB_DESC
  192. {
  193. TEXTE_PCB_DESC()
  194. {
  195. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  196. REGISTER_TYPE( TEXTE_PCB );
  197. propMgr.AddTypeCast( new TYPE_CAST<TEXTE_PCB, BOARD_ITEM> );
  198. propMgr.AddTypeCast( new TYPE_CAST<TEXTE_PCB, EDA_TEXT> );
  199. propMgr.InheritsAfter( TYPE_HASH( TEXTE_PCB ), TYPE_HASH( BOARD_ITEM ) );
  200. propMgr.InheritsAfter( TYPE_HASH( TEXTE_PCB ), TYPE_HASH( EDA_TEXT ) );
  201. }
  202. } _TEXTE_PCB_DESC;