KiCad PCB EDA Suite
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/tool_manager.h>
28#include <vector>
29#include <math/vector2d.h>
30#include <origin_viewitem.h>
31
32class TOOL_MANAGER;
33class EDA_ITEM;
34
35
37{
38public:
39 GRID_HELPER( TOOL_MANAGER* aToolMgr );
40 virtual ~GRID_HELPER();
41
42 VECTOR2I GetGrid() const;
44 VECTOR2I GetOrigin() const;
45
46 void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
47
48 virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
49
50 VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
51
52 void SetSkipPoint( const VECTOR2I& aPoint )
53 {
54 m_skipPoint = aPoint;
55 }
56
61 {
62 m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
63 }
64
65 void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
66 bool GetSnap() const { return m_enableSnap; }
67
68 void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
69 bool GetUseGrid() const { return m_enableGrid; }
70
71 void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
72
73 void SetMask( int aMask ) { m_maskTypes = aMask; }
74 void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
75 void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
76
78 CORNER = 1,
81 ORIGIN = 8,
85 };
86
87protected:
88
89 struct ANCHOR
90 {
91 ANCHOR( const VECTOR2I& aPos, int aFlags = CORNER | SNAPPABLE, EDA_ITEM* aItem = nullptr ) :
92 pos( aPos ),
93 flags( aFlags ),
94 item( aItem )
95 { };
96
98 int flags;
100
101 double Distance( const VECTOR2I& aP ) const
102 {
103 return ( aP - pos ).EuclideanNorm();
104 }
105 };
106
107 void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem )
108 {
109 if( ( aFlags & m_maskTypes ) == aFlags )
110 m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
111 }
112
114 {
115 m_anchors.clear();
116 m_snapItem = nullptr;
117 }
118
123 bool canUseGrid() const
124 {
126 }
127
128protected:
129 std::vector<ANCHOR> m_anchors;
130
132 std::optional<VECTOR2I> m_auxAxis;
133
134 int m_maskTypes; // Mask of allowed snap types
135
136 bool m_enableSnap; // Allow snapping to other items on the layers
137 bool m_enableGrid; // If true, allow snapping to grid
138 bool m_enableSnapLine; // Allow drawing lines from snap points
139 ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors
140 // (NULL if not snapped)
141 VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the source
142 // point
146};
147
148#endif
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
std::optional< VECTOR2I > m_auxAxis
Definition: grid_helper.h:132
GRID_HELPER(TOOL_MANAGER *aToolMgr)
Definition: grid_helper.cpp:35
VECTOR2I m_skipPoint
Definition: grid_helper.h:141
bool GetSnap() const
Definition: grid_helper.h:66
bool m_enableGrid
Definition: grid_helper.h:137
void SetSnap(bool aSnap)
Definition: grid_helper.h:65
VECTOR2I GetVisibleGrid() const
Definition: grid_helper.cpp:59
void SetSkipPoint(const VECTOR2I &aPoint)
Definition: grid_helper.h:52
bool GetUseGrid() const
Definition: grid_helper.h:69
virtual VECTOR2I Align(const VECTOR2I &aPoint) const
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:46
void ClearSkipPoint()
We clear the skip point by setting it to an unreachable position, thereby preventing matching.
Definition: grid_helper.h:60
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:131
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:68
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
Definition: grid_helper.cpp:75
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:51
ANCHOR * m_snapItem
Definition: grid_helper.h:139
bool m_enableSnapLine
Definition: grid_helper.h:138
bool m_enableSnap
Definition: grid_helper.h:136
VECTOR2I AlignGrid(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:91
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:67
void addAnchor(const VECTOR2I &aPos, int aFlags, EDA_ITEM *aItem)
Definition: grid_helper.h:107
bool canUseGrid() const
Check whether it is possible to use the grid – this depends both on local grid helper settings and gl...
Definition: grid_helper.h:123
void clearAnchors()
Definition: grid_helper.h:113
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:143
void SetMaskFlag(int aFlag)
Definition: grid_helper.h:74
void SetMask(int aMask)
Definition: grid_helper.h:73
KIGFX::ORIGIN_VIEWITEM m_viewSnapLine
Definition: grid_helper.h:144
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:145
void ClearMaskFlag(int aFlag)
Definition: grid_helper.h:75
void SetSnapLine(bool aSnap)
Definition: grid_helper.h:71
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:129
bool GetGridSnapping() const
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:195
Master controller class:
Definition: tool_manager.h:55
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:285
ANCHOR(const VECTOR2I &aPos, int aFlags=CORNER|SNAPPABLE, EDA_ITEM *aItem=nullptr)
Definition: grid_helper.h:91
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:101
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590