KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
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>
31#include <layer_ids.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{
49public:
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 ),
57 m_drawCircle( false ),
60 m_connected( aConnected )
61 {
62 }
63
64 virtual ~EDIT_POINT() {}
65
72 virtual VECTOR2I GetPosition() const
73 {
74 return m_position;
75 }
76
80 virtual std::pair<EDA_ITEM*, int> GetConnected() const
81 {
82 return m_connected;
83 }
84
88 inline int GetX() const
89 {
90 return GetPosition().x;
91 }
92
96 inline int GetY() const
97 {
98 return GetPosition().y;
99 }
100
108 virtual void SetPosition( const VECTOR2I& aPosition )
109 {
110 m_position = aPosition;
111 }
112
113 virtual void SetPosition( int x, int y )
114 {
115 m_position.x = x;
116 m_position.y = y;
117 }
118
125 bool WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const;
126
133 {
134 m_constraint.reset( aConstraint );
135 }
136
142 {
143 return m_constraint.get();
144 }
145
149 inline void ClearConstraint()
150 {
151 m_constraint.reset();
152 }
153
159 virtual bool IsConstrained() const
160 {
161 return m_constraint != nullptr;
162 }
163
167 virtual void ApplyConstraint( const GRID_HELPER& aGrid )
168 {
169 if( m_constraint )
170 m_constraint->Apply( aGrid );
171 }
172
173 bool IsActive() const { return m_isActive; }
174 void SetActive( bool aActive = true ) { m_isActive = aActive; }
175
176 bool IsHover() const { return m_isHover; }
177 void SetHover( bool aHover = true ) { m_isHover = aHover; }
178
179 bool DrawCircle() const { return m_drawCircle; }
180 void SetDrawCircle( bool aDrawCircle = true ) { m_drawCircle = aDrawCircle; }
181
183 void SetGridConstraint( GRID_CONSTRAINT_TYPE aConstraint ) { m_gridConstraint = aConstraint; }
184
186 void SetSnapConstraint( SNAP_CONSTRAINT_TYPE aConstraint ) { m_snapConstraint = aConstraint; }
187
188 bool operator==( const EDIT_POINT& aOther ) const
189 {
190 return m_position == aOther.m_position;
191 }
192
194 static const int POINT_SIZE = 8;
195
196#ifdef __WXMAC__
197 static const int BORDER_SIZE = 3;
198 static const int HOVER_SIZE = 6;
199#else
200 static const int BORDER_SIZE = 2;
201 static const int HOVER_SIZE = 5;
202#endif
203
204private:
211
214 std::pair<EDA_ITEM*, int> m_connected;
215
217 std::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > m_constraint;
218};
219
220
227class EDIT_LINE : public EDIT_POINT
228{
229public:
234 EDIT_LINE( EDIT_POINT& aOrigin, EDIT_POINT& aEnd ) :
235 EDIT_POINT( aOrigin.GetPosition()
236 + ( aEnd.GetPosition() / 2 - aOrigin.GetPosition() / 2 ) ),
237 m_origin( aOrigin ), m_end( aEnd )
238 {
240 }
241
243 virtual VECTOR2I GetPosition() const override
244 {
245 return m_origin.GetPosition() / 2 + m_end.GetPosition() / 2;
246 }
247
249 virtual void SetPosition( const VECTOR2I& aPosition ) override
250 {
251 VECTOR2I difference = aPosition - GetPosition();
252
253 m_origin.SetPosition( m_origin.GetPosition() + difference );
254 m_end.SetPosition( m_end.GetPosition() + difference );
255 }
256
258 virtual void ApplyConstraint( const GRID_HELPER& aGrid ) override
259 {
260 if( m_constraint )
261 m_constraint->Apply( aGrid );
262
263 m_origin.ApplyConstraint( aGrid );
264 m_end.ApplyConstraint( aGrid );
265 }
266
273 {
274 m_constraint.reset( aConstraint );
275 }
276
282 {
283 return m_constraint.get();
284 }
285
289 bool IsConstrained() const override
290 {
291 return m_constraint != nullptr;
292 }
293
298 {
299 return m_origin;
300 }
301
302 const EDIT_POINT& GetOrigin() const
303 {
304 return m_origin;
305 }
306
311 {
312 return m_end;
313 }
314
315 const EDIT_POINT& GetEnd() const
316 {
317 return m_end;
318 }
319
323 bool HasCenterPoint() const { return m_hasCenterPoint; }
324
328 void SetHasCenterPoint( bool aHasCenterPoint ) { m_hasCenterPoint = aHasCenterPoint; }
329
333 bool DrawLine() const { return m_showLine; }
334
338 void SetDrawLine( bool aShowLine ) { m_showLine = aShowLine; }
339
340 bool operator==( const EDIT_POINT& aOther ) const
341 {
342 return GetPosition() == aOther.GetPosition();
343 }
344
345 bool operator==( const EDIT_LINE& aOther ) const
346 {
347 return m_origin == aOther.m_origin && m_end == aOther.m_end;
348 }
349
350private:
353
354 bool m_hasCenterPoint = true;
355 bool m_showLine = false;
356
358 std::shared_ptr<EDIT_CONSTRAINT<EDIT_LINE> > m_constraint;
359};
360
361
365class EDIT_POINTS : public EDA_ITEM
366{
367public:
371 EDIT_POINTS( EDA_ITEM* aParent );
372
378 EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
379
384 {
385 return m_parent;
386 }
387
391 void Clear()
392 {
393 m_points.clear();
394 m_lines.clear();
395 m_contours.clear();
396 }
397
403 void AddPoint( const EDIT_POINT& aPoint )
404 {
405 m_points.push_back( aPoint );
406 }
407
413 void AddPoint( const VECTOR2I& aPoint, std::pair<EDA_ITEM*, int> aConnected = { nullptr, 0 } )
414 {
415 AddPoint( EDIT_POINT( aPoint, aConnected ) );
416 }
417
423 void AddLine( const EDIT_LINE& aLine )
424 {
425 m_lines.push_back( aLine );
426 }
427
434 void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
435 {
436 m_lines.emplace_back( aOrigin, aEnd );
437 }
438
447 void AddIndicatorLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
448 {
449 EDIT_LINE& line = m_lines.emplace_back( aOrigin, aEnd );
450 line.SetHasCenterPoint( false );
451 line.SetDrawLine( true );
452 }
453
454
458 void AddBreak()
459 {
460 assert( m_points.size() > 0 );
461 m_contours.push_back( m_points.size() - 1 );
462 }
463
470 int GetContourStartIdx( int aPointIdx ) const;
471
478 int GetContourEndIdx( int aPointIdx ) const;
479
486 bool IsContourStart( int aPointIdx ) const;
487
494 bool IsContourEnd( int aPointIdx ) const;
495
506 EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
507
508 EDIT_LINE* Previous( const EDIT_LINE& aLine );
509
520 EDIT_POINT* Next( const EDIT_POINT& aPoint, bool aTraverseContours = true );
521
522 EDIT_LINE* Next( const EDIT_LINE& aLine );
523
524 EDIT_POINT& Point( unsigned int aIndex )
525 {
526 return m_points[aIndex];
527 }
528
529 const EDIT_POINT& Point( unsigned int aIndex ) const
530 {
531 return m_points[aIndex];
532 }
533
534 EDIT_LINE& Line( unsigned int aIndex )
535 {
536 return m_lines[aIndex];
537 }
538
539 const EDIT_LINE& Line( unsigned int aIndex ) const
540 {
541 return m_lines[aIndex];
542 }
543
547 unsigned int PointsSize() const
548 {
549 return m_points.size();
550 }
551
555 unsigned int LinesSize() const
556 {
557 return m_lines.size();
558 }
559
561 virtual const BOX2I ViewBBox() const override;
562
564 virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
565
567 virtual std::vector<int> ViewGetLayers() const override
568 {
569 return { LAYER_GP_OVERLAY };
570 }
571
572#if defined(DEBUG)
573 void Show( int x, std::ostream& st ) const override
574 {
575 }
576#endif
577
583 virtual wxString GetClass() const override
584 {
585 return wxT( "EDIT_POINTS" );
586 }
587
588 bool SwapX() const { return m_swapX; }
589 void SetSwapX( bool aSwap ) { m_swapX = aSwap; }
590
591 bool SwapY() const { return m_swapY; }
592 void SetSwapY( bool aSwap ) { m_swapY = aSwap; }
593
594private:
596 bool m_swapX;
597 bool m_swapY;
598 std::deque<EDIT_POINT> m_points;
599 std::deque<EDIT_LINE> m_lines;
600 std::list<int> m_contours;
602};
603
604#endif /* EDIT_POINTS_H_ */
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:39
Describe constraints between two edit handles.
Represent a line connecting two EDIT_POINTs.
virtual void SetPosition(const VECTOR2I &aPosition) override
Return coordinates of an EDIT_POINT.
EDIT_LINE(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
const EDIT_POINT & GetEnd() const
bool operator==(const EDIT_LINE &aOther) const
bool IsConstrained() const override
Check if line is constrained.
void SetHasCenterPoint(bool aHasCenterPoint)
Set if the center-point of the line should be shown.
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
bool DrawLine() const
Should the line itself be drawn, or just the end and/or center points?
bool m_showLine
True if the line itself should be drawn.
EDIT_POINT & m_end
End point for a line.
void SetConstraint(EDIT_CONSTRAINT< EDIT_LINE > *aConstraint)
Set a constraint for and EDIT_POINT.
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
void SetDrawLine(bool aShowLine)
Set if the line itself should be drawn.
EDIT_CONSTRAINT< EDIT_LINE > * GetConstraint() const
Return the constraint imposed on an EDIT_POINT.
const EDIT_POINT & GetOrigin() const
EDIT_POINT & m_origin
Origin point for a line.
std::shared_ptr< EDIT_CONSTRAINT< EDIT_LINE > > m_constraint
Constraint for the point, NULL if none.
bool m_hasCenterPoint
True if the line has a (useful) center point.
virtual void ApplyConstraint(const GRID_HELPER &aGrid) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
bool operator==(const EDIT_POINT &aOther) const
bool HasCenterPoint() const
Is the center-point of the line useful to be shown?
unsigned int PointsSize() const
Return number of stored EDIT_POINTs.
bool IsContourStart(int aPointIdx) const
Check if a point with given index is a contour origin.
bool m_allowPoints
If false, only allow editing of EDIT_LINES.
int GetContourEndIdx(int aPointIdx) const
Return index of the contour finish for a point with given index.
void AddPoint(const EDIT_POINT &aPoint)
Add an EDIT_POINT.
const EDIT_LINE & Line(unsigned int aIndex) const
void SetSwapY(bool aSwap)
virtual const BOX2I ViewBBox() const override
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is after the given point in the list.
void Clear()
Clear all stored EDIT_POINTs and EDIT_LINEs.
EDIT_POINT * Next(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is before the given point in the list.
std::deque< EDIT_LINE > m_lines
EDIT_LINEs for modifying m_parent.
std::list< int > m_contours
Indices of end contour points.
virtual std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
EDIT_POINTS(EDA_ITEM *aParent)
void AddPoint(const VECTOR2I &aPoint, std::pair< EDA_ITEM *, int > aConnected={ nullptr, 0 })
Add an EDIT_POINT.
EDIT_LINE & Line(unsigned int aIndex)
virtual wxString GetClass() const override
Get the class name.
bool m_swapY
Parent's Y coords are inverted.
const EDIT_POINT & Point(unsigned int aIndex) const
bool IsContourEnd(int aPointIdx) const
Check is a point with given index is a contour finish.
EDA_ITEM * m_parent
Parent of the EDIT_POINTs.
void AddIndicatorLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Adds an EDIT_LINE that is shown as an indicator, rather than an editable line (no center point drag,...
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
bool SwapX() const
bool m_swapX
Parent's X coords are inverted.
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.
bool SwapY() const
void SetSwapX(bool aSwap)
void AddBreak()
Adds a break, indicating the end of a contour.
int GetContourStartIdx(int aPointIdx) const
Return index of the contour origin for a point with given index.
EDA_ITEM * GetParent() const
Return parent of the EDIT_POINTS.
unsigned int LinesSize() const
Return number of stored EDIT_LINEs.
void AddLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Adds an EDIT_LINE.
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
EDIT_POINT & Point(unsigned int aIndex)
void AddLine(const EDIT_LINE &aLine)
Adds an EDIT_LINE.
Represent a single point that can be used for modifying items.
Definition edit_points.h:48
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:80
void SetGridConstraint(GRID_CONSTRAINT_TYPE aConstraint)
static const int HOVER_SIZE
Border size when hovering.
std::pair< EDA_ITEM *, int > m_connected
An optional connected item record used to mimic polyLine behavior with individual line segments.
int GetY() const
Return Y coordinate of an EDIT_POINT.
Definition edit_points.h:96
bool DrawCircle() const
void ClearConstraint()
Remove previously set constraint.
bool IsActive() const
bool m_isHover
True if this point is being hovered over.
virtual ~EDIT_POINT()
Definition edit_points.h:64
virtual void SetPosition(int x, int y)
virtual bool IsConstrained() const
Check if point is constrained.
static const int POINT_SIZE
Single point size in pixels.
GRID_CONSTRAINT_TYPE m_gridConstraint
Describe the grid snapping behavior.
SNAP_CONSTRAINT_TYPE GetSnapConstraint() const
virtual void SetPosition(const VECTOR2I &aPosition)
Set new coordinates for an EDIT_POINT.
VECTOR2I m_position
Position of EDIT_POINT.
EDIT_POINT(const VECTOR2I &aPoint, std::pair< EDA_ITEM *, int > aConnected={ nullptr, 0 })
Definition edit_points.h:53
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition edit_points.h:72
void SetSnapConstraint(SNAP_CONSTRAINT_TYPE aConstraint)
bool WithinPoint(const VECTOR2I &aPoint, unsigned int aSize) const
Check if given point is within a square centered in the EDIT_POINT position.
SNAP_CONSTRAINT_TYPE m_snapConstraint
Describe the object snapping behavior.
virtual void ApplyConstraint(const GRID_HELPER &aGrid)
Correct coordinates of an EDIT_POINT by applying previously set constraint.
void SetHover(bool aHover=true)
bool m_drawCircle
True if the point is drawn circular.
std::shared_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_constraint
Constraint for the point, NULL if none.
void SetConstraint(EDIT_CONSTRAINT< EDIT_POINT > *aConstraint)
Set a constraint for an EDIT_POINT.
EDIT_CONSTRAINT< EDIT_POINT > * GetConstraint() const
Return the constraint imposed on an EDIT_POINT.
bool IsHover() const
bool operator==(const EDIT_POINT &aOther) const
int GetX() const
Return X coordinate of an EDIT_POINT.
Definition edit_points.h:88
void SetActive(bool aActive=true)
static const int BORDER_SIZE
Border size when not hovering.
GRID_CONSTRAINT_TYPE GetGridConstraint() const
bool m_isActive
True if this point is being manipulated.
void SetDrawCircle(bool aDrawCircle=true)
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:66
SNAP_CONSTRAINT_TYPE
@ OBJECT_LAYERS
GRID_CONSTRAINT_TYPE
@ SNAP_BY_GRID
@ SNAP_TO_GRID
@ LAYER_GP_OVERLAY
General purpose overlay.
Definition layer_ids.h:279
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695