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 "tool/selection.h"
28#include <tool/tool_manager.h>
29#include <vector>
30#include <math/vector2d.h>
31#include <origin_viewitem.h>
32
33class TOOL_MANAGER;
34class EDA_ITEM;
35
37{
38 // When the item doesn't match an override, use the current user grid
40
46};
47
49{
50public:
51 GRID_HELPER( TOOL_MANAGER* aToolMgr );
52 virtual ~GRID_HELPER();
53
54 VECTOR2I GetGrid() const;
56 VECTOR2I GetOrigin() const;
57
58 void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
59
60 virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
61 {
62 return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
63 }
64
65 virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
66 {
67 return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
68 }
69
70 virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
71 virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
72 const VECTOR2D& aOffset ) const;
73
74 VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
75 VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
76 const VECTOR2D& aOffset ) const;
77
81 virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
82
86 virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
87
91 virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
92
93 void SetSkipPoint( const VECTOR2I& aPoint )
94 {
95 m_skipPoint = aPoint;
96 }
97
102 {
103 m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
104 }
105
106 void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
107 bool GetSnap() const { return m_enableSnap; }
108
109 void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
110 bool GetUseGrid() const { return m_enableGrid; }
111
112 void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
113
114 void SetMask( int aMask ) { m_maskTypes = aMask; }
115 void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
116 void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
117
126 };
127
128protected:
129
130 struct ANCHOR
131 {
132 ANCHOR( const VECTOR2I& aPos, int aFlags = CORNER | SNAPPABLE, EDA_ITEM* aItem = nullptr ) :
133 pos( aPos ),
134 flags( aFlags ),
135 item( aItem )
136 { };
137
139 int flags;
141
142 double Distance( const VECTOR2I& aP ) const
143 {
144 return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
145 }
146 };
147
148 void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem )
149 {
150 if( ( aFlags & m_maskTypes ) == aFlags )
151 m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
152 }
153
155 {
156 m_anchors.clear();
157 m_snapItem = nullptr;
158 }
159
164 bool canUseGrid() const;
165
166 VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid,
167 const VECTOR2I& aOffset ) const;
168
169protected:
170 std::vector<ANCHOR> m_anchors;
171
173 std::optional<VECTOR2I> m_auxAxis;
174
175 int m_maskTypes; // Mask of allowed snap types
176
177 bool m_enableSnap; // Allow snapping to other items on the layers
178 bool m_enableGrid; // If true, allow snapping to grid
179 bool m_enableSnapLine; // Allow drawing lines from snap points
180 ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors
181 // (NULL if not snapped)
182 VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
183 // source point
187};
188
189#endif
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
std::optional< VECTOR2I > m_auxAxis
Definition: grid_helper.h:173
VECTOR2I computeNearest(const VECTOR2I &aPoint, const VECTOR2I &aGrid, const VECTOR2I &aOffset) const
VECTOR2I m_skipPoint
Definition: grid_helper.h:182
bool GetSnap() const
Definition: grid_helper.h:107
bool m_enableGrid
Definition: grid_helper.h:178
void SetSnap(bool aSnap)
Definition: grid_helper.h:106
virtual GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const
Gets the coarsest grid that applies to an item.
Definition: grid_helper.h:86
void SetSkipPoint(const VECTOR2I &aPoint)
Definition: grid_helper.h:93
bool GetUseGrid() const
Definition: grid_helper.h:110
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:48
void ClearSkipPoint()
We clear the skip point by setting it to an unreachable position, thereby preventing matching.
Definition: grid_helper.h:101
virtual GRID_HELPER_GRIDS GetSelectionGrid(const SELECTION &aSelection) const
Gets the coarsest grid that applies to a selecion of items.
Definition: grid_helper.cpp:75
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:172
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:109
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
Definition: grid_helper.cpp:98
VECTOR2D GetVisibleGrid() const
Definition: grid_helper.cpp:61
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
Definition: grid_helper.cpp:92
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:53
ANCHOR * m_snapItem
Definition: grid_helper.h:180
bool m_enableSnapLine
Definition: grid_helper.h:179
bool m_enableSnap
Definition: grid_helper.h:177
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:67
void addAnchor(const VECTOR2I &aPos, int aFlags, EDA_ITEM *aItem)
Definition: grid_helper.h:148
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:154
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:184
void SetMaskFlag(int aFlag)
Definition: grid_helper.h:115
void SetMask(int aMask)
Definition: grid_helper.h:114
KIGFX::ORIGIN_VIEWITEM m_viewSnapLine
Definition: grid_helper.h:185
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:60
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:186
void ClearMaskFlag(int aFlag)
Definition: grid_helper.h:116
void SetSnapLine(bool aSnap)
Definition: grid_helper.h:112
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:170
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:65
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:278
GRID_HELPER_GRIDS
Definition: grid_helper.h:37
@ GRID_VIAS
Definition: grid_helper.h:43
@ GRID_TEXT
Definition: grid_helper.h:44
@ GRID_CURRENT
Definition: grid_helper.h:39
@ GRID_GRAPHICS
Definition: grid_helper.h:45
@ GRID_CONNECTABLE
Definition: grid_helper.h:41
@ GRID_WIRES
Definition: grid_helper.h:42
ANCHOR(const VECTOR2I &aPos, int aFlags=CORNER|SNAPPABLE, EDA_ITEM *aItem=nullptr)
Definition: grid_helper.h:132
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:142
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:673
VECTOR2< double > VECTOR2D
Definition: vector2d.h:672