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
193 static const int BORDER_SIZE = 2;
194
196 static const int HOVER_SIZE = 5;
197
198private:
204
207 std::pair<EDA_ITEM*, int> m_connected;
208
210 std::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > m_constraint;
211};
212
213
220class EDIT_LINE : public EDIT_POINT
221{
222public:
227 EDIT_LINE( EDIT_POINT& aOrigin, EDIT_POINT& aEnd ) :
228 EDIT_POINT( aOrigin.GetPosition()
229 + ( aEnd.GetPosition() / 2 - aOrigin.GetPosition() / 2 ) ),
230 m_origin( aOrigin ), m_end( aEnd )
231 {
233 }
234
236 virtual VECTOR2I GetPosition() const override
237 {
238 return m_origin.GetPosition() / 2 + m_end.GetPosition() / 2;
239 }
240
242 virtual void SetPosition( const VECTOR2I& aPosition ) override
243 {
244 VECTOR2I difference = aPosition - GetPosition();
245
246 m_origin.SetPosition( m_origin.GetPosition() + difference );
247 m_end.SetPosition( m_end.GetPosition() + difference );
248 }
249
251 virtual void ApplyConstraint( const GRID_HELPER& aGrid ) override
252 {
253 if( m_constraint )
254 m_constraint->Apply( aGrid );
255
256 m_origin.ApplyConstraint( aGrid );
257 m_end.ApplyConstraint( aGrid );
258 }
259
266 {
267 m_constraint.reset( aConstraint );
268 }
269
275 {
276 return m_constraint.get();
277 }
278
283 {
284 return m_origin;
285 }
286
287 const EDIT_POINT& GetOrigin() const
288 {
289 return m_origin;
290 }
291
296 {
297 return m_end;
298 }
299
300 const EDIT_POINT& GetEnd() const
301 {
302 return m_end;
303 }
304
305 bool operator==( const EDIT_POINT& aOther ) const
306 {
307 return GetPosition() == aOther.GetPosition();
308 }
309
310 bool operator==( const EDIT_LINE& aOther ) const
311 {
312 return m_origin == aOther.m_origin && m_end == aOther.m_end;
313 }
314
315private:
318
320 std::shared_ptr<EDIT_CONSTRAINT<EDIT_LINE> > m_constraint;
321};
322
323
327class EDIT_POINTS : public EDA_ITEM
328{
329public:
333 EDIT_POINTS( EDA_ITEM* aParent );
334
340 EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
341
346 {
347 return m_parent;
348 }
349
355 void AddPoint( const EDIT_POINT& aPoint )
356 {
357 m_points.push_back( aPoint );
358 }
359
365 void AddPoint( const VECTOR2I& aPoint, std::pair<EDA_ITEM*, int> aConnected = { nullptr, 0 } )
366 {
367 AddPoint( EDIT_POINT( aPoint, aConnected ) );
368 }
369
375 void AddLine( const EDIT_LINE& aLine )
376 {
377 m_lines.push_back( aLine );
378 }
379
386 void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
387 {
388 m_lines.emplace_back( aOrigin, aEnd );
389 }
390
394 void AddBreak()
395 {
396 assert( m_points.size() > 0 );
397 m_contours.push_back( m_points.size() - 1 );
398 }
399
406 int GetContourStartIdx( int aPointIdx ) const;
407
414 int GetContourEndIdx( int aPointIdx ) const;
415
422 bool IsContourStart( int aPointIdx ) const;
423
430 bool IsContourEnd( int aPointIdx ) const;
431
442 EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
443
444 EDIT_LINE* Previous( const EDIT_LINE& aLine );
445
456 EDIT_POINT* Next( const EDIT_POINT& aPoint, bool aTraverseContours = true );
457
458 EDIT_LINE* Next( const EDIT_LINE& aLine );
459
460 EDIT_POINT& Point( unsigned int aIndex )
461 {
462 return m_points[aIndex];
463 }
464
465 const EDIT_POINT& Point( unsigned int aIndex ) const
466 {
467 return m_points[aIndex];
468 }
469
470 EDIT_LINE& Line( unsigned int aIndex )
471 {
472 return m_lines[aIndex];
473 }
474
475 const EDIT_LINE& Line( unsigned int aIndex ) const
476 {
477 return m_lines[aIndex];
478 }
479
483 unsigned int PointsSize() const
484 {
485 return m_points.size();
486 }
487
491 unsigned int LinesSize() const
492 {
493 return m_lines.size();
494 }
495
497 virtual const BOX2I ViewBBox() const override;
498
500 virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
501
503 virtual void ViewGetLayers( int aLayers[], int& aCount ) const override
504 {
505 aCount = 1;
506 aLayers[0] = LAYER_GP_OVERLAY ;
507 }
508
509#if defined(DEBUG)
510 void Show( int x, std::ostream& st ) const override
511 {
512 }
513#endif
514
520 virtual wxString GetClass() const override
521 {
522 return wxT( "EDIT_POINTS" );
523 }
524
525private:
527 std::deque<EDIT_POINT> m_points;
528 std::deque<EDIT_LINE> m_lines;
529 std::list<int> m_contours;
531};
532
533#endif /* EDIT_POINTS_H_ */
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
Describe constraints between two edit handles.
Represent a line connecting two EDIT_POINTs.
Definition: edit_points.h:221
virtual void SetPosition(const VECTOR2I &aPosition) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:242
EDIT_LINE(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Definition: edit_points.h:227
const EDIT_POINT & GetEnd() const
Definition: edit_points.h:300
bool operator==(const EDIT_LINE &aOther) const
Definition: edit_points.h:310
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
Definition: edit_points.h:295
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
Definition: edit_points.h:282
EDIT_POINT & m_end
End point for a line.
Definition: edit_points.h:317
void SetConstraint(EDIT_CONSTRAINT< EDIT_LINE > *aConstraint)
Set a constraint for and EDIT_POINT.
Definition: edit_points.h:265
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
Definition: edit_points.h:236
EDIT_CONSTRAINT< EDIT_LINE > * GetConstraint() const
Return the constraint imposed on an EDIT_POINT.
Definition: edit_points.h:274
const EDIT_POINT & GetOrigin() const
Definition: edit_points.h:287
EDIT_POINT & m_origin
Origin point for a line.
Definition: edit_points.h:316
std::shared_ptr< EDIT_CONSTRAINT< EDIT_LINE > > m_constraint
Definition: edit_points.h:320
virtual void ApplyConstraint(const GRID_HELPER &aGrid) override
Correct coordinates of an EDIT_POINT by applying previously set constraint.
Definition: edit_points.h:251
bool operator==(const EDIT_POINT &aOther) const
Definition: edit_points.h:305
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
Definition: edit_points.h:328
unsigned int PointsSize() const
Return number of stored EDIT_POINTs.
Definition: edit_points.h:483
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:530
int GetContourEndIdx(int aPointIdx) const
Return index of the contour finish for a point with given index.
Definition: edit_points.cpp:89
void AddPoint(const EDIT_POINT &aPoint)
Add an EDIT_POINT.
Definition: edit_points.h:355
const EDIT_LINE & Line(unsigned int aIndex) const
Definition: edit_points.h:475
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:528
std::list< int > m_contours
Indices of end contour points.
Definition: edit_points.h:529
void AddPoint(const VECTOR2I &aPoint, std::pair< EDA_ITEM *, int > aConnected={ nullptr, 0 })
Add an EDIT_POINT.
Definition: edit_points.h:365
EDIT_LINE & Line(unsigned int aIndex)
Definition: edit_points.h:470
virtual wxString GetClass() const override
Get the class name.
Definition: edit_points.h:520
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:503
const EDIT_POINT & Point(unsigned int aIndex) const
Definition: edit_points.h:465
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:526
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
Definition: edit_points.h:527
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:50
void AddBreak()
Adds a break, indicating the end of a contour.
Definition: edit_points.h:394
int GetContourStartIdx(int aPointIdx) const
Return index of the contour origin for a point with given index.
Definition: edit_points.cpp:73
EDA_ITEM * GetParent() const
Return parent of the EDIT_POINTS.
Definition: edit_points.h:345
unsigned int LinesSize() const
Return number of stored EDIT_LINEs.
Definition: edit_points.h:491
void AddLine(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
Adds an EDIT_LINE.
Definition: edit_points.h:386
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
EDIT_POINT & Point(unsigned int aIndex)
Definition: edit_points.h:460
void AddLine(const EDIT_LINE &aLine)
Adds an EDIT_LINE.
Definition: edit_points.h:375
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
Definition: edit_points.h:196
std::pair< EDA_ITEM *, int > m_connected
Constraint for the point, NULL if none.
Definition: edit_points.h:207
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:201
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
Border size when not hovering.
Definition: edit_points.h:190
GRID_CONSTRAINT_TYPE m_gridConstraint
Describe the grid snapping behavior.
Definition: edit_points.h:202
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:199
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:203
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:210
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 hovering.
Definition: edit_points.h:193
GRID_CONSTRAINT_TYPE GetGridConstraint() const
Definition: edit_points.h:178
bool m_isActive
True if this point is being manipulated.
Definition: edit_points.h:200
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:69
SNAP_CONSTRAINT_TYPE
@ OBJECT_LAYERS
GRID_CONSTRAINT_TYPE
@ SNAP_BY_GRID
@ SNAP_TO_GRID
@ LAYER_GP_OVERLAY
general purpose overlay
Definition: layer_ids.h:218