2011-08-31 14:59:20 +00:00
|
|
|
/*
|
2011-09-30 18:15:37 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2011-08-31 14:59:20 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 jean-pierre.charras
|
2011-09-30 18:15:37 +00:00
|
|
|
* Copyright (C) 2011 KiCad Developers, see change_log.txt for contributors.
|
2011-08-31 14:59:20 +00: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-10-19 20:32:21 +00:00
|
|
|
/**
|
|
|
|
* @file sch_bitmap.cpp
|
|
|
|
*/
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <class_drawpanel.h>
|
|
|
|
#include <trigo.h>
|
2015-02-21 09:46:44 +00:00
|
|
|
#include <macros.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <sch_bitmap.h>
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
#include <wx/mstream.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* class SCH_BITMAP
|
|
|
|
* This class handle a bitmap image that can be inserted in a schematic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
SCH_BITMAP::SCH_BITMAP( const wxPoint& pos ) :
|
|
|
|
SCH_ITEM( NULL, SCH_BITMAP_T )
|
|
|
|
{
|
|
|
|
m_Pos = pos;
|
|
|
|
m_Layer = LAYER_NOTES; // used only to draw/plot a rectangle,
|
|
|
|
// when a bitmap cannot be drawn or plotted
|
|
|
|
m_Image = new BITMAP_BASE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCH_BITMAP::SCH_BITMAP( const SCH_BITMAP& aSchBitmap ) :
|
|
|
|
SCH_ITEM( aSchBitmap )
|
|
|
|
{
|
|
|
|
m_Pos = aSchBitmap.m_Pos;
|
|
|
|
m_Layer = aSchBitmap.m_Layer;
|
|
|
|
m_Image = new BITMAP_BASE( *aSchBitmap.m_Image );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-09 20:26:55 +00:00
|
|
|
SCH_ITEM& SCH_BITMAP::operator=( const SCH_ITEM& aItem )
|
|
|
|
{
|
|
|
|
wxCHECK_MSG( Type() == aItem.Type(), *this,
|
|
|
|
wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
|
|
|
|
GetClass() );
|
|
|
|
|
|
|
|
if( &aItem != this )
|
|
|
|
{
|
|
|
|
SCH_ITEM::operator=( aItem );
|
|
|
|
|
|
|
|
SCH_BITMAP* bitmap = (SCH_BITMAP*) &aItem;
|
|
|
|
|
|
|
|
delete m_Image;
|
|
|
|
m_Image = new BITMAP_BASE( *bitmap->m_Image );
|
|
|
|
m_Pos = bitmap->m_Pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
bool SCH_BITMAP::ReadImageFile( const wxString& aFullFilename )
|
|
|
|
{
|
|
|
|
return m_Image->ReadImageFile( aFullFilename );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCH_BITMAP::Save( FILE* aFile ) const
|
|
|
|
{
|
|
|
|
if( fprintf( aFile, "$Bitmap\n" ) == EOF )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( fprintf( aFile, "Pos %-4d %-4d\n", m_Pos.x, m_Pos.y ) == EOF )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( fprintf( aFile, "Scale %f\n", m_Image->m_Scale ) == EOF )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( fprintf( aFile, "Data\n" ) == EOF )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !m_Image->SaveData( aFile ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( fprintf( aFile, "\nEndData\n" ) == EOF )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
if( fprintf( aFile, "$EndBitmap\n" ) == EOF )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 14:39:27 +00:00
|
|
|
EDA_ITEM* SCH_BITMAP::Clone() const
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
return new SCH_BITMAP( *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCH_BITMAP::SwapData( SCH_ITEM* aItem )
|
|
|
|
{
|
|
|
|
wxCHECK_RET( aItem->Type() == SCH_BITMAP_T,
|
2011-10-19 20:32:21 +00:00
|
|
|
wxString::Format( wxT( "SCH_BITMAP object cannot swap data with %s object." ),
|
|
|
|
GetChars( aItem->GetClass() ) ) );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
SCH_BITMAP* item = (SCH_BITMAP*) aItem;
|
2015-06-26 13:41:56 +00:00
|
|
|
std::swap( m_Pos, item->m_Pos );
|
|
|
|
std::swap( m_Image, item->m_Image );
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCH_BITMAP::Load( LINE_READER& aLine, wxString& aErrorMsg )
|
|
|
|
{
|
|
|
|
char* line = aLine.Line();
|
|
|
|
|
|
|
|
if( strnicmp( line, "$Bitmap", 7 ) != 0 )
|
|
|
|
{
|
2011-09-30 18:15:37 +00:00
|
|
|
aErrorMsg.Printf( wxT( "Eeschema file bitmap image load error at line %d, aborted" ),
|
2011-10-19 20:32:21 +00:00
|
|
|
aLine.LineNumber() );
|
2011-08-31 14:59:20 +00:00
|
|
|
aErrorMsg << wxT( "\n" ) << FROM_UTF8( (char*) aLine );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
if( !aLine.ReadLine() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
line = aLine.Line();
|
|
|
|
|
|
|
|
if( strnicmp( line, "Pos", 3 ) == 0 )
|
|
|
|
{
|
|
|
|
sscanf( line + 3, "%d %d", &m_Pos.x, &m_Pos.y );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( strnicmp( line, "Scale", 5 ) == 0 )
|
|
|
|
{
|
|
|
|
sscanf( line + 5, "%lf", &m_Image->m_Scale );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( strnicmp( line, "Data", 4 ) == 0 )
|
|
|
|
{
|
|
|
|
m_Image->LoadData( aLine, aErrorMsg );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( strnicmp( line, "$EndBitmap", 4 ) == 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-24 17:48:14 +00:00
|
|
|
const EDA_RECT SCH_BITMAP::GetBoundingBox() const
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
EDA_RECT rect = m_Image->GetBoundingBox();
|
|
|
|
|
|
|
|
rect.Move( m_Pos );
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCH_BITMAP::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
|
2012-09-02 12:06:47 +00:00
|
|
|
GR_DRAWMODE aDrawMode, EDA_COLOR_T aColor )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
wxPoint pos = m_Pos + aOffset;
|
|
|
|
|
|
|
|
if( aColor < 0 ) // Use normal drawing function
|
|
|
|
{
|
2016-05-17 21:56:20 +00:00
|
|
|
// https://bugs.launchpad.net/kicad/+bug/1529163
|
|
|
|
// "Moving images in eeschema on OS X uses
|
|
|
|
// wrong position and shows image flipped"
|
|
|
|
//
|
|
|
|
// Original fix was to only GRSetDrawMode if aColor >= 0, but this made
|
|
|
|
// moving SCH_BITMAP work poorly on other platforms.
|
|
|
|
#ifndef __WXMAC__
|
|
|
|
GRSetDrawMode( aDC, aDrawMode );
|
|
|
|
#endif
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
m_Image->DrawBitmap( aPanel, aDC, pos );
|
|
|
|
}
|
|
|
|
else // draws bounding box only (used to move items)
|
|
|
|
{
|
2016-05-17 21:56:20 +00:00
|
|
|
GRSetDrawMode( aDC, aDrawMode );
|
2011-08-31 14:59:20 +00:00
|
|
|
// To draw the rect, pos is the upper left corner position
|
|
|
|
wxSize size = m_Image->GetSize();
|
|
|
|
pos.x -= size.x / 2;
|
|
|
|
pos.y -= size.y / 2;
|
2011-12-29 20:11:42 +00:00
|
|
|
GRRect( aPanel->GetClipBox(), aDC, pos.x, pos.y,
|
2011-08-31 14:59:20 +00:00
|
|
|
pos.x + size.x, pos.y + size.y, 0, aColor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Function GetSize
|
|
|
|
* returns the actual size (in user units, not in pixels) of the image
|
|
|
|
*/
|
|
|
|
wxSize SCH_BITMAP::GetSize() const
|
|
|
|
{
|
|
|
|
return m_Image->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mirror image relative to a horizontal X axis )
|
|
|
|
*/
|
2012-03-15 14:31:16 +00:00
|
|
|
void SCH_BITMAP::MirrorX( int aXaxis_position )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2015-06-26 13:41:56 +00:00
|
|
|
MIRROR( m_Pos.y, aXaxis_position );
|
2011-08-31 14:59:20 +00:00
|
|
|
m_Image->Mirror( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mirror image relative to a vertical Y axis
|
|
|
|
*/
|
2012-03-15 14:31:16 +00:00
|
|
|
void SCH_BITMAP::MirrorY( int aYaxis_position )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2015-06-26 13:41:56 +00:00
|
|
|
MIRROR( m_Pos.x, aYaxis_position );
|
2011-08-31 14:59:20 +00:00
|
|
|
m_Image->Mirror( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-15 14:31:16 +00:00
|
|
|
void SCH_BITMAP::Rotate( wxPoint aPosition )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2012-03-15 14:31:16 +00:00
|
|
|
RotatePoint( &m_Pos, aPosition, 900 );
|
2011-08-31 14:59:20 +00:00
|
|
|
m_Image->Rotate( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCH_BITMAP::IsSelectStateChanged( const wxRect& aRect )
|
|
|
|
{
|
|
|
|
bool previousState = IsSelected();
|
|
|
|
|
|
|
|
if( aRect.Contains( m_Pos ) )
|
2013-03-30 19:55:26 +00:00
|
|
|
SetFlags( SELECTED );
|
2011-08-31 14:59:20 +00:00
|
|
|
else
|
2013-03-30 19:55:26 +00:00
|
|
|
ClearFlags( SELECTED );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
return previousState != IsSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
2011-12-14 17:25:42 +00:00
|
|
|
void SCH_BITMAP::Show( int nestLevel, std::ostream& os ) const
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
// XML output:
|
|
|
|
wxString s = GetClass();
|
|
|
|
|
|
|
|
NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_Pos << "/>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-03-15 14:31:16 +00:00
|
|
|
bool SCH_BITMAP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
EDA_RECT rect = GetBoundingBox();
|
|
|
|
|
|
|
|
rect.Inflate( aAccuracy );
|
|
|
|
|
2012-03-15 14:31:16 +00:00
|
|
|
return rect.Contains( aPosition );
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-15 14:31:16 +00:00
|
|
|
bool SCH_BITMAP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
EDA_RECT rect = aRect;
|
|
|
|
|
|
|
|
rect.Inflate( aAccuracy );
|
|
|
|
|
|
|
|
if( aContained )
|
|
|
|
return rect.Contains( GetBoundingBox() );
|
|
|
|
|
|
|
|
return rect.Intersects( GetBoundingBox() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-15 14:31:16 +00:00
|
|
|
void SCH_BITMAP::Plot( PLOTTER* aPlotter )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2013-04-04 21:35:01 +00:00
|
|
|
m_Image->PlotImage( aPlotter, m_Pos, GetLayerColor( GetLayer() ), GetPenSize() );
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|