KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_helper.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 The 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 GRID_HELPER_H
25#define GRID_HELPER_H
26
27#include <vector>
28#include <optional>
29
31#include <math/vector2d.h>
36#include <tool/selection.h>
37#include <origin_viewitem.h>
38
39class TOOL_MANAGER; // Forward declaration to avoid hard dependency in tests
40
41class EDA_ITEM;
42
44{
45 // When the item doesn't match an override, use the current user grid
47
53};
54
56{
57 friend void TEST_CLEAR_ANCHORS( GRID_HELPER& helper );
58public:
60 GRID_HELPER( TOOL_MANAGER* aToolMgr, int aConstructionLayer );
61 virtual ~GRID_HELPER();
62
63 VECTOR2I GetGrid() const;
65 VECTOR2I GetOrigin() const;
66
71 virtual void FullReset()
72 {
73 m_constructionGeomPreview.ClearSnapLine();
74 m_snapManager.Clear();
75 m_anchors.clear();
76 }
77
78 // Manual setters used when no TOOL_MANAGER/View is available (e.g. in tests)
79 void SetGridSize( const VECTOR2D& aGrid ) { m_manualGrid = aGrid; }
80 void SetVisibleGridSize( const VECTOR2D& aGrid ) { m_manualVisibleGrid = aGrid; }
81 void SetOrigin( const VECTOR2I& aOrigin ) { m_manualOrigin = aOrigin; }
82 void SetGridSnapping( bool aEnable ) { m_manualGridSnapping = aEnable; }
83
84 void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
85
86 virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
87 {
88 return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
89 }
90
91 virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
92 {
93 return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
94 }
95
96 virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
97 virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
98 const VECTOR2D& aOffset ) const;
99
100 VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
101 VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
102 const VECTOR2D& aOffset ) const;
103
107 virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
108
112 virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
113
117 virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
118
119 void SetSkipPoint( const VECTOR2I& aPoint )
120 {
121 m_skipPoint = aPoint;
122 }
123
128 {
129 m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
130 }
131
132 void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
133 bool GetSnap() const { return m_enableSnap; }
134
135 void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
136 bool GetUseGrid() const { return m_enableGrid; }
137
138 void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
139 void SetSnapLineDirections( const std::vector<VECTOR2I>& aDirections );
140 void SetSnapLineOrigin( const VECTOR2I& aOrigin );
141 void SetSnapLineEnd( const std::optional<VECTOR2I>& aEnd );
142 void ClearSnapLine();
143 std::optional<VECTOR2I> SnapToConstructionLines( const VECTOR2I& aPoint,
144 const VECTOR2I& aNearestGrid,
145 const VECTOR2D& aGrid,
146 double aSnapRange ) const;
147
148 void SetMask( int aMask ) { m_maskTypes = aMask; }
149 void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
150 void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
151
152 std::optional<VECTOR2I> GetSnappedPoint() const;
153
155 {
162
163 // This anchor comes from 'constructed' geometry (e.g. an intersection
164 // with something else), and not from some intrinsic point of an item
165 // (e.g. an endpoint)
168 };
169
170protected:
171
172 struct ANCHOR
173 {
182 ANCHOR( const VECTOR2I& aPos, int aFlags, int aPointTypes, std::vector<EDA_ITEM*> aItems ) :
183 pos( aPos ), flags( aFlags ), pointTypes( aPointTypes ),
184 items( std::move( aItems ) )
185 {
186 }
187
189 int flags;
191
194 std::vector<EDA_ITEM*> items;
195
196 double Distance( const VECTOR2I& aP ) const
197 {
198 return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
199 }
200
201 bool InvolvesItem( const EDA_ITEM& aItem ) const
202 {
203 return std::find( items.begin(), items.end(), &aItem ) != items.end();
204 }
205 };
206
207 void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem,
208 int aPointTypes = POINT_TYPE::PT_NONE )
209 {
210 addAnchor( aPos, aFlags, std::vector<EDA_ITEM*>{ aItem }, aPointTypes );
211 }
212
213 void addAnchor( const VECTOR2I& aPos, int aFlags, std::vector<EDA_ITEM*> aItems,
214 int aPointTypes )
215 {
216 if( ( aFlags & m_maskTypes ) == aFlags )
217 m_anchors.emplace_back( ANCHOR( aPos, aFlags, aPointTypes, std::move( aItems ) ) );
218 }
219
221 {
222 m_anchors.clear();
223 }
224
229 bool canUseGrid() const;
230
231 VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid,
232 const VECTOR2I& aOffset ) const;
233
234protected:
235 void showConstructionGeometry( bool aShow );
236
238
239 void updateSnapPoint( const TYPED_POINT2I& aPoint );
240
247
248 std::vector<ANCHOR> m_anchors;
249
251 std::optional<VECTOR2I> m_auxAxis;
252
253 int m_maskTypes; // Mask of allowed snap types
254
255 bool m_enableSnap; // Allow snapping to other items on the layers
256 bool m_enableGrid; // If true, allow snapping to grid
257 bool m_enableSnapLine; // Allow drawing lines from snap points
258 std::optional<ANCHOR> m_snapItem; // Pointer to the currently snapped item in m_anchors
259 // (NULL if not snapped)
260 VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
261 // source point
264
265 // Manual grid parameters used when no TOOL_MANAGER is provided
270
271private:
274
277
279 std::unique_ptr<KIGFX::ANCHOR_DEBUG> m_anchorDebug;
280};
281
282#endif
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KIGFX::CONSTRUCTION_GEOM m_constructionGeomPreview
Show construction geometry (if any) on the canvas.
std::optional< VECTOR2I > m_auxAxis
VECTOR2I computeNearest(const VECTOR2I &aPoint, const VECTOR2I &aGrid, const VECTOR2I &aOffset) const
std::optional< VECTOR2I > SnapToConstructionLines(const VECTOR2I &aPoint, const VECTOR2I &aNearestGrid, const VECTOR2D &aGrid, double aSnapRange) const
void SetSnapLineDirections(const std::vector< VECTOR2I > &aDirections)
void addAnchor(const VECTOR2I &aPos, int aFlags, EDA_ITEM *aItem, int aPointTypes=POINT_TYPE::PT_NONE)
SNAP_MANAGER & getSnapManager()
VECTOR2I m_skipPoint
bool GetSnap() const
bool m_enableGrid
void SetSnap(bool aSnap)
virtual GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const
Get the coarsest grid that applies to an item.
void SetSkipPoint(const VECTOR2I &aPoint)
void showConstructionGeometry(bool aShow)
SNAP_MANAGER m_snapManager
Manage the construction geometry, snap lines, reference points, etc.
bool GetUseGrid() const
virtual ~GRID_HELPER()
VECTOR2D m_manualVisibleGrid
void addAnchor(const VECTOR2I &aPos, int aFlags, std::vector< EDA_ITEM * > aItems, int aPointTypes)
void SetOrigin(const VECTOR2I &aOrigin)
Definition grid_helper.h:81
void SetSnapLineOrigin(const VECTOR2I &aOrigin)
void SetVisibleGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:80
void ClearSkipPoint()
Clear the skip point by setting it to an unreachable position, thereby preventing matching.
bool m_manualGridSnapping
void SetGridSnapping(bool aEnable)
Definition grid_helper.h:82
friend void TEST_CLEAR_ANCHORS(GRID_HELPER &helper)
VECTOR2I m_manualOrigin
virtual GRID_HELPER_GRIDS GetSelectionGrid(const SELECTION &aSelection) const
Gets the coarsest grid that applies to a selecion of items.
TOOL_MANAGER * m_toolMgr
void SetUseGrid(bool aSnapToGrid)
std::optional< VECTOR2I > GetSnappedPoint() const
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
VECTOR2D GetVisibleGrid() const
std::unique_ptr< KIGFX::ANCHOR_DEBUG > m_anchorDebug
#VIEW_ITEM for visualising anchor points, if enabled.
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
VECTOR2I GetGrid() const
bool m_enableSnapLine
bool m_enableSnap
VECTOR2I GetOrigin() const
bool canUseGrid() const
Check whether it is possible to use the grid – this depends both on local grid helper settings and gl...
void ClearSnapLine()
void SetGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:79
void clearAnchors()
std::optional< ANCHOR > m_snapItem
virtual void FullReset()
Reset all internal state.
Definition grid_helper.h:71
KIGFX::ANCHOR_DEBUG * enableAndGetAnchorDebug()
Enable the anchor debug if permitted and return it.
void SetMaskFlag(int aFlag)
void SetMask(int aMask)
void SetSnapLineEnd(const std::optional< VECTOR2I > &aEnd)
KIGFX::SNAP_INDICATOR m_viewSnapPoint
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition grid_helper.h:86
void updateSnapPoint(const TYPED_POINT2I &aPoint)
KIGFX::ORIGIN_VIEWITEM m_viewAxis
void ClearMaskFlag(int aFlag)
void SetSnapLine(bool aSnap)
VECTOR2D m_manualGrid
std::vector< ANCHOR > m_anchors
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition grid_helper.h:91
View item to draw debug items for anchors.
Shows construction geometry for things like line extensions, arc centers, etc.
View item to draw an origin marker with an optional snap type indicator.
A SNAP_MANAGER glues together the snap line manager and construction manager., along with some other ...
Master controller class:
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:283
GRID_HELPER_GRIDS
Definition grid_helper.h:44
@ GRID_VIAS
Definition grid_helper.h:50
@ GRID_TEXT
Definition grid_helper.h:51
@ GRID_CURRENT
Definition grid_helper.h:46
@ GRID_GRAPHICS
Definition grid_helper.h:52
@ GRID_CONNECTABLE
Definition grid_helper.h:48
@ GRID_WIRES
Definition grid_helper.h:49
STL namespace.
@ PT_NONE
No specific point type.
Definition point_types.h:42
ANCHOR(const VECTOR2I &aPos, int aFlags, int aPointTypes, std::vector< EDA_ITEM * > aItems)
std::vector< EDA_ITEM * > items
Items that are associated with this anchor (can be more than one, e.g.
double Distance(const VECTOR2I &aP) const
bool InvolvesItem(const EDA_ITEM &aItem) const
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694