KiCad PCB EDA Suite
polygon_geom_manager.cpp
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) 2017 Kicad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
25 
26 #include <geometry/direction45.h>
27 
28 
30  m_client( aClient ),
31  m_leaderMode( LEADER_MODE::DIRECT )
32 {}
33 
34 
36 {
37  // if this is the first point, make sure the client is happy
38  // for us to continue
40  return;
41 
42  if( m_leaderPts.size() > 1 )
43  {
44  // there are enough leader points - the next
45  // locked-in point is the end of the first leader
46  // segment
47  m_lockedPoints.push_back( m_leaderPts[1] );
48  }
49  else
50  {
51  // no leader lines, directly add the cursor
52  m_lockedPoints.push_back( aPt );
53  }
54 
55  m_client.OnGeometryChange( *this );
56 }
57 
58 
60 {
61  m_client.OnComplete( *this );
62 }
63 
64 
66 {
67  m_leaderMode = aMode;
68 }
69 
70 
72 {
73  updateLeaderPoints( aPos );
74 }
75 
76 
78 {
79  return m_lockedPoints.size() > 0;
80 }
81 
82 
84 {
85  return m_lockedPoints.size() && m_lockedPoints[0] == aPt;
86 }
87 
88 
90 {
91  if( m_lockedPoints.size() > 0 )
92  {
93  m_lockedPoints.pop_back();
94  }
95 
96  // update the new last segment (was previously
97  // locked in), reusing last constraints
98  if( m_lockedPoints.size() > 0 )
99  {
101  }
102 
103  m_client.OnGeometryChange( *this );
104 }
105 
106 
108 {
109  m_lockedPoints.clear();
110  m_leaderPts.clear();
111 
112  m_client.OnGeometryChange( *this );
113 }
114 
115 
117 {
118  SHAPE_LINE_CHAIN newChain;
119 
121  {
122  // get a restricted 45/H/V line from the last fixed point to the cursor
123  DIRECTION_45 direction( m_lockedPoints.back() - aEndPoint );
124  newChain = direction.BuildInitialTrace( m_lockedPoints.back(), aEndPoint );
125 
126  // Can also add chain back to start, but this rearely produces
127  // usable result
128  //DIRECTION_45 directionToStart( aEndPoint - m_lockedPoints.front() );
129  //newChain.Append( directionToStart.BuildInitialTrace( aEndPoint, m_lockedPoints.front() ) );
130  }
131  else
132  {
133  // direct segment
134  newChain = SHAPE_LINE_CHAIN( m_lockedPoints.back(), aEndPoint );
135  }
136 
137  // rebuild leader point list from the chain
138  m_leaderPts.clear();
139 
140  for( int i = 0; i < newChain.PointCount(); ++i )
141  {
142  m_leaderPts.push_back( newChain.Point( i ) );
143  }
144 
145  m_client.OnGeometryChange( *this );
146 }
147 
148 
149 const std::vector<VECTOR2I>& POLYGON_GEOM_MANAGER::GetLockedInPoints() const
150 {
151  return m_lockedPoints;
152 }
153 
154 
155 const std::vector<VECTOR2I>& POLYGON_GEOM_MANAGER::GetLeaderLinePoints() const
156 {
157  return m_leaderPts;
158 }
virtual void OnGeometryChange(const POLYGON_GEOM_MANAGER &aMgr)=0
Sent when the polygon geometry changes
LEADER_MODE
The kind of the leader line.
POLYGON_GEOM_MANAGER(CLIENT &aClient)
int PointCount() const
Function PointCount()
LEADER_MODE m_leaderMode
The current mode of the leader line
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
void updateLeaderPoints(const VECTOR2I &aEndPoint)
Update the leader line points based on a new endpoint (probably a cursor position) ...
const std::vector< VECTOR2I > & GetLeaderLinePoints() const
Get the points comprising the leader line (the line from the last locked-in point to the current curs...
void SetLeaderMode(LEADER_MODE aMode)
Set the leader mode to use when calculating the leader/returner lines.
const std::vector< VECTOR2I > & GetLockedInPoints() const
Get the "locked-in" points that describe the polygon itself.
Unconstrained point-to-point
"Listener" interface for a class that wants to be updated about polygon geometry changes ...
Class DIRECTION_45.
Definition: direction45.h:33
void AddPoint(const VECTOR2I &aPt)
Lock in a polygon point.
std::vector< VECTOR2I > m_lockedPoints
Point that have been "locked in"
void DeleteLastCorner()
Remove the last-added point from the polygon.
std::vector< VECTOR2I > m_leaderPts
Points in the temporary "leader" line(s)
Class SHAPE_LINE_CHAIN.
bool NewPointClosesOutline(const VECTOR2I &aPt) const
void Reset()
Clear the manager state and start again.
virtual bool OnFirstPoint()=0
Called before the first point is added - clients can do initialisation here, and can veto the start o...
VECTOR2I & Point(int aIndex)
Function Point()
virtual void OnComplete(const POLYGON_GEOM_MANAGER &aMgr)=0
Called when the polygon is complete
void SetCursorPosition(const VECTOR2I &aPos)
Set the current cursor position.
void SetFinished()
Mark the polygon finished and update the client.