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
31PCB_FIELD::PCB_FIELD( FOOTPRINT* aParent, int aFieldId, const wxString& aName ) :
32 PCB_TEXT( aParent, PCB_FIELD_T ),
33 m_id( aFieldId ),
34 m_name( aName )
35{
36}
37
38
39PCB_FIELD::PCB_FIELD( const PCB_TEXT& aText, int aFieldId, const wxString& aName ) :
40 PCB_TEXT( aText ),
41 m_id( aFieldId ),
42 m_name( aName )
43{
44}
45
46
47wxString PCB_FIELD::GetName( bool aUseDefaultName ) const
48{
50 {
51 if( m_id >= 0 && m_id < MANDATORY_FIELDS )
53 else if( m_name.IsEmpty() && aUseDefaultName )
55 else
56 return m_name;
57 }
58 else
59 {
60 wxFAIL_MSG( "Unhandled field owner type." );
61 return m_name;
62 }
63}
64
65
67{
69 {
72 else
73 return m_name;
74 }
75 else
76 {
77 if( m_parent )
78 {
79 wxFAIL_MSG( wxString::Format( "Unhandled field owner type (id %d, parent type %d).",
80 m_id, m_parent->Type() ) );
81 }
82
83 return m_name;
84 }
85}
86
87
89{
92 else
93 return _( "User Field" );
94}
95
96
97wxString PCB_FIELD::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
98{
99 switch( m_id )
100 {
101 case REFERENCE_FIELD:
102 return wxString::Format( _( "Reference '%s'" ),
103 GetParentFootprint()->GetReference() );
104
105 case VALUE_FIELD:
106 return wxString::Format( _( "Value '%s' of %s" ),
108 GetParentFootprint()->GetReference() );
109
110 case FOOTPRINT_FIELD:
111 return wxString::Format( _( "Footprint '%s' of %s" ),
113 GetParentFootprint()->GetReference() );
114 case DATASHEET_FIELD:
115 return wxString::Format( _( "Datasheet '%s' of %s" ),
117 GetParentFootprint()->GetReference() );
118
119 default:
120 break; // avoid unreachable code / missing return statement warnings
121 }
122
123 return wxString::Format( _( "Field '%s' of %s" ), KIUI::EllipsizeMenuText( GetText() ),
124 GetParentFootprint()->GetReference() );
125}
126
127
128double PCB_FIELD::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
129{
130 constexpr double HIDE = std::numeric_limits<double>::max();
131
132 if( !aView )
133 return 0.0;
134
135 KIGFX::PCB_PAINTER* painter = static_cast<KIGFX::PCB_PAINTER*>( aView->GetPainter() );
136 KIGFX::PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
137
139 && renderSettings->m_ForceShowFieldsWhenFPSelected )
140 {
141 return 0.0;
142 }
143
144 // Handle Render tab switches
145 if( IsValue() && !aView->IsLayerVisible( LAYER_FP_VALUES ) )
146 return HIDE;
147
149 return HIDE;
150
151 return PCB_TEXT::ViewGetLOD( aLayer, aView );
152}
153
154
156{
157 return new PCB_FIELD( *this );
158}
159
160
161bool PCB_FIELD::operator==( const BOARD_ITEM& aOther ) const
162{
163 if( aOther.Type() != Type() )
164 return false;
165
166 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
167
168 return m_id == other.m_id && m_name == other.m_name && EDA_TEXT::operator==( other );
169}
170
171
172double PCB_FIELD::Similarity( const BOARD_ITEM& aOther ) const
173{
174 if( m_Uuid == aOther.m_Uuid )
175 return 1.0;
176
177 if( aOther.Type() != Type() )
178 return 0.0;
179
180 const PCB_FIELD& other = static_cast<const PCB_FIELD&>( aOther );
181
182 if( m_id < MANDATORY_FIELDS || other.m_id < MANDATORY_FIELDS )
183 {
184 if( m_id == other.m_id )
185 return 1.0;
186 else
187 return 0.0;
188 }
189
190 if( m_name == other.m_name )
191 return 1.0;
192
193 return EDA_TEXT::Similarity( other );
194}
195
196static struct PCB_FIELD_DESC
197{
199 {
208
209 auto isNotFootprintFootprint =
210 []( INSPECTABLE* aItem ) -> bool
211 {
212 if( PCB_FIELD* field = dynamic_cast<PCB_FIELD*>( aItem ) )
213 return !field->IsFootprint();
214
215 return true;
216 };
217
219 isNotFootprintFootprint );
220 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:247
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
const KIID m_Uuid
Definition: eda_item.h:482
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
bool IsSelected() const
Definition: eda_item.h:106
EDA_ITEM * m_parent
Linked list: Link (parent struct)
Definition: eda_item.h:485
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:95
double Similarity(const EDA_TEXT &aOther) const
Definition: eda_text.cpp:1118
bool operator==(const EDA_TEXT &aRhs) const
Definition: eda_text.h:353
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:163
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:168
PCB specific render settings.
Definition: pcb_painter.h:76
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
wxString GetTextTypeDescription() const override
Definition: pcb_field.cpp:88
bool IsReference() const
Definition: pcb_field.h:67
PCB_FIELD(FOOTPRINT *aParent, int aFieldId, const wxString &aName=wxEmptyString)
Definition: pcb_field.cpp:31
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: pcb_field.cpp:47
bool IsValue() const
Definition: pcb_field.h:68
wxString m_name
Definition: pcb_field.h:121
int m_id
Field index,.
Definition: pcb_field.h:119
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider) const override
Return a user-visible description string of this item.
Definition: pcb_field.cpp:97
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:66
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_field.cpp:128
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:172
bool operator==(const BOARD_ITEM &aOther) const override
Definition: pcb_field.cpp:161
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_field.cpp:155
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Return the level of detail (LOD) of the item.
Definition: pcb_text.cpp:161
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
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:212
@ LAYER_FP_VALUES
show footprints values (when texts are visible)
Definition: layer_ids.h:211
wxString EllipsizeMenuText(const wxString &aString)
Ellipsize text (at the end) to be no more than 36 characters.
Definition: ui_common.cpp:210
static struct PCB_FIELD_DESC _PCB_FIELD_DESC
#define TYPE_HASH(x)
Definition: property.h:64
#define REGISTER_TYPE(x)
Definition: property_mgr.h:331
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