KiCad PCB EDA Suite
pcb_text.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) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2020 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 #include <eda_item.h>
27 #include <pcb_edit_frame.h>
28 #include <base_units.h>
29 #include <bitmaps.h>
30 #include <board.h>
31 #include <core/mirror.h>
32 #include <pcb_text.h>
33 #include <pcb_painter.h>
34 #include <trigo.h>
35 #include <kicad_string.h>
36 
38 
39 
41  BOARD_ITEM( parent, PCB_TEXT_T ),
42  EDA_TEXT()
43 {
44  SetMultilineAllowed( true );
45 }
46 
47 
49 {
50 }
51 
52 
53 wxString PCB_TEXT::GetShownText( int aDepth ) const
54 {
55  BOARD* board = dynamic_cast<BOARD*>( GetParent() );
56 
57  std::function<bool( wxString* )> pcbTextResolver =
58  [&]( wxString* token ) -> bool
59  {
60  if( token->IsSameAs( wxT( "LAYER" ) ) )
61  {
62  *token = GetLayerName();
63  return true;
64  }
65 
66  if( token->Contains( ':' ) )
67  {
68  wxString remainder;
69  wxString ref = token->BeforeFirst( ':', &remainder );
70  BOARD_ITEM* refItem = board->GetItem( KIID( ref ) );
71 
72  if( refItem && refItem->Type() == PCB_FOOTPRINT_T )
73  {
74  FOOTPRINT* refFP = static_cast<FOOTPRINT*>( refItem );
75 
76  if( refFP->ResolveTextVar( &remainder, aDepth + 1 ) )
77  {
78  *token = remainder;
79  return true;
80  }
81  }
82  }
83  return false;
84  };
85 
86  std::function<bool( wxString* )> boardTextResolver =
87  [&]( wxString* token ) -> bool
88  {
89  return board->ResolveTextVar( token, aDepth + 1 );
90  };
91 
92  bool processTextVars = false;
93  wxString text = EDA_TEXT::GetShownText( &processTextVars );
94 
95  if( board && processTextVars && aDepth < 10 )
96  text = ExpandTextVars( text, &pcbTextResolver, &boardTextResolver, board->GetProject() );
97 
98  return text;
99 }
100 
101 
102 void PCB_TEXT::SetTextAngle( double aAngle )
103 {
105 }
106 
107 
108 void PCB_TEXT::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
109 {
110  EDA_UNITS units = aFrame->GetUserUnits();
111 
112  // Don't use GetShownText() here; we want to show the user the variable references
113  aList.emplace_back( _( "PCB Text" ), UnescapeString( GetText() ) );
114 
115  if( IsLocked() )
116  aList.emplace_back( _( "Status" ), _( "Locked" ) );
117 
118  aList.emplace_back( _( "Layer" ), GetLayerName() );
119 
120  aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
121 
122  aList.emplace_back( _( "Angle" ), wxString::Format( "%g", GetTextAngleDegrees() ) );
123 
124  aList.emplace_back( _( "Thickness" ), MessageTextFromValue( units, GetTextThickness() ) );
125  aList.emplace_back( _( "Width" ), MessageTextFromValue( units, GetTextWidth() ) );
126  aList.emplace_back( _( "Height" ), MessageTextFromValue( units, GetTextHeight() ) );
127 }
128 
129 
131 {
132  EDA_RECT rect = GetTextBox();
133 
134  if( GetTextAngle() )
135  rect = rect.GetBoundingBoxRotated( GetTextPos(), GetTextAngle() );
136 
137  return rect;
138 }
139 
140 
141 bool PCB_TEXT::TextHitTest( const wxPoint& aPoint, int aAccuracy ) const
142 {
143  return EDA_TEXT::TextHitTest( aPoint, aAccuracy );
144 }
145 
146 
147 bool PCB_TEXT::TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy ) const
148 {
149  EDA_RECT rect = aRect;
150 
151  rect.Inflate( aAccuracy );
152 
153  if( aContains )
154  return rect.Contains( GetBoundingBox() );
155  else
156  return rect.Intersects( GetTextBox(), GetDrawRotation() );
157 }
158 
159 
160 void PCB_TEXT::Rotate( const wxPoint& aRotCentre, double aAngle )
161 {
162  wxPoint pt = GetTextPos();
163  RotatePoint( &pt, aRotCentre, aAngle );
164  SetTextPos( pt );
165 
166  SetTextAngle( GetTextAngle() + aAngle );
167 }
168 
169 
170 void PCB_TEXT::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
171 {
172  if( aFlipLeftRight )
173  {
174  SetTextX( MIRRORVAL( GetTextPos().x, aCentre.x ) );
176  }
177  else
178  {
179  SetTextY( MIRRORVAL( GetTextPos().y, aCentre.y ) );
180  SetTextAngle( 1800 - GetTextAngle() );
181  }
182 
183  SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
184  SetMirrored( !IsMirrored() );
185 }
186 
187 
188 wxString PCB_TEXT::GetSelectMenuText( EDA_UNITS aUnits ) const
189 {
190  return wxString::Format( _( "PCB Text '%s' on %s"), ShortenedShownText(), GetLayerName() );
191 }
192 
193 
195 {
196  return BITMAPS::text;
197 }
198 
199 
201 {
202  return new PCB_TEXT( *this );
203 }
204 
205 
207 {
208  assert( aImage->Type() == PCB_TEXT_T );
209 
210  std::swap( *((PCB_TEXT*) this), *((PCB_TEXT*) aImage) );
211 }
212 
213 
214 std::shared_ptr<SHAPE> PCB_TEXT::GetEffectiveShape( PCB_LAYER_ID aLayer ) const
215 {
216  return GetEffectiveTextShape();
217 }
218 
219 
220 static struct TEXTE_PCB_DESC
221 {
223  {
230  }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:195
BOARD_ITEM * GetItem(const KIID &aID) const
Definition: board.cpp:815
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:90
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:65
#define TYPE_HASH(x)
Definition: property.h:59
bool TextHitTest(const wxPoint &aPoint, int aAccuracy=0) const override
Test if aPoint is within the bounds of this object.
Definition: pcb_text.cpp:141
bool IsMirrored() const
Definition: eda_text.h:196
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
Definition: pcb_text.cpp:206
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle) const
Useful to calculate bounding box of rotated items, when rotation if not k*90 degrees.
Definition: eda_rect.cpp:511
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:58
void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: pcb_text.cpp:170
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_text.cpp:194
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:194
Implementation of conversion functions that require both schematic and board internal units.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
void SetTextAngle(double aAngle) override
Definition: pcb_text.cpp:102
bool ResolveTextVar(wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the component.
Definition: footprint.cpp:432
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition: lset.cpp:521
EDA_RECT GetTextBox(int aLine=-1, bool aInvertY=false) const
Useful in multiline texts to calculate the full text or a line area (for zones filling,...
Definition: eda_text.cpp:223
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:253
PROJECT * GetProject() const
Definition: board.h:432
double GetTextAngle() const
Definition: eda_text.h:181
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
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: pcb_text.cpp:108
virtual double GetDrawRotation() const
Definition: eda_text.cpp:633
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
static struct TEXTE_PCB_DESC _TEXTE_PCB_DESC
int GetTextThickness() const
Definition: eda_text.h:167
The base class for create windows for drawing purpose.
PCB_TEXT(BOARD_ITEM *parent)
Definition: pcb_text.cpp:40
#define REGISTER_TYPE(x)
Definition: property_mgr.h:248
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER) const override
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: pcb_text.cpp:214
int GetTextHeight() const
Definition: eda_text.h:251
void SetTextX(int aX)
Definition: eda_text.h:256
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:119
PCB specific render settings.
Definition: pcb_painter.h:64
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: pcb_text.cpp:53
Definition: kiid.h:44
~PCB_TEXT()
Definition: pcb_text.cpp:48
PCB_LAYER_ID
A quick note on layer IDs:
virtual BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:46
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:202
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: pcb_text.cpp:188
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.h:202
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
EDA_UNITS
Definition: eda_units.h:38
virtual bool IsLocked() const
Definition: board_item.h:249
double GetTextAngleDegrees() const
Definition: eda_text.h:183
int GetTextWidth() const
Definition: eda_text.h:248
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
std::shared_ptr< SHAPE_COMPOUND > GetEffectiveTextShape() const
Definition: eda_text.cpp:620
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
#define _(s)
Definition: 3d_actions.cpp:33
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:151
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
Handle the component boundary box.
Definition: eda_rect.h:42
virtual bool TextHitTest(const wxPoint &aPoint, int aAccuracy=0) const
Test if aPoint is within the bounds of this object.
Definition: eda_text.cpp:372
T MIRRORVAL(T aPoint, T aMirrorRef)
Returns the mirror of aPoint relative to the aMirrorRef.
Definition: mirror.h:31
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:150
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Rotate this object.
Definition: pcb_text.cpp:160
bool Intersects(const EDA_RECT &aRect) const
Test for a common area between rectangles.
Definition: eda_rect.cpp:150
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:62
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_text.cpp:200
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:174
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: pcb_text.cpp:130
T NormalizeAngle360Min(T Angle)
Normalize angle to be > -360.0 and < 360.0 Angle equal to -360 or +360 are set to 0.
Definition: trigo.h:254
bool ResolveTextVar(wxString *token, int aDepth) const
Definition: board.cpp:220
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:168
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:60
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:173
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:141
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:363
void SetTextY(int aY)
Definition: eda_text.h:257
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:163