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