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 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 
80  {
81  performStep( false );
82 
83  // process the last point again, but in the previous step mode
84  // it doesn't matter if accepted or not, as long as the geometry
85  // is regenerated if needed
88  }
89 
93  bool IsReset() const
94  {
95  return m_step == 0;
96  }
97 
101  void Reset()
102  {
103  m_step = 0;
105  }
106 
110  bool IsComplete() const
111  {
112  return m_step == getMaxStep();
113  }
114 
122  {
123  return m_lastPoint;
124  }
125 
130  bool HasGeometryChanged() const
131  {
132  return m_changed;
133  }
134 
140  {
141  m_changed = false;
142  }
143 
144 protected:
145 
148  {
149  m_changed = true;
150  }
151 
153  int getStep() const
154  {
155  return m_step;
156  }
157 
158 private:
159 
162  virtual bool acceptPoint( const VECTOR2I& aPt ) = 0;
163 
168  virtual int getMaxStep() const = 0;
169 
171  void performStep( bool aForward )
172  {
173  m_step += aForward ? 1 : -1;
174 
175  // clamp to allowed values
176  m_step = std::min( std::max( m_step, 0 ), getMaxStep() );
177  }
178 
180  bool m_changed = false;
181 
184 
189  int m_step = 0;
190 };
191 
192 } // PREVIEW
193 } // KIGFX
194 
195 #endif // PREVIEW_ITEMS_MULTIPOINT_GEOMETRY_MANAGER_H
bool m_changed
Has the gemotry changed such that a client should redraw?
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
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 recognise 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
Gets the last point added (locked in or not).
int getStep() const
Get the current stage of the manager
void performStep(bool aForward)
Moves the manager forward or backward through the stages
VECTOR2I m_lastPoint
The last (raw) point added, which is usually the cursor position
void ClearGeometryChanged()
Clear the geometry changed flag, call after the client code has updated everything as needed...
#define max(a, b)
Definition: auxiliary.h:86
#define min(a, b)
Definition: auxiliary.h:85