KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_field.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Mike Williams, [email protected]
5 * Copyright The 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#include <pcb_field.h>
26#include <footprint.h>
28#include <i18n_utility.h>
29#include <pcb_painter.h>
30#include <api/board/board_types.pb.h>
31#include <string_utils.h>
32#include <board.h>
33
34
35PCB_FIELD::PCB_FIELD( FOOTPRINT* aParent, FIELD_T aFieldId, const wxString& aName ) :
36 PCB_TEXT( aParent, PCB_FIELD_T ),
37 m_id( aFieldId ),
38 m_ordinal( 0 ),
39 m_name( aName )
40{
41 if( m_id == FIELD_T::USER )
42 m_ordinal = aParent->GetNextFieldOrdinal();
43}
44
45
46PCB_FIELD::PCB_FIELD( const PCB_TEXT& aText, FIELD_T aFieldId, const wxString& aName ) :
47 PCB_TEXT( aText.GetParent(), PCB_FIELD_T ),
48 m_id( aFieldId ),
49 m_ordinal( static_cast<int>( aFieldId ) ),
50 m_name( aName )
51{
52 // Copy the text properties from the PCB_TEXT
53 SetText( aText.GetText() );
54 SetVisible( aText.IsVisible() );
55 SetLayer( aText.GetLayer() );
56 SetPosition( aText.GetPosition() );
58}
59
60
61void PCB_FIELD::Serialize( google::protobuf::Any &aContainer ) const
62{
63 kiapi::board::types::Field field;
64
65 google::protobuf::Any anyText;
66 PCB_TEXT::Serialize( anyText );
67 anyText.UnpackTo( field.mutable_text() );
68
69 field.set_name( GetCanonicalName().ToStdString() );
70 field.mutable_id()->set_id( (int) GetId() );
71 field.set_visible( IsVisible() );
72
73 aContainer.PackFrom( field );
74}
75
76
77bool PCB_FIELD::Deserialize( const google::protobuf::Any &aContainer )
78{
79 kiapi::board::types::Field field;
80
81 if( !aContainer.UnpackTo( &field ) )
82 return false;
83
84 if( field.has_id() )
85 setId( (FIELD_T) field.id().id() );
86
87 // Mandatory fields have a blank Name in the KiCad object
88 if( !IsMandatory() )
89 SetName( wxString( field.name().c_str(), wxConvUTF8 ) );
90
91 if( field.has_text() )
92 {
93 google::protobuf::Any anyText;
94 anyText.PackFrom( field.text() );
95 PCB_TEXT::Deserialize( anyText );
96 }
97
98 SetVisible( field.visible() );
99
100 if( field.text().layer() == kiapi::board::types::BoardLayer::BL_UNKNOWN )
101 SetLayer( F_SilkS );
102
103 return true;
104}
105
106
107wxString PCB_FIELD::GetName( bool aUseDefaultName ) const
108{
109 if( IsMandatory() )
110 return GetCanonicalFieldName( m_id );
111 else if( m_name.IsEmpty() && aUseDefaultName )
113 else
114 return m_name;
115}
116
117
119{
120 return GetName( true );
121}
122
123
124wxString PCB_FIELD::GetShownText( bool aAllowExtraText, int aDepth ) const
125{
126 wxUnusedVar( aAllowExtraText );
127
128 const FOOTPRINT* parentFootprint = GetParentFootprint();
129 const BOARD* board = GetBoard();
130 wxString text;
131 bool hasVariantOverride = false;
132
133 if( parentFootprint && board )
134 {
135 const wxString& variantName = board->GetCurrentVariant();
136
137 if( !variantName.IsEmpty() && variantName.CmpNoCase( GetDefaultVariantName() ) != 0 )
138 {
139 if( const FOOTPRINT_VARIANT* variant = parentFootprint->GetVariant( variantName ) )
140 {
141 if( variant->HasFieldValue( GetName() ) )
142 {
143 text = parentFootprint->GetFieldValueForVariant( variantName, GetName() );
144 hasVariantOverride = true;
145 }
146 }
147 }
148 }
149
150 if( !hasVariantOverride )
151 text = GetText();
152
154
155 std::function<bool( wxString* )> resolver = [&]( wxString* token ) -> bool
156 {
157 if( token->IsSameAs( wxT( "LAYER" ) ) )
158 {
159 *token = GetLayerName();
160 return true;
161 }
162
163 if( parentFootprint && parentFootprint->ResolveTextVar( token, aDepth + 1 ) )
164 return true;
165
166 if( board && board->ResolveTextVar( token, aDepth + 1 ) )
167 return true;
168
169 return false;
170 };
171
172 if( text.Contains( wxT( "${" ) ) || text.Contains( wxT( "@{" ) ) )
173 text = ResolveTextVars( text, &resolver, aDepth );
174
175 text.Replace( wxT( "<<<ESC_DOLLAR:" ), wxT( "${" ) );
176 text.Replace( wxT( "<<<ESC_AT:" ), wxT( "@{" ) );
177
178 return text;
179}
180
181
183{
184 return m_id == FIELD_T::REFERENCE
185 || m_id == FIELD_T::VALUE
188}
189
190
192{
193 return IsURL( GetShownText( false ) );
194}
195
196
198{
199 if( IsMandatory() )
200 return GetCanonicalFieldName( m_id );
201 else
202 return _( "User Field" );
203}
204
205
206bool PCB_FIELD::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
207{
208 if( !IsVisible() && !aSearchData.searchAllFields )
209 return false;
210
211 return PCB_TEXT::Matches( aSearchData, aAuxData );
212}
213
214
215wxString PCB_FIELD::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
216{
217 wxString content = aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() );
218 wxString ref = GetParentFootprint()->GetReference();
219
220 switch( m_id )
221 {
223 return wxString::Format( _( "Reference field of %s" ), ref );
224
225 case FIELD_T::VALUE:
226 return wxString::Format( _( "Value field of %s (%s)" ), ref, content );
227
229 return wxString::Format( _( "Footprint field of %s (%s)" ), ref, content );
230
232 return wxString::Format( _( "Datasheet field of %s (%s)" ), ref, content );
233
234 default:
235 if( GetName().IsEmpty() )
236 return wxString::Format( _( "Field of %s (%s)" ), ref, content );
237 else
238 return wxString::Format( _( "%s field of %s (%s)" ), GetName(), ref, content );
239 }
240}
241
242
243double PCB_FIELD::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
244{
245 if( !aView )
246 return LOD_SHOW;
247
248 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
249 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
250
252 && renderSettings->m_ForceShowFieldsWhenFPSelected )
253 {
254 return LOD_SHOW;
255 }
256
257 // Handle Render tab switches
258 if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
259 return LOD_HIDE;
260
262 return LOD_HIDE;
263
264 return PCB_TEXT::ViewGetLOD( aLayer, aView );
265}
266
267
269{
270 return new PCB_FIELD( *this );
271}
272
273
275{
276 assert( aImage->Type() == PCB_FIELD_T );
277
278 std::swap( *((PCB_FIELD*) this), *((PCB_FIELD*) aImage) );
279}
280
281
282bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
283{
284 if( aOther.Type() != Type() )
285 return false;
286
287 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
288
289 return *this == other;
290}
291
292
293bool PCB_FIELD::operator==( const PCB_FIELD& aOther ) const
294{
295 if( IsMandatory() != aOther.IsMandatory() )
296 return false;
297
298 if( IsMandatory() )
299 {
300 if( m_id != aOther.m_id )
301 return false;
302 }
303 else
304 {
305 if( m_ordinal != aOther.m_ordinal )
306 return false;
307 }
308
309 return m_name == aOther.m_name && EDA_TEXT::operator==( aOther );
310}
311
312
313double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
314{
315 if( m_Uuid == aOther.m_Uuid )
316 return 1.0;
317
318 if( aOther.Type() != Type() )
319 return 0.0;
320
321 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
322
323 if( IsMandatory() || other.IsMandatory() )
324 {
325 if( m_id == other.m_id )
326 return 1.0;
327 else
328 return 0.0;
329 }
330
331 if( m_name == other.m_name )
332 return 1.0;
333
334 return EDA_TEXT::Similarity( other );
335}
336
337static struct PCB_FIELD_DESC
338{
340 {
349
350 propMgr.AddProperty( new PROPERTY<PCB_FIELD, wxString>( _HKI( "Name" ),
354
355 // These properties, inherited from EDA_TEXT, have no sense for the board editor
356 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
357 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );
358 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:85
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:236
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:284
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
FOOTPRINT * GetParentFootprint() const
BOARD_ITEM_CONTAINER * GetParent() const
Definition board_item.h:214
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
bool ResolveTextVar(wxString *token, int aDepth) const
Definition board.cpp:501
wxString GetCurrentVariant() const
Definition board.h:404
const KIID m_Uuid
Definition eda_item.h:522
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
bool IsSelected() const
Definition eda_item.h:127
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:39
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:80
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:98
virtual bool IsVisible() const
Definition eda_text.h:187
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition eda_text.cpp:442
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:395
double Similarity(const EDA_TEXT &aOther) const
const TEXT_ATTRIBUTES & GetAttributes() const
Definition eda_text.h:231
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:279
bool operator==(const EDA_TEXT &aRhs) const
Definition eda_text.h:396
Variant information for a footprint.
Definition footprint.h:144
bool ResolveTextVar(wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the component.
int GetNextFieldOrdinal() const
Return the next ordinal for a user field for this footprint.
const FOOTPRINT_VARIANT * GetVariant(const wxString &aVariantName) const
Get a variant by name.
wxString GetFieldValueForVariant(const wxString &aVariantName, const wxString &aFieldName) const
Get a field value for a specific variant.
const wxString & GetReference() const
Definition footprint.h:741
Contains methods for drawing PCB-specific items.
virtual PCB_RENDER_SETTINGS * GetSettings() override
Return a pointer to current settings that are going to be used when drawing items.
PCB specific render settings.
Definition pcb_painter.h:82
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition view_item.h:180
static constexpr double LOD_SHOW
Return this constant from ViewGetLOD() to show the item unconditionally.
Definition view_item.h:185
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:66
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:422
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:220
bool IsMandatory() const
FIELD_T GetId() const
Definition pcb_field.h:112
bool operator==(const PCB_FIELD &aOther) const
PCB_FIELD(FOOTPRINT *aParent, FIELD_T aFieldId, const wxString &aName=wxEmptyString)
Definition pcb_field.cpp:35
void setId(FIELD_T aId)
Definition pcb_field.h:133
wxString GetTextTypeDescription() const override
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
wxString GetShownText(bool aAllowExtraText, int aDepth=0) const override
Return the string actually shown after processing of the base text.
bool IsReference() const
Definition pcb_field.h:68
bool HasHypertext() const
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition pcb_field.cpp:61
bool IsValue() const
Definition pcb_field.h:69
wxString m_name
Definition pcb_field.h:138
FIELD_T m_id
Field id,.
Definition pcb_field.h:136
int m_ordinal
Sort order for non-mandatory fields.
Definition pcb_field.h:137
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
void SetName(const wxString &aName)
Definition pcb_field.h:110
void swapData(BOARD_ITEM *aImage) override
double Similarity(const BOARD_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition pcb_field.cpp:77
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition pcb_text.cpp:175
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition pcb_text.cpp:91
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition pcb_text.cpp:218
virtual VECTOR2I GetPosition() const override
Definition pcb_text.h:82
PCB_TEXT(BOARD_ITEM *parent, KICAD_T idtype=PCB_TEXT_T)
Definition pcb_text.cpp:50
virtual void SetPosition(const VECTOR2I &aPos) override
Definition pcb_text.h:84
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition pcb_text.cpp:114
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition property.h:319
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
Definition property.h:333
Provide class metadata.Helper macro to map type hashes to names.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
void Mask(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName)
Sets a base class property as masked in a derived class.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
wxString ResolveTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aResolver, int &aDepth)
Multi-pass text variable expansion and math expression evaluation.
Definition common.cpp:295
#define _(s)
static FILENAME_RESOLVER * resolver
Some functions to handle hotkeys in KiCad.
@ LAYER_FP_REFERENCES
Show footprints references (when texts are visible).
Definition layer_ids.h:266
@ LAYER_FP_VALUES
Show footprints values (when texts are visible).
Definition layer_ids.h:263
@ F_SilkS
Definition layer_ids.h:100
KICOMMON_API wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
#define _HKI(x)
Definition page_info.cpp:44
static struct PCB_FIELD_DESC _PCB_FIELD_DESC
#define TYPE_HASH(x)
Definition property.h:74
#define NO_SETTER(owner, type)
Definition property.h:828
#define REGISTER_TYPE(x)
wxString GetDefaultVariantName()
wxString UnescapeString(const wxString &aSource)
bool IsURL(wxString aStr)
Performs a URL sniff-test on a string.
wxString GetUserFieldName(int aFieldNdx, bool aTranslateForHI)
#define DO_TRANSLATE
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ USER
The field ID hasn't been set yet; field is invalid.
@ DESCRIPTION
Field Description of part, i.e. "1/4W 1% Metal Film Resistor".
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ DATASHEET
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
wxString GetCanonicalFieldName(FIELD_T aFieldType)
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:90