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
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( !IsMandatory() )
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( IsMandatory() )
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( IsMandatory() )
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 return m_id == REFERENCE_FIELD
136 || m_id == VALUE_FIELD
139}
140
141
143{
144 if( IsMandatory() )
145 return GetCanonicalFieldName( m_id );
146 else
147 return _( "User Field" );
148}
149
150
151wxString PCB_FIELD::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
152{
153 wxString content = aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() );
154 wxString ref = GetParentFootprint()->GetReference();
155
156 switch( m_id )
157 {
158 case REFERENCE_FIELD:
159 return wxString::Format( _( "Reference field of %s" ), ref );
160
161 case VALUE_FIELD:
162 return wxString::Format( _( "Value field of %s (%s)" ), ref, content );
163
164 case FOOTPRINT_FIELD:
165 return wxString::Format( _( "Footprint field of %s (%s)" ), ref, content );
166
167 case DATASHEET_FIELD:
168 return wxString::Format( _( "Datasheet field of %s (%s)" ), ref, content );
169
170 default:
171 if( GetName().IsEmpty() )
172 return wxString::Format( _( "Field of %s (%s)" ), ref, content );
173 else
174 return wxString::Format( _( "%s field of %s (%s)" ), GetName(), ref, content );
175 }
176}
177
178
179double PCB_FIELD::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
180{
181 if( !aView )
182 return LOD_SHOW;
183
184 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
185 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
186
188 && renderSettings->m_ForceShowFieldsWhenFPSelected )
189 {
190 return LOD_SHOW;
191 }
192
193 // Handle Render tab switches
194 if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
195 return LOD_HIDE;
196
198 return LOD_HIDE;
199
200 return PCB_TEXT::ViewGetLOD( aLayer, aView );
201}
202
203
205{
206 return new PCB_FIELD( *this );
207}
208
209
211{
212 assert( aImage->Type() == PCB_FIELD_T );
213
214 std::swap( *((PCB_FIELD*) this), *((PCB_FIELD*) aImage) );
215}
216
217
218bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
219{
220 if( aOther.Type() != Type() )
221 return false;
222
223 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
224
225 return *this == other;
226}
227
228
229bool PCB_FIELD::operator==( const PCB_FIELD& aOther ) const
230{
231 return m_id == aOther.m_id && m_name == aOther.m_name && EDA_TEXT::operator==( aOther );
232}
233
234
235double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
236{
237 if( m_Uuid == aOther.m_Uuid )
238 return 1.0;
239
240 if( aOther.Type() != Type() )
241 return 0.0;
242
243 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
244
245 if( IsMandatory() || other.IsMandatory() )
246 {
247 if( m_id == other.m_id )
248 return 1.0;
249 else
250 return 0.0;
251 }
252
253 if( m_name == other.m_name )
254 return 1.0;
255
256 return EDA_TEXT::Similarity( other );
257}
258
259static struct PCB_FIELD_DESC
260{
262 {
271
272 // These properties, inherited from EDA_TEXT, have no sense for the board editor
273 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
274 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );
275 }
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:290
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:490
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:502
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:1283
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:382
const wxString & GetReference() const
Definition: footprint.h:620
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
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition: view_item.h:174
static constexpr double LOD_SHOW
Return this constant from ViewGetLOD() to show the item unconditionally.
Definition: view_item.h:179
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:418
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:216
bool IsMandatory() const
Definition: pcb_field.cpp:133
void setId(int aId)
Definition: pcb_field.h:119
bool operator==(const PCB_FIELD &aOther) const
Definition: pcb_field.cpp:229
wxString GetTextTypeDescription() const override
Definition: pcb_field.cpp:142
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
void swapData(BOARD_ITEM *aImage) override
Definition: pcb_field.cpp:210
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:235
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:204
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_field.cpp:179
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
Definition: pcb_field.cpp:151
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, const 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:264
@ LAYER_FP_VALUES
Show footprints values (when texts are visible).
Definition: layer_ids.h:261
@ 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:214
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
wxString GetUserFieldName(int aFieldNdx, bool aTranslateForHI)
#define DO_TRANSLATE
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".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ DESCRIPTION_FIELD
Field Description of part, i.e. "1/4W 1% Metal Film Resistor".
@ 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