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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <common.h>
22#include <view/view.h>
23#include <pcb_field.h>
24#include <footprint.h>
26#include <i18n_utility.h>
27#include <pcb_painter.h>
28#include <api/api_utils.h>
29#include <api/board/board_types.pb.h>
30#include <string_utils.h>
31#include <board.h>
32#include <properties/property.h>
34
35
36PCB_FIELD::PCB_FIELD( FOOTPRINT* aParent, FIELD_T aFieldId, const wxString& aName ) :
37 PCB_TEXT( aParent, PCB_FIELD_T ),
38 m_id( aFieldId ),
39 m_ordinal( 0 ),
40 m_name( aName )
41{
42 if( m_id == FIELD_T::USER )
43 m_ordinal = aParent->GetNextFieldOrdinal();
44}
45
46
47PCB_FIELD::PCB_FIELD( const PCB_TEXT& aText, FIELD_T aFieldId, const wxString& aName ) :
48 PCB_TEXT( aText.GetParent(), PCB_FIELD_T ),
49 m_id( aFieldId ),
50 m_ordinal( 0 ),
51 m_name( aName )
52{
54 m_ordinal = GetParentFootprint()->GetNextFieldOrdinal();
55
56 PCB_TEXT::operator=( aText );
57}
58
59
61 PCB_TEXT( aField.GetParent(), PCB_FIELD_T )
62{
63 *this = aField;
64
65 // Assignment keeps the new KIID, and a copy must not churn the uuid written to the file
66 SetUuidDirect( aField.m_Uuid );
67}
68
69
70void PCB_FIELD::Serialize( kiapi::board::types::Field& field ) const
71{
72 PCB_TEXT::Serialize( *field.mutable_text() );
73
74 field.set_name( GetUntranslatedName().ToStdString() );
75 field.mutable_id()->set_id( (int) GetId() );
76 field.set_visible( IsVisible() );
77 kiapi::common::PackCustomProperties( field.mutable_custom_properties(), *this );
78}
79
80
81void PCB_FIELD::Serialize( google::protobuf::Any &aContainer ) const
82{
83 kiapi::board::types::Field field;
84 Serialize( field );
85
86 if( !aContainer.PackFrom( field ) )
87 wxFAIL_MSG( wxT( "Failed to pack PCB_FIELD" ) );
88}
89
90
91bool PCB_FIELD::Deserialize( const kiapi::board::types::Field& field )
92{
93 if( field.has_id() )
94 setId( (FIELD_T) field.id().id() );
95
96 // Mandatory fields have a blank Name in the KiCad object
97 if( !IsMandatory() )
98 SetName( wxString( field.name().c_str(), wxConvUTF8 ) );
99
100 if( field.has_text() )
101 {
102 PCB_TEXT::Deserialize( field.text() );
103 }
104
105 SetVisible( field.visible() );
106
107 if( field.text().layer() == kiapi::board::types::BoardLayer::BL_UNKNOWN )
108 SetLayer( F_SilkS );
109
110 kiapi::common::UnpackCustomProperties( field.custom_properties(), *this );
111
112 return true;
113}
114
115
116bool PCB_FIELD::Deserialize( const google::protobuf::Any &aContainer )
117{
118 kiapi::board::types::Field field;
119
120 if( !aContainer.UnpackTo( &field ) )
121 return false;
122
123 return Deserialize( field );
124}
125
126
127wxString PCB_FIELD::GetName( bool aUseDefaultName ) const
128{
129 if( IsMandatory() )
131 else if( m_name.IsEmpty() && aUseDefaultName )
133 else
134 return m_name;
135}
136
137
139{
140 return GetName( true );
141}
142
143
144wxString PCB_FIELD::GetShownText( RESOLUTION_CONTEXT aContext, int aDepth ) const
145{
146 const BOARD* board = GetBoard();
147 return GetShownText( board ? board->GetCurrentVariant() : wxString(), aContext, aDepth );
148}
149
150
151wxString PCB_FIELD::GetShownText( const wxString& aVariantName, RESOLUTION_CONTEXT aContext,
152 int aDepth ) const
153{
154 const FOOTPRINT* parentFootprint = GetParentFootprint();
155 const BOARD* board = GetBoard();
156 wxString text;
157 bool hasVariantOverride = false;
158 bool hasTextVars = HasTextVars();
159
160 if( parentFootprint && board )
161 {
162 if( !aVariantName.IsEmpty() && aVariantName.CmpNoCase( GetDefaultVariantName() ) != 0 )
163 {
164 if( const FOOTPRINT_VARIANT* variant = parentFootprint->GetVariant( aVariantName ) )
165 {
166 if( variant->HasFieldValue( GetName() ) )
167 {
168 text = parentFootprint->GetFieldValueForVariant( aVariantName, GetName() );
169
170 // Variable references in variant values are considered to be in schematic scope,
171 // and are thus resolved before the footprint gets them.
172 hasTextVars = false;
173
174 hasVariantOverride = true;
175 }
176 }
177 }
178 }
179
180 if( !hasVariantOverride )
181 text = GetText();
182
184
185 std::function<bool( wxString* )> resolver =
186 [&]( wxString* token ) -> bool
187 {
188 if( token->IsSameAs( wxT( "LAYER" ) ) )
189 {
190 *token = GetLayerName();
191 return true;
192 }
193
194 if( parentFootprint && parentFootprint->ResolveTextVar( token, aVariantName, aDepth + 1 ) )
195 return true;
196
197 if( board && board->ResolveTextVar( token, aVariantName, aDepth + 1 ) )
198 return true;
199
200 return false;
201 };
202
203 if( hasTextVars && aContext != RAW_VALUE )
204 {
205 text = ResolveTextVars( text, &resolver, aDepth );
206 FinalizeTextVarExpansion( text, aContext );
207 }
208
209 return text;
210}
211
212
214{
215 return m_id == FIELD_T::REFERENCE
216 || m_id == FIELD_T::VALUE
219}
220
221
223{
224 return IsURL( GetShownText( FOR_GUI ) );
225}
226
227
229{
230 if( IsMandatory() )
232 else
233 return _( "User Field" );
234}
235
236
237bool PCB_FIELD::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
238{
239 if( !IsVisible() && !aSearchData.searchAllFields )
240 return false;
241
242 return PCB_TEXT::Matches( aSearchData, aAuxData );
243}
244
245
246wxString PCB_FIELD::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
247{
248 wxString content = aFull ? GetShownText( FOR_GUI ) : KIUI::EllipsizeMenuText( GetText() );
249 wxString ref = GetParentFootprint()->GetReference();
250
251 switch( m_id )
252 {
254 return wxString::Format( _( "Reference field of %s" ), ref );
255
256 case FIELD_T::VALUE:
257 return wxString::Format( _( "Value field of %s (%s)" ), ref, content );
258
260 return wxString::Format( _( "Footprint field of %s (%s)" ), ref, content );
261
263 return wxString::Format( _( "Datasheet field of %s (%s)" ), ref, content );
264
265 default:
266 if( GetName().IsEmpty() )
267 return wxString::Format( _( "Field of %s (%s)" ), ref, content );
268 else
269 return wxString::Format( _( "%s field of %s (%s)" ), GetName(), ref, content );
270 }
271}
272
273
274double PCB_FIELD::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
275{
276 if( !aView )
277 return LOD_SHOW;
278
279 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
280 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
281
283 && renderSettings->m_ForceShowFieldsWhenFPSelected )
284 {
285 return LOD_SHOW;
286 }
287
288 // Handle Render tab switches
289 if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
290 return LOD_HIDE;
291
293 return LOD_HIDE;
294
295 return PCB_TEXT::ViewGetLOD( aLayer, aView );
296}
297
298
300{
301 return new PCB_FIELD( *this );
302}
303
304
305void PCB_FIELD::CopyFrom( const BOARD_ITEM* aOther )
306{
307 wxCHECK( aOther && aOther->Type() == PCB_FIELD_T, /* void */ );
308 *this = *static_cast<const PCB_FIELD*>( aOther );
309}
310
311
313{
314 assert( aImage->Type() == PCB_FIELD_T );
315
316 std::swap( *((PCB_FIELD*) this), *((PCB_FIELD*) aImage) );
317}
318
319
320bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
321{
322 if( aOther.Type() != Type() )
323 return false;
324
325 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
326
327 return *this == other;
328}
329
330
331bool PCB_FIELD::operator==( const PCB_FIELD& aOther ) const
332{
333 if( IsMandatory() != aOther.IsMandatory() )
334 return false;
335
336 if( IsMandatory() )
337 {
338 if( m_id != aOther.m_id )
339 return false;
340 }
341 else
342 {
343 if( m_ordinal != aOther.m_ordinal )
344 return false;
345 }
346
347 return m_name == aOther.m_name && EDA_TEXT::operator==( aOther );
348}
349
350
351double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
352{
353 if( m_Uuid == aOther.m_Uuid )
354 return 1.0;
355
356 if( aOther.Type() != Type() )
357 return 0.0;
358
359 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
360
361 if( IsMandatory() || other.IsMandatory() )
362 {
363 if( m_id == other.m_id )
364 return 1.0;
365 else
366 return 0.0;
367 }
368
369 if( m_name == other.m_name )
370 return 1.0;
371
372 return EDA_TEXT::Similarity( other );
373}
374
375static struct PCB_FIELD_DESC
376{
378 {
387
388 propMgr.AddProperty( new PROPERTY<PCB_FIELD, wxString>( _HKI( "Name" ),
392
393 // These properties, inherited from EDA_TEXT, have no sense for the board editor
394 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
395 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );
396 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:101
void SetUuidDirect(const KIID &aUuid)
Raw UUID assignment.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:397
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:281
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:411
bool ResolveTextVar(wxString *token, int aDepth) const
Definition board.cpp:708
wxString GetCurrentVariant() const
Definition board.h:530
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
bool IsSelected() const
Definition eda_item.h:134
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:84
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:94
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:118
virtual bool IsVisible() const
Definition eda_text.h:226
bool HasTextVars() const
Indicates the ShownText has text var references which need to be processed.
Definition eda_text.h:139
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:348
double Similarity(const EDA_TEXT &aOther) const
bool operator==(const EDA_TEXT &aRhs) const
Definition eda_text.h:444
Variant information for a footprint.
Definition footprint.h:178
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.
bool ResolveTextVar(wxString *aToken, int aDepth=0) const
Resolve any references to system tokens supported by the component.
wxString GetFieldValueForVariant(const wxString &aVariantName, const wxString &aFieldName) const
Get a field value for a specific variant.
const wxString & GetReference() const
Definition footprint.h:793
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:94
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition view_item.h:176
static constexpr double LOD_SHOW
Return this constant from ViewGetLOD() to show the item unconditionally.
Definition view_item.h:181
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:68
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:457
PAINTER * GetPainter() const
Return the painter object used by the view for drawing VIEW_ITEM objects.
Definition view.h:242
bool IsMandatory() const
FIELD_T GetId() const
Definition pcb_field.h:123
bool operator==(const PCB_FIELD &aOther) const
wxString GetShownText(RESOLUTION_CONTEXT aContext, int aDepth=0) const override
Return the string actually shown after processing of the base text.
PCB_FIELD(FOOTPRINT *aParent, FIELD_T aFieldId, const wxString &aName=wxEmptyString)
Definition pcb_field.cpp:36
void setId(FIELD_T aId)
Definition pcb_field.h:144
wxString GetTextTypeDescription() const override
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
void CopyFrom(const BOARD_ITEM *aOther) override
bool IsReference() const
Definition pcb_field.h:76
bool HasHypertext() const
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
wxString GetUntranslatedName() const
Get the untranslated field name for storage, variable look-up, etc.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition pcb_field.cpp:81
bool IsValue() const
Definition pcb_field.h:77
wxString m_name
Definition pcb_field.h:149
FIELD_T m_id
Field id,.
Definition pcb_field.h:147
int m_ordinal
Sort order for non-mandatory fields.
Definition pcb_field.h:148
void SetName(const wxString &aName)
Definition pcb_field.h:121
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.
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:216
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition pcb_text.cpp:139
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition pcb_text.cpp:259
PCB_TEXT(BOARD_ITEM *parent, KICAD_T idtype=PCB_TEXT_T)
Definition pcb_text.cpp:49
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition pcb_text.cpp:169
PCB_TEXT & operator=(const PCB_TEXT &aOther)
Definition pcb_text.cpp:88
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition property.h:354
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
Definition property.h:368
Provide class metadata.
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.
void FinalizeTextVarExpansion(wxString &aText, RESOLUTION_CONTEXT aContext)
Definition common.cpp:88
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:333
RESOLUTION_CONTEXT
Definition common.h:91
@ FOR_GUI
Definition common.h:93
@ RAW_VALUE
Definition common.h:98
static std::string ToStdString(const wxString &aStr)
#define _(s)
Some functions to handle hotkeys in KiCad.
@ LAYER_FP_REFERENCES
Show footprints references (when texts are visible).
Definition layer_ids.h:262
@ LAYER_FP_VALUES
Show footprints values (when texts are visible).
Definition layer_ids.h:259
@ F_SilkS
Definition layer_ids.h:96
KICOMMON_API wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
KICOMMON_API void PackCustomProperties(google::protobuf::RepeatedPtrField< types::CustomProperty > *aOutput, const EDA_ITEM &aItem)
KICOMMON_API void UnpackCustomProperties(const google::protobuf::RepeatedPtrField< types::CustomProperty > &aInput, EDA_ITEM &aItem)
#define _HKI(x)
Definition page_info.cpp:40
static struct PCB_FIELD_DESC _PCB_FIELD_DESC
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
Definition property.h:74
#define NO_SETTER(owner, type)
Macro to define read-only fields (no setter method available)
Definition property.h:923
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
wxString GetDefaultVariantName()
wxString UnescapeString(const wxString &aSource)
bool IsURL(wxString aStr)
Performs a URL sniff-test on a string.
wxString GetUserFieldName(int aFieldNdx, TRANSLATION aTranslation)
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
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".
@ UNTRANSLATED
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:82