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.GetParent(), PCB_FIELD_T ),
47 m_id( aFieldId ),
48 m_ordinal( static_cast<int>( aFieldId ) ),
49 m_name( aName )
50{
51 // Copy the text properties from the PCB_TEXT
52 SetText( aText.GetText() );
53 SetVisible( aText.IsVisible() );
54 SetLayer( aText.GetLayer() );
55 SetPosition( aText.GetPosition() );
57}
58
59
60void PCB_FIELD::Serialize( google::protobuf::Any &aContainer ) const
61{
62 kiapi::board::types::Field field;
63
64 google::protobuf::Any anyText;
65 PCB_TEXT::Serialize( anyText );
66 anyText.UnpackTo( field.mutable_text() );
67
68 field.set_name( GetCanonicalName().ToStdString() );
69 field.mutable_id()->set_id( (int) GetId() );
70 field.set_visible( IsVisible() );
71
72 aContainer.PackFrom( field );
73}
74
75
76bool PCB_FIELD::Deserialize( const google::protobuf::Any &aContainer )
77{
78 kiapi::board::types::Field field;
79
80 if( !aContainer.UnpackTo( &field ) )
81 return false;
82
83 if( field.has_id() )
84 setId( (FIELD_T) field.id().id() );
85
86 // Mandatory fields have a blank Name in the KiCad object
87 if( !IsMandatory() )
88 SetName( wxString( field.name().c_str(), wxConvUTF8 ) );
89
90 if( field.has_text() )
91 {
92 google::protobuf::Any anyText;
93 anyText.PackFrom( field.text() );
94 PCB_TEXT::Deserialize( anyText );
95 }
96
97 SetVisible( field.visible() );
98
99 if( field.text().layer() == kiapi::board::types::BoardLayer::BL_UNKNOWN )
100 SetLayer( F_SilkS );
101
102 return true;
103}
104
105
106wxString PCB_FIELD::GetName( bool aUseDefaultName ) const
107{
108 if( IsMandatory() )
109 return GetCanonicalFieldName( m_id );
110 else if( m_name.IsEmpty() && aUseDefaultName )
112 else
113 return m_name;
114}
115
116
118{
119 return GetName( true );
120}
121
122
124{
125 return m_id == FIELD_T::REFERENCE
126 || m_id == FIELD_T::VALUE
127 || m_id == FIELD_T::DATASHEET
128 || m_id == FIELD_T::DESCRIPTION;
129}
130
131
133{
134 return IsURL( GetShownText( false ) );
135}
136
137
139{
140 if( IsMandatory() )
141 return GetCanonicalFieldName( m_id );
142 else
143 return _( "User Field" );
144}
145
146
147bool PCB_FIELD::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
148{
149 if( !IsVisible() && !aSearchData.searchAllFields )
150 return false;
151
152 return PCB_TEXT::Matches( aSearchData, aAuxData );
153}
154
155
156wxString PCB_FIELD::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
157{
158 wxString content = aFull ? GetShownText( false ) : KIUI::EllipsizeMenuText( GetText() );
159 wxString ref = GetParentFootprint()->GetReference();
160
161 switch( m_id )
162 {
163 case FIELD_T::REFERENCE:
164 return wxString::Format( _( "Reference field of %s" ), ref );
165
166 case FIELD_T::VALUE:
167 return wxString::Format( _( "Value field of %s (%s)" ), ref, content );
168
169 case FIELD_T::FOOTPRINT:
170 return wxString::Format( _( "Footprint field of %s (%s)" ), ref, content );
171
172 case FIELD_T::DATASHEET:
173 return wxString::Format( _( "Datasheet field of %s (%s)" ), ref, content );
174
175 default:
176 if( GetName().IsEmpty() )
177 return wxString::Format( _( "Field of %s (%s)" ), ref, content );
178 else
179 return wxString::Format( _( "%s field of %s (%s)" ), GetName(), ref, content );
180 }
181}
182
183
184double PCB_FIELD::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
185{
186 if( !aView )
187 return LOD_SHOW;
188
189 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
190 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
191
193 && renderSettings->m_ForceShowFieldsWhenFPSelected )
194 {
195 return LOD_SHOW;
196 }
197
198 // Handle Render tab switches
199 if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
200 return LOD_HIDE;
201
203 return LOD_HIDE;
204
205 return PCB_TEXT::ViewGetLOD( aLayer, aView );
206}
207
208
210{
211 return new PCB_FIELD( *this );
212}
213
214
216{
217 assert( aImage->Type() == PCB_FIELD_T );
218
219 std::swap( *((PCB_FIELD*) this), *((PCB_FIELD*) aImage) );
220}
221
222
223bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
224{
225 if( aOther.Type() != Type() )
226 return false;
227
228 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
229
230 return *this == other;
231}
232
233
234bool PCB_FIELD::operator==( const PCB_FIELD& aOther ) const
235{
236 if( IsMandatory() != aOther.IsMandatory() )
237 return false;
238
239 if( IsMandatory() )
240 {
241 if( m_id != aOther.m_id )
242 return false;
243 }
244 else
245 {
246 if( m_ordinal != aOther.m_ordinal )
247 return false;
248 }
249
250 return m_name == aOther.m_name && EDA_TEXT::operator==( aOther );
251}
252
253
254double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
255{
256 if( m_Uuid == aOther.m_Uuid )
257 return 1.0;
258
259 if( aOther.Type() != Type() )
260 return 0.0;
261
262 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
263
264 if( IsMandatory() || other.IsMandatory() )
265 {
266 if( m_id == other.m_id )
267 return 1.0;
268 else
269 return 0.0;
270 }
271
272 if( m_name == other.m_name )
273 return 1.0;
274
275 return EDA_TEXT::Similarity( other );
276}
277
278static struct PCB_FIELD_DESC
279{
281 {
290
291 propMgr.AddProperty( new PROPERTY<PCB_FIELD, wxString>( _HKI( "Name" ),
295
296 // These properties, inherited from EDA_TEXT, have no sense for the board editor
297 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
298 propMgr.Mask( TYPE_HASH( PCB_FIELD ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );
299 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:232
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:283
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:97
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:97
const KIID m_Uuid
Definition: eda_item.h:502
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:109
bool IsSelected() const
Definition: eda_item.h:126
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:79
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:97
virtual bool IsVisible() const
Definition: eda_text.h:184
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:433
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:386
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1280
const TEXT_ATTRIBUTES & GetAttributes() const
Definition: eda_text.h:228
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:270
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:393
int GetNextFieldOrdinal() const
Return the next ordinal for a user field for this footprint.
Definition: footprint.cpp:657
const wxString & GetReference() const
Definition: footprint.h:625
Contains methods for drawing PCB-specific items.
Definition: pcb_painter.h:182
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:187
PCB specific render settings.
Definition: pcb_painter.h:80
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition: view_item.h:180
static constexpr double LOD_SHOW
Return this constant from ViewGetLOD() to show the item unconditionally.
Definition: view_item.h:185
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:123
FIELD_T GetId() const
Definition: pcb_field.h:110
bool operator==(const PCB_FIELD &aOther) const
Definition: pcb_field.cpp:234
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:131
wxString GetTextTypeDescription() const override
Definition: pcb_field.cpp:138
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: pcb_field.cpp:147
bool IsHypertext() const
Definition: pcb_field.cpp:132
bool IsReference() const
Definition: pcb_field.h:68
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: pcb_field.cpp:106
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: pcb_field.cpp:60
bool IsValue() const
Definition: pcb_field.h:69
wxString m_name
Definition: pcb_field.h:136
FIELD_T m_id
Field id,.
Definition: pcb_field.h:134
int m_ordinal
Sort order for non-mandatory fields.
Definition: pcb_field.h:135
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:117
void SetName(const wxString &aName)
Definition: pcb_field.h:108
void swapData(BOARD_ITEM *aImage) override
Definition: pcb_field.cpp:215
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:254
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: pcb_field.cpp:76
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_field.cpp:209
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_field.cpp:184
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
Definition: pcb_field.cpp:156
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: pcb_text.cpp:174
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: pcb_text.cpp:89
wxString GetShownText(bool aAllowExtraText, int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: pcb_text.cpp:138
double ViewGetLOD(int aLayer, const KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_text.cpp:217
virtual VECTOR2I GetPosition() const override
Definition: pcb_text.h:84
virtual void SetPosition(const VECTOR2I &aPos) override
Definition: pcb_text.h:89
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: pcb_text.cpp:113
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
Definition: property.h:301
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
Definition: property.h:315
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:221
static struct PCB_FIELD_DESC _PCB_FIELD_DESC
#define TYPE_HASH(x)
Definition: property.h:72
#define NO_SETTER(owner, type)
Definition: property.h:792
#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