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, bool aFull ) const
143{
144 wxString content = aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() );
145 wxString ref = GetParentFootprint()->GetReference();
146
147 switch( m_id )
148 {
149 case REFERENCE_FIELD:
150 return wxString::Format( _( "Reference field of %s" ), ref );
151
152 case VALUE_FIELD:
153 return wxString::Format( _( "Value field of %s (%s)" ), ref, content );
154
155 case FOOTPRINT_FIELD:
156 return wxString::Format( _( "Footprint field of %s (%s)" ), ref, content );
157
158 case DATASHEET_FIELD:
159 return wxString::Format( _( "Datasheet field of %s (%s)" ), ref, content );
160
161 default:
162 if( GetName().IsEmpty() )
163 return wxString::Format( _( "Field of %s (%s)" ), ref, content );
164 else
165 return wxString::Format( _( "%s field of %s (%s)" ), GetName(), ref, content );
166 }
167}
168
169
170double PCB_FIELD::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
171{
172 constexpr double HIDE = std::numeric_limits<double>::max();
173
174 if( !aView )
175 return 0.0;
176
177 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
178 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
179
181 && renderSettings->m_ForceShowFieldsWhenFPSelected )
182 {
183 return 0.0;
184 }
185
186 // Handle Render tab switches
187 if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
188 return HIDE;
189
191 return HIDE;
192
193 return PCB_TEXT::ViewGetLOD( aLayer, aView );
194}
195
196
198{
199 return new PCB_FIELD( *this );
200}
201
202
204{
205 assert( aImage->Type() == PCB_FIELD_T );
206
207 std::swap( *((PCB_FIELD*) this), *((PCB_FIELD*) aImage) );
208}
209
210
211bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
212{
213 if( aOther.Type() != Type() )
214 return false;
215
216 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
217
218 return *this == other;
219}
220
221
222bool PCB_FIELD::operator==( const PCB_FIELD& aOther ) const
223{
224 return m_id == aOther.m_id && m_name == aOther.m_name && EDA_TEXT::operator==( aOther );
225}
226
227
228double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
229{
230 if( m_Uuid == aOther.m_Uuid )
231 return 1.0;
232
233 if( aOther.Type() != Type() )
234 return 0.0;
235
236 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
237
238 if( m_id < MANDATORY_FIELDS || other.m_id < MANDATORY_FIELDS )
239 {
240 if( m_id == other.m_id )
241 return 1.0;
242 else
243 return 0.0;
244 }
245
246 if( m_name == other.m_name )
247 return 1.0;
248
249 return EDA_TEXT::Similarity( other );
250}
251
252static struct PCB_FIELD_DESC
253{
255 {
264
265 // These properties, inherited from EDA_TEXT, have no sense for the board editor
266 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
267 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );
268 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:288
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:298
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
const KIID m_Uuid
Definition: eda_item.h:489
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
bool IsSelected() const
Definition: eda_item.h:110
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:500
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
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1278
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:383
const wxString & GetReference() const
Definition: footprint.h:622
Contains methods for drawing PCB-specific items.
Definition: pcb_painter.h:180
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:185
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:419
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:217
void setId(int aId)
Definition: pcb_field.h:119
bool operator==(const PCB_FIELD &aOther) const
Definition: pcb_field.cpp:222
wxString GetTextTypeDescription() const override
Definition: pcb_field.cpp:133
bool IsReference() const
Definition: pcb_field.h:68
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:69
wxString m_name
Definition: pcb_field.h:123
int m_id
Field index,.
Definition: pcb_field.h:121
int GetId() const
Definition: pcb_field.h:107
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:105
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_field.cpp:170
void swapData(BOARD_ITEM *aImage) override
Definition: pcb_field.cpp:203
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:228
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:197
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
Definition: pcb_field.cpp:142
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: pcb_text.cpp:84
wxString GetShownText(bool aAllowExtraText, int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: pcb_text.cpp:133
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_text.cpp:212
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: pcb_text.cpp:108
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.
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()
Definition: property_mgr.h:87
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:212
@ LAYER_FP_VALUES
show footprints values (when texts are visible)
Definition: layer_ids.h:211
@ 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.
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