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
30#include <math/vector2d.h>
35#include <tool/selection.h>
36#include <origin_viewitem.h>
37
38class TOOL_MANAGER; // Forward declaration to avoid hard dependency in tests
39
40class EDA_ITEM;
41
43{
44 // When the item doesn't match an override, use the current user grid
46
52};
53
55{
56 friend void TEST_CLEAR_ANCHORS( GRID_HELPER& helper );
57public:
59 GRID_HELPER( TOOL_MANAGER* aToolMgr, int aConstructionLayer );
60 virtual ~GRID_HELPER();
61
62 VECTOR2I GetGrid() const;
64 VECTOR2I GetOrigin() const;
65
66 // Manual setters used when no TOOL_MANAGER/View is available (e.g. in tests)
67 void SetGridSize( const VECTOR2D& aGrid ) { m_manualGrid = aGrid; }
68 void SetVisibleGridSize( const VECTOR2D& aGrid ) { m_manualVisibleGrid = aGrid; }
69 void SetOrigin( const VECTOR2I& aOrigin ) { m_manualOrigin = aOrigin; }
70 void SetGridSnapping( bool aEnable ) { m_manualGridSnapping = aEnable; }
71
72 void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
73
74 virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
75 {
76 return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
77 }
78
79 virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
80 {
81 return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
82 }
83
84 virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
85 virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
86 const VECTOR2D& aOffset ) const;
87
88 VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
89 VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
90 const VECTOR2D& aOffset ) const;
91
95 virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
96
100 virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
101
105 virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
106
107 void SetSkipPoint( const VECTOR2I& aPoint )
108 {
109 m_skipPoint = aPoint;
110 }
111
116 {
117 m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
118 }
119
120 void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
121 bool GetSnap() const { return m_enableSnap; }
122
123 void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
124 bool GetUseGrid() const { return m_enableGrid; }
125
126 void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
127
128 void SetMask( int aMask ) { m_maskTypes = aMask; }
129 void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
130 void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
131
132 std::optional<VECTOR2I> GetSnappedPoint() const;
133
135 {
142
143 // This anchor comes from 'constructed' geometry (e.g. an intersection
144 // with something else), and not from some intrinsic point of an item
145 // (e.g. an endpoint)
148 };
149
150protected:
151
152 struct ANCHOR
153 {
162 ANCHOR( const VECTOR2I& aPos, int aFlags, int aPointTypes, std::vector<EDA_ITEM*> aItems ) :
163 pos( aPos ), flags( aFlags ), pointTypes( aPointTypes ),
164 items( std::move( aItems ) )
165 {
166 }
167
169 int flags;
171
174 std::vector<EDA_ITEM*> items;
175
176 double Distance( const VECTOR2I& aP ) const
177 {
178 return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
179 }
180
181 bool InvolvesItem( const EDA_ITEM& aItem ) const
182 {
183 return std::find( items.begin(), items.end(), &aItem ) != items.end();
184 }
185 };
186
187 void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem,
188 int aPointTypes = POINT_TYPE::PT_NONE )
189 {
190 addAnchor( aPos, aFlags, std::vector<EDA_ITEM*>{ aItem }, aPointTypes );
191 }
192
193 void addAnchor( const VECTOR2I& aPos, int aFlags, std::vector<EDA_ITEM*> aItems,
194 int aPointTypes )
195 {
196 if( ( aFlags & m_maskTypes ) == aFlags )
197 m_anchors.emplace_back( ANCHOR( aPos, aFlags, aPointTypes, std::move( aItems ) ) );
198 }
199
201 {
202 m_anchors.clear();
203 }
204
209 bool canUseGrid() const;
210
211 VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid,
212 const VECTOR2I& aOffset ) const;
213
214protected:
215 void showConstructionGeometry( bool aShow );
216
218
219 void updateSnapPoint( const TYPED_POINT2I& aPoint );
220
227
228 std::vector<ANCHOR> m_anchors;
229
231 std::optional<VECTOR2I> m_auxAxis;
232
233 int m_maskTypes; // Mask of allowed snap types
234
235 bool m_enableSnap; // Allow snapping to other items on the layers
236 bool m_enableGrid; // If true, allow snapping to grid
237 bool m_enableSnapLine; // Allow drawing lines from snap points
238 std::optional<ANCHOR> m_snapItem; // Pointer to the currently snapped item in m_anchors
239 // (NULL if not snapped)
240 VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
241 // source point
244
245 // Manual grid parameters used when no TOOL_MANAGER is provided
250
251private:
254
257
259 std::unique_ptr<KIGFX::ANCHOR_DEBUG> m_anchorDebug;
260};
261
262#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
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:69
void SetVisibleGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:68
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:70
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 SetGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:67
void clearAnchors()
std::optional< ANCHOR > m_snapItem
KIGFX::ANCHOR_DEBUG * enableAndGetAnchorDebug()
Enable the anchor debug if permitted and return it.
void SetMaskFlag(int aFlag)
void SetMask(int aMask)
KIGFX::SNAP_INDICATOR m_viewSnapPoint
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition grid_helper.h:74
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:79
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:43
@ GRID_VIAS
Definition grid_helper.h:49
@ GRID_TEXT
Definition grid_helper.h:50
@ GRID_CURRENT
Definition grid_helper.h:45
@ GRID_GRAPHICS
Definition grid_helper.h:51
@ GRID_CONNECTABLE
Definition grid_helper.h:47
@ GRID_WIRES
Definition grid_helper.h:48
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