KiCad PCB EDA Suite
class_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-2015 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 
31 #include <fctsys.h>
32 #include <gr_basic.h>
33 #include <base_struct.h>
34 #include <gr_text.h>
35 #include <pcb_edit_frame.h>
36 #include <base_units.h>
37 #include <bitmaps.h>
40 
41 #include <class_board.h>
42 #include <class_pcb_text.h>
43 #include <pcb_painter.h>
44 
46 
47 
49  BOARD_ITEM( parent, PCB_TEXT_T ),
50  EDA_TEXT()
51 {
52  SetMultilineAllowed( true );
53 }
54 
55 
57 {
58 }
59 
60 
61 wxString TEXTE_PCB::GetShownText( int aDepth ) const
62 {
63  BOARD* board = static_cast<BOARD*>( GetParent() );
64  wxASSERT( board );
65 
66  std::function<bool( wxString* )> pcbTextResolver =
67  [&]( wxString* token ) -> bool
68  {
69  if( token->IsSameAs( wxT( "LAYER" ) ) )
70  {
71  *token = GetLayerName();
72  return true;
73  }
74 
75  if( token->Contains( ':' ) )
76  {
77  wxString remainder;
78  wxString ref = token->BeforeFirst( ':', &remainder );
79  BOARD_ITEM* refItem = board->GetItem( KIID( ref ) );
80 
81  if( refItem && refItem->Type() == PCB_MODULE_T )
82  {
83  MODULE* refModule = static_cast<MODULE*>( refItem );
84 
85  if( refModule->ResolveTextVar( &remainder, aDepth + 1 ) )
86  {
87  *token = remainder;
88  return true;
89  }
90  }
91  }
92  return false;
93  };
94 
95  bool processTextVars = false;
96  wxString text = EDA_TEXT::GetShownText( &processTextVars );
97 
98  if( processTextVars && aDepth < 10 )
99  text = ExpandTextVars( text, &pcbTextResolver, board->GetProject() );
100 
101  return text;
102 }
103 
104 
105 void TEXTE_PCB::SetTextAngle( double aAngle )
106 {
108 }
109 
110 
111 void TEXTE_PCB::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
112 {
113  wxString msg;
114 
115  wxCHECK_RET( m_Parent != NULL, wxT( "TEXTE_PCB::GetMsgPanelInfo() m_Parent is NULL." ) );
116 
117  if( m_Parent->Type() == PCB_DIMENSION_T )
118  aList.emplace_back( _( "Dimension" ), GetShownText(), DARKGREEN );
119  else
120  aList.emplace_back( _( "PCB Text" ), GetShownText(), DARKGREEN );
121 
122  aList.emplace_back( _( "Layer" ), GetLayerName(), BLUE );
123 
124  if( !IsMirrored() )
125  aList.emplace_back( _( "Mirror" ), _( "No" ), DARKGREEN );
126  else
127  aList.emplace_back( _( "Mirror" ), _( "Yes" ), DARKGREEN );
128 
129  msg.Printf( wxT( "%.1f" ), GetTextAngle() / 10.0 );
130  aList.emplace_back( _( "Angle" ), msg, DARKGREEN );
131 
132  msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextThickness() );
133  aList.emplace_back( _( "Thickness" ), msg, MAGENTA );
134 
135  msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextWidth() );
136  aList.emplace_back( _( "Width" ), msg, RED );
137 
138  msg = MessageTextFromValue( aFrame->GetUserUnits(), GetTextHeight() );
139  aList.emplace_back( _( "Height" ), msg, RED );
140 }
141 
142 
144 {
145  EDA_RECT rect = GetTextBox();
146 
147  if( GetTextAngle() )
148  rect = rect.GetBoundingBoxRotated( GetTextPos(), GetTextAngle() );
149 
150  return rect;
151 }
152 
153 
154 void TEXTE_PCB::Rotate( const wxPoint& aRotCentre, double aAngle )
155 {
156  wxPoint pt = GetTextPos();
157  RotatePoint( &pt, aRotCentre, aAngle );
158  SetTextPos( pt );
159 
160  SetTextAngle( GetTextAngle() + aAngle );
161 }
162 
163 
164 void TEXTE_PCB::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
165 {
166  if( aFlipLeftRight )
167  SetTextX( aCentre.x - ( GetTextPos().x - aCentre.x ) );
168  else
169  SetTextY( aCentre.y - ( GetTextPos().y - aCentre.y ) );
170 
171  int copperLayerCount = GetBoard()->GetCopperLayerCount();
172 
173  SetLayer( FlipLayer( GetLayer(), copperLayerCount ) );
174  SetMirrored( !IsMirrored() );
175 
176  double text_angle = GetTextAngle();
177  if( text_angle < 1800 )
178  text_angle = 1800 - text_angle;
179  else
180  text_angle = 3600 - text_angle + 1800;
181  SetTextAngle( text_angle );
182 
183  // adjust justified text for mirroring
185  {
187  SetTextX( GetTextPos().x - GetTextBox().GetWidth() );
188  else
189  SetTextX( GetTextPos().x + GetTextBox().GetWidth() );
190  }
191 }
192 
193 
194 wxString TEXTE_PCB::GetSelectMenuText( EDA_UNITS aUnits ) const
195 {
196  return wxString::Format( _( "Pcb Text \"%s\" on %s"), ShortenedShownText(), GetLayerName() );
197 }
198 
199 
201 {
202  return text_xpm;
203 }
204 
205 
207 {
208  return new TEXTE_PCB( *this );
209 }
210 
211 
213 {
214  assert( aImage->Type() == PCB_TEXT_T );
215 
216  std::swap( *((TEXTE_PCB*) this), *((TEXTE_PCB*) aImage) );
217 }
218 
219 
void SetMirrored(bool isMirrored)
Definition: eda_text.h:187
EDA_UNITS
Definition: common.h:198
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
void SetTextAngle(double aAngle) override
EDA_ITEM * m_Parent
Linked list: Link (parent struct)
Definition: base_struct.h:174
bool IsMirrored() const
Definition: eda_text.h:188
PNG memory record (file in memory).
Definition: bitmap_def.h:29
TEXTE_PCB class definition.
bool ResolveTextVar(wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the component.
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Function Rotate Rotate this object.
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
Implementation of conversion functions that require both schematic and board internal units.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Function Flip Flip this object, i.e.
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Function FlippedLayerNumber.
Definition: lset.cpp:485
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:217
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:246
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
PROJECT * GetProject() const
Definition: class_board.h:376
const BITMAP_OPAQUE text_xpm[1]
Definition: text.cpp:27
double GetTextAngle() const
Definition: eda_text.h:173
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
int GetTextThickness() const
Definition: eda_text.h:158
The base class for create windows for drawing purpose.
BOARD_ITEM * GetItem(const KIID &aID)
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
int GetTextHeight() const
Definition: eda_text.h:244
void SetTextX(int aX)
Definition: eda_text.h:249
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:112
PCB_RENDER_SETTINGS Stores PCB specific render settings.
Definition: pcb_painter.h:60
Definition: common.h:68
class MODULE, a footprint
Definition: typeinfo.h:89
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:198
#define NULL
const EDA_RECT GetBoundingBoxRotated(wxPoint aRotCenter, double aAngle)
Function GetBoundingBoxRotated.
virtual BOARD * GetBoard() const
Function GetBoard returns the BOARD in which this BOARD_ITEM resides, or NULL if none.
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:196
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:100
Definition: color4d.h:59
void SetMultilineAllowed(bool aAllow)
Definition: eda_text.h:195
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:124
Definition: color4d.h:56
int GetTextWidth() const
Definition: eda_text.h:241
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
#define _(s)
Definition: 3d_actions.cpp:33
int GetCopperLayerCount() const
Function GetCopperLayerCount.
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
wxString ExpandTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aLocalResolver, const PROJECT *aProject)
Expand '${var-name}' templates in text.
Definition: common.cpp:382
const wxPoint & GetTextPos() const
Definition: eda_text.h:247
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:166
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
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:237
BOARD_ITEM_CONTAINER * GetParent() const
wxString GetLayerName() const
Function GetLayerName returns the name of the PCB layer on which the item resides.
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:133
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetTextY(int aY)
Definition: eda_text.h:250
TEXTE_PCB(BOARD_ITEM *parent)
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193