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 <sch_draw_panel.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  if( m_Width > 0 )
158  return m_Width;
159 
160  if( m_Width == 0 )
161  return GetDefaultLineThickness();
162 
163  return -1; // a value to use a minimal pen size
164 }
165 
166 
168  const wxPoint& aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode,
169  void* aData, const TRANSFORM& aTransform )
170 {
171  wxPoint pos1, pos2;
172 
174 
175  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
176  {
177  if( IsSelected() )
178  color = GetItemSelectedColor();
179  }
180  else
181  {
182  color = aColor;
183  }
184 
185  pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
186  pos2 = aTransform.TransformCoordinate( m_End ) + aOffset;
187 
188  FILL_T fill = aData ? NO_FILL : m_Fill;
189 
190  if( aColor != COLOR4D::UNSPECIFIED )
191  fill = NO_FILL;
192 
193  GRSetDrawMode( aDC, aDrawMode );
194 
195  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
196  if( fill == FILLED_WITH_BG_BODYCOLOR && !aData )
197  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize( ),
200  else if( m_Fill == FILLED_SHAPE && !aData )
201  GRFilledRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y,
202  GetPenSize(), color, color );
203  else
204  GRRect( clipbox, aDC, pos1.x, pos1.y, pos2.x, pos2.y, GetPenSize(), color );
205 
206  /* Set to one (1) to draw bounding box around rectangle to validate
207  * bounding box calculation. */
208 #if 0
209  EDA_RECT bBox = GetBoundingBox();
210  bBox.RevertYAxis();
211  bBox = aTransform.TransformCoordinate( bBox );
212  bBox.Move( aOffset );
213  GRRect( clipbox, aDC, bBox, 0, LIGHTMAGENTA );
214 #endif
215 }
216 
217 
219 {
220  wxString msg;
221 
222  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
223 
224  msg = MessageTextFromValue( aUnits, m_Width, true );
225 
226  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
227 }
228 
229 
231 {
232  EDA_RECT rect;
233 
234  rect.SetOrigin( m_Pos );
235  rect.SetEnd( m_End );
236  rect.Inflate( ( GetPenSize()+1 ) / 2 );
237 
238  rect.RevertYAxis();
239 
240  return rect;
241 }
242 
243 
244 bool LIB_RECTANGLE::HitTest( const wxPoint& aPosition ) const
245 {
246  int mindist = ( GetPenSize() / 2 ) + 1;
247 
248  // Have a minimal tolerance for hit test
249  if( mindist < MINIMUM_SELECTION_DISTANCE )
250  mindist = MINIMUM_SELECTION_DISTANCE;
251 
252  return HitTest( aPosition, mindist, DefaultTransform );
253 }
254 
255 
256 bool LIB_RECTANGLE::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
257 {
258  if( aThreshold < 0 )
259  aThreshold = GetPenSize() / 2;
260 
261  wxPoint actualStart = aTransform.TransformCoordinate( m_Pos );
262  wxPoint actualEnd = aTransform.TransformCoordinate( m_End );
263 
264  // locate lower segment
265  wxPoint start, end;
266 
267  start = actualStart;
268  end.x = actualEnd.x;
269  end.y = actualStart.y;
270 
271  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
272  return true;
273 
274  // locate right segment
275  start.x = actualEnd.x;
276  end.y = actualEnd.y;
277 
278  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
279  return true;
280 
281  // locate upper segment
282  start.y = actualEnd.y;
283  end.x = actualStart.x;
284 
285  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
286  return true;
287 
288  // locate left segment
289  start = actualStart;
290  end.x = actualStart.x;
291  end.y = actualEnd.y;
292 
293  if( TestSegmentHit( aPosition, start, end, aThreshold ) )
294  return true;
295 
296  return false;
297 }
298 
299 
301 {
302  return wxString::Format( _( "Rectangle from (%s, %s) to (%s, %s)" ),
303  MessageTextFromValue( aUnits, m_Pos.x ),
304  MessageTextFromValue( aUnits, m_Pos.y ),
305  MessageTextFromValue( aUnits, m_End.x ),
306  MessageTextFromValue( aUnits, m_End.y ) );
307 }
308 
309 
311 {
312  return add_rectangle_xpm;
313 }
314 
315 
316 void LIB_RECTANGLE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
317 {
318  wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
319  wxT( "Invalid edit mode for LIB_RECTANGLE object." ) );
320 
321  if( aEditMode == IS_NEW )
322  {
323  m_Pos = m_End = aPosition;
324  }
325  else if( aEditMode == IS_RESIZED )
326  {
328  || abs( m_Pos.y - aPosition.y ) < MINIMUM_SELECTION_DISTANCE;
329 
331  {
334  }
335  else
336  {
339  }
340 
341  }
342  else if( aEditMode == IS_MOVED )
343  {
345  m_initialCursorPos = aPosition;
346  }
347 
348  m_Flags = aEditMode;
349 }
350 
351 
352 bool LIB_RECTANGLE::ContinueEdit( const wxPoint aPosition )
353 {
354  wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
355  wxT( "Bad call to ContinueEdit(). LIB_RECTANGLE is not being edited." ) );
356 
357  return false;
358 }
359 
360 
361 void LIB_RECTANGLE::EndEdit( const wxPoint& aPosition, bool aAbort )
362 {
363  wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
364  wxT( "Bad call to EndEdit(). LIB_RECTANGLE is not being edited." ) );
365 
366  m_Flags = 0;
367  m_isHeightLocked = false;
368  m_isWidthLocked = false;
369 }
370 
371 
372 void LIB_RECTANGLE::CalcEdit( const wxPoint& aPosition )
373 {
374  if( m_Flags == IS_NEW )
375  {
376  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.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
virtual void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
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:43
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:223
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:53
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:1043
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:224
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:124
virtual EDA_RECT * GetClipBox()
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:174
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
The base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:67
This file contains miscellaneous commonly used macros and functions.
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125
#define IS_NEW
New item, just created.
Definition: base_struct.h:114
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
wxString GetSelectMenuText(EDA_UNITS_T aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1003
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:147
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
void Move(const wxPoint &aPosition) override
Move a draw object to aPosition.
Base plotter engine class.
Definition: plotter.h:97
LIB_RECTANGLE(LIB_PART *aParent)
TRANSFORM DefaultTransform
Definition: eeschema.cpp:59
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:178
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:154
void EndEdit(const wxPoint &aPosition, bool aAbort=false) override
End an object editing action.
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:115
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
void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:54
COLOR4D GetItemSelectedColor()
void CalcEdit(const wxPoint &aPosition) override
Calculates the attributes of an item at aPosition when it is being edited.
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_UNITS_T
Definition: common.h:159
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:113