KiCad PCB EDA Suite
lib_rectangle.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) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2004-2017 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 <fctsys.h>
30 #include <gr_basic.h>
31 #include <macros.h>
32 #include <class_drawpanel.h>
33 #include <plotter.h>
34 #include <trigo.h>
35 #include <base_units.h>
36 #include <msgpanel.h>
37 #include <bitmaps.h>
38 
39 #include <general.h>
40 #include <lib_rectangle.h>
41 #include <transform.h>
42 
43 
45  LIB_ITEM( LIB_RECTANGLE_T, aParent )
46 {
47  m_Width = 0;
48  m_Fill = NO_FILL;
49  m_isFillable = true;
50  m_isHeightLocked = false;
51  m_isWidthLocked = false;
52  m_isStartPointSelected = false;
53 }
54 
55 
57 {
58  return new LIB_RECTANGLE( *this );
59 }
60 
61 
62 int LIB_RECTANGLE::compare( const LIB_ITEM& aOther ) const
63 {
64  wxASSERT( aOther.Type() == LIB_RECTANGLE_T );
65 
66  const LIB_RECTANGLE* tmp = ( LIB_RECTANGLE* ) &aOther;
67 
68  if( m_Pos.x != tmp->m_Pos.x )
69  return m_Pos.x - tmp->m_Pos.x;
70 
71  if( m_Pos.y != tmp->m_Pos.y )
72  return m_Pos.y - tmp->m_Pos.y;
73 
74  if( m_End.x != tmp->m_End.x )
75  return m_End.x - tmp->m_End.x;
76 
77  if( m_End.y != tmp->m_End.y )
78  return m_End.y - tmp->m_End.y;
79 
80  return 0;
81 }
82 
83 
84 void LIB_RECTANGLE::SetOffset( const wxPoint& aOffset )
85 {
86  m_Pos += aOffset;
87  m_End += aOffset;
88 }
89 
90 
91 bool LIB_RECTANGLE::Inside( EDA_RECT& aRect ) const
92 {
93  return aRect.Contains( m_Pos.x, -m_Pos.y ) || aRect.Contains( m_End.x, -m_End.y );
94 }
95 
96 
97 void LIB_RECTANGLE::Move( const wxPoint& aPosition )
98 {
99  wxPoint size = m_End - m_Pos;
100  m_Pos = aPosition;
101  m_End = aPosition + size;
102 }
103 
104 
106 {
107  m_Pos.x -= aCenter.x;
108  m_Pos.x *= -1;
109  m_Pos.x += aCenter.x;
110  m_End.x -= aCenter.x;
111  m_End.x *= -1;
112  m_End.x += aCenter.x;
113 }
114 
115 
117 {
118  m_Pos.y -= aCenter.y;
119  m_Pos.y *= -1;
120  m_Pos.y += aCenter.y;
121  m_End.y -= aCenter.y;
122  m_End.y *= -1;
123  m_End.y += aCenter.y;
124 }
125 
126 
127 void LIB_RECTANGLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
128 {
129  int rot_angle = aRotateCCW ? -900 : 900;
130  RotatePoint( &m_Pos, aCenter, rot_angle );
131  RotatePoint( &m_End, aCenter, rot_angle );
132 }
133 
134 
135 void LIB_RECTANGLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
136  const TRANSFORM& aTransform )
137 {
138  wxASSERT( aPlotter != NULL );
139 
140  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
141  wxPoint end = aTransform.TransformCoordinate( m_End ) + aOffset;
142 
143  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
144  {
146  aPlotter->Rect( pos, end, FILLED_WITH_BG_BODYCOLOR, 0 );
147  }
148 
149  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
150  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
151  aPlotter->Rect( pos, end, already_filled ? NO_FILL : m_Fill, GetPenSize() );
152 }
153 
154 
156 {
157  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
158 }
159 
160 
162  const wxPoint& aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode,
163  void* aData, const TRANSFORM& aTransform )
164 {
165  wxPoint pos1, pos2;
166 
168 
169  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
170  {
171  if( IsSelected() )
172  color = GetItemSelectedColor();
173  }
174  else
175  {
176  color = aColor;
177  }
178 
179  pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
180  pos2 = aTransform.TransformCoordinate( m_End ) + aOffset;
181 
182  FILL_T fill = aData ? NO_FILL : m_Fill;
183 
184  if( aColor != COLOR4D::UNSPECIFIED )
185  fill = NO_FILL;
186 
187  GRSetDrawMode( aDC, aDrawMode );
188 
189  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
190  if( fill == FILLED_WITH_BG_BODYCOLOR && !aData )
191  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize( ),
194  else if( m_Fill == FILLED_SHAPE && !aData )
195  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y,
196  GetPenSize(), color, color );
197  else
198  GRRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize(), color );
199 
200  /* Set to one (1) to draw bounding box around rectangle to validate
201  * bounding box calculation. */
202 #if 0
203  EDA_RECT bBox = GetBoundingBox();
204  bBox.RevertYAxis();
205  bBox = aTransform.TransformCoordinate( bBox );
206  bBox.Move( aOffset );
207  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
208 #endif
209 }
210 
211 
213 {
214  wxString msg;
215 
216  LIB_ITEM::GetMsgPanelInfo( aList );
217 
218  msg = StringFromValue( g_UserUnit, m_Width, true );
219 
220  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
221 }
222 
223 
225 {
226  EDA_RECT rect;
227 
228  rect.SetOrigin( m_Pos );
229  rect.SetEnd( m_End );
230  rect.Inflate( ( GetPenSize()+1 ) / 2 );
231 
232  rect.RevertYAxis();
233 
234  return rect;
235 }
236 
237 
238 bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition ) const
239 {
240  int mindist = ( GetPenSize() / 2 ) + 1;
241 
242  // Have a minimal tolerance for hit test
243  if( mindist < MINIMUM_SELECTION_DISTANCE )
244  mindist = MINIMUM_SELECTION_DISTANCE;
245 
246  return HitTest( aPosition, mindist, DefaultTransform );
247 }
248 
249 
250 bool LIB_RECTANGLE::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
251 {
252  if( aThreshold < 0 )
253  aThreshold = GetPenSize() / 2;
254 
255  wxPoint actualStart = aTransform.TransformCoordinate( m_Pos );
256  wxPoint actualEnd = aTransform.TransformCoordinate( m_End );
257 
258  // locate lower segment
259  wxPoint start, end;
260 
261  start = actualStart;
262  end.x = actualEnd.x;
263  end.y = actualStart.y;
264 
265  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
266  return true;
267 
268  // locate right segment
269  start.x = actualEnd.x;
270  end.y = actualEnd.y;
271 
272  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
273  return true;
274 
275  // locate upper segment
276  start.y = actualEnd.y;
277  end.x = actualStart.x;
278 
279  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
280  return true;
281 
282  // locate left segment
283  start = actualStart;
284  end.x = actualStart.x;
285  end.y = actualEnd.y;
286 
287  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
288  return true;
289 
290  return false;
291 }
292 
293 
295 {
296  return wxString::Format( _( "Rectangle from (%s, %s) to (%s, %s)" ),
301 }
302 
303 
305 {
306  return add_rectangle_xpm;
307 }
308 
309 
310 void LIB_RECTANGLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
311 {
312  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
313  wxT( "Invalid edit mode for LIB_RECTANGLE object." ) );
314 
315  if( aEditMode == IS_NEW )
316  {
317  m_Pos = m_End = aPosition;
318  }
319  else if( aEditMode == IS_RESIZED )
320  {
322  || abs( m_Pos.y - aPosition.y ) < MINIMUM_SELECTION_DISTANCE;
323 
325  {
328  }
329  else
330  {
333  }
334 
336  }
337  else if( aEditMode == IS_MOVED )
338  {
340  m_initialCursorPos = aPosition;
342  }
343 
344  m_Flags = aEditMode;
345 }
346 
347 
348 bool LIB_RECTANGLE::ContinueEdit( const wxPoint aPosition )
349 {
350  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
351  wxT( "Bad call to ContinueEdit(). LIB_RECTANGLE is not being edited." ) );
352 
353  return false;
354 }
355 
356 
357 void LIB_RECTANGLE::EndEdit( const wxPoint& aPosition, bool aAbort )
358 {
359  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
360  wxT( "Bad call to EndEdit(). LIB_RECTANGLE is not being edited." ) );
361 
362  m_Flags = 0;
363  m_isHeightLocked = false;
364  m_isWidthLocked = false;
365  SetEraseLastDrawItem( false );
366 }
367 
368 
369 void LIB_RECTANGLE::calcEdit( const wxPoint& aPosition )
370 {
371  if( m_Flags == IS_NEW )
372  {
373  m_End = aPosition;
375  }
376  else if( m_Flags == IS_RESIZED )
377  {
378  if( m_isHeightLocked )
379  {
381  m_Pos.x = aPosition.x;
382  else
383  m_End.x = aPosition.x;
384  }
385  else if( m_isWidthLocked )
386  {
388  m_Pos.y = aPosition.y;
389  else
390  m_End.y = aPosition.y;
391  }
392  else
393  {
395  m_Pos = aPosition;
396  else
397  m_End = aPosition;
398  }
399  }
400  else if( m_Flags == IS_MOVED )
401  {
402  Move( m_initialPos + aPosition - m_initialCursorPos );
403  }
404 }
Definition: colors.h:57
void SetOffset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
wxString CoordinateToString(int aValue, bool aConvertToMils)
Function CoordinateToString is a helper to convert the integer coordinate aValue to a string in inche...
Definition: base_units.cpp:118
KICAD_T Type() const
Function Type()
Definition: base_struct.h:209
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
void BeginEdit(STATUS_FLAGS aEditMode, const wxPoint aStartPoint=wxPoint(0, 0)) override
Begin an editing a component library draw item in aEditMode at aPosition.
PNG memory record (file in memory).
Definition: bitmap_types.h:41
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:318
void SetEraseLastDrawItem(bool aErase=true)
Implementation of conversion functions that require both schematic and board internal units...
virtual void SetColor(COLOR4D color)=0
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_draw_item.h:52
bool Contains(const wxPoint &aPoint) const
Function Contains.
void GRFilledRect(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:1138
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:205
int GetPenSize() const override
int color
Definition: DXF_plotter.cpp:62
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
int compare(const LIB_ITEM &aOther) const override
Provide the draw object specific comparison called by the == and < operators.
bool IsSelected() const
Definition: base_struct.h:232
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:124
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
bool Inside(EDA_RECT &aRect) const override
Test if any part of the draw object is inside rectangle bounds of aRect.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:167
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
Definition: eda_rect.h:144
bool m_isHeightLocked
Definition: lib_rectangle.h:41
#define abs(a)
Definition: auxiliary.h:84
void calcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
The base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:66
This file contains miscellaneous commonly used macros and functions.
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
#define IS_NEW
New item, just created.
Definition: base_struct.h:109
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1098
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:37
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
void SetEnd(int x, int y)
Definition: eda_rect.h:134
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
bool m_isFillable
Flag to indicate if draw item is fillable.
Define a library symbol object.
unsigned STATUS_FLAGS
Definition: base_struct.h:142
EDA_RECT * GetClipBox()
virtual void Rotate()
Rotate the draw item.
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform) override
Draw the item on aPanel.
bool m_isWidthLocked
Definition: lib_rectangle.h:40
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
void Move(const wxPoint &aPosition) override
Move a draw object to aPosition.
void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
Base plotter engine class.
Definition: plotter.h:96
LIB_RECTANGLE(LIB_PART *aParent)
virtual void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
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
TRANSFORM DefaultTransform
Definition: eeschema.cpp:58
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 Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) override
Plot the draw item using the plot object.
wxPoint m_initialPos
Temporary position when moving an existing item.
FILL_T m_Fill
The body fill type.
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:186
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
bool HitTest(const wxPoint &aPosition) const override
Function HitTest tests if aPosition is contained within or on the bounding area of an item...
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:162
void EndEdit(const wxPoint &aPosition, bool aAbort=false) override
End an object editing action.
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:110
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH)=0
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:52
COLOR4D GetItemSelectedColor()
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
bool m_isStartPointSelected
Definition: lib_rectangle.h:42
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Function TestSegmentHit test for hit on line segment i.e.
Definition: trigo.cpp:122
Message panel definition file.
const EDA_RECT GetBoundingBox() const override
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
wxPoint m_initialCursorPos
Initial cursor position at the beginning of a move.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
#define IS_MOVED
Item being moved.
Definition: base_struct.h:108