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
307 bool operator==( const EDIT_POINT& aOther ) const
308 {
309 return GetPosition() == aOther.GetPosition();
310 }
311
312 bool operator==( const EDIT_LINE& aOther ) const
313 {
314 return m_origin == aOther.m_origin && m_end == aOther.m_end;
315 }
316
317private:
320
322 std::shared_ptr<EDIT_CONSTRAINT<EDIT_LINE> > m_constraint;
323};
324
325
329class EDIT_POINTS : public EDA_ITEM
330{
331public:
335 EDIT_POINTS( EDA_ITEM* aParent );
336
342 EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
343
348 {
349 return m_parent;
350 }
351
357 void AddPoint( const EDIT_POINT& aPoint )
358 {
359 m_points.push_back( aPoint );
360 }
361
367 void AddPoint( const VECTOR2I& aPoint, std::pair<EDA_ITEM*, int> aConnected = { nullptr, 0 } )
368 {
369 AddPoint( EDIT_POINT( aPoint, aConnected ) );
370 }
371
377 void AddLine( const EDIT_LINE& aLine )
378 {
379 m_lines.push_back( aLine );
380 }
381
388 void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
389 {
390 m_lines.emplace_back( aOrigin, aEnd );
391 }
392
396 void AddBreak()
397 {
398 assert( m_points.size() > 0 );
399 m_contours.push_back( m_points.size() - 1 );
400 }
401
408 int GetContourStartIdx( int aPointIdx ) const;
409
416 int GetContourEndIdx( int aPointIdx ) const;
417
424 bool IsContourStart( int aPointIdx ) const;
425
432 bool IsContourEnd( int aPointIdx ) const;
433
444 EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
445
446 EDIT_LINE* Previous( const EDIT_LINE& aLine );
447
458 EDIT_POINT* Next( const EDIT_POINT& aPoint, bool aTraverseContours = true );
459
460 EDIT_LINE* Next( const EDIT_LINE& aLine );
461
462 EDIT_POINT& Point( unsigned int aIndex )
463 {
464 return m_points[aIndex];
465 }
466
467 const EDIT_POINT& Point( unsigned int aIndex ) const
468 {
469 return m_points[aIndex];
470 }
471
472 EDIT_LINE& Line( unsigned int aIndex )
473 {
474 return m_lines[aIndex];
475 }
476
477 const EDIT_LINE& Line( unsigned int aIndex ) const
478 {
479 return m_lines[aIndex];
480 }
481
485 unsigned int PointsSize() const
486 {
487 return m_points.size();
488 }
489
493 unsigned int LinesSize() const
494 {
495 return m_lines.size();
496 }
497
499 virtual const BOX2I ViewBBox() const override;
500
502 virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
503
505 virtual void ViewGetLayers( int aLayers[], int& aCount ) const override
506 {
507 aCount = 1;
508 aLayers[0] = LAYER_GP_OVERLAY ;
509 }
510
511#if defined(DEBUG)
512 void Show( int x, std::ostream& st ) const override
513 {
514 }
515#endif
516
522 virtual wxString GetClass() const override
523 {
524 return wxT( "EDIT_POINTS" );
525 }
526
527 bool SwapX() const { return m_swapX; }
528 void SetSwapX( bool aSwap ) { m_swapX = aSwap; }
529
530 bool SwapY() const { return m_swapY; }
531 void SetSwapY( bool aSwap ) { m_swapY = aSwap; }
532
533private:
535 bool m_swapX;
536 bool m_swapY;
537 std::deque<EDIT_POINT> m_points;
538 std::deque<EDIT_LINE> m_lines;
539 std::list<int> m_contours;
541};
542
543#endif /* EDIT_POINTS_H_ */
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
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:312
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
EDIT_POINT & m_end
End point for a line.
Definition: edit_points.h:319
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
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:318
std::shared_ptr< EDIT_CONSTRAINT< EDIT_LINE > > m_constraint
Definition: edit_points.h:322
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:307
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
Definition: edit_points.h:330
unsigned int PointsSize() const
Return number of stored EDIT_POINTs.
Definition: edit_points.h:485
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:540
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:357
const EDIT_LINE & Line(unsigned int aIndex) const
Definition: edit_points.h:477
void SetSwapY(bool aSwap)
Definition: edit_points.h:531
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.
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:538
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:539
void AddPoint(const VECTOR2I &aPoint, std::pair< EDA_ITEM *, int > aConnected={ nullptr, 0 })
Add an EDIT_POINT.
Definition: edit_points.h:367
EDIT_LINE & Line(unsigned int aIndex)
Definition: edit_points.h:472
virtual wxString GetClass() const override
Get the class name.
Definition: edit_points.h:522
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Return the all the layers within the VIEW the object is painted on.
Definition: edit_points.h:505
bool m_swapY
Parent's Y coords are inverted.
Definition: edit_points.h:536
const EDIT_POINT & Point(unsigned int aIndex) const
Definition: edit_points.h:467
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:534
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:537
bool SwapX() const
Definition: edit_points.h:527
bool m_swapX
Parent's X coords are inverted.
Definition: edit_points.h:535
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:530
void SetSwapX(bool aSwap)
Definition: edit_points.h:528
void AddBreak()
Adds a break, indicating the end of a contour.
Definition: edit_points.h:396
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:347
unsigned int LinesSize() const
Return number of stored EDIT_LINEs.
Definition: edit_points.h:493
void AddLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Adds an EDIT_LINE.
Definition: edit_points.h:388
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
EDIT_POINT & Point(unsigned int aIndex)
Definition: edit_points.h:462
void AddLine(const EDIT_LINE &aLine)
Adds an EDIT_LINE.
Definition: edit_points.h:377
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:222