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>
26using 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{
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{
62
63 return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
64}
65
66
68{
70
71 return VECTOR2I( origin );
72}
73
74
75void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin )
76{
77 if( aEnable )
78 {
79 m_auxAxis = aOrigin;
80 m_viewAxis.SetPosition( aOrigin );
82 }
83 else
84 {
85 m_auxAxis = std::optional<VECTOR2I>();
87 }
88}
89
90
92{
93 const VECTOR2D gridOffset( GetOrigin() );
94 const VECTOR2D grid( GetGrid() );
95
96 VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x,
97 KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y );
98
99 return nearest;
100}
101
102
104{
105 if( !canUseGrid() )
106 return aPoint;
107
108 VECTOR2I nearest = AlignGrid( aPoint );
109
110 if( !m_auxAxis )
111 return nearest;
112
113 if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
114 nearest.x = m_auxAxis->x;
115
116 if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
117 nearest.y = m_auxAxis->y;
118
119 return nearest;
120}
121
122
std::optional< VECTOR2I > m_auxAxis
Definition: grid_helper.h:132
GRID_HELPER(TOOL_MANAGER *aToolMgr)
Definition: grid_helper.cpp:35
bool m_enableGrid
Definition: grid_helper.h:137
VECTOR2I GetVisibleGrid() const
Definition: grid_helper.cpp:59
virtual VECTOR2I Align(const VECTOR2I &aPoint) const
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:131
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
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
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:145
const VECTOR2D & GetGridOrigin() const
VECTOR2D GetVisibleGridSize() const
Return the visible grid size in x and y directions.
const VECTOR2D & GetGridSize() const
Return the grid size.
void SetPosition(const VECTOR2I &aPosition) override
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:195
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1529
Master controller class:
Definition: tool_manager.h:55
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:285
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:418
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:85
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590