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.

139 lines
4.5 KiB

  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. #ifndef PCB_FIELD_H
  25. #define PCB_FIELD_H
  26. #include <pcb_text.h>
  27. #include <template_fieldnames.h>
  28. class BOARD_DESIGN_SETTINGS;
  29. class PCB_FIELD : public PCB_TEXT
  30. {
  31. public:
  32. PCB_FIELD( FOOTPRINT* aParent, FIELD_T aFieldId, const wxString& aName = wxEmptyString );
  33. PCB_FIELD( const PCB_TEXT& aText, FIELD_T aFieldId, const wxString& aName = wxEmptyString );
  34. void Serialize( google::protobuf::Any &aContainer ) const override;
  35. bool Deserialize( const google::protobuf::Any &aContainer ) override;
  36. static inline bool ClassOf( const EDA_ITEM* aItem )
  37. {
  38. return aItem && PCB_FIELD_T == aItem->Type();
  39. }
  40. wxString GetClass() const override { return wxT( "PCB_FIELD" ); }
  41. bool IsType( const std::vector<KICAD_T>& aScanTypes ) const override
  42. {
  43. if( BOARD_ITEM::IsType( aScanTypes ) )
  44. return true;
  45. for( KICAD_T scanType : aScanTypes )
  46. {
  47. if( scanType == PCB_FIELD_LOCATE_REFERENCE_T && m_id == FIELD_T::REFERENCE )
  48. return true;
  49. else if( scanType == PCB_FIELD_LOCATE_VALUE_T && m_id == FIELD_T::VALUE )
  50. return true;
  51. else if( scanType == PCB_FIELD_LOCATE_DATASHEET_T && m_id == FIELD_T::DATASHEET )
  52. return true;
  53. }
  54. return false;
  55. }
  56. bool IsReference() const { return m_id == FIELD_T::REFERENCE; }
  57. bool IsValue() const { return m_id == FIELD_T::VALUE; }
  58. bool IsDatasheet() const { return m_id == FIELD_T::DATASHEET; }
  59. bool IsComponentClass() const { return GetName() == wxT( "Component Class" ); }
  60. bool IsMandatory() const;
  61. bool IsHypertext() const;
  62. wxString GetTextTypeDescription() const override;
  63. bool Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const override;
  64. wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
  65. double ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const override;
  66. EDA_ITEM* Clone() const override;
  67. /**
  68. * Same as Clone, but returns a PCB_FIELD item.
  69. *
  70. * Useful mainly for python scripts, because Clone returns an EDA_ITEM.
  71. */
  72. PCB_FIELD* CloneField() const { return (PCB_FIELD*) Clone(); }
  73. /**
  74. * Return the field name (not translated).
  75. *
  76. * @param aUseDefaultName When true return the default field name if the field name is
  77. * empty. Otherwise the default field name is returned.
  78. */
  79. wxString GetName( bool aUseDefaultName = true ) const;
  80. /**
  81. * Get a non-language-specific name for a field which can be used for storage, variable
  82. * look-up, etc.
  83. */
  84. wxString GetCanonicalName() const;
  85. void SetName( const wxString& aName ) { m_name = aName; }
  86. FIELD_T GetId() const { return m_id; }
  87. int GetOrdinal() const
  88. {
  89. return IsMandatory() ? (int) m_id : m_ordinal;
  90. }
  91. void SetOrdinal( int aOrdinal )
  92. {
  93. m_id = FIELD_T::USER;
  94. m_ordinal = aOrdinal;
  95. }
  96. double Similarity( const BOARD_ITEM& aOther ) const override;
  97. bool operator==( const PCB_FIELD& aOther ) const;
  98. bool operator==( const BOARD_ITEM& aOther ) const override;
  99. protected:
  100. void swapData( BOARD_ITEM* aImage ) override;
  101. private:
  102. void setId( FIELD_T aId ) { m_id = aId; }
  103. private:
  104. FIELD_T m_id; ///< Field id, @see enum FIELD_T
  105. int m_ordinal; ///< Sort order for non-mandatory fields
  106. wxString m_name;
  107. };
  108. #endif