KiCad PCB EDA Suite
edit_points.h
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) 2014-2017 CERN
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef EDIT_POINTS_H_
28 #define EDIT_POINTS_H_
29 
30 #include <eda_item.h>
32 
33 #include <list>
34 #include <deque>
35 #include <memory>
36 
37 #include "edit_constraints.h"
38 #include <view/view.h>
39 
40 
48 {
49 public:
53  EDIT_POINT( const VECTOR2I& aPoint, std::pair<EDA_ITEM*, int> aConnected = { nullptr, 0 } ) :
54  m_position( aPoint ),
55  m_isActive( false ),
56  m_isHover( false ),
58  m_connected( aConnected )
59  {
60  }
61 
62  virtual ~EDIT_POINT() {}
63 
70  virtual VECTOR2I GetPosition() const
71  {
72  return m_position;
73  }
74 
78  virtual std::pair<EDA_ITEM*, int> GetConnected() const
79  {
80  return m_connected;
81  }
82 
86  inline int GetX() const
87  {
88  return GetPosition().x;
89  }
90 
94  inline int GetY() const
95  {
96  return GetPosition().y;
97  }
98 
106  virtual void SetPosition( const VECTOR2I& aPosition )
107  {
108  m_position = aPosition;
109  }
110 
111  virtual void SetPosition( int x, int y )
112  {
113  m_position.x = x;
114  m_position.y = y;
115  }
116 
123  bool WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const;
124 
131  {
132  m_constraint.reset( aConstraint );
133  }
134 
140  {
141  return m_constraint.get();
142  }
143 
147  inline void ClearConstraint()
148  {
149  m_constraint.reset();
150  }
151 
157  inline bool IsConstrained() const
158  {
159  return m_constraint != NULL;
160  }
161 
165  virtual void ApplyConstraint()
166  {
167  if( m_constraint )
168  m_constraint->Apply();
169  }
170 
171  bool IsActive() const { return m_isActive; }
172  void SetActive( bool aActive = true ) { m_isActive = aActive; }
173 
174  bool IsHover() const { return m_isHover; }
175  void SetHover( bool aHover = true ) { m_isHover = aHover; }
176 
178  void SetGridConstraint( GRID_CONSTRAINT_TYPE aConstraint ) { m_gridConstraint = aConstraint; }
179 
180  bool operator==( const EDIT_POINT& aOther ) const
181  {
182  return m_position == aOther.m_position;
183  }
184 
186  static const int POINT_SIZE = 10;
187 
189  static const int BORDER_SIZE = 2;
190 
192  static const int HOVER_SIZE = 5;
193 
194 private:
196  bool m_isActive;
197  bool m_isHover;
199 
202  std::pair<EDA_ITEM*, int> m_connected;
203 
205  std::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > m_constraint;
206 };
207 
208 
215 class EDIT_LINE : public EDIT_POINT
216 {
217 public:
222  EDIT_LINE( EDIT_POINT& aOrigin, EDIT_POINT& aEnd ) :
223  EDIT_POINT( aOrigin.GetPosition() + ( aEnd.GetPosition() - aOrigin.GetPosition() ) / 2 ),
224  m_origin( aOrigin ),
225  m_end( aEnd )
226  {
228  }
229 
231  virtual VECTOR2I GetPosition() const override
232  {
233  return ( m_origin.GetPosition() + m_end.GetPosition() ) / 2;
234  }
235 
237  virtual void SetPosition( const VECTOR2I& aPosition ) override
238  {
239  VECTOR2I difference = aPosition - GetPosition();
240 
241  m_origin.SetPosition( m_origin.GetPosition() + difference );
242  m_end.SetPosition( m_end.GetPosition() + difference );
243  }
244 
246  virtual void ApplyConstraint() override
247  {
248  if( m_constraint )
249  m_constraint->Apply();
250 
253  }
254 
261  {
262  m_constraint.reset( aConstraint );
263  }
264 
270  {
271  return m_constraint.get();
272  }
273 
278  {
279  return m_origin;
280  }
281 
282  const EDIT_POINT& GetOrigin() const
283  {
284  return m_origin;
285  }
286 
291  {
292  return m_end;
293  }
294 
295  const EDIT_POINT& GetEnd() const
296  {
297  return m_end;
298  }
299 
300  bool operator==( const EDIT_POINT& aOther ) const
301  {
302  return GetPosition() == aOther.GetPosition();
303  }
304 
305  bool operator==( const EDIT_LINE& aOther ) const
306  {
307  return m_origin == aOther.m_origin && m_end == aOther.m_end;
308  }
309 
310 private:
313 
315  std::shared_ptr<EDIT_CONSTRAINT<EDIT_LINE> > m_constraint;
316 };
317 
318 
322 class EDIT_POINTS : public EDA_ITEM
323 {
324 public:
328  EDIT_POINTS( EDA_ITEM* aParent );
329 
335  EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
336 
341  {
342  return m_parent;
343  }
344 
350  void AddPoint( const EDIT_POINT& aPoint )
351  {
352  m_points.push_back( aPoint );
353  }
354 
360  void AddPoint( const VECTOR2I& aPoint, std::pair<EDA_ITEM*, int> aConnected = { nullptr, 0 } )
361  {
362  AddPoint( EDIT_POINT( aPoint, aConnected ) );
363  }
364 
370  void AddLine( const EDIT_LINE& aLine )
371  {
372  m_lines.push_back( aLine );
373  }
374 
381  void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
382  {
383  m_lines.emplace_back( aOrigin, aEnd );
384  }
385 
389  void AddBreak()
390  {
391  assert( m_points.size() > 0 );
392  m_contours.push_back( m_points.size() - 1 );
393  }
394 
401  int GetContourStartIdx( int aPointIdx ) const;
402 
409  int GetContourEndIdx( int aPointIdx ) const;
410 
417  bool IsContourStart( int aPointIdx ) const;
418 
425  bool IsContourEnd( int aPointIdx ) const;
426 
437  EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
438 
439  EDIT_LINE* Previous( const EDIT_LINE& aLine );
440 
451  EDIT_POINT* Next( const EDIT_POINT& aPoint, bool aTraverseContours = true );
452 
453  EDIT_LINE* Next( const EDIT_LINE& aLine );
454 
455  EDIT_POINT& Point( unsigned int aIndex )
456  {
457  return m_points[aIndex];
458  }
459 
460  const EDIT_POINT& Point( unsigned int aIndex ) const
461  {
462  return m_points[aIndex];
463  }
464 
465  EDIT_LINE& Line( unsigned int aIndex )
466  {
467  return m_lines[aIndex];
468  }
469 
470  const EDIT_LINE& Line( unsigned int aIndex ) const
471  {
472  return m_lines[aIndex];
473  }
474 
478  unsigned int PointsSize() const
479  {
480  return m_points.size();
481  }
482 
486  unsigned int LinesSize() const
487  {
488  return m_lines.size();
489  }
490 
492  virtual const BOX2I ViewBBox() const override;
493 
495  virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
496 
498  virtual void ViewGetLayers( int aLayers[], int& aCount ) const override
499  {
500  aCount = 1;
501  aLayers[0] = LAYER_GP_OVERLAY ;
502  }
503 
504 #if defined(DEBUG)
505  void Show( int x, std::ostream& st ) const override
506  {
507  }
508 #endif
509 
515  virtual wxString GetClass() const override
516  {
517  return wxT( "EDIT_POINTS" );
518  }
519 
520 private:
522  std::deque<EDIT_POINT> m_points;
523  std::deque<EDIT_LINE> m_lines;
524  std::list<int> m_contours;
526 };
527 
528 #endif /* EDIT_POINTS_H_ */
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Return the all the layers within the VIEW the object is painted on.
Definition: edit_points.h:498
std::shared_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_constraint
Definition: edit_points.h:205
void SetConstraint(EDIT_CONSTRAINT< EDIT_LINE > *aConstraint)
Set a constraint for and EDIT_POINT.
Definition: edit_points.h:260
EDIT_LINE(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:222
std::shared_ptr< EDIT_CONSTRAINT< EDIT_LINE > > m_constraint
Definition: edit_points.h:315
void AddPoint(const VECTOR2I &aPoint, std::pair< EDA_ITEM *, int > aConnected={ nullptr, 0 })
Add an EDIT_POINT.
Definition: edit_points.h:360
int GetContourEndIdx(int aPointIdx) const
Return index of the contour finish for a point with given index.
Definition: edit_points.cpp:87
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:524
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:522
virtual void SetPosition(int x, int y)
Definition: edit_points.h:111
void SetConstraint(EDIT_CONSTRAINT< EDIT_POINT > *aConstraint)
Set a constraint for and EDIT_POINT.
Definition: edit_points.h:130
EDIT_LINE & Line(unsigned int aIndex)
Definition: edit_points.h:465
const EDIT_LINE & Line(unsigned int aIndex) const
Definition: edit_points.h:470
std::deque< EDIT_LINE > m_lines
EDIT_LINEs for modifying m_parent.
Definition: edit_points.h:523
void AddBreak()
Adds a break, indicating the end of a contour.
Definition: edit_points.h:389
Represent a line connecting two EDIT_POINTs.
Definition: edit_points.h:215
void AddLine(const EDIT_LINE &aLine)
Adds an EDIT_LINE.
Definition: edit_points.h:370
EDIT_POINT * Next(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is before the given point in the list.
virtual const BOX2I ViewBBox() const override
static const int BORDER_SIZE
Border size when hovering.
Definition: edit_points.h:189
bool m_allowPoints
If false, only allow editing of EDIT_LINES.
Definition: edit_points.h:525
bool IsContourStart(int aPointIdx) const
Check if a point with given index is a contour origin.
Definition: edit_points.cpp:99
EDA_ITEM * m_parent
Parent of the EDIT_POINTs.
Definition: edit_points.h:521
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:231
bool IsContourEnd(int aPointIdx) const
Check is a point with given index is a contour finish.
int GetY() const
Return Y coordinate of an EDIT_POINT.
Definition: edit_points.h:94
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
EDIT_CONSTRAINT< EDIT_LINE > * GetConstraint() const
Return the constraint imposed on an EDIT_POINT.
Definition: edit_points.h:269
GRID_CONSTRAINT_TYPE GetGridConstraint() const
Definition: edit_points.h:177
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
Definition: edit_points.h:277
void AddPoint(const EDIT_POINT &aPoint)
Add an EDIT_POINT.
Definition: edit_points.h:350
EDIT_POINT & m_end
End point for a line.
Definition: edit_points.h:312
const EDIT_POINT & GetEnd() const
Definition: edit_points.h:295
bool IsActive() const
Definition: edit_points.h:171
void ClearConstraint()
Remove previously set constraint.
Definition: edit_points.h:147
const EDIT_POINT & Point(unsigned int aIndex) const
Definition: edit_points.h:460
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
Definition: edit_points.h:290
void SetActive(bool aActive=true)
Definition: edit_points.h:172
EDIT_POINT * FindPoint(const VECTOR2I &aLocation, KIGFX::VIEW *aView)
Return a point that is at given coordinates or NULL if there is no such point.
Definition: edit_points.cpp:48
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:70
#define NULL
bool operator==(const EDIT_LINE &aOther) const
Definition: edit_points.h:305
bool m_isHover
True if this point is being hovered over.
Definition: edit_points.h:197
unsigned int LinesSize() const
Return number of stored EDIT_LINEs.
Definition: edit_points.h:486
virtual void SetPosition(const VECTOR2I &aPosition) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:237
int GetX() const
Return X coordinate of an EDIT_POINT.
Definition: edit_points.h:86
int GetContourStartIdx(int aPointIdx) const
Return index of the contour origin for a point with given index.
Definition: edit_points.cpp:71
virtual ~EDIT_POINT()
Definition: edit_points.h:62
bool m_isActive
True if this point is being manipulated.
Definition: edit_points.h:196
VECTOR2I m_position
Position of EDIT_POINT.
Definition: edit_points.h:195
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
Definition: edit_points.h:322
EDIT_POINT(const VECTOR2I &aPoint, std::pair< EDA_ITEM *, int > aConnected={ nullptr, 0 })
Definition: edit_points.h:53
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:300
void AddLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Adds an EDIT_LINE.
Definition: edit_points.h:381
EDA_ITEM * GetParent() const
Return parent of the EDIT_POINTS.
Definition: edit_points.h:340
virtual std::pair< EDA_ITEM *, int > GetConnected() const
Return a connected item record comprising an EDA_ITEM* and a STARTPOINT/ENDPOINT flag.
Definition: edit_points.h:78
Board layer functions and definitions.
EDIT_POINT & Point(unsigned int aIndex)
Definition: edit_points.h:455
virtual void ApplyConstraint() override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:246
const EDIT_POINT & GetOrigin() const
Definition: edit_points.h:282
void SetHover(bool aHover=true)
Definition: edit_points.h:175
bool IsConstrained() const
Check if point is constrained.
Definition: edit_points.h:157
bool operator==(const EDIT_POINT &aOther) const
Single point size in pixels.
Definition: edit_points.h:180
EDIT_POINTS(EDA_ITEM *aParent)
Definition: edit_points.cpp:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:149
static const int POINT_SIZE
Border size when not hovering.
Definition: edit_points.h:186
unsigned int PointsSize() const
Return number of stored EDIT_POINTs.
Definition: edit_points.h:478
EDIT_CONSTRAINT< EDIT_POINT > * GetConstraint() const
Return the constraint imposed on an EDIT_POINT.
Definition: edit_points.h:139
virtual wxString GetClass() const override
Get the class name.
Definition: edit_points.h:515
EDIT_POINT & m_origin
Origin point for a line.
Definition: edit_points.h:311
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is after the given point in the list.
Represent a single point that can be used for modifying items.
Definition: edit_points.h:47
bool IsHover() const
Definition: edit_points.h:174
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
std::pair< EDA_ITEM *, int > m_connected
Constraint for the point, NULL if none.
Definition: edit_points.h:202
void SetGridConstraint(GRID_CONSTRAINT_TYPE aConstraint)
Definition: edit_points.h:178
GRID_CONSTRAINT_TYPE m_gridConstraint
Describe the grid snapping behavior.
Definition: edit_points.h:198
static const int HOVER_SIZE
Definition: edit_points.h:192
virtual void ApplyConstraint()
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:165
bool WithinPoint(const VECTOR2I &aPoint, unsigned int aSize) const
Check if given point is within a square centered in the EDIT_POINT position.
Definition: edit_points.cpp:31
GRID_CONSTRAINT_TYPE
virtual void SetPosition(const VECTOR2I &aPosition)
Set new coordinates for an EDIT_POINT.
Definition: edit_points.h:106