KiCad PCB EDA Suite
grid_helper.cpp
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) 2018-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 
25 #include <functional>
26 using namespace std::placeholders;
27 
28 #include <math/util.h> // for KiROUND
29 #include <math/vector2d.h>
30 #include <tool/tool_manager.h>
31 #include <view/view.h>
32 #include <tool/grid_helper.h>
33 
34 
36  m_toolMgr( aToolMgr )
37 {
38  m_maskTypes = ALL;
39  m_enableSnap = true;
40  m_enableSnapLine = true;
41  m_enableGrid = true;
42  m_snapItem = nullptr;
43 }
44 
45 
47 {
48 }
49 
50 
52 {
54 
55  return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
56 }
57 
58 
60 {
61  VECTOR2D origin = m_toolMgr->GetView()->GetGAL()->GetGridOrigin();
62 
63  return VECTOR2I( origin );
64 }
65 
66 
67 void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin )
68 {
69  if( aEnable )
70  {
71  m_auxAxis = aOrigin;
72  m_viewAxis.SetPosition( wxPoint( aOrigin ) );
73  m_toolMgr->GetView()->SetVisible( &m_viewAxis, true );
74  }
75  else
76  {
78  m_toolMgr->GetView()->SetVisible( &m_viewAxis, false );
79  }
80 }
81 
82 
84 {
85  const VECTOR2D gridOffset( GetOrigin() );
86  const VECTOR2D grid( GetGrid() );
87 
88  VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x,
89  KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y );
90 
91  return nearest;
92 }
93 
94 
95 VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint ) const
96 {
97  if( !canUseGrid() )
98  return aPoint;
99 
100  VECTOR2I nearest = AlignGrid( aPoint );
101 
102  if( !m_auxAxis )
103  return nearest;
104 
105  if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
106  nearest.x = m_auxAxis->x;
107 
108  if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
109  nearest.y = m_auxAxis->y;
110 
111  return nearest;
112 }
113 
114 
void SetPosition(const wxPoint &aPosition) override
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:130
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:283
ANCHOR * m_snapItem
Definition: grid_helper.h:137
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
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
GRID_HELPER(TOOL_MANAGER *aToolMgr)
Definition: grid_helper.cpp:35
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:51
Master controller class:
Definition: tool_manager.h:54
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
Definition: grid_helper.cpp:67
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
const VECTOR2D & GetGridSize() const
Return the grid size.
virtual VECTOR2I Align(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:95
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:73
boost::optional< T > OPT
Definition: optional.h:7
const VECTOR2D & GetGridOrigin() const
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1449
bool m_enableGrid
Definition: grid_helper.h:135
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:46
VECTOR2I AlignGrid(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:83