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-2017 KiCad Developers, see change_log.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 <fctsys.h>
30 #include <class_drawpanel.h>
31 #include <trigo.h>
32 #include <macros.h>
33 #include <bitmaps.h>
34 
35 #include <sch_bitmap.h>
36 
37 #include <wx/mstream.h>
38 
39 
41  SCH_ITEM( NULL, SCH_BITMAP_T )
42 {
43  m_pos = pos;
44  m_Layer = LAYER_NOTES; // used only to draw/plot a rectangle,
45  // when a bitmap cannot be drawn or plotted
46  m_image = new BITMAP_BASE();
47 }
48 
49 
50 SCH_BITMAP::SCH_BITMAP( const SCH_BITMAP& aSchBitmap ) :
51  SCH_ITEM( aSchBitmap )
52 {
53  m_pos = aSchBitmap.m_pos;
54  m_Layer = aSchBitmap.m_Layer;
55  m_image = new BITMAP_BASE( *aSchBitmap.m_image );
56 }
57 
58 
60 {
61  wxCHECK_MSG( Type() == aItem.Type(), *this,
62  wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
63  GetClass() );
64 
65  if( &aItem != this )
66  {
67  SCH_ITEM::operator=( aItem );
68 
69  SCH_BITMAP* bitmap = (SCH_BITMAP*) &aItem;
70 
71  delete m_image;
72  m_image = new BITMAP_BASE( *bitmap->m_image );
73  m_pos = bitmap->m_pos;
74  }
75 
76  return *this;
77 }
78 
79 
80 bool SCH_BITMAP::ReadImageFile( const wxString& aFullFilename )
81 {
82  return m_image->ReadImageFile( aFullFilename );
83 }
84 
85 
87 {
88  return new SCH_BITMAP( *this );
89 }
90 
91 
93 {
94  wxCHECK_RET( aItem->Type() == SCH_BITMAP_T,
95  wxString::Format( wxT( "SCH_BITMAP object cannot swap data with %s object." ),
96  GetChars( aItem->GetClass() ) ) );
97 
98  SCH_BITMAP* item = (SCH_BITMAP*) aItem;
99  std::swap( m_pos, item->m_pos );
100  std::swap( m_image, item->m_image );
101 }
102 
103 
105 {
106  EDA_RECT rect = m_image->GetBoundingBox();
107 
108  rect.Move( m_pos );
109 
110  return rect;
111 }
112 
113 
114 void SCH_BITMAP::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
115  GR_DRAWMODE aDrawMode, COLOR4D aColor )
116 {
117  wxPoint pos = m_pos + aOffset;
118 
119  if( aColor == COLOR4D::UNSPECIFIED ) // Use normal drawing function
120  {
121  // https://bugs.launchpad.net/kicad/+bug/1529163
122  // "Moving images in eeschema on OS X uses
123  // wrong position and shows image flipped"
124  //
125  // Original fix was to only GRSetDrawMode if aColor >= 0, but this made
126  // moving SCH_BITMAP work poorly on other platforms.
127 #ifndef __WXMAC__
128  GRSetDrawMode( aDC, aDrawMode );
129 #endif
130 
131  m_image->DrawBitmap( aPanel, aDC, pos );
132  }
133  else // draws bounding box only (used to move items)
134  {
135  GRSetDrawMode( aDC, aDrawMode );
136  // To draw the rect, pos is the upper left corner position
137  wxSize size = m_image->GetSize();
138  pos.x -= size.x / 2;
139  pos.y -= size.y / 2;
140  GRRect( aPanel->GetClipBox(), aDC, pos.x, pos.y,
141  pos.x + size.x, pos.y + size.y, 0, aColor );
142  }
143 }
144 
145 
146 wxSize SCH_BITMAP::GetSize() const
147 {
148  return m_image->GetSize();
149 }
150 
151 
152 void SCH_BITMAP::MirrorX( int aXaxis_position )
153 {
154  MIRROR( m_pos.y, aXaxis_position );
155  m_image->Mirror( true );
156 }
157 
158 
159 void SCH_BITMAP::MirrorY( int aYaxis_position )
160 {
161  MIRROR( m_pos.x, aYaxis_position );
162  m_image->Mirror( false );
163 }
164 
165 
166 void SCH_BITMAP::Rotate( wxPoint aPosition )
167 {
168  RotatePoint( &m_pos, aPosition, 900 );
169  m_image->Rotate( false );
170 }
171 
172 
173 bool SCH_BITMAP::IsSelectStateChanged( const wxRect& aRect )
174 {
175  bool previousState = IsSelected();
176 
177  if( aRect.Contains( m_pos ) )
178  SetFlags( SELECTED );
179  else
180  ClearFlags( SELECTED );
181 
182  return previousState != IsSelected();
183 }
184 
185 
186 #if defined(DEBUG)
187 void SCH_BITMAP::Show( int nestLevel, std::ostream& os ) const
188 {
189  // XML output:
190  wxString s = GetClass();
191 
192  NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << "/>\n";
193 }
194 
195 
196 #endif
197 
198 
199 bool SCH_BITMAP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
200 {
201  EDA_RECT rect = GetBoundingBox();
202 
203  rect.Inflate( aAccuracy );
204 
205  return rect.Contains( aPosition );
206 }
207 
208 
209 bool SCH_BITMAP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
210 {
211  EDA_RECT rect = aRect;
212 
213  rect.Inflate( aAccuracy );
214 
215  if( aContained )
216  return rect.Contains( GetBoundingBox() );
217 
218  return rect.Intersects( GetBoundingBox() );
219 }
220 
221 
222 void SCH_BITMAP::Plot( PLOTTER* aPlotter )
223 {
224  m_image->PlotImage( aPlotter, m_pos, GetLayerColor( GetLayer() ), GetPenSize() );
225 }
226 
227 
229 {
230  return image_xpm;
231 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
SCH_LAYER_ID m_Layer
SCH_ITEM & operator=(const SCH_ITEM &aItem)
Definition: sch_bitmap.cpp:59
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, GR_DRAWMODE aDrawMode, COLOR4D aColor=COLOR4D::UNSPECIFIED) override
Function Draw Draw a schematic item.
Definition: sch_bitmap.cpp:114
PNG memory record (file in memory).
Definition: bitmap_types.h:38
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:296
bool Contains(const wxPoint &aPoint) const
Function Contains.
wxSize GetSize() const
Function GetSize.
bool IsSelected() const
Definition: base_struct.h:235
wxPoint m_pos
Definition: sch_bitmap.h:44
SCH_LAYER_ID GetLayer() const
Function GetLayer returns the layer this item is on.
void PlotImage(PLOTTER *aPlotter, const wxPoint &aPos, COLOR4D aDefaultColor, int aDefaultPensize)
Function PlotImage Plot bitmap on plotter.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:167
const EDA_RECT GetBoundingBox() const
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
Definition: sch_bitmap.cpp:228
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: sch_bitmap.cpp:104
void Mirror(bool aVertically)
Function Mirror Mirror image vertically (i.e.
void MirrorY(int aYaxis_position) override
Function MirrorY mirrors item relative to the Y axis about aYaxis_position.
Definition: sch_bitmap.cpp:159
This file contains miscellaneous commonly used macros and functions.
This class handle bitmap images in KiCad.
void Rotate(wxPoint aPosition) override
Function Rotate rotates the item around aPosition 90 degrees in the clockwise direction.
Definition: sch_bitmap.cpp:166
void MIRROR(T &aPoint, const T &aMirrorRef)
Definition: macros.h:111
#define SELECTED
Definition: base_struct.h:134
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1076
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:267
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:41
wxSize GetSize() const
Definition: sch_bitmap.cpp:146
void SwapData(SCH_ITEM *aItem) override
Function SwapData swap the internal data structures aItem with the schematic item.
Definition: sch_bitmap.cpp:92
EDA_RECT * GetClipBox()
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Operator assignment is used to assign the members of aItem to another object.
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:42
Base plotter engine class.
Definition: class_plotter.h:97
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void MirrorX(int aXaxis_position) override
Function MirrorX mirrors item relative to the X axis about aXaxis_position.
Definition: sch_bitmap.cpp:152
Class EDA_RECT handles the component boundary box.
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: sch_bitmap.cpp:86
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
bool HitTest(const wxPoint &aPosition, int aAccuracy) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item...
Definition: sch_bitmap.cpp:199
virtual wxString GetClass() const override
Function GetClass returns the class name.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:268
SCH_BITMAP(const wxPoint &pos=wxPoint(0, 0))
Definition: sch_bitmap.cpp:40
bool ReadImageFile(const wxString &aFullFilename)
Function ReadImageFile Reads and stores in memory an image file.
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores an image file.
Definition: sch_bitmap.cpp:80
bool IsSelectStateChanged(const wxRect &aRect) override
Function IsSelectStateChanged checks if the selection state of an item inside aRect has changed...
Definition: sch_bitmap.cpp:173
void DrawBitmap(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPos)
void Rotate(bool aRotateCCW)
Function Rotate Rotate image CW or CCW.
BITMAP_BASE * m_image
Definition: sch_bitmap.h:45
wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_bitmap.h:88
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
void Plot(PLOTTER *aPlotter) override
Function Plot plots the schematic item to aPlotter.
Definition: sch_bitmap.cpp:222
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
wxImage * m_image
virtual int GetPenSize() const
Function GetPenSize virtual pure.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39