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 <class_plotter.h>
34 #include <trigo.h>
35 #include <wxstruct.h>
36 #include <base_units.h>
37 #include <msgpanel.h>
38 #include <bitmaps.h>
39 
40 #include <general.h>
41 #include <lib_rectangle.h>
42 #include <transform.h>
43 
44 
46  LIB_ITEM( LIB_RECTANGLE_T, aParent )
47 {
48  m_Width = 0;
49  m_Fill = NO_FILL;
50  m_isFillable = true;
51  m_typeName = _( "Rectangle" );
52  m_isHeightLocked = false;
53  m_isWidthLocked = false;
54  m_isStartPointSelected = false;
55 }
56 
57 
59 {
60  return new LIB_RECTANGLE( *this );
61 }
62 
63 
64 int LIB_RECTANGLE::compare( const LIB_ITEM& aOther ) const
65 {
66  wxASSERT( aOther.Type() == LIB_RECTANGLE_T );
67 
68  const LIB_RECTANGLE* tmp = ( LIB_RECTANGLE* ) &aOther;
69 
70  if( m_Pos.x != tmp->m_Pos.x )
71  return m_Pos.x - tmp->m_Pos.x;
72 
73  if( m_Pos.y != tmp->m_Pos.y )
74  return m_Pos.y - tmp->m_Pos.y;
75 
76  if( m_End.x != tmp->m_End.x )
77  return m_End.x - tmp->m_End.x;
78 
79  if( m_End.y != tmp->m_End.y )
80  return m_End.y - tmp->m_End.y;
81 
82  return 0;
83 }
84 
85 
86 void LIB_RECTANGLE::SetOffset( const wxPoint& aOffset )
87 {
88  m_Pos += aOffset;
89  m_End += aOffset;
90 }
91 
92 
93 bool LIB_RECTANGLE::Inside( EDA_RECT& aRect ) const
94 {
95  return aRect.Contains( m_Pos.x, -m_Pos.y ) || aRect.Contains( m_End.x, -m_End.y );
96 }
97 
98 
99 void LIB_RECTANGLE::Move( const wxPoint& aPosition )
100 {
101  wxPoint size = m_End - m_Pos;
102  m_Pos = aPosition;
103  m_End = aPosition + size;
104 }
105 
106 
108 {
109  m_Pos.x -= aCenter.x;
110  m_Pos.x *= -1;
111  m_Pos.x += aCenter.x;
112  m_End.x -= aCenter.x;
113  m_End.x *= -1;
114  m_End.x += aCenter.x;
115 }
116 
117 
119 {
120  m_Pos.y -= aCenter.y;
121  m_Pos.y *= -1;
122  m_Pos.y += aCenter.y;
123  m_End.y -= aCenter.y;
124  m_End.y *= -1;
125  m_End.y += aCenter.y;
126 }
127 
128 
129 void LIB_RECTANGLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
130 {
131  int rot_angle = aRotateCCW ? -900 : 900;
132  RotatePoint( &m_Pos, aCenter, rot_angle );
133  RotatePoint( &m_End, aCenter, rot_angle );
134 }
135 
136 
137 void LIB_RECTANGLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
138  const TRANSFORM& aTransform )
139 {
140  wxASSERT( aPlotter != NULL );
141 
142  wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
143  wxPoint end = aTransform.TransformCoordinate( m_End ) + aOffset;
144 
145  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
146  {
148  aPlotter->Rect( pos, end, FILLED_WITH_BG_BODYCOLOR, 0 );
149  }
150 
151  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
152  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
153  aPlotter->Rect( pos, end, already_filled ? NO_FILL : m_Fill, GetPenSize() );
154 }
155 
156 
158 {
159  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
160 }
161 
162 
164  const wxPoint& aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode,
165  void* aData, const TRANSFORM& aTransform )
166 {
167  wxPoint pos1, pos2;
168 
170 
171  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
172  {
173  if( IsSelected() )
174  color = GetItemSelectedColor();
175  }
176  else
177  {
178  color = aColor;
179  }
180 
181  pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
182  pos2 = aTransform.TransformCoordinate( m_End ) + aOffset;
183 
184  FILL_T fill = aData ? NO_FILL : m_Fill;
185 
186  if( aColor != COLOR4D::UNSPECIFIED )
187  fill = NO_FILL;
188 
189  GRSetDrawMode( aDC, aDrawMode );
190 
191  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
192  if( fill == FILLED_WITH_BG_BODYCOLOR && !aData )
193  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize( ),
196  else if( m_Fill == FILLED_SHAPE && !aData )
197  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y,
198  GetPenSize(), color, color );
199  else
200  GRRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize(), color );
201 
202  /* Set to one (1) to draw bounding box around rectangle to validate
203  * bounding box calculation. */
204 #if 0
205  EDA_RECT bBox = GetBoundingBox();
206  bBox.RevertYAxis();
207  bBox = aTransform.TransformCoordinate( bBox );
208  bBox.Move( aOffset );
209  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
210 #endif
211 }
212 
213 
215 {
216  wxString msg;
217 
218  LIB_ITEM::GetMsgPanelInfo( aList );
219 
220  msg = StringFromValue( g_UserUnit, m_Width, true );
221 
222  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
223 }
224 
225 
227 {
228  EDA_RECT rect;
229 
230  rect.SetOrigin( m_Pos );
231  rect.SetEnd( m_End );
232  rect.Inflate( ( GetPenSize()+1 ) / 2 );
233 
234  rect.RevertYAxis();
235 
236  return rect;
237 }
238 
239 
240 bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition ) const
241 {
242  int mindist = ( GetPenSize() / 2 ) + 1;
243 
244  // Have a minimal tolerance for hit test
245  if( mindist < MINIMUM_SELECTION_DISTANCE )
246  mindist = MINIMUM_SELECTION_DISTANCE;
247 
248  return HitTest( aPosition, mindist, DefaultTransform );
249 }
250 
251 
252 bool LIB_RECTANGLE::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
253 {
254  if( aThreshold < 0 )
255  aThreshold = GetPenSize() / 2;
256 
257  wxPoint actualStart = aTransform.TransformCoordinate( m_Pos );
258  wxPoint actualEnd = aTransform.TransformCoordinate( m_End );
259 
260  // locate lower segment
261  wxPoint start, end;
262 
263  start = actualStart;
264  end.x = actualEnd.x;
265  end.y = actualStart.y;
266 
267  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
268  return true;
269 
270  // locate right segment
271  start.x = actualEnd.x;
272  end.y = actualEnd.y;
273 
274  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
275  return true;
276 
277  // locate upper segment
278  start.y = actualEnd.y;
279  end.x = actualStart.x;
280 
281  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
282  return true;
283 
284  // locate left segment
285  start = actualStart;
286  end.x = actualStart.x;
287  end.y = actualEnd.y;
288 
289  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
290  return true;
291 
292  return false;
293 }
294 
295 
297 {
298  return wxString::Format( _( "Rectangle from (%s, %s) to (%s, %s)" ),
303 }
304 
305 
307 {
308  return add_rectangle_xpm;
309 }
310 
311 
312 void LIB_RECTANGLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
313 {
314  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
315  wxT( "Invalid edit mode for LIB_RECTANGLE object." ) );
316 
317  if( aEditMode == IS_NEW )
318  {
319  m_Pos = m_End = aPosition;
320  }
321  else if( aEditMode == IS_RESIZED )
322  {
324  || abs( m_Pos.y - aPosition.y ) < MINIMUM_SELECTION_DISTANCE;
325 
327  {
330  }
331  else
332  {
335  }
336 
338  }
339  else if( aEditMode == IS_MOVED )
340  {
342  m_initialCursorPos = aPosition;
344  }
345 
346  m_Flags = aEditMode;
347 }
348 
349 
350 bool LIB_RECTANGLE::ContinueEdit( const wxPoint aPosition )
351 {
352  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
353  wxT( "Bad call to ContinueEdit(). LIB_RECTANGLE is not being edited." ) );
354 
355  return false;
356 }
357 
358 
359 void LIB_RECTANGLE::EndEdit( const wxPoint& aPosition, bool aAbort )
360 {
361  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
362  wxT( "Bad call to EndEdit(). LIB_RECTANGLE is not being edited." ) );
363 
364  m_Flags = 0;
365  m_isHeightLocked = false;
366  m_isWidthLocked = false;
367  SetEraseLastDrawItem( false );
368 }
369 
370 
371 void LIB_RECTANGLE::calcEdit( const wxPoint& aPosition )
372 {
373  if( m_Flags == IS_NEW )
374  {
375  m_End = aPosition;
377  }
378  else if( m_Flags == IS_RESIZED )
379  {
380  if( m_isHeightLocked )
381  {
383  m_Pos.x = aPosition.x;
384  else
385  m_End.x = aPosition.x;
386  }
387  else if( m_isWidthLocked )
388  {
390  m_Pos.y = aPosition.y;
391  else
392  m_End.y = aPosition.y;
393  }
394  else
395  {
397  m_Pos = aPosition;
398  else
399  m_End = aPosition;
400  }
401  }
402  else if( m_Flags == IS_MOVED )
403  {
404  Move( m_initialPos + aPosition - m_initialCursorPos );
405  }
406 }
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:225
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:38
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:296
wxString m_typeName
Name of object displayed in the message panel.
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:1116
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:204
int GetPenSize() const override
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:248
void SetOrigin(const wxPoint &pos)
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
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) ...
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:127
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:1076
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:41
Base window classes and related definitions.
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
void SetEnd(int x, int y)
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:158
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:56
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: class_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:202
Class EDA_RECT handles the component boundary box.
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:178
void EndEdit(const wxPoint &aPosition, bool aAbort=false) override
End an object editing action.
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:128
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:70
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:142
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:126