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