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