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.

276 lines
7.5 KiB

3 years ago
3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Mike Williams, mike@mikebwilliams.com
  5. * Copyright The 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. #include <pcb_field.h>
  25. #include <footprint.h>
  26. #include <board_design_settings.h>
  27. #include <i18n_utility.h>
  28. #include <pcb_painter.h>
  29. #include <api/board/board_types.pb.h>
  30. PCB_FIELD::PCB_FIELD( FOOTPRINT* aParent, int aFieldId, const wxString& aName ) :
  31. PCB_TEXT( aParent, PCB_FIELD_T ),
  32. m_id( aFieldId ),
  33. m_name( aName )
  34. {
  35. }
  36. PCB_FIELD::PCB_FIELD( const PCB_TEXT& aText, int aFieldId, const wxString& aName ) :
  37. PCB_TEXT( aText ),
  38. m_id( aFieldId ),
  39. m_name( aName )
  40. {
  41. }
  42. void PCB_FIELD::Serialize( google::protobuf::Any &aContainer ) const
  43. {
  44. kiapi::board::types::Field field;
  45. google::protobuf::Any anyText;
  46. PCB_TEXT::Serialize( anyText );
  47. anyText.UnpackTo( field.mutable_text() );
  48. field.set_name( GetCanonicalName().ToStdString() );
  49. field.mutable_id()->set_id( GetId() );
  50. aContainer.PackFrom( field );
  51. }
  52. bool PCB_FIELD::Deserialize( const google::protobuf::Any &aContainer )
  53. {
  54. kiapi::board::types::Field field;
  55. if( !aContainer.UnpackTo( &field ) )
  56. return false;
  57. if( field.has_id() )
  58. setId( field.id().id() );
  59. // Mandatory fields have a blank Name in the KiCad object
  60. if( !IsMandatory() )
  61. SetName( wxString( field.name().c_str(), wxConvUTF8 ) );
  62. if( field.has_text() )
  63. {
  64. google::protobuf::Any anyText;
  65. anyText.PackFrom( field.text() );
  66. PCB_TEXT::Deserialize( anyText );
  67. }
  68. if( field.text().layer() == kiapi::board::types::BoardLayer::BL_UNKNOWN )
  69. SetLayer( F_SilkS );
  70. return true;
  71. }
  72. wxString PCB_FIELD::GetName( bool aUseDefaultName ) const
  73. {
  74. if( m_parent && m_parent->Type() == PCB_FOOTPRINT_T )
  75. {
  76. if( IsMandatory() )
  77. return GetCanonicalFieldName( m_id );
  78. else if( m_name.IsEmpty() && aUseDefaultName )
  79. return GetUserFieldName( m_id, !DO_TRANSLATE );
  80. else
  81. return m_name;
  82. }
  83. else
  84. {
  85. wxFAIL_MSG( "Unhandled field owner type." );
  86. return m_name;
  87. }
  88. }
  89. wxString PCB_FIELD::GetCanonicalName() const
  90. {
  91. if( m_parent && m_parent->Type() == PCB_FOOTPRINT_T )
  92. {
  93. if( IsMandatory() )
  94. return GetCanonicalFieldName( m_id );
  95. else
  96. return m_name;
  97. }
  98. else
  99. {
  100. if( m_parent )
  101. {
  102. wxFAIL_MSG( wxString::Format( "Unhandled field owner type (id %d, parent type %d).",
  103. m_id, m_parent->Type() ) );
  104. }
  105. return m_name;
  106. }
  107. }
  108. bool PCB_FIELD::IsMandatory() const
  109. {
  110. return m_id == REFERENCE_FIELD
  111. || m_id == VALUE_FIELD
  112. || m_id == DATASHEET_FIELD
  113. || m_id == DESCRIPTION_FIELD;
  114. }
  115. wxString PCB_FIELD::GetTextTypeDescription() const
  116. {
  117. if( IsMandatory() )
  118. return GetCanonicalFieldName( m_id );
  119. else
  120. return _( "User Field" );
  121. }
  122. wxString PCB_FIELD::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
  123. {
  124. wxString content = aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() );
  125. wxString ref = GetParentFootprint()->GetReference();
  126. switch( m_id )
  127. {
  128. case REFERENCE_FIELD:
  129. return wxString::Format( _( "Reference field of %s" ), ref );
  130. case VALUE_FIELD:
  131. return wxString::Format( _( "Value field of %s (%s)" ), ref, content );
  132. case FOOTPRINT_FIELD:
  133. return wxString::Format( _( "Footprint field of %s (%s)" ), ref, content );
  134. case DATASHEET_FIELD:
  135. return wxString::Format( _( "Datasheet field of %s (%s)" ), ref, content );
  136. default:
  137. if( GetName().IsEmpty() )
  138. return wxString::Format( _( "Field of %s (%s)" ), ref, content );
  139. else
  140. return wxString::Format( _( "%s field of %s (%s)" ), GetName(), ref, content );
  141. }
  142. }
  143. double PCB_FIELD::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
  144. {
  145. if( !aView )
  146. return LOD_SHOW;
  147. KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
  148. KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
  149. if( GetParentFootprint() && GetParentFootprint()->IsSelected()
  150. && renderSettings->m_ForceShowFieldsWhenFPSelected )
  151. {
  152. return LOD_SHOW;
  153. }
  154. // Handle Render tab switches
  155. if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
  156. return LOD_HIDE;
  157. if( IsReference() && !aView->IsLayerVisible( LAYER_FP_REFERENCES ) )
  158. return LOD_HIDE;
  159. return PCB_TEXT::ViewGetLOD( aLayer, aView );
  160. }
  161. EDA_ITEM* PCB_FIELD::Clone() const
  162. {
  163. return new PCB_FIELD( *this );
  164. }
  165. void PCB_FIELD::swapData( BOARD_ITEM* aImage )
  166. {
  167. assert( aImage->Type() == PCB_FIELD_T );
  168. std::swap( *((PCB_FIELD*) this), *((PCB_FIELD*) aImage) );
  169. }
  170. bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
  171. {
  172. if( aOther.Type() != Type() )
  173. return false;
  174. const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
  175. return *this == other;
  176. }
  177. bool PCB_FIELD::operator==( const PCB_FIELD& aOther ) const
  178. {
  179. return m_id == aOther.m_id && m_name == aOther.m_name && EDA_TEXT::operator==( aOther );
  180. }
  181. double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
  182. {
  183. if( m_Uuid == aOther.m_Uuid )
  184. return 1.0;
  185. if( aOther.Type() != Type() )
  186. return 0.0;
  187. const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
  188. if( IsMandatory() || other.IsMandatory() )
  189. {
  190. if( m_id == other.m_id )
  191. return 1.0;
  192. else
  193. return 0.0;
  194. }
  195. if( m_name == other.m_name )
  196. return 1.0;
  197. return EDA_TEXT::Similarity( other );
  198. }
  199. static struct PCB_FIELD_DESC
  200. {
  201. PCB_FIELD_DESC()
  202. {
  203. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  204. REGISTER_TYPE( PCB_FIELD );
  205. propMgr.AddTypeCast( new TYPE_CAST<PCB_FIELD, PCB_TEXT> );
  206. propMgr.AddTypeCast( new TYPE_CAST<PCB_FIELD, BOARD_ITEM> );
  207. propMgr.AddTypeCast( new TYPE_CAST<PCB_FIELD, EDA_TEXT> );
  208. propMgr.InheritsAfter( TYPE_HASH( PCB_FIELD ), TYPE_HASH( BOARD_ITEM ) );
  209. propMgr.InheritsAfter( TYPE_HASH( PCB_FIELD ), TYPE_HASH( PCB_TEXT ) );
  210. propMgr.InheritsAfter( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ) );
  211. // These properties, inherited from EDA_TEXT, have no sense for the board editor
  212. propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
  213. propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );
  214. }
  215. } _PCB_FIELD_DESC;