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
33
34PCB_FIELD::PCB_FIELD( FOOTPRINT* aParent, FIELD_T aFieldId, const wxString& aName ) :
35 PCB_TEXT( aParent, PCB_FIELD_T ),
36 m_id( aFieldId ),
37 m_ordinal( 0 ),
38 m_name( aName )
39{
40 if( m_id == FIELD_T::USER )
41 m_ordinal = aParent->GetNextFieldOrdinal();
42}
43
44
45PCB_FIELD::PCB_FIELD( const PCB_TEXT& aText, FIELD_T aFieldId, const wxString& aName ) :
46 PCB_TEXT( aText ),
47 m_id( aFieldId ),
48 m_ordinal( static_cast<int>( aFieldId ) ),
49 m_name( aName )
50{
51}
52
53
54void PCB_FIELD::Serialize( google::protobuf::Any &aContainer ) const
55{
56 kiapi::board::types::Field field;
57
58 google::protobuf::Any anyText;
59 PCB_TEXT::Serialize( anyText );
60 anyText.UnpackTo( field.mutable_text() );
61
62 field.set_name( GetCanonicalName().ToStdString() );
63 field.mutable_id()->set_id( (int) GetId() );
64 field.set_visible( IsVisible() );
65
66 aContainer.PackFrom( field );
67}
68
69
70bool PCB_FIELD::Deserialize( const google::protobuf::Any &aContainer )
71{
72 kiapi::board::types::Field field;
73
74 if( !aContainer.UnpackTo( &field ) )
75 return false;
76
77 if( field.has_id() )
78 setId( (FIELD_T) field.id().id() );
79
80 // Mandatory fields have a blank Name in the KiCad object
81 if( !IsMandatory() )
82 SetName( wxString( field.name().c_str(), wxConvUTF8 ) );
83
84 if( field.has_text() )
85 {
86 google::protobuf::Any anyText;
87 anyText.PackFrom( field.text() );
88 PCB_TEXT::Deserialize( anyText );
89 }
90
91 SetVisible( field.visible() );
92
93 if( field.text().layer() == kiapi::board::types::BoardLayer::BL_UNKNOWN )
95
96 return true;
97}
98
99
100wxString PCB_FIELD::GetName( bool aUseDefaultName ) const
101{
102 if( IsMandatory() )
103 return GetCanonicalFieldName( m_id );
104 else if( m_name.IsEmpty() && aUseDefaultName )
106 else
107 return m_name;
108}
109
110
112{
113 return GetName( true );
114}
115
116
118{
119 return m_id == FIELD_T::REFERENCE
120 || m_id == FIELD_T::VALUE
121 || m_id == FIELD_T::DATASHEET
122 || m_id == FIELD_T::DESCRIPTION;
123}
124
125
127{
128 return IsURL( GetShownText( false ) );
129}
130
131
133{
134 if( IsMandatory() )
135 return GetCanonicalFieldName( m_id );
136 else
137 return _( "User Field" );
138}
139
140
141wxString PCB_FIELD::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
142{
143 wxString content = aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() );
144 wxString ref = GetParentFootprint()->GetReference();
145
146 switch( m_id )
147 {
148 case FIELD_T::REFERENCE:
149 return wxString::Format( _( "Reference field of %s" ), ref );
150
151 case FIELD_T::VALUE:
152 return wxString::Format( _( "Value field of %s (%s)" ), ref, content );
153
154 case FIELD_T::FOOTPRINT:
155 return wxString::Format( _( "Footprint field of %s (%s)" ), ref, content );
156
157 case FIELD_T::DATASHEET:
158 return wxString::Format( _( "Datasheet field of %s (%s)" ), ref, content );
159
160 default:
161 if( GetName().IsEmpty() )
162 return wxString::Format( _( "Field of %s (%s)" ), ref, content );
163 else
164 return wxString::Format( _( "%s field of %s (%s)" ), GetName(), ref, content );
165 }
166}
167
168
169double PCB_FIELD::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
170{
171 if( !aView )
172 return LOD_SHOW;
173
174 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
175 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
176
178 && renderSettings->m_ForceShowFieldsWhenFPSelected )
179 {
180 return LOD_SHOW;
181 }
182
183 // Handle Render tab switches
184 if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
185 return LOD_HIDE;
186
188 return LOD_HIDE;
189
190 return PCB_TEXT::ViewGetLOD( aLayer, aView );
191}
192
193
195{
196 return new PCB_FIELD( *this );
197}
198
199
201{
202 assert( aImage->Type() == PCB_FIELD_T );
203
204 std::swap( *((PCB_FIELD*) this), *((PCB_FIELD*) aImage) );
205}
206
207
208bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
209{
210 if( aOther.Type() != Type() )
211 return false;
212
213 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
214
215 return *this == other;
216}
217
218
219bool PCB_FIELD::operator==( const PCB_FIELD& aOther ) const
220{
221 if( IsMandatory() != aOther.IsMandatory() )
222 return false;
223
224 if( IsMandatory() )
225 {
226 if( m_id != aOther.m_id )
227 return false;
228 }
229 else
230 {
231 if( m_ordinal != aOther.m_ordinal )
232 return false;
233 }
234
235 return m_name == aOther.m_name && EDA_TEXT::operator==( aOther );
236}
237
238
239double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
240{
241 if( m_Uuid == aOther.m_Uuid )
242 return 1.0;
243
244 if( aOther.Type() != Type() )
245 return 0.0;
246
247 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
248
249 if( IsMandatory() || other.IsMandatory() )
250 {
251 if( m_id == other.m_id )
252 return 1.0;
253 else
254 return 0.0;
255 }
256
257 if( m_name == other.m_name )
258 return 1.0;
259
260 return EDA_TEXT::Similarity( other );
261}
262
263static struct PCB_FIELD_DESC
264{
266 {
275
276 propMgr.AddProperty( new PROPERTY<PCB_FIELD, wxString>( _HKI( "Name" ),
280
281 // These properties, inherited from EDA_TEXT, have no sense for the board editor
282 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
283 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );
284 }
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:488
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
bool IsSelected() const
Definition: eda_item.h:110
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:174
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:379
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1279
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:382
int GetNextFieldOrdinal() const
Return the next ordinal for a user field for this footprint.
Definition: footprint.cpp:648
const wxString & GetReference() const
Definition: footprint.h:621
Contains methods for drawing PCB-specific items.
Definition: pcb_painter.h:181
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:186
PCB specific render settings.
Definition: pcb_painter.h:79
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:117
FIELD_T GetId() const
Definition: pcb_field.h:108
bool operator==(const PCB_FIELD &aOther) const
Definition: pcb_field.cpp:219
PCB_FIELD(FOOTPRINT *aParent, FIELD_T aFieldId, const wxString &aName=wxEmptyString)
Definition: pcb_field.cpp:34
void setId(FIELD_T aId)
Definition: pcb_field.h:129
wxString GetTextTypeDescription() const override
Definition: pcb_field.cpp:132
bool IsHypertext() const
Definition: pcb_field.cpp:126
bool IsReference() const
Definition: pcb_field.h:68
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: pcb_field.cpp:100
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: pcb_field.cpp:54
bool IsValue() const
Definition: pcb_field.h:69
wxString m_name
Definition: pcb_field.h:134
FIELD_T m_id
Field id,.
Definition: pcb_field.h:132
int m_ordinal
Sort order for non-mandatory fields.
Definition: pcb_field.h:133
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:106
void swapData(BOARD_ITEM *aImage) override
Definition: pcb_field.cpp:200
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:239
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: pcb_field.cpp:70
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_field.cpp:194
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_field.cpp:169
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
Definition: pcb_field.cpp:141
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
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition: property.h:300
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
Definition: property.h:314
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
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
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:265
@ LAYER_FP_VALUES
Show footprints values (when texts are visible).
Definition: layer_ids.h:262
@ 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 NO_SETTER(owner, type)
Definition: property.h:791
#define REGISTER_TYPE(x)
Definition: property_mgr.h:371
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...
wxString GetCanonicalFieldName(FIELD_T aFieldType)
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90