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.

189 lines
6.5 KiB

11 years ago
11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2014-2023 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 _TEMPLATE_FIELDNAME_H_
  25. #define _TEMPLATE_FIELDNAME_H_
  26. #include <richio.h>
  27. #include <template_fieldnames_lexer.h>
  28. class TEMPLATE_FIELDNAMES_LEXER;
  29. /**
  30. * The set of all field indices assuming an array like sequence that a SCH_COMPONENT or
  31. * LIB_PART can hold.
  32. *
  33. * The first fields are fixed fields and are defined by #MANDATORY_FIELDS. After that come
  34. * an unlimited number of user defined fields, only some of which have indices defined here.
  35. */
  36. enum MANDATORY_FIELD_T {
  37. REFERENCE_FIELD = 0, ///< Field Reference of part, i.e. "IC21"
  38. VALUE_FIELD, ///< Field Value of part, i.e. "3.3K"
  39. FOOTPRINT_FIELD, ///< Field Name Module PCB, i.e. "16DIP300"
  40. DATASHEET_FIELD, ///< name of datasheet
  41. DESCRIPTION_FIELD, ///< Field Description of part, i.e. "1/4W 1% Metal Film Resistor"
  42. /// The first 5 are mandatory, and must be instantiated in SCH_COMPONENT
  43. /// and LIB_PART constructors
  44. MANDATORY_FIELDS
  45. };
  46. // A helper to call GetDefaultFieldName with or without translation.
  47. // Translation should be used only to display field names in dialogs
  48. #define DO_TRANSLATE true
  49. /**
  50. * Hold a name of a symbol's field, field value, and default visibility.
  51. *
  52. * Template fieldnames are wanted field names for use in the symbol property editors.
  53. */
  54. struct TEMPLATE_FIELDNAME
  55. {
  56. TEMPLATE_FIELDNAME() :
  57. m_Visible( false ),
  58. m_URL( false )
  59. {
  60. }
  61. TEMPLATE_FIELDNAME( const wxString& aName ) :
  62. m_Name( aName ),
  63. m_Visible( false ),
  64. m_URL( false )
  65. {
  66. }
  67. TEMPLATE_FIELDNAME( const TEMPLATE_FIELDNAME& ref )
  68. {
  69. m_Name = ref.m_Name;
  70. m_Visible = ref.m_Visible;
  71. m_URL = ref.m_URL;
  72. }
  73. /**
  74. * Serialize this object out as text into the given #OUTPUTFORMATTER.
  75. */
  76. void Format( OUTPUTFORMATTER* out, int nestLevel ) const ;
  77. /**
  78. * Fill this object from information in the input stream \a aSpec, which is a
  79. * #TEMPLATE_FIELDNAMES_LEXER.
  80. *
  81. * The entire textual element spec is <br>(field (name _yourfieldname_)(value _yourvalue_)
  82. * visible))</br>. The presence of value is optional, the presence of visible is optional.
  83. * When this function is called, the input token stream given by \a aSpec is assumed to be
  84. * positioned at the '^' in the following example, i.e. just after the identifying keyword
  85. * and before the content specifying stuff.<br>(field ^ (....) )</br>.
  86. *
  87. * @param aSpec is the input token stream of keywords and symbols.
  88. */
  89. void Parse( TEMPLATE_FIELDNAMES_LEXER* aSpec );
  90. /**
  91. * Return a default symbol field name for field \a aFieldNdx for all components.
  92. *
  93. * These field names are not modifiable but template field names are.
  94. *
  95. * @param aFieldNdx The field number index, > 0.
  96. * @param aTranslateForHI If true, return the translated field name,
  97. * else get the canonical name (defualt). Translation is intended only for dialogs
  98. */
  99. static const wxString GetDefaultFieldName( int aFieldNdx, bool aTranslateForHI = false );
  100. wxString m_Name; // The field name
  101. bool m_Visible; // Field defaults to being visible in schematic.
  102. bool m_URL; // If field should have a browse button
  103. };
  104. typedef std::vector< TEMPLATE_FIELDNAME > TEMPLATE_FIELDNAMES;
  105. class TEMPLATES
  106. {
  107. public:
  108. TEMPLATES() :
  109. m_resolvedDirty( true )
  110. { }
  111. /**
  112. * Serialize this object out as text into the given #OUTPUTFORMATTER.
  113. */
  114. void Format( OUTPUTFORMATTER* out, int nestLevel, bool aGlobal ) const ;
  115. /**
  116. * Insert or append a wanted symbol field name into the field names template.
  117. *
  118. * Should be used for any symbol property editor. If the name already exists, it
  119. * overwrites the same name.
  120. *
  121. * @param aFieldName is a full description of the wanted field, and it must not match
  122. * any of the default field names.
  123. * @param aGlobal indicates whether to add to the global or project table.
  124. */
  125. void AddTemplateFieldName( const TEMPLATE_FIELDNAME& aFieldName, bool aGlobal );
  126. /**
  127. * Add a serialized list of template field names.
  128. */
  129. void AddTemplateFieldNames( const wxString& aSerializedFieldNames );
  130. /**
  131. * Delete the entire contents.
  132. */
  133. void DeleteAllFieldNameTemplates( bool aGlobal );
  134. /**
  135. * Return a template field name list for read only access.
  136. */
  137. const TEMPLATE_FIELDNAMES& GetTemplateFieldNames();
  138. /**
  139. * Return a specific list (global or project) for read only access.
  140. */
  141. const TEMPLATE_FIELDNAMES& GetTemplateFieldNames( bool aGlobal );
  142. /**
  143. * Search for \a aName in the template field name list.
  144. *
  145. * @param aName A wxString object containing the field name to search for.
  146. * @return the template field name if found; NULL otherwise.
  147. */
  148. const TEMPLATE_FIELDNAME* GetFieldName( const wxString& aName );
  149. protected:
  150. void resolveTemplates();
  151. void parse( TEMPLATE_FIELDNAMES_LEXER* in, bool aGlobal );
  152. private:
  153. TEMPLATE_FIELDNAMES m_globals;
  154. TEMPLATE_FIELDNAMES m_project;
  155. // Combined list. Project templates override global ones.
  156. TEMPLATE_FIELDNAMES m_resolved;
  157. bool m_resolvedDirty;
  158. };
  159. #endif // _TEMPLATE_FIELDNAME_H_