KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_field.h
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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2022 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#ifndef CLASS_SCH_FIELD_H
27#define CLASS_SCH_FIELD_H
28
29
30#include <eda_text.h>
31#include <sch_item.h>
32#include <template_fieldnames.h>
33#include <general.h>
34#include <string_utils.h>
35#include "scintilla_tricks.h"
36
37class SCH_EDIT_FRAME;
38class SCH_TEXT;
39
40
52class SCH_FIELD : public SCH_ITEM, public EDA_TEXT
53{
54public:
55 SCH_FIELD( const VECTOR2I& aPos, int aFieldId, SCH_ITEM* aParent,
56 const wxString& aName = wxEmptyString );
57
58 SCH_FIELD( SCH_ITEM* aParent, int aFieldId = INVALID_FIELD,
59 const wxString& aName = wxEmptyString );
60
61 SCH_FIELD( SCH_TEXT* aText );
62
63 SCH_FIELD( const SCH_FIELD& aText );
64
65 ~SCH_FIELD() override
66 { }
67
68 SCH_FIELD& operator=( const SCH_FIELD& aField );
69
70 static bool ClassOf( const EDA_ITEM* aItem )
71 {
72 return aItem && SCH_FIELD_T == aItem->Type();
73 }
74
75 wxString GetClass() const override
76 {
77 return wxT( "SCH_FIELD" );
78 }
79
80 bool IsType( const std::vector<KICAD_T>& aScanTypes ) const override
81 {
82 if( SCH_ITEM::IsType( aScanTypes ) )
83 return true;
84
85 for( KICAD_T scanType : aScanTypes )
86 {
88 return true;
89 else if ( scanType == SCH_FIELD_LOCATE_VALUE_T && m_id == VALUE_FIELD )
90 return true;
91 else if ( scanType == SCH_FIELD_LOCATE_FOOTPRINT_T && m_id == FOOTPRINT_FIELD )
92 return true;
93 else if ( scanType == SCH_FIELD_LOCATE_DATASHEET_T && m_id == DATASHEET_FIELD )
94 return true;
95 }
96
97 return false;
98 }
99
100 wxString GetFriendlyName() const override
101 {
102 return _( "Field" );
103 }
104
105 bool IsHypertext() const override
106 {
107 if( GetCanonicalName() == wxT( "Intersheetrefs" ) )
108 return true;
109
110 return IsURL( GetShownText( false ) );
111 }
112
113 void DoHypertextAction( EDA_DRAW_FRAME* aFrame ) const override;
114
122 wxString GetName( bool aUseDefaultName = true ) const;
123
128 wxString GetCanonicalName() const;
129
130 void SetName( const wxString& aName );
131
132 void SetText( const wxString& aText ) override;
133
139 const wxString& GetInternalName() { return m_name; }
140
141 int GetId() const { return m_id; }
142
143 void SetId( int aId );
144
152 wxString GetShownName() const;
153 wxString GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraText,
154 int aDepth = 0 ) const;
155
156 wxString GetShownText( bool aAllowExtraText, int aDepth = 0 ) const override;
157
167 wxString GetFullText( int unit = 1 ) const;
168
174 bool IsEmpty()
175 {
176 wxString name( m_name );
177 wxString value( GetText() );
178
179 return name.Trim().empty() && value.Trim().empty();
180 }
181
182 int GetSchTextSize() const { return GetTextWidth(); }
183 void SetSchTextSize( int aSize ) { SetTextSize( VECTOR2I( aSize, aSize ) ); }
184
185 COLOR4D GetFieldColor() const;
186
187 void SetLastResolvedState( const SCH_ITEM* aItem ) override
188 {
189 const SCH_FIELD* aField = dynamic_cast<const SCH_FIELD*>( aItem );
190
191 if( aField )
193 }
194
195 std::vector<int> ViewGetLayers() const override;
196
198
202 EDA_ANGLE GetDrawRotation() const override;
203
204 const BOX2I GetBoundingBox() const override;
205
210 bool IsHorizJustifyFlipped() const;
211 bool IsVertJustifyFlipped() const;
212
215
218
219 bool IsNameShown() const { return m_showName; }
220 void SetNameShown( bool aShown = true ) { m_showName = aShown; }
221
228 bool IsNamedVariable() const { return m_isNamedVariable; }
229
230 bool CanAutoplace() const { return m_allowAutoPlace; }
231 void SetCanAutoplace( bool aCanPlace ) { m_allowAutoPlace = aCanPlace; }
232
233 void SwapData( SCH_ITEM* aItem ) override;
234
235 int GetPenWidth() const override;
236
237 bool IsAutoAdded() const { return m_autoAdded; }
238 void SetAutoAdded( bool aAutoAdded ) { m_autoAdded = aAutoAdded; }
239
240 bool ShowInChooser() const { return m_showInChooser; }
241 void SetShowInChooser( bool aShow = true ) { m_showInChooser = aShow; }
242
243 void ClearCaches() override;
244 void ClearRenderCache() override;
245
246 std::vector<std::unique_ptr<KIFONT::GLYPH>>*
247 GetRenderCache( const wxString& forResolvedText, const VECTOR2I& forPosition,
248 TEXT_ATTRIBUTES& aAttrs ) const;
249
250 void Move( const VECTOR2I& aMoveVector ) override
251 {
252 Offset( aMoveVector );
253 }
254
255 void Rotate( const VECTOR2I& aCenter, bool aRotateCCW ) override;
256
257 void MirrorVertically( int aCenter ) override;
258 void MirrorHorizontally( int aCenter ) override;
259
260 void BeginEdit( const VECTOR2I& aStartPoint ) override;
261 void CalcEdit( const VECTOR2I& aPosition ) override;
262
263 void OnScintillaCharAdded( SCINTILLA_TRICKS* aScintillaTricks,
264 wxStyledTextEvent &aEvent ) const;
265
266 bool Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const override;
267
268 bool Replace( const EDA_SEARCH_DATA& aSearchData, void* aAuxData = nullptr ) override;
269
270 wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
271 void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
272
273 BITMAPS GetMenuImage() const override;
274
275 bool IsReplaceable() const override;
276
278
279 VECTOR2I GetPosition() const override;
280 void SetPosition( const VECTOR2I& aPosition ) override;
281
283
284 bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
285 bool HitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const override;
286
287 void Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
288 const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed ) override;
289
290 void Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
291 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed ) override;
292
293 EDA_ITEM* Clone() const override;
294
302 void ImportValues( const SCH_FIELD& aSource );
303
311 void Copy( SCH_FIELD* aTarget ) const;
312
313 bool IsMandatory() const;
314
315 bool operator <( const SCH_ITEM& aItem ) const override;
316
317 double Similarity( const SCH_ITEM& aItem ) const override;
318
319 bool operator==( const SCH_ITEM& aItem ) const override;
320 bool operator==( const SCH_FIELD& aItem ) const;
321
322#if defined(DEBUG)
323 void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
324#endif
325
326protected:
327 KIFONT::FONT* getDrawFont() const override;
328
329 const KIFONT::METRICS& getFontMetrics() const override { return GetFontMetrics(); }
330
343 int compare( const SCH_ITEM& aOther, int aCompareFlags = 0 ) const override;
344
345private:
346 int m_id;
347
348 wxString m_name;
349
354
357
358 mutable bool m_renderCacheValid;
360 mutable std::vector<std::unique_ptr<KIFONT::GLYPH>> m_renderCache;
361
363};
364
365
366#endif /* CLASS_SCH_FIELD_H */
const char * name
Definition: DXF_plotter.cpp:59
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
The base class for create windows for drawing purpose.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:260
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:526
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
int GetTextWidth() const
Definition: eda_text.h:251
void Offset(const VECTOR2I &aOffset)
Definition: eda_text.cpp:589
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Base plotter engine class.
Definition: plotter.h:105
Schematic editor (Eeschema) main window.
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:53
void ClearRenderCache() override
Definition: sch_field.cpp:373
int compare(const SCH_ITEM &aOther, int aCompareFlags=0) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: sch_field.cpp:1638
wxString GetClass() const override
Return the class name.
Definition: sch_field.h:75
COLOR4D m_lastResolvedColor
Definition: sch_field.h:362
bool IsAutoAdded() const
Definition: sch_field.h:237
GR_TEXT_V_ALIGN_T GetEffectiveVertJustify() const
Definition: sch_field.cpp:743
bool IsMandatory() const
Definition: sch_field.cpp:1507
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
Definition: sch_field.cpp:1116
void Rotate(const VECTOR2I &aCenter, bool aRotateCCW) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
Definition: sch_field.cpp:1025
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: sch_field.cpp:334
~SCH_FIELD() override
Definition: sch_field.h:65
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_field.cpp:615
std::vector< std::unique_ptr< KIFONT::GLYPH > > m_renderCache
Definition: sch_field.h:360
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1485
void SetEffectiveHorizJustify(GR_TEXT_H_ALIGN_T)
Definition: sch_field.cpp:666
int GetSchTextSize() const
Definition: sch_field.h:182
bool Replace(const EDA_SEARCH_DATA &aSearchData, void *aAuxData=nullptr) override
Perform a text replace using the find and replace criteria in aSearchData on items that support text ...
Definition: sch_field.cpp:943
void SetLastResolvedState(const SCH_ITEM *aItem) override
Definition: sch_field.h:187
bool m_showName
Render the field name in addition to its value.
Definition: sch_field.h:350
void Plot(PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS &aPlotOpts, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed) override
Plot the item to aPlotter.
Definition: sch_field.cpp:1354
bool IsType(const std::vector< KICAD_T > &aScanTypes) const override
Check whether the item is one of the listed types.
Definition: sch_field.h:80
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: sch_field.cpp:147
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: sch_field.cpp:1285
static bool ClassOf(const EDA_ITEM *aItem)
Definition: sch_field.h:70
bool IsNameShown() const
Definition: sch_field.h:219
bool IsHypertext() const override
Allow items to support hypertext actions when hovered/clicked.
Definition: sch_field.h:105
bool m_autoAdded
Was this field automatically added to a LIB_SYMBOL?
Definition: sch_field.h:355
double Similarity(const SCH_ITEM &aItem) const override
Return a measure of how likely the other object is to represent the same object.
Definition: sch_field.cpp:1597
void Print(const SCH_RENDER_SETTINGS *aSettings, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aForceNoFill, bool aDimmed) override
Print an item.
Definition: sch_field.cpp:426
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: sch_field.cpp:1302
SCH_LAYER_ID GetDefaultLayer() const
Definition: sch_field.cpp:565
bool IsHorizJustifyFlipped() const
Return whether the field will be rendered with the horizontal justification inverted due to rotation ...
Definition: sch_field.cpp:643
bool IsVertJustifyFlipped() const
Definition: sch_field.cpp:700
EDA_ANGLE GetDrawRotation() const override
Adjusters to allow EDA_TEXT to draw/print/etc.
Definition: sch_field.cpp:593
void SetEffectiveVertJustify(GR_TEXT_V_ALIGN_T)
Definition: sch_field.cpp:723
void CalcEdit(const VECTOR2I &aPosition) override
Calculate the attributes of an item at aPosition when it is being edited.
Definition: sch_field.cpp:1110
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: sch_field.cpp:757
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
Definition: sch_field.cpp:1092
void SetCanAutoplace(bool aCanPlace)
Definition: sch_field.h:231
bool m_isNamedVariable
If the field name is a variable name, e.g.
Definition: sch_field.h:352
void DoHypertextAction(EDA_DRAW_FRAME *aFrame) const override
Definition: sch_field.cpp:1163
int GetPenWidth() const override
Definition: sch_field.cpp:349
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: sch_field.cpp:1254
COLOR4D GetFieldColor() const
Definition: sch_field.cpp:539
bool IsNamedVariable() const
Named variables are fields whose names are variables like ${VAR}.
Definition: sch_field.h:228
wxString GetFriendlyName() const override
Definition: sch_field.h:100
int GetId() const
Definition: sch_field.h:141
bool operator==(const SCH_ITEM &aItem) const override
Definition: sch_field.cpp:1558
SCH_FIELD & operator=(const SCH_FIELD &aField)
Definition: sch_field.cpp:117
void ImportValues(const SCH_FIELD &aSource)
Copy parameters from a SCH_FIELD source.
Definition: sch_field.cpp:510
void SetAutoAdded(bool aAutoAdded)
Definition: sch_field.h:238
bool operator<(const SCH_ITEM &aItem) const override
Definition: sch_field.cpp:1535
wxString GetShownName() const
Get the fields name as displayed on the schematic or in the symbol fields table.
Definition: sch_field.cpp:203
VECTOR2I GetLibPosition() const
Definition: sch_field.h:277
bool IsEmpty()
Return true if both the name and value of the field are empty.
Definition: sch_field.h:174
bool m_renderCacheValid
Definition: sch_field.h:358
void BeginEdit(const VECTOR2I &aStartPoint) override
Begin drawing a symbol library draw item at aPosition.
Definition: sch_field.cpp:1104
void SetShowInChooser(bool aShow=true)
Definition: sch_field.h:241
bool IsReplaceable() const override
Override this method in any derived object that supports test find and replace.
Definition: sch_field.cpp:925
void SetSchTextSize(int aSize)
Definition: sch_field.h:183
GR_TEXT_H_ALIGN_T GetEffectiveHorizJustify() const
Definition: sch_field.cpp:686
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
Definition: sch_field.cpp:559
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1229
void SetPosition(const VECTOR2I &aPosition) override
Definition: sch_field.cpp:1465
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
Definition: sch_field.cpp:519
void SetName(const wxString &aName)
Definition: sch_field.cpp:1204
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:209
void Move(const VECTOR2I &aMoveVector) override
Move the item by aMoveVector to a new position.
Definition: sch_field.h:250
VECTOR2I m_renderCachePos
Definition: sch_field.h:359
bool CanAutoplace() const
Definition: sch_field.h:230
std::vector< std::unique_ptr< KIFONT::GLYPH > > * GetRenderCache(const wxString &forResolvedText, const VECTOR2I &forPosition, TEXT_ATTRIBUTES &aAttrs) const
Definition: sch_field.cpp:381
void SetId(int aId)
Definition: sch_field.cpp:159
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: sch_field.cpp:1124
bool ShowInChooser() const
Definition: sch_field.h:240
void ClearCaches() override
Definition: sch_field.cpp:366
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1214
VECTOR2I GetParentPosition() const
Definition: sch_field.cpp:1501
int m_id
Field index,.
Definition: sch_field.h:346
const KIFONT::METRICS & getFontMetrics() const override
Definition: sch_field.h:329
const wxString & GetInternalName()
Get the initial name of the field set at creation (or set by SetName()).
Definition: sch_field.h:139
bool m_showInChooser
This field is available as a data column for the chooser.
Definition: sch_field.h:356
void OnScintillaCharAdded(SCINTILLA_TRICKS *aScintillaTricks, wxStyledTextEvent &aEvent) const
Definition: sch_field.cpp:807
wxString m_name
Definition: sch_field.h:348
void SetNameShown(bool aShown=true)
Definition: sch_field.h:220
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
Definition: sch_field.cpp:1080
void Copy(SCH_FIELD *aTarget) const
Copy parameters of this field to another field.
Definition: sch_field.cpp:153
KIFONT::FONT * getDrawFont() const override
Definition: sch_field.cpp:355
bool m_allowAutoPlace
This field can be autoplaced.
Definition: sch_field.h:351
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:167
const KIFONT::METRICS & GetFontMetrics() const
Definition: sch_item.cpp:465
bool IsType(const std::vector< KICAD_T > &aScanTypes) const override
Check whether the item is one of the listed types.
Definition: sch_item.h:182
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
#define _(s)
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:438
bool IsURL(wxString aStr)
Performs a URL sniff-test on a string.
@ DATASHEET_FIELD
name of datasheet
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ INVALID_FIELD
The field ID hasn't been set yet; field is invalid.
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
GR_TEXT_H_ALIGN_T
This is API surface mapped to common.types.HorizontalAlignment.
GR_TEXT_V_ALIGN_T
This is API surface mapped to common.types.VertialAlignment.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ SCH_FIELD_LOCATE_REFERENCE_T
Definition: typeinfo.h:179
@ SCH_FIELD_LOCATE_FOOTPRINT_T
Definition: typeinfo.h:181
@ SCH_FIELD_T
Definition: typeinfo.h:150
@ SCH_FIELD_LOCATE_VALUE_T
Definition: typeinfo.h:180
@ SCH_FIELD_LOCATE_DATASHEET_T
Definition: typeinfo.h:182
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695