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 (C) 1992-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
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
32
33PCB_FIELD::PCB_FIELD( FOOTPRINT* aParent, int aFieldId, const wxString& aName ) :
34 PCB_TEXT( aParent, PCB_FIELD_T ),
35 m_id( aFieldId ),
36 m_name( aName )
37{
38}
39
40
41PCB_FIELD::PCB_FIELD( const PCB_TEXT& aText, int aFieldId, const wxString& aName ) :
42 PCB_TEXT( aText ),
43 m_id( aFieldId ),
44 m_name( aName )
45{
46}
47
48
49void PCB_FIELD::Serialize( google::protobuf::Any &aContainer ) const
50{
51 kiapi::board::types::Field field;
52
53 google::protobuf::Any anyText;
54 PCB_TEXT::Serialize( anyText );
55 anyText.UnpackTo( field.mutable_text() );
56
57 field.set_name( GetCanonicalName().ToStdString() );
58 field.mutable_id()->set_id( GetId() );
59
60 aContainer.PackFrom( field );
61}
62
63
64bool PCB_FIELD::Deserialize( const google::protobuf::Any &aContainer )
65{
66 kiapi::board::types::Field field;
67
68 if( !aContainer.UnpackTo( &field ) )
69 return false;
70
71 if( field.has_id() )
72 setId( field.id().id() );
73
74 // Mandatory fields have a blank Name in the KiCad object
75 if( m_id >= MANDATORY_FIELDS )
76 SetName( wxString( field.name().c_str(), wxConvUTF8 ) );
77
78 if( field.has_text() )
79 {
80 google::protobuf::Any anyText;
81 anyText.PackFrom( field.text() );
82 PCB_TEXT::Deserialize( anyText );
83 }
84
85 if( field.text().layer() == kiapi::board::types::BoardLayer::BL_UNKNOWN )
87
88 return true;
89}
90
91
92wxString PCB_FIELD::GetName( bool aUseDefaultName ) const
93{
95 {
96 if( m_id >= 0 && m_id < MANDATORY_FIELDS )
98 else if( m_name.IsEmpty() && aUseDefaultName )
100 else
101 return m_name;
102 }
103 else
104 {
105 wxFAIL_MSG( "Unhandled field owner type." );
106 return m_name;
107 }
108}
109
110
112{
114 {
115 if( m_id < MANDATORY_FIELDS )
116 return GetCanonicalFieldName( m_id );
117 else
118 return m_name;
119 }
120 else
121 {
122 if( m_parent )
123 {
124 wxFAIL_MSG( wxString::Format( "Unhandled field owner type (id %d, parent type %d).",
125 m_id, m_parent->Type() ) );
126 }
127
128 return m_name;
129 }
130}
131
132
134{
135 if( m_id < MANDATORY_FIELDS )
136 return GetCanonicalFieldName( m_id );
137 else
138 return _( "User Field" );
139}
140
141
142wxString PCB_FIELD::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
143{
144 switch( m_id )
145 {
146 case REFERENCE_FIELD:
147 return wxString::Format( _( "Reference '%s'" ),
148 GetParentFootprint()->GetReference() );
149
150 case VALUE_FIELD:
151 return wxString::Format( _( "Value '%s' of %s" ),
153 GetParentFootprint()->GetReference() );
154
155 case FOOTPRINT_FIELD:
156 return wxString::Format( _( "Footprint '%s' of %s" ),
158 GetParentFootprint()->GetReference() );
159 case DATASHEET_FIELD:
160 return wxString::Format( _( "Datasheet '%s' of %s" ),
162 GetParentFootprint()->GetReference() );
163
164 default:
165 break; // avoid unreachable code / missing return statement warnings
166 }
167
168 return wxString::Format( _( "Field '%s' of %s" ), KIUI::EllipsizeMenuText( GetText() ),
169 GetParentFootprint()->GetReference() );
170}
171
172
173double PCB_FIELD::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
174{
175 constexpr double HIDE = std::numeric_limits<double>::max();
176
177 if( !aView )
178 return 0.0;
179
180 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
181 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
182
184 && renderSettings->m_ForceShowFieldsWhenFPSelected )
185 {
186 return 0.0;
187 }
188
189 // Handle Render tab switches
190 if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
191 return HIDE;
192
194 return HIDE;
195
196 return PCB_TEXT::ViewGetLOD( aLayer, aView );
197}
198
199
201{
202 return new PCB_FIELD( *this );
203}
204
205
207{
208 assert( aImage->Type() == PCB_FIELD_T );
209
210 std::swap( *((PCB_FIELD*) this), *((PCB_FIELD*) aImage) );
211}
212
213
214bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
215{
216 if( aOther.Type() != Type() )
217 return false;
218
219 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
220
221 return *this == other;
222}
223
224
225bool PCB_FIELD::operator==( const PCB_FIELD& aOther ) const
226{
227 return m_id == aOther.m_id && m_name == aOther.m_name && EDA_TEXT::operator==( aOther );
228}
229
230
231double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
232{
233 if( m_Uuid == aOther.m_Uuid )
234 return 1.0;
235
236 if( aOther.Type() != Type() )
237 return 0.0;
238
239 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
240
241 if( m_id < MANDATORY_FIELDS || other.m_id < MANDATORY_FIELDS )
242 {
243 if( m_id == other.m_id )
244 return 1.0;
245 else
246 return 0.0;
247 }
248
249 if( m_name == other.m_name )
250 return 1.0;
251
252 return EDA_TEXT::Similarity( other );
253}
254
255static struct PCB_FIELD_DESC
256{
258 {
267
268 auto isNotFootprintFootprint =
269 []( INSPECTABLE* aItem ) -> bool
270 {
271 if( PCB_FIELD* field = dynamic_cast<PCB_FIELD*>( aItem ) )
272 return !field->IsFootprint();
273
274 return true;
275 };
276
278 isNotFootprintFootprint );
279 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:265
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:248
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
const KIID m_Uuid
Definition: eda_item.h:485
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
bool IsSelected() const
Definition: eda_item.h:109
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:488
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:83
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1141
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:357
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
Contains methods for drawing PCB-specific items.
Definition: pcb_painter.h:165
virtual PCB_RENDER_SETTINGS * GetSettings() override
Return a pointer to current settings that are going to be used when drawing items.
Definition: pcb_painter.h:170
PCB specific render settings.
Definition: pcb_painter.h:78
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:412
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:215
void setId(int aId)
Definition: pcb_field.h:121
bool operator==(const PCB_FIELD &aOther) const
Definition: pcb_field.cpp:225
wxString GetTextTypeDescription() const override
Definition: pcb_field.cpp:133
bool IsReference() const
Definition: pcb_field.h:70
PCB_FIELD(FOOTPRINT *aParent, int aFieldId, const wxString &aName=wxEmptyString)
Definition: pcb_field.cpp:33
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: pcb_field.cpp:92
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: pcb_field.cpp:49
bool IsValue() const
Definition: pcb_field.h:71
wxString m_name
Definition: pcb_field.h:125
int m_id
Field index,.
Definition: pcb_field.h:123
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
Definition: pcb_field.cpp:142
int GetId() const
Definition: pcb_field.h:109
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: pcb_field.cpp:111
void SetName(const wxString &aName)
Definition: pcb_field.h:107
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_field.cpp:173
void swapData(BOARD_ITEM *aImage) override
Definition: pcb_field.cpp:206
double Similarity(const BOARD_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
Definition: pcb_field.cpp:231
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: pcb_field.cpp:64
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_field.cpp:200
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: pcb_text.cpp:86
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_text.cpp:269
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: pcb_text.cpp:131
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
void OverrideAvailability(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName, std::function< bool(INSPECTABLE *)> aFunc)
Sets an override availability functor for a base class property of a given derived class.
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
#define _HKI(x)
#define _(s)
Some functions to handle hotkeys in KiCad.
@ LAYER_FP_REFERENCES
show footprints references (when texts are visible)
Definition: layer_ids.h:215
@ LAYER_FP_VALUES
show footprints values (when texts are visible)
Definition: layer_ids.h:214
@ F_SilkS
Definition: layer_ids.h:104
KICOMMON_API wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:213
static struct PCB_FIELD_DESC _PCB_FIELD_DESC
#define TYPE_HASH(x)
Definition: property.h:71
#define REGISTER_TYPE(x)
Definition: property_mgr.h:371
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslateForHI=false)
Return a default symbol field name for field aFieldNdx for all components.
wxString GetCanonicalFieldName(int idx)
@ DATASHEET_FIELD
name of datasheet
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ MANDATORY_FIELDS
The first 5 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86