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 <vector>
28 #include <math/vector2d.h>
29 #include <origin_viewitem.h>
30 
31 class TOOL_MANAGER;
32 class EDA_ITEM;
33 
34 
36 {
37 public:
38  GRID_HELPER( TOOL_MANAGER* aToolMgr );
39  ~GRID_HELPER();
40 
41  VECTOR2I GetGrid() const;
42  VECTOR2I GetOrigin() const;
43 
44  void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
45 
46  virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
47 
48  VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
49 
50  void SetSkipPoint( const VECTOR2I& aPoint )
51  {
52  m_skipPoint = aPoint;
53  }
54 
59  {
60  m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
61  }
62 
63  void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
64  bool GetSnap() const { return m_enableSnap; }
65 
66  void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
67  bool GetUseGrid() const { return m_enableGrid; }
68 
69  void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
70 
71 protected:
72  enum ANCHOR_FLAGS {
73  CORNER = 1,
74  OUTLINE = 2,
75  SNAPPABLE = 4,
76  ORIGIN = 8,
77  VERTICAL = 16,
79  };
80 
81  struct ANCHOR
82  {
83  ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, EDA_ITEM* aItem = NULL ) :
84  pos( aPos ),
85  flags( aFlags ),
86  item( aItem )
87  { };
88 
89  VECTOR2I pos;
90  int flags;
92 
93  double Distance( const VECTOR2I& aP ) const
94  {
95  return ( aP - pos ).EuclideanNorm();
96  }
97  };
98 
99  void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem )
100  {
101  m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
102  }
103 
105  {
106  m_anchors.clear();
107  }
108 
109 protected:
110  std::vector<ANCHOR> m_anchors;
111 
114 
115  bool m_enableSnap; // Allow snapping to other items on the layers
116  bool m_enableGrid; // If true, allow snapping to grid
117  bool m_enableSnapLine; // Allow drawing lines from snap points
118  ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors
119  // (NULL if not snapped)
120  VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the source
121  // point
125 };
126 
127 #endif
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:113
bool GetSnap() const
Definition: grid_helper.h:64
ANCHOR * m_snapItem
Definition: grid_helper.h:118
void addAnchor(const VECTOR2I &aPos, int aFlags, EDA_ITEM *aItem)
Definition: grid_helper.h:99
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:112
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:124
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:58
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
bool GetUseGrid() const
Definition: grid_helper.h:67
GRID_HELPER(TOOL_MANAGER *aToolMgr)
Definition: grid_helper.cpp:35
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:50
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:122
Master controller class:
Definition: tool_manager.h:52
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
Definition: grid_helper.cpp:66
#define NULL
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:93
bool m_enableSnap
Definition: grid_helper.h:115
bool m_enableSnapLine
Definition: grid_helper.h:117
void SetSnap(bool aSnap)
Definition: grid_helper.h:63
virtual VECTOR2I Align(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:94
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:66
ANCHOR(VECTOR2I aPos, int aFlags=CORNER|SNAPPABLE, EDA_ITEM *aItem=NULL)
Definition: grid_helper.h:83
VECTOR2I m_skipPoint
Definition: grid_helper.h:120
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:149
boost::optional< T > OPT
Definition: optional.h:7
void SetSnapLine(bool aSnap)
Definition: grid_helper.h:69
bool m_enableGrid
Definition: grid_helper.h:116
void ClearSkipPoint()
We clear the skip point by setting it to an unreachable position, thereby preventing matching.
Definition: grid_helper.h:58
KIGFX::ORIGIN_VIEWITEM m_viewSnapLine
Definition: grid_helper.h:123
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:110
void SetSkipPoint(const VECTOR2I &aPoint)
Definition: grid_helper.h:50
void clearAnchors()
Definition: grid_helper.h:104
VECTOR2I AlignGrid(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:82