KiCad PCB EDA Suite
sch_bitmap.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) 2011 jean-pierre.charras
5 * Copyright (C) 2011-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
29#include <sch_draw_panel.h>
30#include <plotters/plotter.h>
32#include <bitmaps.h>
33#include <base_units.h>
34#include <common.h>
35#include <eda_draw_frame.h>
36#include <core/mirror.h>
37#include <sch_bitmap.h>
38#include <trigo.h>
39
40#include <wx/mstream.h>
41
42
44 SCH_ITEM( nullptr, SCH_BITMAP_T )
45{
46 m_pos = pos;
47 m_layer = LAYER_NOTES; // used only to draw/plot a rectangle,
48 // when a bitmap cannot be drawn or plotted
49 m_image = new BITMAP_BASE();
50}
51
52
53SCH_BITMAP::SCH_BITMAP( const SCH_BITMAP& aSchBitmap ) :
54 SCH_ITEM( aSchBitmap )
55{
56 m_pos = aSchBitmap.m_pos;
57 m_layer = aSchBitmap.m_layer;
58 m_image = new BITMAP_BASE( *aSchBitmap.m_image );
59}
60
61
63{
64 wxCHECK_MSG( Type() == aItem.Type(), *this,
65 wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
66 GetClass() );
67
68 if( &aItem != this )
69 {
70 SCH_ITEM::operator=( aItem );
71
72 SCH_BITMAP* bitmap = (SCH_BITMAP*) &aItem;
73
74 delete m_image;
75 m_image = new BITMAP_BASE( *bitmap->m_image );
76 m_pos = bitmap->m_pos;
77 }
78
79 return *this;
80}
81
82
83bool SCH_BITMAP::ReadImageFile( const wxString& aFullFilename )
84{
85 if( m_image->ReadImageFile( aFullFilename ) )
86 {
87 m_image->SetPixelSizeIu( 254000.0 / m_image->GetPPI() );
88 return true;
89 }
90
91 return false;
92}
93
94
95void SCH_BITMAP::SetImage( wxImage* aImage )
96{
97 m_image->SetImage( aImage );
98 m_image->SetPixelSizeIu( 254000.0 / m_image->GetPPI() );
99}
100
101
103{
104 return new SCH_BITMAP( *this );
105}
106
107
109{
110 wxCHECK_RET( aItem->Type() == SCH_BITMAP_T,
111 wxString::Format( wxT( "SCH_BITMAP object cannot swap data with %s object." ),
112 aItem->GetClass() ) );
113
114 SCH_BITMAP* item = (SCH_BITMAP*) aItem;
115 std::swap( m_pos, item->m_pos );
116 std::swap( m_image, item->m_image );
117}
118
119
121{
122 BOX2I bbox = m_image->GetBoundingBox();
123
124 bbox.Move( m_pos );
125
126 return bbox;
127}
128
129
130void SCH_BITMAP::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset )
131{
132 VECTOR2I pos = m_pos + aOffset;
133
134 m_image->DrawBitmap( aSettings->GetPrintDC(), pos );
135}
136
137
139{
140 return m_image->GetSize();
141}
142
143
145{
146 MIRROR( m_pos.y, aCenter );
147 m_image->Mirror( true );
148}
149
150
152{
153 MIRROR( m_pos.x, aCenter );
154 m_image->Mirror( false );
155}
156
157
158void SCH_BITMAP::Rotate( const VECTOR2I& aCenter )
159{
160 RotatePoint( m_pos, aCenter, ANGLE_90 );
161 m_image->Rotate( false );
162}
163
164
165#if defined(DEBUG)
166void SCH_BITMAP::Show( int nestLevel, std::ostream& os ) const
167{
168 // XML output:
169 wxString s = GetClass();
170
171 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << "/>\n";
172}
173#endif
174
175
176bool SCH_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
177{
178 BOX2I rect = GetBoundingBox();
179
180 rect.Inflate( aAccuracy );
181
182 return rect.Contains( aPosition );
183}
184
185
186bool SCH_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
187{
188 BOX2I rect = aRect;
189
190 rect.Inflate( aAccuracy );
191
192 if( aContained )
193 return rect.Contains( GetBoundingBox() );
194
195 return rect.Intersects( GetBoundingBox() );
196}
197
198
199void SCH_BITMAP::Plot( PLOTTER* aPlotter, bool aBackground ) const
200{
201 if( aBackground )
202 {
203 m_image->PlotImage( aPlotter, m_pos,
204 aPlotter->RenderSettings()->GetLayerColor( GetLayer() ),
205 aPlotter->RenderSettings()->GetDefaultPenWidth() );
206 }
207}
208
209
211{
212 return BITMAPS::image;
213}
214
215
216void SCH_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
217{
218 aList.emplace_back( _( "Bitmap" ), wxEmptyString );
219
220 aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetSize().x ) );
221 aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetSize().y ) );
222}
223
224
225void SCH_BITMAP::ViewGetLayers( int aLayers[], int& aCount ) const
226{
227 aCount = 2;
228 aLayers[0] = LAYER_DRAW_BITMAPS;
229 aLayers[1] = LAYER_SELECTION_SHADOWS;
230}
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:52
void Rotate(bool aRotateCCW)
Rotate image CW or CCW.
void PlotImage(PLOTTER *aPlotter, const VECTOR2I &aPos, const KIGFX::COLOR4D &aDefaultColor, int aDefaultPensize) const
Plot bitmap on plotter.
const BOX2I GetBoundingBox() const
Return the orthogonal, bounding box of this object for display purposes.
void Mirror(bool aVertically)
Mirror image vertically (i.e.
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:69
void DrawBitmap(wxDC *aDC, const VECTOR2I &aPos)
void SetImage(wxImage *aImage)
Definition: bitmap_base.cpp:71
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
int GetPPI() const
Definition: bitmap_base.h:123
VECTOR2I GetSize() const
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:269
void Move(const Vec &aMoveVector)
Move the rectangle by the aMoveVector.
Definition: box2.h:111
bool Contains(const Vec &aPoint) const
Definition: box2.h:141
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
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:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
int GetDefaultPenWidth() const
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
wxDC * GetPrintDC() const
Base plotter engine class.
Definition: plotter.h:110
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:141
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:41
void Print(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset) override
Print a schematic item.
Definition: sch_bitmap.cpp:130
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: sch_bitmap.cpp:102
SCH_BITMAP(const VECTOR2I &pos=VECTOR2I(0, 0))
Definition: sch_bitmap.cpp:43
void Rotate(const VECTOR2I &aCenter) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
Definition: sch_bitmap.cpp:158
VECTOR2I m_pos
Definition: sch_bitmap.h:153
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: sch_bitmap.cpp:225
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_bitmap.cpp:120
wxString GetClass() const override
Return the class name.
Definition: sch_bitmap.h:83
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
Definition: sch_bitmap.cpp:144
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_bitmap.cpp:216
void SetImage(wxImage *aImage)
Definition: sch_bitmap.cpp:95
VECTOR2I GetSize() const
Definition: sch_bitmap.cpp:138
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: sch_bitmap.cpp:210
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: sch_bitmap.cpp:176
BITMAP_BASE * m_image
Definition: sch_bitmap.h:154
SCH_BITMAP & operator=(const SCH_ITEM &aItem)
Definition: sch_bitmap.cpp:62
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
Definition: sch_bitmap.cpp:108
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
Definition: sch_bitmap.cpp:83
void Plot(PLOTTER *aPlotter, bool aBackground) const override
Plot the schematic item to aPlotter.
Definition: sch_bitmap.cpp:199
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
Definition: sch_bitmap.cpp:151
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition: sch_item.cpp:70
virtual wxString GetClass() const override
Return the class name.
Definition: sch_item.h:157
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:246
SCH_LAYER_ID m_layer
Definition: sch_item.h:491
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
The common library.
#define _(s)
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:431
@ LAYER_DRAW_BITMAPS
to handle and draw images bitmaps
Definition: layer_ids.h:223
@ LAYER_NOTES
Definition: layer_ids.h:358
@ LAYER_SELECTION_SHADOWS
Definition: layer_ids.h:381
void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition: mirror.h:40
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
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
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
@ SCH_BITMAP_T
Definition: typeinfo.h:148