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.

188 lines
6.4 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. /// The first 4 are mandatory, and must be instantiated in SCH_COMPONENT
  42. /// and LIB_PART constructors
  43. MANDATORY_FIELDS
  44. };
  45. // A helper to call GetDefaultFieldName with or without translation.
  46. // Translation should be used only to display field names in dialogs
  47. #define DO_TRANSLATE true
  48. /**
  49. * Hold a name of a symbol's field, field value, and default visibility.
  50. *
  51. * Template fieldnames are wanted field names for use in the symbol property editors.
  52. */
  53. struct TEMPLATE_FIELDNAME
  54. {
  55. TEMPLATE_FIELDNAME() :
  56. m_Visible( false ),
  57. m_URL( false )
  58. {
  59. }
  60. TEMPLATE_FIELDNAME( const wxString& aName ) :
  61. m_Name( aName ),
  62. m_Visible( false ),
  63. m_URL( false )
  64. {
  65. }
  66. TEMPLATE_FIELDNAME( const TEMPLATE_FIELDNAME& ref )
  67. {
  68. m_Name = ref.m_Name;
  69. m_Visible = ref.m_Visible;
  70. m_URL = ref.m_URL;
  71. }
  72. /**
  73. * Serialize this object out as text into the given #OUTPUTFORMATTER.
  74. */
  75. void Format( OUTPUTFORMATTER* out, int nestLevel ) const ;
  76. /**
  77. * Fill this object from information in the input stream \a aSpec, which is a
  78. * #TEMPLATE_FIELDNAMES_LEXER.
  79. *
  80. * The entire textual element spec is <br>(field (name _yourfieldname_)(value _yourvalue_)
  81. * visible))</br>. The presence of value is optional, the presence of visible is optional.
  82. * When this function is called, the input token stream given by \a aSpec is assumed to be
  83. * positioned at the '^' in the following example, i.e. just after the identifying keyword
  84. * and before the content specifying stuff.<br>(field ^ (....) )</br>.
  85. *
  86. * @param aSpec is the input token stream of keywords and symbols.
  87. */
  88. void Parse( TEMPLATE_FIELDNAMES_LEXER* aSpec );
  89. /**
  90. * Return a default symbol field name for field \a aFieldNdx for all components.
  91. *
  92. * These field names are not modifiable but template field names are.
  93. *
  94. * @param aFieldNdx The field number index, > 0.
  95. * @param aTranslateForHI If true, return the translated field name,
  96. * else get the canonical name (defualt). Translation is intended only for dialogs
  97. */
  98. static const wxString GetDefaultFieldName( int aFieldNdx, bool aTranslateForHI = false );
  99. wxString m_Name; // The field name
  100. bool m_Visible; // Field defaults to being visible in schematic.
  101. bool m_URL; // If field should have a browse button
  102. };
  103. typedef std::vector< TEMPLATE_FIELDNAME > TEMPLATE_FIELDNAMES;
  104. class TEMPLATES
  105. {
  106. public:
  107. TEMPLATES() :
  108. m_resolvedDirty( true )
  109. { }
  110. /**
  111. * Serialize this object out as text into the given #OUTPUTFORMATTER.
  112. */
  113. void Format( OUTPUTFORMATTER* out, int nestLevel, bool aGlobal ) const ;
  114. /**
  115. * Insert or append a wanted symbol field name into the field names template.
  116. *
  117. * Should be used for any symbol property editor. If the name already exists, it
  118. * overwrites the same name.
  119. *
  120. * @param aFieldName is a full description of the wanted field, and it must not match
  121. * any of the default field names.
  122. * @param aGlobal indicates whether to add to the global or project table.
  123. */
  124. void AddTemplateFieldName( const TEMPLATE_FIELDNAME& aFieldName, bool aGlobal );
  125. /**
  126. * Add a serialized list of template field names.
  127. */
  128. void AddTemplateFieldNames( const wxString& aSerializedFieldNames );
  129. /**
  130. * Delete the entire contents.
  131. */
  132. void DeleteAllFieldNameTemplates( bool aGlobal );
  133. /**
  134. * Return a template field name list for read only access.
  135. */
  136. const TEMPLATE_FIELDNAMES& GetTemplateFieldNames();
  137. /**
  138. * Return a specific list (global or project) for read only access.
  139. */
  140. const TEMPLATE_FIELDNAMES& GetTemplateFieldNames( bool aGlobal );
  141. /**
  142. * Search for \a aName in the template field name list.
  143. *
  144. * @param aName A wxString object containing the field name to search for.
  145. * @return the template field name if found; NULL otherwise.
  146. */
  147. const TEMPLATE_FIELDNAME* GetFieldName( const wxString& aName );
  148. protected:
  149. void resolveTemplates();
  150. void parse( TEMPLATE_FIELDNAMES_LEXER* in, bool aGlobal );
  151. private:
  152. TEMPLATE_FIELDNAMES m_globals;
  153. TEMPLATE_FIELDNAMES m_project;
  154. // Combined list. Project templates override global ones.
  155. TEMPLATE_FIELDNAMES m_resolved;
  156. bool m_resolvedDirty;
  157. };
  158. #endif // _TEMPLATE_FIELDNAME_H_