KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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
29#include <math/util.h> // for KiROUND
30#include <math/vector2d.h>
31#include <tool/tool_manager.h>
32#include <view/view.h>
33#include <tool/grid_helper.h>
35
36
38 m_toolMgr( aToolMgr )
39{
41 m_enableSnap = true;
42 m_enableSnapLine = true;
43 m_enableGrid = true;
44 m_snapItem = nullptr;
45}
46
47
49{
50}
51
52
56
57 return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
58}
59
60
62{
64}
65
66
68{
71 return VECTOR2I( origin );
72}
73
76{
77 GRID_HELPER_GRIDS grid = GetItemGrid( aSelection.Front() );
78
79 // Find the largest grid of all the items and use that
80 for( EDA_ITEM* item : aSelection )
81 {
82 GRID_HELPER_GRIDS itemGrid = GetItemGrid( item );
83
84 if( GetGridSize( itemGrid ) > GetGridSize( grid ) )
85 grid = itemGrid;
86 }
87
88 return grid;
89}
90
91
93{
94 return m_toolMgr->GetView()->GetGAL()->GetGridSize();
95}
96
97
98void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin )
99{
100 if( aEnable )
101 {
102 m_auxAxis = aOrigin;
103 m_viewAxis.SetPosition( aOrigin );
105 }
106 else
107 {
108 m_auxAxis = std::optional<VECTOR2I>();
109 m_toolMgr->GetView()->SetVisible( &m_viewAxis, false );
110 }
111}
112
113
115{
116 return computeNearest( aPoint, GetGrid(), GetOrigin() );
117}
118
119
121 const VECTOR2D& aOffset ) const
122{
123 return computeNearest( aPoint, aGrid, aOffset );
124}
125
126
128 const VECTOR2I& aOffset ) const
129{
130 return VECTOR2I( KiROUND( (double) ( aPoint.x - aOffset.x ) / aGrid.x ) * aGrid.x + aOffset.x,
131 KiROUND( (double) ( aPoint.y - aOffset.y ) / aGrid.y ) * aGrid.y + aOffset.y );
132}
133
134
136{
137 return Align( aPoint, GetGrid(), GetOrigin() );
138}
139
140
141VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
142 const VECTOR2D& aOffset ) const
143{
144 if( !canUseGrid() )
145 return aPoint;
146
147 VECTOR2I nearest = AlignGrid( aPoint, aGrid, aOffset );
148
149 if( !m_auxAxis )
150 return nearest;
151
152 if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
153 nearest.x = m_auxAxis->x;
154
155 if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
156 nearest.y = m_auxAxis->y;
157
158 return nearest;
159}
160
161
163{
165}
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:173
VECTOR2I computeNearest(const VECTOR2I &aPoint, const VECTOR2I &aGrid, const VECTOR2I &aOffset) const
GRID_HELPER(TOOL_MANAGER *aToolMgr)
Definition: grid_helper.cpp:37
bool m_enableGrid
Definition: grid_helper.h:178
virtual GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const
Gets the coarsest grid that applies to an item.
Definition: grid_helper.h:86
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:48
virtual GRID_HELPER_GRIDS GetSelectionGrid(const SELECTION &aSelection) const
Gets the coarsest grid that applies to a selecion of items.
Definition: grid_helper.cpp:75
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:172
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
Definition: grid_helper.cpp:98
VECTOR2D GetVisibleGrid() const
Definition: grid_helper.cpp:61
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
Definition: grid_helper.cpp:92
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:53
ANCHOR * m_snapItem
Definition: grid_helper.h:180
bool m_enableSnapLine
Definition: grid_helper.h:179
bool m_enableSnap
Definition: grid_helper.h:177
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...
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:60
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:186
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:65
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.
bool GetGridSnapping() const
void SetPosition(const VECTOR2I &aPosition) override
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:197
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1546
EDA_ITEM * Front() const
Definition: selection.h:208
Master controller class:
Definition: tool_manager.h:57
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:378
GRID_HELPER_GRIDS
Definition: grid_helper.h:37
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:424
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:588