KiCad PCB EDA Suite
multistep_geom_manager.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) 2017-2021 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 
24 #ifndef PREVIEW_ITEMS_MULTISTEP_GEOMETRY_MANAGER_H
25 #define PREVIEW_ITEMS_MULTISTEP_GEOMETRY_MANAGER_H
26 
27 #include <math/vector2d.h>
28 #include <algorithm>
29 
30 namespace KIGFX {
31 namespace PREVIEW {
32 
47 {
48 public:
50  {}
51 
60  void AddPoint( const VECTOR2I& aPt, bool aLockIn )
61  {
62  // hold onto the raw point separately to the managed geometry
63  m_lastPoint = aPt;
64 
65  // update the geometry
66  bool accepted = acceptPoint( aPt );
67 
68  // advance or regress the manager
69  if( aLockIn )
70  performStep( accepted );
71 
73  }
74 
79  {
80  performStep( false );
81 
82  // process the last point again, but in the previous step mode it doesn't matter if
83  // accepted or not, as long as the geometry is regenerated if needed.
86  }
87 
91  bool IsReset() const
92  {
93  return m_step == 0;
94  }
95 
99  void Reset()
100  {
101  m_step = 0;
103  }
104 
108  bool IsComplete() const
109  {
110  return m_step == getMaxStep();
111  }
112 
120  {
121  return m_lastPoint;
122  }
123 
127  bool HasGeometryChanged() const
128  {
129  return m_changed;
130  }
131 
137  {
138  m_changed = false;
139  }
140 
141 protected:
142 
145  {
146  m_changed = true;
147  }
148 
150  int getStep() const
151  {
152  return m_step;
153  }
154 
155 private:
156 
159  virtual bool acceptPoint( const VECTOR2I& aPt ) = 0;
160 
165  virtual int getMaxStep() const = 0;
166 
168  void performStep( bool aForward )
169  {
170  m_step += aForward ? 1 : -1;
171 
172  // clamp to allowed values
173  m_step = std::min( std::max( m_step, 0 ), getMaxStep() );
174  }
175 
177  bool m_changed = false;
178 
181 
185  int m_step = 0;
186 };
187 
188 } // PREVIEW
189 } // KIGFX
190 
191 #endif // PREVIEW_ITEMS_MULTIPOINT_GEOMETRY_MANAGER_H
bool m_changed
The last (raw) point added, which is usually the cursor position.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
virtual bool acceptPoint(const VECTOR2I &aPt)=0
< Function that accepts a point for a stage, or rejects it to return to the previous stage
virtual int getMaxStep() const =0
The highest step this manager has - used to recognize completion and to clamp the step as it advances...
void RemoveLastPoint()
Undo the last point, and move the manager back to the previous step.
void Reset()
Reset the manager to the initial state.
void AddPoint(const VECTOR2I &aPt, bool aLockIn)
Add a point to the construction manager.
int m_step
The current manager step, from 0 to some highest number that depends on the manager.
void setGeometryChanged()
< Mark the geometry as changed for clients to notice
A geometry manager that works by accepting a sequence of points and advancing though stages of geomet...
VECTOR2I GetLastPoint() const
Get the last point added (locked in or not).
void performStep(bool aForward)
Has the geometry changed such that a client should redraw?
void ClearGeometryChanged()
Clear the geometry changed flag, call after the client code has updated everything as needed.