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, EDA_ITEM* aConnection = nullptr ) :
55  m_position( aPoint ),
56  m_connection( aConnection )
57  {
58  }
59 
60  virtual ~EDIT_POINT() {}
61 
68  virtual VECTOR2I GetPosition() const
69  {
70  return m_position;
71  }
72 
73  virtual EDA_ITEM* GetConnection() const
74  {
75  return m_connection;
76  }
77 
83  inline int GetX() const
84  {
85  return GetPosition().x;
86  }
87 
93  inline int GetY() const
94  {
95  return GetPosition().y;
96  }
97 
105  virtual void SetPosition( const VECTOR2I& aPosition )
106  {
107  m_position = aPosition;
108  }
109 
110  virtual void SetPosition( int x, int y )
111  {
112  m_position.x = x;
113  m_position.y = y;
114  }
115 
123  bool WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const;
124 
132  {
133  m_constraint.reset( aConstraint );
134  }
135 
143  {
144  return m_constraint.get();
145  }
146 
152  inline void ClearConstraint()
153  {
154  m_constraint.reset();
155  }
156 
163  inline bool IsConstrained() const
164  {
165  return m_constraint != NULL;
166  }
167 
173  virtual void ApplyConstraint()
174  {
175  if( m_constraint )
176  m_constraint->Apply();
177  }
178 
179  bool operator==( const EDIT_POINT& aOther ) const
180  {
181  return m_position == aOther.m_position;
182  }
183 
185  static const int POINT_SIZE = 10;
186 
187 private:
190 
194 
196  std::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > m_constraint;
197 };
198 
199 
207 class EDIT_LINE : public EDIT_POINT
208 {
209 public:
216  EDIT_LINE( EDIT_POINT& aOrigin, EDIT_POINT& aEnd ) :
217  EDIT_POINT( aOrigin.GetPosition() + ( aEnd.GetPosition() - aOrigin.GetPosition() ) / 2 ),
218  m_origin( aOrigin ), m_end( aEnd )
219  {
220  }
221 
223  virtual VECTOR2I GetPosition() const override
224  {
225  return ( m_origin.GetPosition() + m_end.GetPosition() ) / 2;
226  }
227 
229  virtual void SetPosition( const VECTOR2I& aPosition ) override
230  {
231  VECTOR2I difference = aPosition - GetPosition();
232 
233  m_origin.SetPosition( m_origin.GetPosition() + difference );
234  m_end.SetPosition( m_end.GetPosition() + difference );
235  }
236 
238  virtual void ApplyConstraint() override
239  {
242 
243  if( m_constraint )
244  m_constraint->Apply();
245  }
246 
254  {
255  m_constraint.reset( aConstraint );
256  }
257 
265  {
266  return m_constraint.get();
267  }
268 
275  {
276  return m_origin;
277  }
278 
279  const EDIT_POINT& GetOrigin() const
280  {
281  return m_origin;
282  }
283 
290  {
291  return m_end;
292  }
293 
294  const EDIT_POINT& GetEnd() const
295  {
296  return m_end;
297  }
298 
299  bool operator==( const EDIT_POINT& aOther ) const
300  {
301  return GetPosition() == aOther.GetPosition();
302  }
303 
304  bool operator==( const EDIT_LINE& aOther ) const
305  {
306  return m_origin == aOther.m_origin && m_end == aOther.m_end;
307  }
308 
309 private:
312 
314  std::shared_ptr<EDIT_CONSTRAINT<EDIT_LINE> > m_constraint;
315 };
316 
317 
323 class EDIT_POINTS : public EDA_ITEM
324 {
325 public:
331  EDIT_POINTS( EDA_ITEM* aParent );
332 
339  EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
340 
347  {
348  return m_parent;
349  }
350 
357  void AddPoint( const EDIT_POINT& aPoint )
358  {
359  m_points.push_back( aPoint );
360  }
361 
368  void AddPoint( const VECTOR2I& aPoint, EDA_ITEM* aConnection = nullptr )
369  {
370  AddPoint( EDIT_POINT( aPoint, aConnection ) );
371  }
372 
379  void AddLine( const EDIT_LINE& aLine )
380  {
381  m_lines.push_back( aLine );
382  }
383 
391  void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
392  {
393  m_lines.push_back( EDIT_LINE( aOrigin, aEnd ) );
394  }
395 
401  void AddBreak()
402  {
403  assert( m_points.size() > 0 );
404  m_contours.push_back( m_points.size() - 1 );
405  }
406 
414  int GetContourStartIdx( int aPointIdx ) const;
415 
423  int GetContourEndIdx( int aPointIdx ) const;
424 
432  bool IsContourStart( int aPointIdx ) const;
433 
441  bool IsContourEnd( int aPointIdx ) const;
442 
454  EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
455 
456  EDIT_LINE* Previous( const EDIT_LINE& aLine );
457 
469  EDIT_POINT* Next( const EDIT_POINT& aPoint, bool aTraverseContours = true );
470 
471  EDIT_LINE* Next( const EDIT_LINE& aLine );
472 
473  EDIT_POINT& Point( unsigned int aIndex )
474  {
475  return m_points[aIndex];
476  }
477 
478  const EDIT_POINT& Point( unsigned int aIndex ) const
479  {
480  return m_points[aIndex];
481  }
482 
483  EDIT_LINE& Line( unsigned int aIndex )
484  {
485  return m_lines[aIndex];
486  }
487 
488  const EDIT_LINE& Line( unsigned int aIndex ) const
489  {
490  return m_lines[aIndex];
491  }
492 
498  unsigned int PointsSize() const
499  {
500  return m_points.size();
501  }
502 
508  unsigned int LinesSize() const
509  {
510  return m_lines.size();
511  }
512 
514  virtual const BOX2I ViewBBox() const override;
515 
517  virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
518 
520  virtual void ViewGetLayers( int aLayers[], int& aCount ) const override
521  {
522  aCount = 1;
523  aLayers[0] = LAYER_GP_OVERLAY ;
524  }
525 
526 #if defined(DEBUG)
527  void Show( int x, std::ostream& st ) const override
528  {
529  }
530 #endif
531 
535  virtual wxString GetClass() const override
536  {
537  return wxT( "EDIT_POINTS" );
538  }
539 
540 private:
542  std::deque<EDIT_POINT> m_points;
543  std::deque<EDIT_LINE> m_lines;
544  std::list<int> m_contours;
545 };
546 
547 #endif /* EDIT_POINTS_H_ */
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: edit_points.h:520
std::shared_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_constraint
Constraint for the point, NULL if none
Definition: edit_points.h:196
void SetConstraint(EDIT_CONSTRAINT< EDIT_LINE > *aConstraint)
Function SetConstraint()
Definition: edit_points.h:253
EDIT_LINE(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Constructor.
Definition: edit_points.h:216
std::shared_ptr< EDIT_CONSTRAINT< EDIT_LINE > > m_constraint
Constraint for the point, NULL if none
Definition: edit_points.h:314
int GetContourEndIdx(int aPointIdx) const
Function GetContourEndIdx()
Definition: edit_points.cpp:88
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:544
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:542
virtual void SetPosition(int x, int y)
Definition: edit_points.h:110
void SetConstraint(EDIT_CONSTRAINT< EDIT_POINT > *aConstraint)
Function SetConstraint()
Definition: edit_points.h:131
EDIT_LINE & Line(unsigned int aIndex)
Definition: edit_points.h:483
const EDIT_LINE & Line(unsigned int aIndex) const
Definition: edit_points.h:488
std::deque< EDIT_LINE > m_lines
EDIT_LINEs for modifying m_parent.
Definition: edit_points.h:543
void AddBreak()
Function AddBreak()
Definition: edit_points.h:401
Class EDIT_LINE.
Definition: edit_points.h:207
void AddLine(const EDIT_LINE &aLine)
Function AddLine()
Definition: edit_points.h:379
virtual const BOX2I ViewBBox() const override
bool IsContourStart(int aPointIdx) const
Function IsContourStart()
EDA_ITEM * m_parent
Parent of the EDIT_POINTs.
Definition: edit_points.h:541
virtual VECTOR2I GetPosition() const override
Definition: edit_points.h:223
bool IsContourEnd(int aPointIdx) const
Function IsContourEnd()
int GetY() const
Function GetY()
Definition: edit_points.h:93
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
virtual EDA_ITEM * GetConnection() const
Definition: edit_points.h:73
EDIT_CONSTRAINT< EDIT_LINE > * GetConstraint() const
Function GetConstraint()
Definition: edit_points.h:264
EDIT_POINT & GetOrigin()
Function GetOrigin()
Definition: edit_points.h:274
void AddPoint(const EDIT_POINT &aPoint)
Function AddPoint()
Definition: edit_points.h:357
EDIT_POINT & m_end
End point for a line.
Definition: edit_points.h:311
const EDIT_POINT & GetEnd() const
Definition: edit_points.h:294
EDA_ITEM * Next() const
Definition: base_struct.h:218
void ClearConstraint()
Function ClearConstraint()
Definition: edit_points.h:152
const EDIT_POINT & Point(unsigned int aIndex) const
Definition: edit_points.h:478
EDIT_POINT & GetEnd()
Function GetEnd()
Definition: edit_points.h:289
EDIT_POINT * FindPoint(const VECTOR2I &aLocation, KIGFX::VIEW *aView)
Function FindPoint()
Definition: edit_points.cpp:46
virtual VECTOR2I GetPosition() const
Function GetPosition()
Definition: edit_points.h:68
bool operator==(const EDIT_LINE &aOther) const
Definition: edit_points.h:304
unsigned int LinesSize() const
Function LinesSize()
Definition: edit_points.h:508
virtual void SetPosition(const VECTOR2I &aPosition) override
Definition: edit_points.h:229
int GetX() const
Function GetX()
Definition: edit_points.h:83
int GetContourStartIdx(int aPointIdx) const
Function GetContourStartIdx()
Definition: edit_points.cpp:72
virtual ~EDIT_POINT()
Definition: edit_points.h:60
VECTOR2I m_position
Position of EDIT_POINT
Definition: edit_points.h:189
Class EDIT_POINTS.
Definition: edit_points.h:323
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:299
void AddLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Function AddLine()
Definition: edit_points.h:391
EDA_ITEM * GetParent() const
Function GetParent()
Definition: edit_points.h:346
void AddPoint(const VECTOR2I &aPoint, EDA_ITEM *aConnection=nullptr)
Function AddPoint()
Definition: edit_points.h:368
Board layer functions and definitions.
EDIT_POINT & Point(unsigned int aIndex)
Definition: edit_points.h:473
virtual void ApplyConstraint() override
Definition: edit_points.h:238
const EDIT_POINT & GetOrigin() const
Definition: edit_points.h:279
bool IsConstrained() const
Function IsConstrained()
Definition: edit_points.h:163
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:179
EDIT_POINTS(EDA_ITEM *aParent)
Constructor.
Definition: edit_points.cpp:40
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
static const int POINT_SIZE
Single point size in pixels
Definition: edit_points.h:185
EDIT_POINT(const VECTOR2I &aPoint, EDA_ITEM *aConnection=nullptr)
Constructor.
Definition: edit_points.h:54
unsigned int PointsSize() const
Function PointsSize()
Definition: edit_points.h:498
EDIT_CONSTRAINT< EDIT_POINT > * GetConstraint() const
Function GetConstraint()
Definition: edit_points.h:142
virtual wxString GetClass() const override
Get class name.
Definition: edit_points.h:535
EDIT_POINT & m_origin
Origin point for a line.
Definition: edit_points.h:310
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Function Previous()
Class EDIT_POINT.
Definition: edit_points.h:46
Class VIEW.
Definition: view.h:61
EDA_ITEM * m_connection
An optional item to a connected item.
Definition: edit_points.h:193
virtual void ApplyConstraint()
Function ApplyConstraint()
Definition: edit_points.h:173
bool WithinPoint(const VECTOR2I &aPoint, unsigned int aSize) const
Function WithinPoint()
Definition: edit_points.cpp:29
virtual void SetPosition(const VECTOR2I &aPosition)
Function SetPosition()
Definition: edit_points.h:105