KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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
51}
52
53
54SCH_BITMAP::SCH_BITMAP( const SCH_BITMAP& aSchBitmap ) :
55 SCH_ITEM( aSchBitmap )
56{
57 m_pos = aSchBitmap.m_pos;
58 m_layer = aSchBitmap.m_layer;
59 m_bitmapBase = new BITMAP_BASE( *aSchBitmap.m_bitmapBase );
60}
61
62
64{
65 wxCHECK_MSG( Type() == aItem.Type(), *this,
66 wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
67 GetClass() );
68
69 if( &aItem != this )
70 {
71 SCH_ITEM::operator=( aItem );
72
73 SCH_BITMAP* bitmap = (SCH_BITMAP*) &aItem;
74
75 delete m_bitmapBase;
76 m_bitmapBase = new BITMAP_BASE( *bitmap->m_bitmapBase );
77 m_pos = bitmap->m_pos;
78 }
79
80 return *this;
81}
82
83
84bool SCH_BITMAP::ReadImageFile( const wxString& aFullFilename )
85{
86 if( m_bitmapBase->ReadImageFile( aFullFilename ) )
87 {
89 return true;
90 }
91
92 return false;
93}
94
95
96bool SCH_BITMAP::ReadImageFile( wxMemoryBuffer& aBuffer )
97{
98 if( m_bitmapBase->ReadImageFile( aBuffer ) )
99 {
101 return true;
102 }
103
104 return false;
105}
106
107
109{
110 return new SCH_BITMAP( *this );
111}
112
113
115{
116 SCH_ITEM::SwapFlags( aItem );
117
118 wxCHECK_RET( aItem->Type() == SCH_BITMAP_T,
119 wxString::Format( wxT( "SCH_BITMAP object cannot swap data with %s object." ),
120 aItem->GetClass() ) );
121
122 SCH_BITMAP* item = (SCH_BITMAP*) aItem;
123 std::swap( m_pos, item->m_pos );
124 std::swap( m_bitmapBase, item->m_bitmapBase );
125}
126
127
129{
131 bbox.Move( m_pos );
132
133 return bbox;
134}
135
136
137void SCH_BITMAP::Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
138 const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed )
139{
140 VECTOR2I pos = m_pos + aOffset;
141
142 m_bitmapBase->DrawBitmap( aSettings->GetPrintDC(), pos, aSettings->GetBackgroundColor() );
143}
144
145
147{
148 return m_bitmapBase->GetSize();
149}
150
151
153{
154 MIRROR( m_pos.y, aCenter );
155 m_bitmapBase->Mirror( true );
156}
157
158
160{
161 MIRROR( m_pos.x, aCenter );
162 m_bitmapBase->Mirror( false );
163}
164
165
166void SCH_BITMAP::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
167{
168 RotatePoint( m_pos, aCenter, aRotateCCW ? ANGLE_270 : ANGLE_90 );
169 m_bitmapBase->Rotate( aRotateCCW );
170}
171
172
173#if defined(DEBUG)
174void SCH_BITMAP::Show( int nestLevel, std::ostream& os ) const
175{
176 // XML output:
177 wxString s = GetClass();
178
179 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << "/>\n";
180}
181#endif
182
183
184bool SCH_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
185{
186 BOX2I rect = GetBoundingBox();
187
188 rect.Inflate( aAccuracy );
189
190 return rect.Contains( aPosition );
191}
192
193
194bool SCH_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
195{
196 BOX2I rect = aRect;
197
198 rect.Inflate( aAccuracy );
199
200 if( aContained )
201 return rect.Contains( GetBoundingBox() );
202
203 return rect.Intersects( GetBoundingBox() );
204}
205
206
207void SCH_BITMAP::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
208 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
209{
210 if( aBackground )
211 {
212 m_bitmapBase->PlotImage( aPlotter, m_pos,
213 aPlotter->RenderSettings()->GetLayerColor( GetLayer() ),
214 aPlotter->RenderSettings()->GetDefaultPenWidth() );
215 }
216}
217
218
220{
221 return BITMAPS::image;
222}
223
224
225void SCH_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
226{
227 aList.emplace_back( _( "Bitmap" ), wxEmptyString );
228
229 aList.emplace_back( _( "PPI" ), wxString::Format( wxT( "%d "), GetImage()->GetPPI() ) );
230 aList.emplace_back( _( "Scale" ), wxString::Format( wxT( "%f "), GetImageScale() ) );
231
232 aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetSize().x ) );
233 aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetSize().y ) );
234}
235
236
237void SCH_BITMAP::ViewGetLayers( int aLayers[], int& aCount ) const
238{
239 aCount = 2;
240 aLayers[0] = LAYER_DRAW_BITMAPS;
241 aLayers[1] = LAYER_SELECTION_SHADOWS;
242}
243
244
245static struct SCH_BITMAP_DESC
246{
248 {
252 }
254
255
256bool SCH_BITMAP::operator==( const SCH_ITEM& aItem ) const
257{
258 if( Type() != aItem.Type() )
259 return false;
260
261 const SCH_BITMAP* bitmap = static_cast<const SCH_BITMAP*>( &aItem );
262
263 if( GetPosition() != bitmap->GetPosition() )
264 return false;
265
266 if( GetSize() != bitmap->GetSize() )
267 return false;
268
269 if( GetImage() != bitmap->GetImage() )
270 return false;
271
272 return true;
273}
274
275
276double SCH_BITMAP::Similarity( const SCH_ITEM& aItem ) const
277{
278 if( Type() != aItem.Type() )
279 return 0.0;
280
281 if( m_Uuid == aItem.m_Uuid )
282 return 1.0;
283
284 const SCH_BITMAP* bitmap = static_cast<const SCH_BITMAP*>( &aItem );
285
286 if( GetImage() != bitmap->GetImage() )
287 return 0.0;
288
289 // If it is the same image but a different UUID and a different size,
290 // then it _might be different_.
291 if( GetSize() != bitmap->GetSize() )
292 return 0.5;
293
294 return 1.0;
295}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:48
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 DrawBitmap(wxDC *aDC, const VECTOR2I &aPos, const KIGFX::COLOR4D &aBackgroundColor=KIGFX::COLOR4D::UNSPECIFIED)
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:65
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
int GetPPI() const
Definition: bitmap_base.h:117
VECTOR2I GetSize() const
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:270
void Move(const Vec &aMoveVector)
Move the rectangle by the aMoveVector.
Definition: box2.h:112
bool Contains(const Vec &aPoint) const
Definition: box2.h:142
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:507
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:88
const KIID m_Uuid
Definition: eda_item.h:485
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
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:104
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:135
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:41
void Rotate(const VECTOR2I &aCenter, bool aRotateCCW) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
Definition: sch_bitmap.cpp:166
double GetImageScale() const
Definition: sch_bitmap.h:67
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: sch_bitmap.cpp:108
SCH_BITMAP(const VECTOR2I &pos=VECTOR2I(0, 0))
Definition: sch_bitmap.cpp:43
VECTOR2I GetPosition() const override
Definition: sch_bitmap.h:145
void Plot(PLOTTER *aPlotter, bool aBackground, const SCH_PLOT_OPTS &aPlotOpts, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aDimmed) override
Plot the item to aPlotter.
Definition: sch_bitmap.cpp:207
VECTOR2I m_pos
Definition: sch_bitmap.h:168
BITMAP_BASE * GetImage() const
Definition: sch_bitmap.h:54
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: sch_bitmap.cpp:237
BITMAP_BASE * m_bitmapBase
Definition: sch_bitmap.h:169
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: sch_bitmap.cpp:128
wxString GetClass() const override
Return the class name.
Definition: sch_bitmap.h:82
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
Definition: sch_bitmap.cpp:152
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:225
VECTOR2I GetSize() const
Definition: sch_bitmap.cpp:146
double Similarity(const SCH_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
Definition: sch_bitmap.cpp:276
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: sch_bitmap.cpp:219
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:184
SCH_BITMAP & operator=(const SCH_ITEM &aItem)
Definition: sch_bitmap.cpp:63
void SwapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
Definition: sch_bitmap.cpp:114
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
Definition: sch_bitmap.cpp:84
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
Definition: sch_bitmap.cpp:159
bool operator==(const SCH_ITEM &aOther) const override
Definition: sch_bitmap.cpp:256
void Print(const SCH_RENDER_SETTINGS *aSettings, int aUnit, int aBodyStyle, const VECTOR2I &aOffset, bool aForceNoFill, bool aDimmed) override
Print an item.
Definition: sch_bitmap.cpp:137
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:172
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition: sch_item.cpp:100
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:287
void SwapFlags(SCH_ITEM *aItem)
Swap the non-temp and non-edit flags.
Definition: sch_item.cpp:343
wxString GetClass() const override
Return the class name.
Definition: sch_item.h:182
SCH_LAYER_ID m_layer
Definition: sch_item.h:708
const KIGFX::COLOR4D & GetBackgroundColor() const override
Return current background color settings.
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
The common library.
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:437
static constexpr EDA_ANGLE ANGLE_270
Definition: eda_angle.h:440
@ LAYER_DRAW_BITMAPS
to handle and draw images bitmaps
Definition: layer_ids.h:227
@ LAYER_NOTES
Definition: layer_ids.h:370
@ LAYER_SELECTION_SHADOWS
Definition: layer_ids.h:394
void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition: mirror.h:40
#define TYPE_HASH(x)
Definition: property.h:71
#define REGISTER_TYPE(x)
Definition: property_mgr.h:366
static struct SCH_BITMAP_DESC _SCH_BITMAP_DESC
constexpr int MilsToIU(int mils) const
Definition: base_units.h:93
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:228
@ SCH_BITMAP_T
Definition: typeinfo.h:164