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  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 
25 #ifndef EDIT_POINTS_H_
26 #define EDIT_POINTS_H_
27 
28 #include <base_struct.h>
30 
31 #include <list>
32 #include <deque>
33 
34 #include "edit_constraints.h"
35 
36 #include <memory>
37 
38 #include <view/view.h>
39 
47 {
48 public:
54  EDIT_POINT( const VECTOR2I& aPoint ) :
55  m_position( aPoint ) {};
56 
57  virtual ~EDIT_POINT() {}
58 
65  virtual VECTOR2I GetPosition() const
66  {
67  return m_position;
68  }
69 
75  inline int GetX() const
76  {
77  return GetPosition().x;
78  }
79 
85  inline int GetY() const
86  {
87  return GetPosition().y;
88  }
89 
97  virtual void SetPosition( const VECTOR2I& aPosition )
98  {
99  m_position = aPosition;
100  }
101 
109  bool WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const;
110 
118  {
119  m_constraint.reset( aConstraint );
120  }
121 
129  {
130  return m_constraint.get();
131  }
132 
138  inline void ClearConstraint()
139  {
140  m_constraint.reset();
141  }
142 
149  inline bool IsConstrained() const
150  {
151  return m_constraint != NULL;
152  }
153 
159  virtual void ApplyConstraint()
160  {
161  if( m_constraint )
162  m_constraint->Apply();
163  }
164 
165  bool operator==( const EDIT_POINT& aOther ) const
166  {
167  return m_position == aOther.m_position;
168  }
169 
171  static const int POINT_SIZE = 10;
172 
173 private:
176 
178  std::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > m_constraint;
179 };
180 
181 
189 class EDIT_LINE : public EDIT_POINT
190 {
191 public:
198  EDIT_LINE( EDIT_POINT& aOrigin, EDIT_POINT& aEnd ) :
199  EDIT_POINT( aOrigin.GetPosition() + ( aEnd.GetPosition() - aOrigin.GetPosition() ) / 2 ),
200  m_origin( aOrigin ), m_end( aEnd )
201  {
202  }
203 
205  virtual VECTOR2I GetPosition() const override
206  {
207  return ( m_origin.GetPosition() + m_end.GetPosition() ) / 2;
208  }
209 
211  virtual void SetPosition( const VECTOR2I& aPosition ) override
212  {
213  VECTOR2I difference = aPosition - GetPosition();
214 
215  m_origin.SetPosition( m_origin.GetPosition() + difference );
216  m_end.SetPosition( m_end.GetPosition() + difference );
217  }
218 
220  virtual void ApplyConstraint() override
221  {
224 
225  if( m_constraint )
226  m_constraint->Apply();
227  }
228 
236  {
237  m_constraint.reset( aConstraint );
238  }
239 
247  {
248  return m_constraint.get();
249  }
250 
257  {
258  return m_origin;
259  }
260 
261  const EDIT_POINT& GetOrigin() const
262  {
263  return m_origin;
264  }
265 
272  {
273  return m_end;
274  }
275 
276  const EDIT_POINT& GetEnd() const
277  {
278  return m_end;
279  }
280 
281  bool operator==( const EDIT_POINT& aOther ) const
282  {
283  return GetPosition() == aOther.GetPosition();
284  }
285 
286  bool operator==( const EDIT_LINE& aOther ) const
287  {
288  return m_origin == aOther.m_origin && m_end == aOther.m_end;
289  }
290 
291 private:
294 
296  std::shared_ptr<EDIT_CONSTRAINT<EDIT_LINE> > m_constraint;
297 };
298 
299 
305 class EDIT_POINTS : public EDA_ITEM
306 {
307 public:
313  EDIT_POINTS( EDA_ITEM* aParent );
314 
321  EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
322 
329  {
330  return m_parent;
331  }
332 
339  void AddPoint( const EDIT_POINT& aPoint )
340  {
341  m_points.push_back( aPoint );
342  }
343 
350  void AddPoint( const VECTOR2I& aPoint )
351  {
352  AddPoint( EDIT_POINT( aPoint ) );
353  }
354 
361  void AddLine( const EDIT_LINE& aLine )
362  {
363  m_lines.push_back( aLine );
364  }
365 
373  void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
374  {
375  m_lines.push_back( EDIT_LINE( aOrigin, aEnd ) );
376  }
377 
383  void AddBreak()
384  {
385  assert( m_points.size() > 0 );
386  m_contours.push_back( m_points.size() - 1 );
387  }
388 
396  int GetContourStartIdx( int aPointIdx ) const;
397 
405  int GetContourEndIdx( int aPointIdx ) const;
406 
414  bool IsContourStart( int aPointIdx ) const;
415 
423  bool IsContourEnd( int aPointIdx ) const;
424 
436  EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
437 
438  EDIT_LINE* Previous( const EDIT_LINE& aLine );
439 
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 
480  unsigned int PointsSize() const
481  {
482  return m_points.size();
483  }
484 
490  unsigned int LinesSize() const
491  {
492  return m_lines.size();
493  }
494 
496  virtual const BOX2I ViewBBox() const override;
497 
499  virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
500 
502  virtual void ViewGetLayers( int aLayers[], int& aCount ) const override
503  {
504  aCount = 1;
505  aLayers[0] = LAYER_GP_OVERLAY ;
506  }
507 
508 #if defined(DEBUG)
509  void Show( int x, std::ostream& st ) const override
510  {
511  }
512 #endif
513 
517  virtual wxString GetClass() const override
518  {
519  return wxT( "EDIT_POINTS" );
520  }
521 
522 private:
524  std::deque<EDIT_POINT> m_points;
525  std::deque<EDIT_LINE> m_lines;
526  std::list<int> m_contours;
527 };
528 
529 #endif /* EDIT_POINTS_H_ */
const EDIT_POINT & GetEnd() const
Definition: edit_points.h:276
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
>
Definition: edit_points.h:502
void AddPoint(const VECTOR2I &aPoint)
Function AddPoint()
Definition: edit_points.h:350
std::shared_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_constraint
Constraint for the point, NULL if none
Definition: edit_points.h:178
void SetConstraint(EDIT_CONSTRAINT< EDIT_LINE > *aConstraint)
Function SetConstraint()
Definition: edit_points.h:235
EDIT_LINE(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Constructor.
Definition: edit_points.h:198
std::shared_ptr< EDIT_CONSTRAINT< EDIT_LINE > > m_constraint
Constraint for the point, NULL if none
Definition: edit_points.h:296
EDA_ITEM * GetParent() const
Function GetParent()
Definition: edit_points.h:328
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:526
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:524
unsigned int LinesSize() const
Function LinesSize()
Definition: edit_points.h:490
bool WithinPoint(const VECTOR2I &aPoint, unsigned int aSize) const
Function WithinPoint()
Definition: edit_points.cpp:28
void SetConstraint(EDIT_CONSTRAINT< EDIT_POINT > *aConstraint)
Function SetConstraint()
Definition: edit_points.h:117
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:281
EDIT_LINE & Line(unsigned int aIndex)
Definition: edit_points.h:465
std::deque< EDIT_LINE > m_lines
EDIT_LINEs for modifying m_parent.
Definition: edit_points.h:525
const EDIT_POINT & Point(unsigned int aIndex) const
Definition: edit_points.h:460
void AddBreak()
Function AddBreak()
Definition: edit_points.h:383
Class EDIT_LINE.
Definition: edit_points.h:189
void AddLine(const EDIT_LINE &aLine)
Function AddLine()
Definition: edit_points.h:361
virtual const BOX2I ViewBBox() const override
>
EDA_ITEM * m_parent
Parent of the EDIT_POINTs.
Definition: edit_points.h:523
virtual VECTOR2I GetPosition() const override
>
Definition: edit_points.h:205
int GetX() const
Function GetX()
Definition: edit_points.h:75
unsigned int PointsSize() const
Function PointsSize()
Definition: edit_points.h:480
EDA_ITEM * Next() const
Definition: base_struct.h:206
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
>
EDIT_POINT & GetOrigin()
Function GetOrigin()
Definition: edit_points.h:256
void AddPoint(const EDIT_POINT &aPoint)
Function AddPoint()
Definition: edit_points.h:339
int GetContourEndIdx(int aPointIdx) const
Function GetContourEndIdx()
Definition: edit_points.cpp:87
EDIT_POINT & m_end
End point for a line.
Definition: edit_points.h:293
int GetY() const
Function GetY()
Definition: edit_points.h:85
void ClearConstraint()
Function ClearConstraint()
Definition: edit_points.h:138
EDIT_POINT & GetEnd()
Function GetEnd()
Definition: edit_points.h:271
EDIT_CONSTRAINT< EDIT_POINT > * GetConstraint() const
Function GetConstraint()
Definition: edit_points.h:128
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:165
EDIT_POINT * FindPoint(const VECTOR2I &aLocation, KIGFX::VIEW *aView)
Function FindPoint()
Definition: edit_points.cpp:45
bool operator==(const EDIT_LINE &aOther) const
Definition: edit_points.h:286
EDIT_CONSTRAINT< EDIT_LINE > * GetConstraint() const
Function GetConstraint()
Definition: edit_points.h:246
virtual void SetPosition(const VECTOR2I &aPosition) override
>
Definition: edit_points.h:211
general purpose overlay
virtual ~EDIT_POINT()
Definition: edit_points.h:57
VECTOR2I m_position
Position of EDIT_POINT
Definition: edit_points.h:175
Class EDIT_POINTS.
Definition: edit_points.h:305
virtual VECTOR2I GetPosition() const
Function GetPosition()
Definition: edit_points.h:65
void AddLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Function AddLine()
Definition: edit_points.h:373
const EDIT_LINE & Line(unsigned int aIndex) const
Definition: edit_points.h:470
bool IsContourStart(int aPointIdx) const
Function IsContourStart()
Definition: edit_points.cpp:99
Board layer functions and definitions.
bool IsContourEnd(int aPointIdx) const
Function IsContourEnd()
EDIT_POINT & Point(unsigned int aIndex)
Definition: edit_points.h:455
virtual void ApplyConstraint() override
>
Definition: edit_points.h:220
EDIT_POINTS(EDA_ITEM *aParent)
Constructor.
Definition: edit_points.cpp:39
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
static const int POINT_SIZE
Single point size in pixels
Definition: edit_points.h:171
virtual wxString GetClass() const override
Get class name.
Definition: edit_points.h:517
EDIT_POINT & m_origin
Origin point for a line.
Definition: edit_points.h:292
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Function Previous()
Class EDIT_POINT.
Definition: edit_points.h:46
bool IsConstrained() const
Function IsConstrained()
Definition: edit_points.h:149
Basic classes for most KiCad items.
Class VIEW.
Definition: view.h:58
const EDIT_POINT & GetOrigin() const
Definition: edit_points.h:261
int GetContourStartIdx(int aPointIdx) const
Function GetContourStartIdx()
Definition: edit_points.cpp:71
EDIT_POINT(const VECTOR2I &aPoint)
Constructor.
Definition: edit_points.h:54
virtual void ApplyConstraint()
Function ApplyConstraint()
Definition: edit_points.h:159
virtual void SetPosition(const VECTOR2I &aPosition)
Function SetPosition()
Definition: edit_points.h:97