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 (C) 2020-2022 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 ),
59 m_connected( aConnected )
60 {
61 }
62
63 virtual ~EDIT_POINT() {}
64
71 virtual VECTOR2I GetPosition() const
72 {
73 return m_position;
74 }
75
79 virtual std::pair<EDA_ITEM*, int> GetConnected() const
80 {
81 return m_connected;
82 }
83
87 inline int GetX() const
88 {
89 return GetPosition().x;
90 }
91
95 inline int GetY() const
96 {
97 return GetPosition().y;
98 }
99
107 virtual void SetPosition( const VECTOR2I& aPosition )
108 {
109 m_position = aPosition;
110 }
111
112 virtual void SetPosition( int x, int y )
113 {
114 m_position.x = x;
115 m_position.y = y;
116 }
117
124 bool WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const;
125
132 {
133 m_constraint.reset( aConstraint );
134 }
135
141 {
142 return m_constraint.get();
143 }
144
148 inline void ClearConstraint()
149 {
150 m_constraint.reset();
151 }
152
158 inline bool IsConstrained() const
159 {
160 return m_constraint != nullptr;
161 }
162
166 virtual void ApplyConstraint( const GRID_HELPER& aGrid )
167 {
168 if( m_constraint )
169 m_constraint->Apply( aGrid );
170 }
171
172 bool IsActive() const { return m_isActive; }
173 void SetActive( bool aActive = true ) { m_isActive = aActive; }
174
175 bool IsHover() const { return m_isHover; }
176 void SetHover( bool aHover = true ) { m_isHover = aHover; }
177
179 void SetGridConstraint( GRID_CONSTRAINT_TYPE aConstraint ) { m_gridConstraint = aConstraint; }
180
182 void SetSnapConstraint( SNAP_CONSTRAINT_TYPE aConstraint ) { m_snapConstraint = aConstraint; }
183
184 bool operator==( const EDIT_POINT& aOther ) const
185 {
186 return m_position == aOther.m_position;
187 }
188
190 static const int POINT_SIZE = 8;
191
192#ifdef __WXMAC__
193 static const int BORDER_SIZE = 3;
194 static const int HOVER_SIZE = 6;
195#else
196 static const int BORDER_SIZE = 2;
197 static const int HOVER_SIZE = 5;
198#endif
199
200private:
206
209 std::pair<EDA_ITEM*, int> m_connected;
210
212 std::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > m_constraint;
213};
214
215
222class EDIT_LINE : public EDIT_POINT
223{
224public:
229 EDIT_LINE( EDIT_POINT& aOrigin, EDIT_POINT& aEnd ) :
230 EDIT_POINT( aOrigin.GetPosition()
231 + ( aEnd.GetPosition() / 2 - aOrigin.GetPosition() / 2 ) ),
232 m_origin( aOrigin ), m_end( aEnd )
233 {
235 }
236
238 virtual VECTOR2I GetPosition() const override
239 {
240 return m_origin.GetPosition() / 2 + m_end.GetPosition() / 2;
241 }
242
244 virtual void SetPosition( const VECTOR2I& aPosition ) override
245 {
246 VECTOR2I difference = aPosition - GetPosition();
247
248 m_origin.SetPosition( m_origin.GetPosition() + difference );
249 m_end.SetPosition( m_end.GetPosition() + difference );
250 }
251
253 virtual void ApplyConstraint( const GRID_HELPER& aGrid ) override
254 {
255 if( m_constraint )
256 m_constraint->Apply( aGrid );
257
258 m_origin.ApplyConstraint( aGrid );
259 m_end.ApplyConstraint( aGrid );
260 }
261
268 {
269 m_constraint.reset( aConstraint );
270 }
271
277 {
278 return m_constraint.get();
279 }
280
285 {
286 return m_origin;
287 }
288
289 const EDIT_POINT& GetOrigin() const
290 {
291 return m_origin;
292 }
293
298 {
299 return m_end;
300 }
301
302 const EDIT_POINT& GetEnd() const
303 {
304 return m_end;
305 }
306
310 bool HasCenterPoint() const { return m_hasCenterPoint; }
311
315 void SetHasCenterPoint( bool aHasCenterPoint ) { m_hasCenterPoint = aHasCenterPoint; }
316
320 bool DrawLine() const { return m_showLine; }
321
325 void SetDrawLine( bool aShowLine ) { m_showLine = aShowLine; }
326
327 bool operator==( const EDIT_POINT& aOther ) const
328 {
329 return GetPosition() == aOther.GetPosition();
330 }
331
332 bool operator==( const EDIT_LINE& aOther ) const
333 {
334 return m_origin == aOther.m_origin && m_end == aOther.m_end;
335 }
336
337private:
340
341 bool m_hasCenterPoint = true;
342 bool m_showLine = false;
343
345 std::shared_ptr<EDIT_CONSTRAINT<EDIT_LINE> > m_constraint;
346};
347
348
352class EDIT_POINTS : public EDA_ITEM
353{
354public:
358 EDIT_POINTS( EDA_ITEM* aParent );
359
365 EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
366
371 {
372 return m_parent;
373 }
374
378 void Clear()
379 {
380 m_points.clear();
381 m_lines.clear();
382 m_contours.clear();
383 }
384
390 void AddPoint( const EDIT_POINT& aPoint )
391 {
392 m_points.push_back( aPoint );
393 }
394
400 void AddPoint( const VECTOR2I& aPoint, std::pair<EDA_ITEM*, int> aConnected = { nullptr, 0 } )
401 {
402 AddPoint( EDIT_POINT( aPoint, aConnected ) );
403 }
404
410 void AddLine( const EDIT_LINE& aLine )
411 {
412 m_lines.push_back( aLine );
413 }
414
421 void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
422 {
423 m_lines.emplace_back( aOrigin, aEnd );
424 }
425
434 void AddIndicatorLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
435 {
436 EDIT_LINE& line = m_lines.emplace_back( aOrigin, aEnd );
437 line.SetHasCenterPoint( false );
438 line.SetDrawLine( true );
439 }
440
441
445 void AddBreak()
446 {
447 assert( m_points.size() > 0 );
448 m_contours.push_back( m_points.size() - 1 );
449 }
450
457 int GetContourStartIdx( int aPointIdx ) const;
458
465 int GetContourEndIdx( int aPointIdx ) const;
466
473 bool IsContourStart( int aPointIdx ) const;
474
481 bool IsContourEnd( int aPointIdx ) const;
482
493 EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
494
495 EDIT_LINE* Previous( const EDIT_LINE& aLine );
496
507 EDIT_POINT* Next( const EDIT_POINT& aPoint, bool aTraverseContours = true );
508
509 EDIT_LINE* Next( const EDIT_LINE& aLine );
510
511 EDIT_POINT& Point( unsigned int aIndex )
512 {
513 return m_points[aIndex];
514 }
515
516 const EDIT_POINT& Point( unsigned int aIndex ) const
517 {
518 return m_points[aIndex];
519 }
520
521 EDIT_LINE& Line( unsigned int aIndex )
522 {
523 return m_lines[aIndex];
524 }
525
526 const EDIT_LINE& Line( unsigned int aIndex ) const
527 {
528 return m_lines[aIndex];
529 }
530
534 unsigned int PointsSize() const
535 {
536 return m_points.size();
537 }
538
542 unsigned int LinesSize() const
543 {
544 return m_lines.size();
545 }
546
548 virtual const BOX2I ViewBBox() const override;
549
551 virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
552
554 virtual std::vector<int> ViewGetLayers() const override
555 {
556 return { LAYER_GP_OVERLAY };
557 }
558
559#if defined(DEBUG)
560 void Show( int x, std::ostream& st ) const override
561 {
562 }
563#endif
564
570 virtual wxString GetClass() const override
571 {
572 return wxT( "EDIT_POINTS" );
573 }
574
575 bool SwapX() const { return m_swapX; }
576 void SetSwapX( bool aSwap ) { m_swapX = aSwap; }
577
578 bool SwapY() const { return m_swapY; }
579 void SetSwapY( bool aSwap ) { m_swapY = aSwap; }
580
581private:
583 bool m_swapX;
584 bool m_swapY;
585 std::deque<EDIT_POINT> m_points;
586 std::deque<EDIT_LINE> m_lines;
587 std::list<int> m_contours;
589};
590
591#endif /* EDIT_POINTS_H_ */
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
Describe constraints between two edit handles.
Represent a line connecting two EDIT_POINTs.
Definition: edit_points.h:223
virtual void SetPosition(const VECTOR2I &aPosition) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:244
EDIT_LINE(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Definition: edit_points.h:229
const EDIT_POINT & GetEnd() const
Definition: edit_points.h:302
bool operator==(const EDIT_LINE &aOther) const
Definition: edit_points.h:332
void SetHasCenterPoint(bool aHasCenterPoint)
Set if the center-point of the line should be shown.
Definition: edit_points.h:315
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
Definition: edit_points.h:297
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
Definition: edit_points.h:284
bool DrawLine() const
Should the line itself be drawn, or just the end and/or center points?
Definition: edit_points.h:320
bool m_showLine
True if the line itself should be drawn.
Definition: edit_points.h:342
EDIT_POINT & m_end
End point for a line.
Definition: edit_points.h:339
void SetConstraint(EDIT_CONSTRAINT< EDIT_LINE > *aConstraint)
Set a constraint for and EDIT_POINT.
Definition: edit_points.h:267
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:238
void SetDrawLine(bool aShowLine)
Set if the line itself should be drawn.
Definition: edit_points.h:325
EDIT_CONSTRAINT< EDIT_LINE > * GetConstraint() const
Return the constraint imposed on an EDIT_POINT.
Definition: edit_points.h:276
const EDIT_POINT & GetOrigin() const
Definition: edit_points.h:289
EDIT_POINT & m_origin
Origin point for a line.
Definition: edit_points.h:338
std::shared_ptr< EDIT_CONSTRAINT< EDIT_LINE > > m_constraint
Definition: edit_points.h:345
bool m_hasCenterPoint
True if the line has a (useful) center point.
Definition: edit_points.h:341
virtual void ApplyConstraint(const GRID_HELPER &aGrid) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:253
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:327
bool HasCenterPoint() const
Is the center-point of the line useful to be shown?
Definition: edit_points.h:310
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
Definition: edit_points.h:353
unsigned int PointsSize() const
Return number of stored EDIT_POINTs.
Definition: edit_points.h:534
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.
Definition: edit_points.h:588
int GetContourEndIdx(int aPointIdx) const
Return index of the contour finish for a point with given index.
Definition: edit_points.cpp:93
void AddPoint(const EDIT_POINT &aPoint)
Add an EDIT_POINT.
Definition: edit_points.h:390
const EDIT_LINE & Line(unsigned int aIndex) const
Definition: edit_points.h:526
void SetSwapY(bool aSwap)
Definition: edit_points.h:579
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.
Definition: edit_points.h:378
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.
Definition: edit_points.h:586
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:587
virtual std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
Definition: edit_points.h:554
void AddPoint(const VECTOR2I &aPoint, std::pair< EDA_ITEM *, int > aConnected={ nullptr, 0 })
Add an EDIT_POINT.
Definition: edit_points.h:400
EDIT_LINE & Line(unsigned int aIndex)
Definition: edit_points.h:521
virtual wxString GetClass() const override
Get the class name.
Definition: edit_points.h:570
bool m_swapY
Parent's Y coords are inverted.
Definition: edit_points.h:584
const EDIT_POINT & Point(unsigned int aIndex) const
Definition: edit_points.h:516
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.
Definition: edit_points.h:582
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,...
Definition: edit_points.h:434
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:585
bool SwapX() const
Definition: edit_points.h:575
bool m_swapX
Parent's X coords are inverted.
Definition: edit_points.h:583
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:54
bool SwapY() const
Definition: edit_points.h:578
void SetSwapX(bool aSwap)
Definition: edit_points.h:576
void AddBreak()
Adds a break, indicating the end of a contour.
Definition: edit_points.h:445
int GetContourStartIdx(int aPointIdx) const
Return index of the contour origin for a point with given index.
Definition: edit_points.cpp:77
EDA_ITEM * GetParent() const
Return parent of the EDIT_POINTS.
Definition: edit_points.h:370
unsigned int LinesSize() const
Return number of stored EDIT_LINEs.
Definition: edit_points.h:542
void AddLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Adds an EDIT_LINE.
Definition: edit_points.h:421
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
EDIT_POINT & Point(unsigned int aIndex)
Definition: edit_points.h:511
void AddLine(const EDIT_LINE &aLine)
Adds an EDIT_LINE.
Definition: edit_points.h:410
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:79
void SetGridConstraint(GRID_CONSTRAINT_TYPE aConstraint)
Definition: edit_points.h:179
static const int HOVER_SIZE
Border size when hovering.
Definition: edit_points.h:197
std::pair< EDA_ITEM *, int > m_connected
Constraint for the point, NULL if none.
Definition: edit_points.h:209
int GetY() const
Return Y coordinate of an EDIT_POINT.
Definition: edit_points.h:95
void ClearConstraint()
Remove previously set constraint.
Definition: edit_points.h:148
bool IsActive() const
Definition: edit_points.h:172
bool m_isHover
True if this point is being hovered over.
Definition: edit_points.h:203
virtual ~EDIT_POINT()
Definition: edit_points.h:63
virtual void SetPosition(int x, int y)
Definition: edit_points.h:112
static const int POINT_SIZE
Definition: edit_points.h:190
GRID_CONSTRAINT_TYPE m_gridConstraint
Describe the grid snapping behavior.
Definition: edit_points.h:204
SNAP_CONSTRAINT_TYPE GetSnapConstraint() const
Definition: edit_points.h:181
virtual void SetPosition(const VECTOR2I &aPosition)
Set new coordinates for an EDIT_POINT.
Definition: edit_points.h:107
VECTOR2I m_position
Position of EDIT_POINT.
Definition: edit_points.h:201
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:71
void SetSnapConstraint(SNAP_CONSTRAINT_TYPE aConstraint)
Definition: edit_points.h:182
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:33
SNAP_CONSTRAINT_TYPE m_snapConstraint
Describe the object snapping behavior.
Definition: edit_points.h:205
virtual void ApplyConstraint(const GRID_HELPER &aGrid)
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:166
void SetHover(bool aHover=true)
Definition: edit_points.h:176
bool IsConstrained() const
Check if point is constrained.
Definition: edit_points.h:158
std::shared_ptr< EDIT_CONSTRAINT< EDIT_POINT > > m_constraint
Definition: edit_points.h:212
void SetConstraint(EDIT_CONSTRAINT< EDIT_POINT > *aConstraint)
Set a constraint for and EDIT_POINT.
Definition: edit_points.h:131
EDIT_CONSTRAINT< EDIT_POINT > * GetConstraint() const
Return the constraint imposed on an EDIT_POINT.
Definition: edit_points.h:140
bool IsHover() const
Definition: edit_points.h:175
bool operator==(const EDIT_POINT &aOther) const
Single point size in pixels.
Definition: edit_points.h:184
int GetX() const
Return X coordinate of an EDIT_POINT.
Definition: edit_points.h:87
void SetActive(bool aActive=true)
Definition: edit_points.h:173
static const int BORDER_SIZE
Border size when not hovering.
Definition: edit_points.h:196
GRID_CONSTRAINT_TYPE GetGridConstraint() const
Definition: edit_points.h:178
bool m_isActive
True if this point is being manipulated.
Definition: edit_points.h:202
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
SNAP_CONSTRAINT_TYPE
@ OBJECT_LAYERS
GRID_CONSTRAINT_TYPE
@ SNAP_BY_GRID
@ SNAP_TO_GRID
@ LAYER_GP_OVERLAY
general purpose overlay
Definition: layer_ids.h:219