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 (C) 2021 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/tool_manager.h>
36#include <tool/selection.h>
37#include <origin_viewitem.h>
38
39class EDA_ITEM;
40
42{
43 // When the item doesn't match an override, use the current user grid
45
51};
52
54{
55public:
56 GRID_HELPER( TOOL_MANAGER* aToolMgr, int aConstructionLayer );
57 virtual ~GRID_HELPER();
58
59 VECTOR2I GetGrid() const;
61 VECTOR2I GetOrigin() const;
62
63 void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
64
65 virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
66 {
67 return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
68 }
69
70 virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
71 {
72 return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
73 }
74
75 virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
76 virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
77 const VECTOR2D& aOffset ) const;
78
79 VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
80 VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
81 const VECTOR2D& aOffset ) const;
82
86 virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
87
91 virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
92
96 virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
97
98 void SetSkipPoint( const VECTOR2I& aPoint )
99 {
100 m_skipPoint = aPoint;
101 }
102
107 {
108 m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
109 }
110
111 void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
112 bool GetSnap() const { return m_enableSnap; }
113
114 void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
115 bool GetUseGrid() const { return m_enableGrid; }
116
117 void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
118
119 void SetMask( int aMask ) { m_maskTypes = aMask; }
120 void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
121 void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
122
123 std::optional<VECTOR2I> GetSnappedPoint() const;
124
126 {
133 // This anchor comes from 'constructed' geometry (e.g. an intersection
134 // with something else), and not from some intrinsic point of an item
135 // (e.g. an endpoint)
138 };
139
140protected:
141
142 struct ANCHOR
143 {
153 ANCHOR( const VECTOR2I& aPos, int aFlags, int aPointTypes, std::vector<EDA_ITEM*> aItems ) :
154 pos( aPos ), flags( aFlags ), pointTypes( aPointTypes ),
155 items( std::move( aItems ) )
156 {
157 }
158
160 int flags;
162 // Items that are associated with this anchor (can be more than one, e.g. for an intersection)
163 std::vector<EDA_ITEM*> items;
164
165 double Distance( const VECTOR2I& aP ) const
166 {
167 return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
168 }
169
170 bool InvolvesItem( const EDA_ITEM& aItem ) const
171 {
172 return std::find( items.begin(), items.end(), &aItem ) != items.end();
173 }
174 };
175
176 void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem,
177 int aPointTypes = POINT_TYPE::PT_NONE )
178 {
179 addAnchor( aPos, aFlags, std::vector<EDA_ITEM*>{ aItem }, aPointTypes );
180 }
181
182 void addAnchor( const VECTOR2I& aPos, int aFlags, std::vector<EDA_ITEM*> aItems,
183 int aPointTypes )
184 {
185 if( ( aFlags & m_maskTypes ) == aFlags )
186 m_anchors.emplace_back( ANCHOR( aPos, aFlags, aPointTypes, std::move( aItems ) ) );
187 }
188
190 {
191 m_anchors.clear();
192 }
193
198 bool canUseGrid() const;
199
200 VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid,
201 const VECTOR2I& aOffset ) const;
202
203protected:
204 void showConstructionGeometry( bool aShow );
205
207
208 void updateSnapPoint( const TYPED_POINT2I& aPoint );
209
216
217 std::vector<ANCHOR> m_anchors;
218
220 std::optional<VECTOR2I> m_auxAxis;
221
222 int m_maskTypes; // Mask of allowed snap types
223
224 bool m_enableSnap; // Allow snapping to other items on the layers
225 bool m_enableGrid; // If true, allow snapping to grid
226 bool m_enableSnapLine; // Allow drawing lines from snap points
227 std::optional<ANCHOR> m_snapItem; // Pointer to the currently snapped item in m_anchors
228 // (NULL if not snapped)
229 VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
230 // source point
233
234private:
235 // Construction helper - this is what actually shows construction geometry
236 // (if any) on the canvas.
238
239 // Snap manager - this is what manages the construction geometry,
240 // snap lines, reference points, etc.
242
244 std::unique_ptr<KIGFX::ANCHOR_DEBUG> m_anchorDebug;
245};
246
247#endif
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
KIGFX::CONSTRUCTION_GEOM m_constructionGeomPreview
Definition: grid_helper.h:237
std::optional< VECTOR2I > m_auxAxis
Definition: grid_helper.h:220
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)
Definition: grid_helper.h:176
SNAP_MANAGER & getSnapManager()
Definition: grid_helper.h:206
VECTOR2I m_skipPoint
Definition: grid_helper.h:229
bool GetSnap() const
Definition: grid_helper.h:112
bool m_enableGrid
Definition: grid_helper.h:225
void SetSnap(bool aSnap)
Definition: grid_helper.h:111
virtual GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const
Gets the coarsest grid that applies to an item.
Definition: grid_helper.h:91
void SetSkipPoint(const VECTOR2I &aPoint)
Definition: grid_helper.h:98
void showConstructionGeometry(bool aShow)
Definition: grid_helper.cpp:99
SNAP_MANAGER m_snapManager
Definition: grid_helper.h:241
bool GetUseGrid() const
Definition: grid_helper.h:115
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:75
void addAnchor(const VECTOR2I &aPos, int aFlags, std::vector< EDA_ITEM * > aItems, int aPointTypes)
Definition: grid_helper.h:182
void ClearSkipPoint()
We clear the skip point by setting it to an unreachable position, thereby preventing matching.
Definition: grid_helper.h:106
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:219
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:114
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:244
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:226
bool m_enableSnap
Definition: grid_helper.h:224
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 clearAnchors()
Definition: grid_helper.h:189
std::optional< ANCHOR > m_snapItem
Definition: grid_helper.h:227
KIGFX::ANCHOR_DEBUG * enableAndGetAnchorDebug()
Enable the anchor debug if permitted and return it.
Definition: grid_helper.cpp:85
void SetMaskFlag(int aFlag)
Definition: grid_helper.h:120
void SetMask(int aMask)
Definition: grid_helper.h:119
KIGFX::SNAP_INDICATOR m_viewSnapPoint
Definition: grid_helper.h:231
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:65
void updateSnapPoint(const TYPED_POINT2I &aPoint)
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:232
void ClearMaskFlag(int aFlag)
Definition: grid_helper.h:121
void SetSnapLine(bool aSnap)
Definition: grid_helper.h:117
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:217
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:70
View item to draw debug items for anchors.
Definition: anchor_debug.h:44
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:42
@ GRID_VIAS
Definition: grid_helper.h:48
@ GRID_TEXT
Definition: grid_helper.h:49
@ GRID_CURRENT
Definition: grid_helper.h:44
@ GRID_GRAPHICS
Definition: grid_helper.h:50
@ GRID_CONNECTABLE
Definition: grid_helper.h:46
@ GRID_WIRES
Definition: grid_helper.h:47
STL namespace.
ANCHOR(const VECTOR2I &aPos, int aFlags, int aPointTypes, std::vector< EDA_ITEM * > aItems)
Definition: grid_helper.h:153
std::vector< EDA_ITEM * > items
Definition: grid_helper.h:163
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:165
bool InvolvesItem(const EDA_ITEM &aItem) const
Definition: grid_helper.h:170
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690