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 ) );
77 }
78
79 virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
80 {
81 return AlignGrid( aPoint, GetGridSize( aGrid ) );
82 }
83
84 virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
85 virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid ) const;
86
87 VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
88 VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid ) const;
89
93 virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
94
98 virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
99
103 virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
104
105 void SetSkipPoint( const VECTOR2I& aPoint )
106 {
107 m_skipPoint = aPoint;
108 }
109
114 {
115 m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
116 }
117
118 void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
119 bool GetSnap() const { return m_enableSnap; }
120
121 void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
122 bool GetUseGrid() const { return m_enableGrid; }
123
124 void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
125
126 void SetMask( int aMask ) { m_maskTypes = aMask; }
127 void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
128 void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
129
130 std::optional<VECTOR2I> GetSnappedPoint() const;
131
133 {
140
141 // This anchor comes from 'constructed' geometry (e.g. an intersection
142 // with something else), and not from some intrinsic point of an item
143 // (e.g. an endpoint)
146 };
147
148protected:
149
150 struct ANCHOR
151 {
160 ANCHOR( const VECTOR2I& aPos, int aFlags, int aPointTypes, std::vector<EDA_ITEM*> aItems ) :
161 pos( aPos ), flags( aFlags ), pointTypes( aPointTypes ),
162 items( std::move( aItems ) )
163 {
164 }
165
167 int flags;
169
172 std::vector<EDA_ITEM*> items;
173
174 double Distance( const VECTOR2I& aP ) const
175 {
176 return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
177 }
178
179 bool InvolvesItem( const EDA_ITEM& aItem ) const
180 {
181 return std::find( items.begin(), items.end(), &aItem ) != items.end();
182 }
183 };
184
185 void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem,
186 int aPointTypes = POINT_TYPE::PT_NONE )
187 {
188 addAnchor( aPos, aFlags, std::vector<EDA_ITEM*>{ aItem }, aPointTypes );
189 }
190
191 void addAnchor( const VECTOR2I& aPos, int aFlags, std::vector<EDA_ITEM*> aItems,
192 int aPointTypes )
193 {
194 if( ( aFlags & m_maskTypes ) == aFlags )
195 m_anchors.emplace_back( ANCHOR( aPos, aFlags, aPointTypes, std::move( aItems ) ) );
196 }
197
199 {
200 m_anchors.clear();
201 }
202
207 bool canUseGrid() const;
208
209 VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid ) const;
210
211protected:
212 void showConstructionGeometry( bool aShow );
213
215
216 void updateSnapPoint( const TYPED_POINT2I& aPoint );
217
224
225 std::vector<ANCHOR> m_anchors;
226
228 std::optional<VECTOR2I> m_auxAxis;
229
230 int m_maskTypes; // Mask of allowed snap types
231
232 bool m_enableSnap; // Allow snapping to other items on the layers
233 bool m_enableGrid; // If true, allow snapping to grid
234 bool m_enableSnapLine; // Allow drawing lines from snap points
235 std::optional<ANCHOR> m_snapItem; // Pointer to the currently snapped item in m_anchors
236 // (NULL if not snapped)
237 VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
238 // source point
241
242 // Manual grid parameters used when no TOOL_MANAGER is provided
247
248private:
251
254
256 std::unique_ptr<KIGFX::ANCHOR_DEBUG> m_anchorDebug;
257};
258
259#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.
Definition: grid_helper.h:250
std::optional< VECTOR2I > m_auxAxis
Definition: grid_helper.h:228
void addAnchor(const VECTOR2I &aPos, int aFlags, EDA_ITEM *aItem, int aPointTypes=POINT_TYPE::PT_NONE)
Definition: grid_helper.h:185
SNAP_MANAGER & getSnapManager()
Definition: grid_helper.h:214
VECTOR2I m_skipPoint
Definition: grid_helper.h:237
bool GetSnap() const
Definition: grid_helper.h:119
bool m_enableGrid
Definition: grid_helper.h:233
void SetSnap(bool aSnap)
Definition: grid_helper.h:118
virtual GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const
Get the coarsest grid that applies to an item.
Definition: grid_helper.h:98
void SetSkipPoint(const VECTOR2I &aPoint)
Definition: grid_helper.h:105
void showConstructionGeometry(bool aShow)
SNAP_MANAGER m_snapManager
Manage the construction geometry, snap lines, reference points, etc.
Definition: grid_helper.h:253
bool GetUseGrid() const
Definition: grid_helper.h:122
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:99
VECTOR2D m_manualVisibleGrid
Definition: grid_helper.h:244
void addAnchor(const VECTOR2I &aPos, int aFlags, std::vector< EDA_ITEM * > aItems, int aPointTypes)
Definition: grid_helper.h:191
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.
Definition: grid_helper.h:113
bool m_manualGridSnapping
Definition: grid_helper.h:246
void SetGridSnapping(bool aEnable)
Definition: grid_helper.h:70
friend void TEST_CLEAR_ANCHORS(GRID_HELPER &helper)
VECTOR2I m_manualOrigin
Definition: grid_helper.h:245
virtual GRID_HELPER_GRIDS GetSelectionGrid(const SELECTION &aSelection) const
Gets the coarsest grid that applies to a selecion of items.
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:227
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:121
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.
Definition: grid_helper.h:256
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
VECTOR2I GetGrid() const
bool m_enableSnapLine
Definition: grid_helper.h:234
bool m_enableSnap
Definition: grid_helper.h:232
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()
Definition: grid_helper.h:198
std::optional< ANCHOR > m_snapItem
Definition: grid_helper.h:235
KIGFX::ANCHOR_DEBUG * enableAndGetAnchorDebug()
Enable the anchor debug if permitted and return it.
void SetMaskFlag(int aFlag)
Definition: grid_helper.h:127
VECTOR2I computeNearest(const VECTOR2I &aPoint, const VECTOR2I &aGrid) const
void SetMask(int aMask)
Definition: grid_helper.h:126
KIGFX::SNAP_INDICATOR m_viewSnapPoint
Definition: grid_helper.h:239
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
Definition: grid_helper.h:240
void ClearMaskFlag(int aFlag)
Definition: grid_helper.h:128
void SetSnapLine(bool aSnap)
Definition: grid_helper.h:124
VECTOR2D m_manualGrid
Definition: grid_helper.h:243
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:225
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:79
View item to draw debug items for anchors.
Definition: anchor_debug.h:45
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:
Definition: tool_manager.h:62
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.
ANCHOR(const VECTOR2I &aPos, int aFlags, int aPointTypes, std::vector< EDA_ITEM * > aItems)
Definition: grid_helper.h:160
std::vector< EDA_ITEM * > items
Items that are associated with this anchor (can be more than one, e.g.
Definition: grid_helper.h:172
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:174
bool InvolvesItem(const EDA_ITEM &aItem) const
Definition: grid_helper.h:179
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695
VECTOR2< double > VECTOR2D
Definition: vector2d.h:694