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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef EDIT_POINTS_H_
24#define EDIT_POINTS_H_
25
26#include <eda_item.h>
27#include <layer_ids.h>
28
29#include <list>
30#include <deque>
31#include <memory>
32
33#include "edit_relations.h"
34#include <view/view.h>
35
36
44{
45public:
50 EDIT_POINT( const VECTOR2I& aPoint, std::pair<EDA_ITEM*, int> aConnected = { nullptr, 0 } ) :
51 m_position( aPoint ),
52 m_isActive( false ),
53 m_isHover( false ),
54 m_isSelected( false ),
55 m_isDependent( false ),
56 m_drawCircle( false ),
57 m_drivesWholeShape( 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
132 void SetRelation( std::unique_ptr<EDIT_RELATION> aRelation )
133 {
134 m_relation = std::move( aRelation );
135 }
136
142 {
143 return m_relation.get();
144 }
145
151 virtual bool IsConstrained() const
152 {
153 return m_relation != nullptr;
154 }
155
159 virtual void ApplyRelation( const GRID_HELPER& aGrid )
160 {
161 if( m_relation )
162 m_relation->Apply( *this, aGrid );
163 }
164
165 bool IsActive() const { return m_isActive; }
166 void SetActive( bool aActive = true ) { m_isActive = aActive; }
167
168 bool IsHover() const { return m_isHover; }
169 void SetHover( bool aHover = true ) { m_isHover = aHover; }
170
171 bool IsSelected() const { return m_isSelected; }
172 void SetSelected( bool aSelected = true ) { m_isSelected = aSelected; }
173
174 bool IsDependent() const { return m_isDependent; }
175 void SetDependent( bool aDependent = true ) { m_isDependent = aDependent; }
176
177 bool DrawCircle() const { return m_drawCircle; }
178 void SetDrawCircle( bool aDrawCircle = true ) { m_drawCircle = aDrawCircle; }
179
182 bool DrivesWholeShape() const { return m_drivesWholeShape; }
183 void SetDrivesWholeShape( bool aDrives = true ) { m_drivesWholeShape = aDrives; }
184
186 void SetGridConstraint( GRID_CONSTRAINT_TYPE aConstraint ) { m_gridConstraint = aConstraint; }
187
189 void SetSnapConstraint( SNAP_CONSTRAINT_TYPE aConstraint ) { m_snapConstraint = aConstraint; }
190
191 bool operator==( const EDIT_POINT& aOther ) const
192 {
193 return m_position == aOther.m_position;
194 }
195
197 static const int POINT_SIZE = 8;
198
199#ifdef __WXMAC__
200 static const int BORDER_SIZE = 3;
201 static const int HOVER_SIZE = 6;
202#else
203 static const int BORDER_SIZE = 2;
204 static const int HOVER_SIZE = 5;
205#endif
206
207private:
217
220 std::pair<EDA_ITEM*, int> m_connected;
221
223 std::shared_ptr<EDIT_RELATION> m_relation;
224};
225
226
233class EDIT_LINE : public EDIT_POINT
234{
235public:
240 EDIT_LINE( EDIT_POINT& aOrigin, EDIT_POINT& aEnd ) :
241 EDIT_POINT( aOrigin.GetPosition()
242 + ( aEnd.GetPosition() / 2 - aOrigin.GetPosition() / 2 ) ),
243 m_origin( aOrigin ), m_end( aEnd )
244 {
246 }
247
249 virtual VECTOR2I GetPosition() const override
250 {
251 return m_origin.GetPosition() / 2 + m_end.GetPosition() / 2;
252 }
253
255 virtual void SetPosition( const VECTOR2I& aPosition ) override
256 {
257 VECTOR2I difference = aPosition - GetPosition();
258
259 m_origin.SetPosition( m_origin.GetPosition() + difference );
260 m_end.SetPosition( m_end.GetPosition() + difference );
261 }
262
264 virtual void ApplyRelation( const GRID_HELPER& aGrid ) override
265 {
266 if( m_dragPolicy )
267 m_dragPolicy->Apply( *this, aGrid );
268
269 if( m_relation )
270 m_relation->Apply( *this, aGrid );
271
272 m_origin.ApplyRelation( aGrid );
273 m_end.ApplyRelation( aGrid );
274 }
275
281 void SetRelation( std::unique_ptr<EDIT_RELATION> aRelation )
282 {
283 m_relation = std::move( aRelation );
284 }
285
291 {
292 return m_relation.get();
293 }
294
295 void SetDragPolicy( std::unique_ptr<POLYGON_EDGE_DRAG_POLICY> aPolicy )
296 {
297 m_dragPolicy = std::move( aPolicy );
298 }
299
301
305 bool IsConstrained() const override
306 {
307 return m_relation != nullptr || m_dragPolicy != nullptr;
308 }
309
314 {
315 return m_origin;
316 }
317
318 const EDIT_POINT& GetOrigin() const
319 {
320 return m_origin;
321 }
322
327 {
328 return m_end;
329 }
330
331 const EDIT_POINT& GetEnd() const
332 {
333 return m_end;
334 }
335
339 bool HasCenterPoint() const { return m_hasCenterPoint; }
340
344 void SetHasCenterPoint( bool aHasCenterPoint ) { m_hasCenterPoint = aHasCenterPoint; }
345
349 bool DrawLine() const { return m_showLine; }
350
354 void SetDrawLine( bool aShowLine ) { m_showLine = aShowLine; }
355
356 bool operator==( const EDIT_POINT& aOther ) const
357 {
358 return GetPosition() == aOther.GetPosition();
359 }
360
361 bool operator==( const EDIT_LINE& aOther ) const
362 {
363 return m_origin == aOther.m_origin && m_end == aOther.m_end;
364 }
365
366private:
369
370 bool m_hasCenterPoint = true;
371 bool m_showLine = false;
372
374 std::shared_ptr<EDIT_RELATION> m_relation;
375 std::shared_ptr<POLYGON_EDGE_DRAG_POLICY> m_dragPolicy;
376};
377
378
382class EDIT_POINTS : public EDA_ITEM
383{
384public:
388 EDIT_POINTS( EDA_ITEM* aParent );
389
396 EDIT_POINT* FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView );
397
402 {
403 return m_parent;
404 }
405
409 void Clear()
410 {
411 m_points.clear();
412 m_lines.clear();
413 m_contours.clear();
414 }
415
421 void AddPoint( const EDIT_POINT& aPoint )
422 {
423 m_points.push_back( aPoint );
424 }
425
432 void AddPoint( const VECTOR2I& aPoint, std::pair<EDA_ITEM*, int> aConnected = { nullptr, 0 } )
433 {
434 AddPoint( EDIT_POINT( aPoint, aConnected ) );
435 }
436
442 void AddLine( const EDIT_LINE& aLine )
443 {
444 m_lines.push_back( aLine );
445 }
446
453 void AddLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
454 {
455 m_lines.emplace_back( aOrigin, aEnd );
456 }
457
466 void AddIndicatorLine( EDIT_POINT& aOrigin, EDIT_POINT& aEnd )
467 {
468 EDIT_LINE& line = m_lines.emplace_back( aOrigin, aEnd );
469 line.SetHasCenterPoint( false );
470 line.SetDrawLine( true );
471 }
472
473
477 void AddBreak()
478 {
479 assert( m_points.size() > 0 );
480 m_contours.push_back( m_points.size() - 1 );
481 }
482
489 int GetContourStartIdx( int aPointIdx ) const;
490
497 int GetContourEndIdx( int aPointIdx ) const;
498
505 bool IsContourStart( int aPointIdx ) const;
506
513 bool IsContourEnd( int aPointIdx ) const;
514
525 EDIT_POINT* Previous( const EDIT_POINT& aPoint, bool aTraverseContours = true );
526
527 EDIT_LINE* Previous( const EDIT_LINE& aLine );
528
539 EDIT_POINT* Next( const EDIT_POINT& aPoint, bool aTraverseContours = true );
540
541 EDIT_LINE* Next( const EDIT_LINE& aLine );
542
543 EDIT_POINT& Point( unsigned int aIndex )
544 {
545 return m_points[aIndex];
546 }
547
548 const EDIT_POINT& Point( unsigned int aIndex ) const
549 {
550 return m_points[aIndex];
551 }
552
553 EDIT_LINE& Line( unsigned int aIndex )
554 {
555 return m_lines[aIndex];
556 }
557
558 const EDIT_LINE& Line( unsigned int aIndex ) const
559 {
560 return m_lines[aIndex];
561 }
562
566 unsigned int PointsSize() const
567 {
568 return m_points.size();
569 }
570
574 unsigned int LinesSize() const
575 {
576 return m_lines.size();
577 }
578
579 virtual const BOX2I ViewBBox() const override;
580
581 virtual void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override;
582
583 virtual std::vector<int> ViewGetLayers() const override
584 {
585 return { LAYER_GP_OVERLAY };
586 }
587
588#if defined(DEBUG)
589 void Show( int x, std::ostream& st ) const override
590 {
591 }
592#endif
593
599 virtual wxString GetClass() const override
600 {
601 return wxT( "EDIT_POINTS" );
602 }
603
604 bool SwapX() const { return m_swapX; }
605 void SetSwapX( bool aSwap ) { m_swapX = aSwap; }
606
607 bool SwapY() const { return m_swapY; }
608 void SetSwapY( bool aSwap ) { m_swapY = aSwap; }
609
617 bool LineStateVisible() const { return m_lineStateVisible; }
618 void SetLineStateVisible( bool aShow ) { m_lineStateVisible = aShow; }
619
620private:
622 bool m_swapX;
623 bool m_swapY;
624 std::deque<EDIT_POINT> m_points;
625 std::deque<EDIT_LINE> m_lines;
626 std::list<int> m_contours;
628
631};
632
633#endif /* EDIT_POINTS_H_ */
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:84
Represent a line connecting two EDIT_POINTs.
virtual void SetPosition(const VECTOR2I &aPosition) override
Return coordinates of an EDIT_POINT.
POLYGON_EDGE_DRAG_POLICY * GetDragPolicy() const
EDIT_LINE(EDIT_POINT &aOrigin, EDIT_POINT &aEnd)
const EDIT_POINT & GetEnd() const
bool operator==(const EDIT_LINE &aOther) const
void SetRelation(std::unique_ptr< EDIT_RELATION > aRelation)
Set a relation for an EDIT_LINE.
bool IsConstrained() const override
Check if line is constrained.
virtual void ApplyRelation(const GRID_HELPER &aGrid) override
Correct coordinates of an EDIT_POINT by applying its relation.
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.
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
void SetDragPolicy(std::unique_ptr< POLYGON_EDGE_DRAG_POLICY > aPolicy)
std::shared_ptr< POLYGON_EDGE_DRAG_POLICY > m_dragPolicy
void SetDrawLine(bool aShowLine)
Set if the line itself should be drawn.
std::shared_ptr< EDIT_RELATION > m_relation
Relation and geometry policy for the line, NULL if absent.
EDIT_RELATION * GetRelation() const
Return the relation imposed on an EDIT_LINE.
const EDIT_POINT & GetOrigin() const
EDIT_POINT & m_origin
Origin point for a line.
bool m_hasCenterPoint
True if the line has a (useful) center point.
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
Return the bounding box of the item covering all its layers.
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.
bool LineStateVisible() const
Whether an edge handle draws its own hover, picked and active state.
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.
void SetLineStateVisible(bool aShow)
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.
bool m_lineStateVisible
If false, an edge handle draws plain whatever state it carries.
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
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:44
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
void SetDrivesWholeShape(bool aDrives=true)
bool DrawCircle() const
std::shared_ptr< EDIT_RELATION > m_relation
Relation for the point, NULL if none.
bool IsActive() const
bool m_isHover
True if this point is being hovered over.
bool DrivesWholeShape() const
Whether dragging this handle rebuilds the whole shape instead of placing one of its points,...
EDIT_RELATION * GetRelation() const
Return the relation imposed on an EDIT_POINT.
virtual void ApplyRelation(const GRID_HELPER &aGrid)
Correct coordinates of an EDIT_POINT by applying its relation.
virtual ~EDIT_POINT()
Definition edit_points.h:64
virtual void SetPosition(int x, int y)
virtual bool IsConstrained() const
Check if point is constrained.
bool m_isSelected
True if the user has picked this point.
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
void SetDependent(bool aDependent=true)
bool IsDependent() const
bool m_drivesWholeShape
True if dragging it rebuilds the whole shape.
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:50
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.
bool m_isDependent
True if this point follows a point the user picked.
void SetHover(bool aHover=true)
bool m_drawCircle
True if the point is drawn circular.
void SetSelected(bool aSelected=true)
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 SetRelation(std::unique_ptr< EDIT_RELATION > aRelation)
Set a relation for an EDIT_POINT.
void SetActive(bool aActive=true)
static const int BORDER_SIZE
Border size when not hovering.
GRID_CONSTRAINT_TYPE GetGridConstraint() const
bool IsSelected() 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: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:275
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708