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
67 // Manual setters used when no TOOL_MANAGER/View is available (e.g. in tests)
68 void SetGridSize( const VECTOR2D& aGrid ) { m_manualGrid = aGrid; }
69 void SetVisibleGridSize( const VECTOR2D& aGrid ) { m_manualVisibleGrid = aGrid; }
70 void SetOrigin( const VECTOR2I& aOrigin ) { m_manualOrigin = aOrigin; }
71 void SetGridSnapping( bool aEnable ) { m_manualGridSnapping = aEnable; }
72
73 void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
74
75 virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
76 {
77 return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
78 }
79
80 virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
81 {
82 return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
83 }
84
85 virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
86 virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
87 const VECTOR2D& aOffset ) const;
88
89 VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
90 VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
91 const VECTOR2D& aOffset ) const;
92
96 virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
97
101 virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
102
106 virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
107
108 void SetSkipPoint( const VECTOR2I& aPoint )
109 {
110 m_skipPoint = aPoint;
111 }
112
117 {
118 m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
119 }
120
121 void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
122 bool GetSnap() const { return m_enableSnap; }
123
124 void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
125 bool GetUseGrid() const { return m_enableGrid; }
126
127 void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
128 void SetSnapLineDirections( const std::vector<VECTOR2I>& aDirections );
129 void SetSnapLineOrigin( const VECTOR2I& aOrigin );
130 void SetSnapLineEnd( const std::optional<VECTOR2I>& aEnd );
131 void ClearSnapLine();
132 std::optional<VECTOR2I> SnapToConstructionLines( const VECTOR2I& aPoint,
133 const VECTOR2I& aNearestGrid,
134 const VECTOR2D& aGrid,
135 double aSnapRange ) const;
136
137 void SetMask( int aMask ) { m_maskTypes = aMask; }
138 void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
139 void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
140
141 std::optional<VECTOR2I> GetSnappedPoint() const;
142
144 {
151
152 // This anchor comes from 'constructed' geometry (e.g. an intersection
153 // with something else), and not from some intrinsic point of an item
154 // (e.g. an endpoint)
157 };
158
159protected:
160
161 struct ANCHOR
162 {
171 ANCHOR( const VECTOR2I& aPos, int aFlags, int aPointTypes, std::vector<EDA_ITEM*> aItems ) :
172 pos( aPos ), flags( aFlags ), pointTypes( aPointTypes ),
173 items( std::move( aItems ) )
174 {
175 }
176
178 int flags;
180
183 std::vector<EDA_ITEM*> items;
184
185 double Distance( const VECTOR2I& aP ) const
186 {
187 return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
188 }
189
190 bool InvolvesItem( const EDA_ITEM& aItem ) const
191 {
192 return std::find( items.begin(), items.end(), &aItem ) != items.end();
193 }
194 };
195
196 void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem,
197 int aPointTypes = POINT_TYPE::PT_NONE )
198 {
199 addAnchor( aPos, aFlags, std::vector<EDA_ITEM*>{ aItem }, aPointTypes );
200 }
201
202 void addAnchor( const VECTOR2I& aPos, int aFlags, std::vector<EDA_ITEM*> aItems,
203 int aPointTypes )
204 {
205 if( ( aFlags & m_maskTypes ) == aFlags )
206 m_anchors.emplace_back( ANCHOR( aPos, aFlags, aPointTypes, std::move( aItems ) ) );
207 }
208
210 {
211 m_anchors.clear();
212 }
213
218 bool canUseGrid() const;
219
220 VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid,
221 const VECTOR2I& aOffset ) const;
222
223protected:
224 void showConstructionGeometry( bool aShow );
225
227
228 void updateSnapPoint( const TYPED_POINT2I& aPoint );
229
236
237 std::vector<ANCHOR> m_anchors;
238
240 std::optional<VECTOR2I> m_auxAxis;
241
242 int m_maskTypes; // Mask of allowed snap types
243
244 bool m_enableSnap; // Allow snapping to other items on the layers
245 bool m_enableGrid; // If true, allow snapping to grid
246 bool m_enableSnapLine; // Allow drawing lines from snap points
247 std::optional<ANCHOR> m_snapItem; // Pointer to the currently snapped item in m_anchors
248 // (NULL if not snapped)
249 VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
250 // source point
253
254 // Manual grid parameters used when no TOOL_MANAGER is provided
259
260private:
263
266
268 std::unique_ptr<KIGFX::ANCHOR_DEBUG> m_anchorDebug;
269};
270
271#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:70
void SetSnapLineOrigin(const VECTOR2I &aOrigin)
void SetVisibleGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:69
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:71
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:68
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)
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:75
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:80
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