Files
kicad/pcbnew/class_text_mod.cpp
T

505 lines
14 KiB
C++
Raw Normal View History

2012-01-14 14:50:32 -05:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
2012-01-14 14:50:32 -05:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
2011-09-23 09:57:12 -04:00
/**
* @file class_module.cpp
* @brief TEXT_MODULE class implementation.
*/
#include <fctsys.h>
#include <gr_basic.h>
#include <wxstruct.h>
#include <trigo.h>
#include <class_drawpanel.h>
#include <drawtxt.h>
#include <kicad_string.h>
#include <colors_selection.h>
#include <richio.h>
#include <macros.h>
#include <wxBasePcbFrame.h>
2013-01-12 12:32:24 -05:00
#include <msgpanel.h>
#include <base_units.h>
2011-09-23 09:57:12 -04:00
#include <class_board.h>
#include <class_module.h>
2011-09-23 09:57:12 -04:00
2016-12-02 18:58:12 +01:00
#include <view/view.h>
#include <pcbnew.h>
2013-04-07 13:55:18 +02:00
TEXTE_MODULE::TEXTE_MODULE( MODULE* parent, TEXT_TYPE text_type ) :
2012-01-14 14:50:32 -05:00
BOARD_ITEM( parent, PCB_MODULE_TEXT_T ),
EDA_TEXT()
{
MODULE* module = static_cast<MODULE*>( m_Parent );
2007-08-04 20:05:54 +00:00
2013-04-07 13:55:18 +02:00
m_Type = text_type;
2009-11-12 15:43:38 +00:00
m_NoShow = false;
2016-02-11 16:02:37 +01:00
// Set text thickness to a default value
2013-04-07 13:55:18 +02:00
m_Thickness = Millimeter2iu( 0.15 );
2014-06-24 11:17:18 -05:00
SetLayer( F_SilkS );
2012-01-14 14:50:32 -05:00
// Set position and give a default layer if a valid parent footprint exists
if( module && ( module->Type() == PCB_MODULE_T ) )
2007-08-04 20:05:54 +00:00
{
2013-03-13 14:53:58 -04:00
m_Pos = module->GetPosition();
2007-08-23 04:28:46 +00:00
2013-04-07 13:55:18 +02:00
if( IsBackLayer( module->GetLayer() ) )
{
2014-06-24 11:17:18 -05:00
SetLayer( B_SilkS );
2013-04-07 13:55:18 +02:00
m_Mirror = true;
}
2007-08-04 20:05:54 +00:00
}
SetDrawCoord();
}
2007-08-04 20:05:54 +00:00
2007-09-01 12:00:30 +00:00
TEXTE_MODULE::~TEXTE_MODULE()
{
}
void TEXTE_MODULE::Rotate( const wxPoint& aRotCentre, double aAngle )
{
// Used in footprint edition
// Note also in module editor, m_Pos0 = m_Pos
RotatePoint( &m_Pos, aRotCentre, aAngle );
SetOrientation( GetOrientation() + aAngle );
SetLocalCoord();
}
void TEXTE_MODULE::Flip( const wxPoint& aCentre )
{
// flipping the footprint is relative to the X axis
MIRROR( m_Pos.y, aCentre.y );
NEGATE_AND_NORMALIZE_ANGLE_POS( m_Orient );
SetLayer( FlipLayer( GetLayer() ) );
m_Mirror = IsBackLayer( GetLayer() );
SetLocalCoord();
}
void TEXTE_MODULE::Mirror( const wxPoint& aCentre, bool aMirrorAroundXAxis )
{
// Used in modedit, to transform the footprint
// the mirror is around the Y axis or X axis if aMirrorAroundXAxis = true
// the position is mirrored, but the text itself is not mirrored
if( aMirrorAroundXAxis )
MIRROR( m_Pos.y, aCentre.y );
else
MIRROR( m_Pos.x, aCentre.x );
NEGATE_AND_NORMALIZE_ANGLE_POS( m_Orient );
SetLocalCoord();
}
void TEXTE_MODULE::Move( const wxPoint& aMoveVector )
{
m_Pos += aMoveVector;
SetLocalCoord();
}
2012-01-14 14:50:32 -05:00
int TEXTE_MODULE::GetLength() const
{
2007-08-04 20:05:54 +00:00
return m_Text.Len();
}
2012-01-14 14:50:32 -05:00
void TEXTE_MODULE::SetDrawCoord()
{
const MODULE* module = static_cast<const MODULE*>( m_Parent );
2007-08-04 20:05:54 +00:00
m_Pos = m_Pos0;
if( module )
{
double angle = module->GetOrientation();
RotatePoint( &m_Pos.x, &m_Pos.y, angle );
m_Pos += module->GetPosition();
}
}
2007-08-04 20:05:54 +00:00
2012-01-14 14:50:32 -05:00
void TEXTE_MODULE::SetLocalCoord()
{
const MODULE* module = static_cast<const MODULE*>( m_Parent );
if( module )
{
m_Pos0 = m_Pos - module->GetPosition();
double angle = module->GetOrientation();
RotatePoint( &m_Pos0.x, &m_Pos0.y, -angle );
}
else
2009-08-11 10:27:21 +00:00
{
m_Pos0 = m_Pos;
}
}
2014-05-04 19:08:36 +02:00
bool TEXTE_MODULE::HitTest( const wxPoint& aPosition ) const
{
2008-08-09 08:05:42 +00:00
wxPoint rel_pos;
EDA_RECT area = GetTextBox( -1, -1 );
2008-04-01 05:21:50 +00:00
/* Rotate refPos to - angle to test if refPos is within area (which
* is relative to an horizontal text)
2008-08-09 08:05:42 +00:00
*/
rel_pos = aPosition;
2008-08-09 08:05:42 +00:00
RotatePoint( &rel_pos, m_Pos, -GetDrawRotation() );
2008-03-04 04:22:27 +00:00
if( area.Contains( rel_pos ) )
2007-08-04 20:05:54 +00:00
return true;
2007-08-04 20:05:54 +00:00
return false;
}
2008-08-09 08:05:42 +00:00
const EDA_RECT TEXTE_MODULE::GetBoundingBox() const
{
double angle = GetDrawRotation();
EDA_RECT text_area = GetTextBox( -1, -1 );
2008-04-01 05:21:50 +00:00
if( angle )
text_area = text_area.GetBoundingBoxRotated( m_Pos, angle );
2008-04-01 05:21:50 +00:00
return text_area;
}
2008-08-09 08:05:42 +00:00
void TEXTE_MODULE::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, GR_DRAWMODE aDrawMode,
const wxPoint& aOffset )
{
if( aPanel == NULL )
return;
/* parent must *not* be NULL (a footprint text without a footprint
parent has no sense) */
wxASSERT( m_Parent );
2007-08-04 20:05:54 +00:00
BOARD* brd = GetBoard( );
EDA_COLOR_T color = brd->GetLayerColor( GetLayer() );
LAYER_ID text_layer = GetLayer();
if( !brd->IsLayerVisible( m_Layer )
|| (IsFrontLayer( text_layer ) && !brd->IsElementVisible( MOD_TEXT_FR_VISIBLE ))
|| (IsBackLayer( text_layer ) && !brd->IsElementVisible( MOD_TEXT_BK_VISIBLE )) )
return;
2007-08-04 20:05:54 +00:00
// Invisible texts are still drawn (not plotted) in MOD_TEXT_INVISIBLE
// Just because we must have to edit them (at least to make them visible)
if( m_NoShow )
{
if( !brd->IsElementVisible( MOD_TEXT_INVISIBLE ) )
return;
color = brd->GetVisibleElementColor( MOD_TEXT_INVISIBLE );
}
2008-03-04 04:22:27 +00:00
DISPLAY_OPTIONS* displ_opts = (DISPLAY_OPTIONS*)aPanel->GetDisplayOptions();
2015-05-27 22:13:51 +02:00
// shade text if high contrast mode is active
if( ( aDrawMode & GR_ALLOW_HIGHCONTRAST ) && displ_opts && displ_opts->m_ContrastModeDisplay )
2015-05-27 22:13:51 +02:00
{
LAYER_ID curr_layer = ( (PCB_SCREEN*) aPanel->GetScreen() )->m_Active_Layer;
2015-05-27 22:13:51 +02:00
if( !IsOnLayer( curr_layer ) )
ColorTurnToDarkDarkGray( &color );
}
// Draw mode compensation for the width
int width = m_Thickness;
if( displ_opts && displ_opts->m_DisplayModTextFill == SKETCH )
2007-08-04 20:05:54 +00:00
width = -width;
GRSetDrawMode( aDC, aDrawMode );
wxPoint pos = m_Pos - aOffset;
2007-08-04 20:05:54 +00:00
// Draw the text anchor point
if( brd->IsElementVisible( ANCHOR_VISIBLE ) )
2007-08-04 20:05:54 +00:00
{
EDA_COLOR_T anchor_color = brd->GetVisibleElementColor(ANCHOR_VISIBLE);
GRDrawAnchor( aPanel->GetClipBox(), aDC, pos.x, pos.y, DIM_ANCRE_TEXTE, anchor_color );
2009-10-10 01:25:53 +00:00
}
2007-08-04 20:05:54 +00:00
// Draw the text proper, with the right attributes
wxSize size = m_Size;
double orient = GetDrawRotation();
2007-08-04 20:05:54 +00:00
// If the text is mirrored : negate size.x (mirror / Y axis)
if( m_Mirror )
2007-08-04 20:05:54 +00:00
size.x = -size.x;
DrawGraphicText( aPanel->GetClipBox(), aDC, pos, color, GetShownText(), orient,
2009-11-12 15:43:38 +00:00
size, m_HJustify, m_VJustify, width, m_Italic, m_Bold );
// Enable these line to draw the bounding box (debug test purpose only)
#if 0
{
EDA_RECT BoundaryBox = GetBoundingBox();
GRRect( aPanel->GetClipBox(), aDC, BoundaryBox, 0, BROWN );
}
#endif
}
void TEXTE_MODULE::DrawUmbilical( EDA_DRAW_PANEL* aPanel,
wxDC* aDC,
GR_DRAWMODE aDrawMode,
const wxPoint& aOffset )
{
MODULE* parent = static_cast<MODULE*>( GetParent() );
2011-12-29 15:11:42 -05:00
if( !parent )
return;
GRSetDrawMode( aDC, GR_XOR );
2011-12-29 15:11:42 -05:00
GRLine( aPanel->GetClipBox(), aDC,
2013-03-18 15:36:07 -04:00
parent->GetPosition(), GetTextPosition() + aOffset,
0, UMBILICAL_COLOR);
}
double TEXTE_MODULE::GetDrawRotation() const
{
MODULE* module = (MODULE*) m_Parent;
double rotation = m_Orient;
if( module )
2013-03-13 14:53:58 -04:00
rotation += module->GetOrientation();
2007-08-04 20:05:54 +00:00
// Keep angle between -90 .. 90 deg. Otherwise the text is not easy to read
2007-08-04 20:05:54 +00:00
while( rotation > 900 )
rotation -= 1800;
while( rotation < -900 )
rotation += 1800;
2007-08-04 20:05:54 +00:00
return rotation;
}
2007-08-06 20:26:59 +00:00
2008-03-04 04:22:27 +00:00
// see class_text_mod.h
2013-01-12 12:32:24 -05:00
void TEXTE_MODULE::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList )
{
MODULE* module = (MODULE*) m_Parent;
2009-08-11 10:27:21 +00:00
if( module == NULL ) // Happens in modedit, and for new texts
return;
2009-08-11 10:27:21 +00:00
wxString msg, Line;
2008-03-04 04:22:27 +00:00
static const wxString text_type_msg[3] =
{
2008-08-09 08:05:42 +00:00
_( "Ref." ), _( "Value" ), _( "Text" )
};
2008-03-04 04:22:27 +00:00
2013-03-13 14:53:58 -04:00
Line = module->GetReference();
aList.push_back( MSG_PANEL_ITEM( _( "Footprint" ), Line, DARKCYAN ) );
Line = GetShownText();
2013-01-12 12:32:24 -05:00
aList.push_back( MSG_PANEL_ITEM( _( "Text" ), Line, BROWN ) );
2013-04-07 13:55:18 +02:00
wxASSERT( m_Type >= TEXT_is_REFERENCE && m_Type <= TEXT_is_DIVERS );
aList.push_back( MSG_PANEL_ITEM( _( "Type" ), text_type_msg[m_Type], DARKGREEN ) );
if( m_NoShow )
2008-08-09 08:05:42 +00:00
msg = _( "No" );
else
2008-08-09 08:05:42 +00:00
msg = _( "Yes" );
2013-01-12 12:32:24 -05:00
aList.push_back( MSG_PANEL_ITEM( _( "Display" ), msg, DARKGREEN ) );
2013-04-07 13:55:18 +02:00
// Display text layer
aList.push_back( MSG_PANEL_ITEM( _( "Layer" ), GetLayerName(), DARKGREEN ) );
2013-01-12 12:32:24 -05:00
if( m_Mirror )
msg = _( " Yes" );
2013-04-07 13:55:18 +02:00
else
msg = _( " No" );
2013-01-12 12:32:24 -05:00
aList.push_back( MSG_PANEL_ITEM( _( "Mirror" ), msg, DARKGREEN ) );
msg.Printf( wxT( "%.1f" ), GetOrientationDegrees() );
aList.push_back( MSG_PANEL_ITEM( _( "Angle" ), msg, DARKGREEN ) );
2013-01-12 12:32:24 -05:00
msg = ::CoordinateToString( m_Thickness );
aList.push_back( MSG_PANEL_ITEM( _( "Thickness" ), msg, DARKGREEN ) );
2013-01-12 12:32:24 -05:00
msg = ::CoordinateToString( m_Size.x );
aList.push_back( MSG_PANEL_ITEM( _( "Width" ), msg, RED ) );
2013-01-12 12:32:24 -05:00
msg = ::CoordinateToString( m_Size.y );
aList.push_back( MSG_PANEL_ITEM( _( "Height" ), msg, RED ) );
}
wxString TEXTE_MODULE::GetSelectMenuText() const
{
wxString text;
const wxChar *reference = GetChars( static_cast<MODULE*>( GetParent() )->GetReference() );
switch( m_Type )
{
case TEXT_is_REFERENCE:
text.Printf( _( "Reference %s" ), reference );
break;
case TEXT_is_VALUE:
text.Printf( _( "Value %s of %s" ), GetChars( GetShownText() ), reference );
break;
default: // wrap this one in quotes:
text.Printf( _( "Text \"%s\" on %s of %s" ), GetChars( ShortenedShownText() ),
GetChars( GetLayerName() ), reference );
break;
}
return text;
}
EDA_ITEM* TEXTE_MODULE::Clone() const
2012-01-14 14:50:32 -05:00
{
return new TEXTE_MODULE( *this );
}
2014-01-07 14:09:27 +01:00
const BOX2I TEXTE_MODULE::ViewBBox() const
{
double angle = GetDrawRotation();
EDA_RECT text_area = GetTextBox( -1, -1 );
if( angle )
text_area = text_area.GetBoundingBoxRotated( m_Pos, angle );
return BOX2I( text_area.GetPosition(), text_area.GetSize() );
}
void TEXTE_MODULE::ViewGetLayers( int aLayers[], int& aCount ) const
{
if( m_NoShow ) // Hidden text
aLayers[0] = ITEM_GAL_LAYER( MOD_TEXT_INVISIBLE );
//else if( IsFrontLayer( m_Layer ) )
//aLayers[0] = ITEM_GAL_LAYER( MOD_TEXT_FR_VISIBLE );
//else if( IsBackLayer( m_Layer ) )
//aLayers[0] = ITEM_GAL_LAYER( MOD_TEXT_BK_VISIBLE );
else
aLayers[0] = GetLayer();
aCount = 1;
}
2016-12-09 12:04:32 +01:00
unsigned int TEXTE_MODULE::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
{
const int MAX = std::numeric_limits<unsigned int>::max();
2016-12-02 18:58:12 +01:00
if( !aView )
return 0;
2016-12-02 18:58:12 +01:00
if( m_Type == TEXT_is_VALUE && !aView->IsLayerVisible( ITEM_GAL_LAYER( MOD_VALUES_VISIBLE ) ) )
return MAX;
2016-12-02 18:58:12 +01:00
if( m_Type == TEXT_is_REFERENCE && !aView->IsLayerVisible( ITEM_GAL_LAYER( MOD_REFERENCES_VISIBLE ) ) )
return MAX;
2016-12-02 18:58:12 +01:00
if( IsFrontLayer( m_Layer ) && ( !aView->IsLayerVisible( ITEM_GAL_LAYER( MOD_TEXT_FR_VISIBLE ) ) ||
!aView->IsLayerVisible( ITEM_GAL_LAYER( MOD_FR_VISIBLE ) ) ) )
return MAX;
2016-12-02 18:58:12 +01:00
if( IsBackLayer( m_Layer ) && ( !aView->IsLayerVisible( ITEM_GAL_LAYER( MOD_TEXT_BK_VISIBLE ) ) ||
!aView->IsLayerVisible( ITEM_GAL_LAYER( MOD_BK_VISIBLE ) ) ) )
return MAX;
return 0;
}
2014-10-04 17:15:38 +02:00
wxString TEXTE_MODULE::GetShownText() const
{
/* First order optimization: no % means that no processing is
* needed; just hope that RVO and copy constructor implementation
* avoid to copy the whole block; anyway it should be better than
* rebuild the string one character at a time...
* Also it seems wise to only expand macros in user text (but there
* is no technical reason, probably) */
if( (m_Type != TEXT_is_DIVERS) || (wxString::npos == m_Text.find('%')) )
return m_Text;
wxString newbuf;
2014-10-04 17:15:38 +02:00
const MODULE *module = static_cast<MODULE*>( GetParent() );
for( wxString::const_iterator it = m_Text.begin(); it != m_Text.end(); ++it )
2014-10-04 17:15:38 +02:00
{
// Process '%' and copy everything else
if( *it != '%' )
newbuf.append(*it);
else
{
/* Look at the next character (if is it there) and append
* its expansion */
++it;
2014-10-04 17:15:38 +02:00
if( it != m_Text.end() )
{
switch( char(*it) )
{
case '%':
newbuf.append( '%' );
break;
case 'R':
if( module )
newbuf.append( module->GetReference() );
break;
case 'V':
if( module )
newbuf.append( module->GetValue() );
break;
default:
newbuf.append( '?' );
break;
}
}
else
break; // The string is over and we can't ++ anymore
}
}
2014-10-04 17:15:38 +02:00
return newbuf;
}