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  m_isActive( false )
58  {
59  }
60 
61  virtual ~EDIT_POINT() {}
62 
69  virtual VECTOR2I GetPosition() const
70  {
71  return m_position;
72  }
73 
74  virtual EDA_ITEM* GetConnection() const
75  {
76  return m_connection;
77  }
78 
84  inline int GetX() const
85  {
86  return GetPosition().x;
87  }
88 
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 
124  bool WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const;
125 
133  {
134  m_constraint.reset( aConstraint );
135  }
136 
144  {
145  return m_constraint.get();
146  }
147 
153  inline void ClearConstraint()
154  {
155  m_constraint.reset();
156  }
157 
164  inline bool IsConstrained() const
165  {
166  return m_constraint != NULL;
167  }
168 
174  virtual void ApplyConstraint()
175  {
176  if( m_constraint )
177  m_constraint->Apply();
178  }
179 
180  inline bool IsActive() const
181  {
182  return m_isActive;
183  }
184 
185  inline void SetActive( bool aActive = true )
186  {
187  m_isActive = aActive;
188  }
189 
190  bool operator==( const EDIT_POINT& aOther ) const
191  {
192  return m_position == aOther.m_position;
193  }
194 
196  static const int POINT_SIZE = 10;
197 
198 private:
201 
205 
207  std::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > m_constraint;
208 
211 };
212 
213 
221 class EDIT_LINE : public EDIT_POINT
222 {
223 public:
230  EDIT_LINE( EDIT_POINT& aOrigin, EDIT_POINT& aEnd ) :
231  EDIT_POINT( aOrigin.GetPosition() + ( aEnd.GetPosition() - aOrigin.GetPosition() ) / 2 ),
232  m_origin( aOrigin ), m_end( aEnd )
233  {
234  }
235 
237  virtual VECTOR2I GetPosition() const override
238  {
239  return ( m_origin.GetPosition() + m_end.GetPosition() ) / 2;
240  }
241 
243  virtual void SetPosition( const VECTOR2I& aPosition ) override
244  {
245  VECTOR2I difference = aPosition - GetPosition();
246 
247  m_origin.SetPosition( m_origin.GetPosition() + difference );
248  m_end.SetPosition( m_end.GetPosition() + difference );
249  }
250 
252  virtual void ApplyConstraint() override
253  {
254  if( m_constraint )
255  m_constraint->Apply();
256 
259  }
260 
268  {
269  m_constraint.reset( aConstraint );
270  }
271 
279  {
280  return m_constraint.get();
281  }
282 
289  {
290  return m_origin;
291  }
292 
293  const EDIT_POINT& GetOrigin() const
294  {
295  return m_origin;
296  }
297 
304  {
305  return m_end;
306  }
307 
308  const EDIT_POINT& GetEnd() const
309  {
310  return m_end;
311  }
312 
313  bool operator==( const EDIT_POINT& aOther ) const
314  {
315  return GetPosition() == aOther.GetPosition();
316  }
317 
318  bool operator==( const EDIT_LINE& aOther ) const
319  {
320  return m_origin == aOther.m_origin && m_end == aOther.m_end;
321  }
322 
323 private:
326 
328  std::shared_ptr<EDIT_CONSTRAINT<EDIT_LINE> > m_constraint;
329 };
330 
331 
337 class EDIT_POINTS : public EDA_ITEM
338 {
339 public:
345  EDIT_POINTS( EDA_ITEM* aParent );
346 
353  EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
354 
361  {
362  return m_parent;
363  }
364 
371  void AddPoint( const EDIT_POINT& aPoint )
372  {
373  m_points.push_back( aPoint );
374  }
375 
382  void AddPoint( const VECTOR2I& aPoint, EDA_ITEM* aConnection = nullptr )
383  {
384  AddPoint( EDIT_POINT( aPoint, aConnection ) );
385  }
386 
393  void AddLine( const EDIT_LINE& aLine )
394  {
395  m_lines.push_back( aLine );
396  }
397 
405  void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
406  {
407  m_lines.emplace_back( aOrigin, aEnd );
408  }
409 
415  void AddBreak()
416  {
417  assert( m_points.size() > 0 );
418  m_contours.push_back( m_points.size() - 1 );
419  }
420 
428  int GetContourStartIdx( int aPointIdx ) const;
429 
437  int GetContourEndIdx( int aPointIdx ) const;
438 
446  bool IsContourStart( int aPointIdx ) const;
447 
455  bool IsContourEnd( int aPointIdx ) const;
456 
468  EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
469 
470  EDIT_LINE* Previous( const EDIT_LINE& aLine );
471 
483  EDIT_POINT* Next( const EDIT_POINT& aPoint, bool aTraverseContours = true );
484 
485  EDIT_LINE* Next( const EDIT_LINE& aLine );
486 
487  EDIT_POINT& Point( unsigned int aIndex )
488  {
489  return m_points[aIndex];
490  }
491 
492  const EDIT_POINT& Point( unsigned int aIndex ) const
493  {
494  return m_points[aIndex];
495  }
496 
497  EDIT_LINE& Line( unsigned int aIndex )
498  {
499  return m_lines[aIndex];
500  }
501 
502  const EDIT_LINE& Line( unsigned int aIndex ) const
503  {
504  return m_lines[aIndex];
505  }
506 
512  unsigned int PointsSize() const
513  {
514  return m_points.size();
515  }
516 
522  unsigned int LinesSize() const
523  {
524  return m_lines.size();
525  }
526 
528  virtual const BOX2I ViewBBox() const override;
529 
531  virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
532 
534  virtual void ViewGetLayers( int aLayers[], int& aCount ) const override
535  {
536  aCount = 1;
537  aLayers[0] = LAYER_GP_OVERLAY ;
538  }
539 
540 #if defined(DEBUG)
541  void Show( int x, std::ostream& st ) const override
542  {
543  }
544 #endif
545 
549  virtual wxString GetClass() const override
550  {
551  return wxT( "EDIT_POINTS" );
552  }
553 
554 private:
556  std::deque<EDIT_POINT> m_points;
557  std::deque<EDIT_LINE> m_lines;
558  std::list<int> m_contours;
560 };
561 
562 #endif /* EDIT_POINTS_H_ */
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: edit_points.h:534
std::shared_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_constraint
Constraint for the point, NULL if none
Definition: edit_points.h:207
void SetConstraint(EDIT_CONSTRAINT< EDIT_LINE > *aConstraint)
Function SetConstraint()
Definition: edit_points.h:267
EDIT_LINE(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Constructor.
Definition: edit_points.h:230
std::shared_ptr< EDIT_CONSTRAINT< EDIT_LINE > > m_constraint
Constraint for the point, NULL if none
Definition: edit_points.h:328
int GetContourEndIdx(int aPointIdx) const
Function GetContourEndIdx()
Definition: edit_points.cpp:86
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:558
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:556
virtual void SetPosition(int x, int y)
Definition: edit_points.h:111
void SetConstraint(EDIT_CONSTRAINT< EDIT_POINT > *aConstraint)
Function SetConstraint()
Definition: edit_points.h:132
EDIT_LINE & Line(unsigned int aIndex)
Definition: edit_points.h:497
const EDIT_LINE & Line(unsigned int aIndex) const
Definition: edit_points.h:502
std::deque< EDIT_LINE > m_lines
EDIT_LINEs for modifying m_parent.
Definition: edit_points.h:557
void AddBreak()
Function AddBreak()
Definition: edit_points.h:415
EDIT_LINE.
Definition: edit_points.h:221
void AddLine(const EDIT_LINE &aLine)
Function AddLine()
Definition: edit_points.h:393
EDIT_POINT * Next(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Function Next()
virtual const BOX2I ViewBBox() const override
bool m_allowPoints
If false, only allow editing of EDIT_LINES.
Definition: edit_points.h:559
bool IsContourStart(int aPointIdx) const
Function IsContourStart()
Definition: edit_points.cpp:98
EDA_ITEM * m_parent
Parent of the EDIT_POINTs.
Definition: edit_points.h:555
virtual VECTOR2I GetPosition() const override
Definition: edit_points.h:237
bool IsContourEnd(int aPointIdx) const
Function IsContourEnd()
int GetY() const
Function GetY()
Definition: edit_points.h:94
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
virtual EDA_ITEM * GetConnection() const
Definition: edit_points.h:74
EDIT_CONSTRAINT< EDIT_LINE > * GetConstraint() const
Function GetConstraint()
Definition: edit_points.h:278
EDIT_POINT & GetOrigin()
Function GetOrigin()
Definition: edit_points.h:288
void AddPoint(const EDIT_POINT &aPoint)
Function AddPoint()
Definition: edit_points.h:371
EDIT_POINT & m_end
End point for a line.
Definition: edit_points.h:325
const EDIT_POINT & GetEnd() const
Definition: edit_points.h:308
bool IsActive() const
Definition: edit_points.h:180
void ClearConstraint()
Function ClearConstraint()
Definition: edit_points.h:153
const EDIT_POINT & Point(unsigned int aIndex) const
Definition: edit_points.h:492
EDIT_POINT & GetEnd()
Function GetEnd()
Definition: edit_points.h:303
void SetActive(bool aActive=true)
Definition: edit_points.h:185
EDIT_POINT * FindPoint(const VECTOR2I &aLocation, KIGFX::VIEW *aView)
Function FindPoint()
Definition: edit_points.cpp:47
virtual VECTOR2I GetPosition() const
Function GetPosition()
Definition: edit_points.h:69
#define NULL
bool operator==(const EDIT_LINE &aOther) const
Definition: edit_points.h:318
unsigned int LinesSize() const
Function LinesSize()
Definition: edit_points.h:522
virtual void SetPosition(const VECTOR2I &aPosition) override
Definition: edit_points.h:243
int GetX() const
Function GetX()
Definition: edit_points.h:84
int GetContourStartIdx(int aPointIdx) const
Function GetContourStartIdx()
Definition: edit_points.cpp:70
virtual ~EDIT_POINT()
Definition: edit_points.h:61
bool m_isActive
True if this point is being manipulated
Definition: edit_points.h:210
VECTOR2I m_position
Position of EDIT_POINT
Definition: edit_points.h:200
EDIT_POINTS.
Definition: edit_points.h:337
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:313
void AddLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Function AddLine()
Definition: edit_points.h:405
EDA_ITEM * GetParent() const
Function GetParent()
Definition: edit_points.h:360
void AddPoint(const VECTOR2I &aPoint, EDA_ITEM *aConnection=nullptr)
Function AddPoint()
Definition: edit_points.h:382
Board layer functions and definitions.
EDIT_POINT & Point(unsigned int aIndex)
Definition: edit_points.h:487
virtual void ApplyConstraint() override
Definition: edit_points.h:252
const EDIT_POINT & GetOrigin() const
Definition: edit_points.h:293
bool IsConstrained() const
Function IsConstrained()
Definition: edit_points.h:164
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:190
EDIT_POINTS(EDA_ITEM *aParent)
Constructor.
Definition: edit_points.cpp:41
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
static const int POINT_SIZE
Single point size in pixels
Definition: edit_points.h:196
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:512
EDIT_CONSTRAINT< EDIT_POINT > * GetConstraint() const
Function GetConstraint()
Definition: edit_points.h:143
virtual wxString GetClass() const override
Get class name.
Definition: edit_points.h:549
EDIT_POINT & m_origin
Origin point for a line.
Definition: edit_points.h:324
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Function Previous()
EDIT_POINT.
Definition: edit_points.h:46
VIEW.
Definition: view.h:61
EDA_ITEM * m_connection
An optional item to a connected item.
Definition: edit_points.h:204
virtual void ApplyConstraint()
Function ApplyConstraint()
Definition: edit_points.h:174
bool WithinPoint(const VECTOR2I &aPoint, unsigned int aSize) const
Function WithinPoint()
Definition: edit_points.cpp:30
virtual void SetPosition(const VECTOR2I &aPosition)
Function SetPosition()
Definition: edit_points.h:106