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.

237 lines
7.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2022 CERN
  6. * Copyright (C) 2004-2022 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. /**
  26. * @file lib_field.h
  27. */
  28. #ifndef CLASS_LIBENTRY_FIELDS_H
  29. #define CLASS_LIBENTRY_FIELDS_H
  30. #include <eda_text.h>
  31. #include <lib_item.h>
  32. class SCH_LEGACY_PLUGIN_CACHE;
  33. /**
  34. * Field object used in symbol libraries. At least MANDATORY_FIELDS are always present in a
  35. * RAM-resident library symbol. All constructors must ensure this because the symbol property
  36. * editor assumes it.
  37. * <p>
  38. * A field is a string linked to a symbol. Unlike purely graphical text, fields can be used in
  39. * netlist generation and other tools (BOM).
  40. *
  41. * The first 5 fields have a special meaning:
  42. *
  43. * 0 = REFERENCE_FIELD
  44. * 1 = VALUE_FIELD
  45. * 2 = FOOTPRINT_FIELD (default Footprint)
  46. * 3 = DATASHEET_FIELD (user doc link)
  47. * 4 = DESCRIPTION_FIELD
  48. *
  49. * others = free fields
  50. * </p>
  51. *
  52. * @see enum MANDATORY_FIELD_T
  53. */
  54. class LIB_FIELD : public LIB_ITEM, public EDA_TEXT
  55. {
  56. public:
  57. LIB_FIELD( int aId = 2 );
  58. LIB_FIELD( int aId, const wxString& aName );
  59. LIB_FIELD( LIB_SYMBOL* aParent, int aId = 2, const wxString& aName = wxEmptyString );
  60. // Do not create a copy constructor. The one generated by the compiler is adequate.
  61. wxString GetClass() const override
  62. {
  63. return wxT( "LIB_FIELD" );
  64. }
  65. static inline bool ClassOf( const EDA_ITEM* aItem )
  66. {
  67. return aItem && aItem->Type() == LIB_FIELD_T;
  68. }
  69. wxString GetTypeName() const override
  70. {
  71. return _( "Field" );
  72. }
  73. /**
  74. * Object constructor initialization helper.
  75. */
  76. void Init( int aId );
  77. /**
  78. * Return the field name (not translated).
  79. *
  80. * The first four field IDs are reserved and therefore always return their respective
  81. * names.
  82. *
  83. * The user definable fields will return FieldN where N is the ID of the field when the
  84. * m_name member is empty unless false is passed to \a aUseDefaultName.
  85. */
  86. wxString GetName( bool aUseDefaultName = true ) const;
  87. /**
  88. * Get a non-language-specific name for a field which can be used for storage, variable
  89. * look-up, etc.
  90. */
  91. wxString GetCanonicalName() const;
  92. /**
  93. * Set a user definable field name to \a aName.
  94. *
  95. * Reserved fields such as value and reference are not renamed. If the field name is
  96. * changed, the field modified flag is set. If the field is the child of a symbol, the
  97. * parent symbol's modified flag is also set.
  98. *
  99. * @param aName - User defined field name.
  100. */
  101. void SetName( const wxString& aName );
  102. int GetId() const { return m_id; }
  103. void SetId( int aId );
  104. int GetPenWidth() const override;
  105. KIFONT::FONT* getDrawFont() const override;
  106. /**
  107. * Copy parameters of this field to another field. Pointers are not copied.
  108. *
  109. * @param aTarget Target field to copy values to.
  110. */
  111. void Copy( LIB_FIELD* aTarget ) const;
  112. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  113. const BOX2I GetBoundingBox() const override;
  114. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  115. bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
  116. LIB_FIELD& operator=( const LIB_FIELD& field );
  117. /**
  118. * Return the text of a field.
  119. *
  120. * If the field is the reference field, the unit number is used to
  121. * create a pseudo reference text. If the base reference field is U,
  122. * the string U?A will be returned for unit = 1.
  123. *
  124. * @param unit - The package unit number. Only effects reference field.
  125. * @return Field text.
  126. */
  127. wxString GetFullText( int unit = 1 ) const;
  128. wxString GetShownText( bool aAllowExtraText, int aDepth = 0 ) const override;
  129. SCH_LAYER_ID GetDefaultLayer() const;
  130. void BeginEdit( const VECTOR2I& aStartPoint ) override;
  131. void Offset( const VECTOR2I& aOffset ) override;
  132. void MoveTo( const VECTOR2I& aPosition ) override;
  133. VECTOR2I GetPosition() const override { return EDA_TEXT::GetTextPos(); }
  134. void MirrorHorizontal( const VECTOR2I& aCenter ) override;
  135. void MirrorVertical( const VECTOR2I& aCenter ) override;
  136. void Rotate( const VECTOR2I& aCenter, bool aRotateCCW = true ) override;
  137. void Plot( PLOTTER* aPlotter, bool aBackground, const VECTOR2I& aOffset,
  138. const TRANSFORM& aTransform, bool aDimmed ) const override;
  139. wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const override;
  140. BITMAPS GetMenuImage() const override;
  141. EDA_ITEM* Clone() const override;
  142. bool IsMandatory() const;
  143. bool IsAutoAdded() const { return m_autoAdded; }
  144. void SetAutoAdded( bool aAutoAdded ) { m_autoAdded = aAutoAdded; }
  145. bool IsNameShown() const { return m_showName; }
  146. void SetNameShown( bool aShown = true ) { m_showName = aShown; }
  147. bool CanAutoplace() const { return m_allowAutoPlace; }
  148. void SetCanAutoplace( bool aCanPlace ) { m_allowAutoPlace = aCanPlace; }
  149. bool ShowInChooser() const { return m_showInChooser; }
  150. void SetShowInChooser( bool aShow = true ) { m_showInChooser = aShow; }
  151. private:
  152. /**
  153. * @copydoc LIB_ITEM::compare()
  154. *
  155. * The field specific sort order is as follows:
  156. *
  157. * - Field ID, REFERENCE, VALUE, etc.
  158. * - Field string, case insensitive compare.
  159. * - Field horizontal (X) position.
  160. * - Field vertical (Y) position.
  161. * - Field width.
  162. * - Field height.
  163. */
  164. int compare( const LIB_ITEM& aOther, int aCompareFlags = 0 ) const override;
  165. /**
  166. * Print the field.
  167. *
  168. * If \a aData not NULL, \a aData must point a wxString which is used instead of
  169. * the m_Text
  170. */
  171. void print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, void* aData,
  172. const TRANSFORM& aTransform, bool aDimmed ) override;
  173. /**
  174. * Calculate the new circle at \a aPosition when editing.
  175. *
  176. * @param aPosition - The position to edit the circle in drawing coordinates.
  177. */
  178. void CalcEdit( const VECTOR2I& aPosition ) override;
  179. friend class SCH_LEGACY_PLUGIN_CACHE; // Required to access m_name.
  180. int m_id; ///< @see enum MANDATORY_FIELD_T
  181. wxString m_name; ///< Name (not the field text value itself, that is #EDA_TEXT::m_Text)
  182. bool m_autoAdded; ///< Was this field automatically added to a LIB_SYMBOL?
  183. bool m_showName; ///< Render the field's name in addition to its value
  184. bool m_allowAutoPlace; ///< This field can be autoplaced when converted to a SCH_FIELD
  185. bool m_showInChooser; ///< This field is available as a data column for the chooser
  186. };
  187. #endif // CLASS_LIBENTRY_FIELDS_H