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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
24#include "sch_bitmap.h"
25
26#include <bitmap_base.h>
27#include <bitmaps.h>
28#include <base_units.h>
29#include <common.h>
30#include <core/mirror.h>
31#include <eda_draw_frame.h>
33#include <plotters/plotter.h>
34#include <sch_draw_panel.h>
36#include <api/api_utils.h>
37#include <api/schematic/schematic_types.pb.h>
38
39#include <properties/property.h>
41
42
44 SCH_ITEM( nullptr, SCH_BITMAP_T ),
46{
47 m_referenceImage.SetPosition( pos );
48 m_layer = LAYER_NOTES; // used only to draw/plot a rectangle,
49 // when a bitmap cannot be drawn or plotted
50}
51
52
53SCH_BITMAP::SCH_BITMAP( const SCH_BITMAP& aSchBitmap ) :
54 SCH_ITEM( aSchBitmap ),
56{
57}
58
59
61{
62 wxCHECK_MSG( Type() == aItem.Type(), *this,
63 wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
64 GetClass() );
65
66 if( &aItem != this )
67 {
68 SCH_ITEM::operator=( aItem );
69
70 const SCH_BITMAP& bitmap = static_cast<const SCH_BITMAP&>( aItem );
72 }
73
74 return *this;
75}
76
77
79{
80 return new SCH_BITMAP( *this );
81}
82
83
85{
86 wxCHECK_RET( aItem->Type() == SCH_BITMAP_T,
87 wxString::Format( wxT( "SCH_BITMAP object cannot swap data with %s object." ),
88 aItem->GetClass() ) );
89
90 SCH_BITMAP* item = (SCH_BITMAP*) aItem;
91 m_referenceImage.SwapData( item->m_referenceImage );
92}
93
94
96{
97 return m_referenceImage.GetBoundingBox();
98}
99
100
102{
103 return m_referenceImage.GetPosition();
104}
105
106
107void SCH_BITMAP::SetPosition( const VECTOR2I& aPosition )
108{
109 m_referenceImage.SetPosition( aPosition );
110}
111
112
113void SCH_BITMAP::Move( const VECTOR2I& aMoveVector )
114{
115 SetPosition( GetPosition() + aMoveVector );
116}
117
118
119void SCH_BITMAP::Serialize( google::protobuf::Any& aContainer ) const
120{
121 using namespace kiapi::common;
122
123 kiapi::schematic::types::SchematicImage image;
124
125 image.mutable_id()->set_value( m_Uuid.AsStdString() );
126 PackVector2( *image.mutable_position(), m_referenceImage.GetPosition(), schIUScale );
127 PackVector2( *image.mutable_transform_origin_offset(), m_referenceImage.GetTransformOriginOffset(), schIUScale );
128
129 image.mutable_image_scale()->set_value( m_referenceImage.GetImageScale() );
130 image.set_locked( IsLocked() ? types::LockedState::LS_LOCKED : types::LockedState::LS_UNLOCKED );
131
132 m_referenceImage.PackToBytes( *image.mutable_image_data() );
133
134 kiapi::common::PackCustomProperties( image.mutable_custom_properties(), *this );
135
136 image.mutable_orientation()->set_value_degrees( m_referenceImage.GetOrientation().AsDegrees() );
137 image.set_opacity( m_referenceImage.GetOpacity() );
138
139 if( !aContainer.PackFrom( image ) )
140 wxFAIL_MSG( wxT( "Failed to pack SCH_BITMAP" ) );
141}
142
143
144bool SCH_BITMAP::Deserialize( const google::protobuf::Any& aContainer )
145{
146 using namespace kiapi::common;
147
148 kiapi::schematic::types::SchematicImage image;
149
150 if( !aContainer.UnpackTo( &image ) )
151 return false;
152
153 const_cast<KIID&>( m_Uuid ) = KIID( image.id().value() );
154
155 if( !image.image_data().empty() )
156 {
157 if( !m_referenceImage.UnpackFromBytes( image.image_data() ) )
158 return false;
159 }
160
161 if( image.has_image_scale() )
162 m_referenceImage.SetImageScale( image.image_scale().value() );
163
164 SetPosition( UnpackVector2( image.position(), schIUScale ) );
165 m_referenceImage.SetTransformOriginOffset( UnpackVector2( image.transform_origin_offset(), schIUScale ) );
166
167 SetLocked( image.locked() == types::LockedState::LS_LOCKED );
168 kiapi::common::UnpackCustomProperties( image.custom_properties(), *this );
169
170 m_referenceImage.SetOrientation( EDA_ANGLE( image.orientation().value_degrees(), DEGREES_T ) );
171 m_referenceImage.SetOpacity( image.opacity() );
172
173 return true;
174}
175
176
178{
180}
181
182
184{
186}
187
188
189void SCH_BITMAP::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
190{
191 m_referenceImage.Rotate( aCenter, aRotateCCW ? ANGLE_90 : ANGLE_270 );
192}
193
194
195#if defined(DEBUG)
196void SCH_BITMAP::Show( int nestLevel, std::ostream& os ) const
197{
198 // XML output:
199 wxString s = GetClass();
200
201 NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << GetPosition() << "/>\n";
202}
203#endif
204
205
206bool SCH_BITMAP::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
207{
208 return KIGEOM::BoxHitTest( aPosition, GetBoundingBox(), aAccuracy );
209}
210
211
212bool SCH_BITMAP::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
213{
214 return KIGEOM::BoxHitTest( aRect, GetBoundingBox(), aContained, aAccuracy );
215}
216
217
218bool SCH_BITMAP::HitTest( const SHAPE_LINE_CHAIN& aPoly, bool aContained ) const
219{
220 return KIGEOM::BoxHitTest( aPoly, GetBoundingBox(), aContained );
221}
222
223
224
225void SCH_BITMAP::Plot( PLOTTER* aPlotter, bool aBackground, const SCH_PLOT_OPTS& aPlotOpts,
226 int aUnit, int aBodyStyle, const VECTOR2I& aOffset, bool aDimmed )
227{
228 if( aBackground )
229 {
230 const RENDER_SETTINGS& cfg = *aPlotter->RenderSettings();
231
232 m_referenceImage.PlotImage( aPlotter, GetPosition(), cfg.GetLayerColor( GetLayer() ),
233 cfg.GetDefaultPenWidth() );
234 }
235}
236
237
239{
240 return BITMAPS::image;
241}
242
243
244void SCH_BITMAP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
245{
246 aList.emplace_back( _( "Bitmap" ), wxEmptyString );
247
248 aList.emplace_back( _( "PPI" ),
249 wxString::Format( wxT( "%d " ), m_referenceImage.GetImage().GetPPI() ) );
250 aList.emplace_back( _( "Scale" ),
251 wxString::Format( wxT( "%f " ), m_referenceImage.GetImageScale() ) );
252
253 aList.emplace_back( _( "Width" ),
254 aFrame->MessageTextFromValue( m_referenceImage.GetSize().x ) );
255 aList.emplace_back( _( "Height" ),
256 aFrame->MessageTextFromValue( m_referenceImage.GetSize().y ) );
257}
258
259
260std::vector<int> SCH_BITMAP::ViewGetLayers() const
261{
263}
264
265
266bool SCH_BITMAP::operator==( const SCH_ITEM& aItem ) const
267{
268 if( Type() != aItem.Type() )
269 return false;
270
271 const SCH_BITMAP& bitmap = static_cast<const SCH_BITMAP&>( aItem );
272 return m_referenceImage == bitmap.m_referenceImage;
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 return m_referenceImage.Similarity( bitmap.m_referenceImage );
286}
287
288
290{
291 return m_referenceImage.GetTransformOriginOffset().x;
292}
293
294
296{
297 VECTOR2I offset = m_referenceImage.GetTransformOriginOffset();
298 offset.x = aX;
299 m_referenceImage.SetTransformOriginOffset( offset );
300}
301
302
304{
305 return m_referenceImage.GetTransformOriginOffset().y;
306}
307
308
310{
311 VECTOR2I offset = m_referenceImage.GetTransformOriginOffset();
312 offset.y = aY;
313 m_referenceImage.SetTransformOriginOffset( offset );
314}
315
316
318{
319 return m_referenceImage.GetImageScale();
320}
321
322
323void SCH_BITMAP::SetImageScale( double aScale )
324{
325 m_referenceImage.SetImageScale( aScale );
326}
327
328
330{
331 return m_referenceImage.GetImage().GetSize().x;
332}
333
334
335void SCH_BITMAP::SetWidth( int aWidth )
336{
337 m_referenceImage.SetWidth( aWidth );
338}
339
340
342{
343 return m_referenceImage.GetImage().GetSize().y;
344}
345
346
347void SCH_BITMAP::SetHeight( int aHeight )
348{
349 m_referenceImage.SetHeight( aHeight );
350}
351
352
354{
355 return m_referenceImage.GetOrientation();
356}
357
358
360{
361 m_referenceImage.SetOrientation( aAngle );
362}
363
364
366{
367 return m_referenceImage.GetOpacity();
368}
369
370
371void SCH_BITMAP::SetOpacity( double aOpacity )
372{
373 m_referenceImage.SetOpacity( aOpacity );
374}
375
376
377static struct SCH_BITMAP_DESC
378{
380 {
384
385 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Position X" ),
387
388
389 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Position Y" ),
391
392 const wxString groupImage = _HKI( "Image Properties" );
393
394 // clang-format off
395 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, double>( _HKI( "Scale" ),
397 groupImage );
398
399 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Transform Offset X" ),
402 groupImage );
403
404 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Transform Offset Y" ),
407 groupImage );
408
409 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, EDA_ANGLE>( _HKI( "Orientation" ),
412 groupImage );
413
414 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, double>( _HKI( "Opacity" ),
416 groupImage );
417
418 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Width" ),
420 groupImage );
421
422 propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( _HKI( "Height" ),
424 groupImage );
425 // clang-format on
426 }
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
BITMAPS
A list of all bitmap identifiers.
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
The base class for create windows for drawing purpose.
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:84
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
Definition kiid.h:46
Base plotter engine class.
Definition plotter.h:138
RENDER_SETTINGS * RenderSettings()
Definition plotter.h:169
Provide class metadata.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
Object to handle a bitmap image that can be inserted in a schematic.
Definition sch_bitmap.h:36
EDA_ANGLE GetOrientation() const
void Rotate(const VECTOR2I &aCenter, bool aRotateCCW) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
double GetImageScale() const
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
SCH_BITMAP(const VECTOR2I &pos=VECTOR2I(0, 0))
void SetWidth(int aWidth)
void SetOpacity(double aOpacity)
VECTOR2I GetPosition() const override
int GetTransformOriginOffsetY() const
void SetHeight(int aHeight)
virtual std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
int GetWidth() const
int GetY() const
Definition sch_bitmap.h:56
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.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
wxString GetClass() const override
Return the class name.
Definition sch_bitmap.h:64
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
double GetOpacity() const
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.
void swapData(SCH_ITEM *aItem) override
Swap the internal data structures aItem with the schematic item.
double Similarity(const SCH_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
int GetHeight() const
void SetY(int aY)
Definition sch_bitmap.h:57
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
int GetX() const
Definition sch_bitmap.h:53
void SetTransformOriginOffsetX(int aX)
void SetOrientation(EDA_ANGLE aAngle)
void SetPosition(const VECTOR2I &aPosition) override
void Move(const VECTOR2I &aMoveVector) override
Move the item by aMoveVector to a new position.
SCH_BITMAP & operator=(const SCH_ITEM &aItem)
void SetX(int aX)
Definition sch_bitmap.h:54
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
bool operator==(const SCH_ITEM &aOther) const override
REFERENCE_IMAGE m_referenceImage
Definition sch_bitmap.h:139
int GetTransformOriginOffsetX() const
void SetTransformOriginOffsetY(int aY)
void SetImageScale(double aScale)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
void SetLocked(bool aLocked) override
Definition sch_item.h:262
SCH_ITEM & operator=(const SCH_ITEM &aPin)
Definition sch_item.cpp:91
bool IsLocked() const override
Definition sch_item.cpp:179
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:353
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:65
wxString GetClass() const override
Return the class name.
Definition sch_item.h:180
SCH_LAYER_ID m_layer
Definition sch_item.h:843
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
@ DEGREES_T
Definition eda_angle.h:30
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:477
a few functions useful in geometry calculations.
@ LAYER_DRAW_BITMAPS
Draw images.
Definition layer_ids.h:280
@ LAYER_NOTES
Definition layer_ids.h:498
@ LAYER_SELECTION_SHADOWS
Definition layer_ids.h:526
@ LEFT_RIGHT
Flip left to right (around the Y axis)
Definition mirror.h:27
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:28
bool BoxHitTest(const VECTOR2I &aHitPoint, const BOX2I &aHittee, int aAccuracy)
Perform a point-to-box hit test.
KICOMMON_API void PackCustomProperties(google::protobuf::RepeatedPtrField< types::CustomProperty > *aOutput, const EDA_ITEM &aItem)
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void UnpackCustomProperties(const google::protobuf::RepeatedPtrField< types::CustomProperty > &aInput, EDA_ITEM &aItem)
#define _HKI(x)
Definition page_info.cpp:40
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
Definition property.h:74
@ PT_COORD
Coordinate expressed in distance units (mm/inch)
Definition property.h:65
@ PT_DECIDEGREE
Angle expressed in decidegrees.
Definition property.h:67
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
static struct SCH_BITMAP_DESC _SCH_BITMAP_DESC
@ SCH_BITMAP_T
Definition typeinfo.h:160
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708