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#include "tool/grid_helper.h"
25
26#include <functional>
27
28#include <advanced_config.h>
30#include <gal/painter.h>
31#include <math/util.h> // for KiROUND
32#include <math/vector2d.h>
33#include <render_settings.h>
34#include <tool/tool_manager.h>
35#include <view/view.h>
37
38
39GRID_HELPER::GRID_HELPER( TOOL_MANAGER* aToolMgr, int aConstructionLayer ) :
40 m_toolMgr( aToolMgr ), m_snapManager( m_constructionGeomPreview )
41{
43 m_enableSnap = true;
44 m_enableSnapLine = true;
45 m_enableGrid = true;
46 m_snapItem = std::nullopt;
47
48 KIGFX::VIEW* view = m_toolMgr->GetView();
49 KIGFX::RENDER_SETTINGS* settings = view->GetPainter()->GetSettings();
50
51 const KIGFX::COLOR4D constructionColour = settings->GetLayerColor( aConstructionLayer );
53 m_constructionGeomPreview.SetColor( constructionColour.WithAlpha( 0.7 ) );
54
57
59 [view, this]( bool aAnythingShown )
60 {
61 const bool currentlyVisible = view->IsVisible( &m_constructionGeomPreview );
62
63 if( currentlyVisible && aAnythingShown )
64 {
66 }
67 else
68 {
69 view->SetVisible( &m_constructionGeomPreview, aAnythingShown );
70 }
71 } );
72}
73
74
77 KIGFX::VIEW& view = *m_toolMgr->GetView();
81 view.Remove( m_anchorDebug.get() );
82}
83
84
86{
87 static bool permitted = ADVANCED_CFG::GetCfg().m_EnableSnapAnchorsDebug;
88 if( permitted && !m_anchorDebug )
89 {
90 KIGFX::VIEW& view = *m_toolMgr->GetView();
91 m_anchorDebug = std::make_unique<KIGFX::ANCHOR_DEBUG>();
92 view.Add( m_anchorDebug.get() );
93 view.SetVisible( m_anchorDebug.get(), true );
94 }
95 return m_anchorDebug.get();
96}
97
98
100{
102}
103
104
106{
109
112 else
114}
115
116
118{
120
121 return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
122}
123
124
126{
128}
129
130
132{
134
135 return VECTOR2I( origin );
136}
137
138
140{
141 GRID_HELPER_GRIDS grid = GetItemGrid( aSelection.Front() );
142
143 // Find the largest grid of all the items and use that
144 for( EDA_ITEM* item : aSelection )
145 {
146 GRID_HELPER_GRIDS itemGrid = GetItemGrid( item );
147
148 if( GetGridSize( itemGrid ) > GetGridSize( grid ) )
149 grid = itemGrid;
150 }
151
152 return grid;
153}
154
155
157{
158 return m_toolMgr->GetView()->GetGAL()->GetGridSize();
159}
160
161
162void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin )
163{
164 if( aEnable )
165 {
166 m_auxAxis = aOrigin;
167 m_viewAxis.SetPosition( aOrigin );
169 }
170 else
171 {
172 m_auxAxis = std::optional<VECTOR2I>();
173 m_toolMgr->GetView()->SetVisible( &m_viewAxis, false );
174 }
175}
176
177
179{
180 return computeNearest( aPoint, GetGrid(), GetOrigin() );
181}
182
183
185 const VECTOR2D& aOffset ) const
186{
187 return computeNearest( aPoint, aGrid, aOffset );
188}
189
190
192 const VECTOR2I& aOffset ) const
193{
194 return VECTOR2I( KiROUND( (double) ( aPoint.x - aOffset.x ) / aGrid.x ) * aGrid.x + aOffset.x,
195 KiROUND( (double) ( aPoint.y - aOffset.y ) / aGrid.y ) * aGrid.y + aOffset.y );
196}
197
198
200{
201 return Align( aPoint, GetGrid(), GetOrigin() );
202}
203
204
205VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
206 const VECTOR2D& aOffset ) const
207{
208 if( !canUseGrid() )
209 return aPoint;
210
211 VECTOR2I nearest = AlignGrid( aPoint, aGrid, aOffset );
212
213 if( !m_auxAxis )
214 return nearest;
215
216 if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
217 nearest.x = m_auxAxis->x;
218
219 if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
220 nearest.y = m_auxAxis->y;
221
222 return nearest;
223}
224
225
227{
229}
230
231
232std::optional<VECTOR2I> GRID_HELPER::GetSnappedPoint() const
233{
234 if( m_snapItem )
235 return m_snapItem->pos;
236
237 return std::nullopt;
238}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
KIGFX::CONSTRUCTION_GEOM m_constructionGeomPreview
Definition: grid_helper.h:237
std::optional< VECTOR2I > m_auxAxis
Definition: grid_helper.h:220
VECTOR2I computeNearest(const VECTOR2I &aPoint, const VECTOR2I &aGrid, const VECTOR2I &aOffset) const
bool m_enableGrid
Definition: grid_helper.h:225
virtual GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const
Gets the coarsest grid that applies to an item.
Definition: grid_helper.h:91
void showConstructionGeometry(bool aShow)
Definition: grid_helper.cpp:99
SNAP_MANAGER m_snapManager
Definition: grid_helper.h:241
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:75
virtual GRID_HELPER_GRIDS GetSelectionGrid(const SELECTION &aSelection) const
Gets the coarsest grid that applies to a selecion of items.
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:219
std::optional< VECTOR2I > GetSnappedPoint() const
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
VECTOR2D GetVisibleGrid() const
std::unique_ptr< KIGFX::ANCHOR_DEBUG > m_anchorDebug
VIEW_ITEM for visualising anchor points, if enabled.
Definition: grid_helper.h:244
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
VECTOR2I GetGrid() const
bool m_enableSnapLine
Definition: grid_helper.h:226
bool m_enableSnap
Definition: grid_helper.h:224
VECTOR2I GetOrigin() const
bool canUseGrid() const
Check whether it is possible to use the grid – this depends both on local grid helper settings and gl...
std::optional< ANCHOR > m_snapItem
Definition: grid_helper.h:227
KIGFX::ANCHOR_DEBUG * enableAndGetAnchorDebug()
Enable the anchor debug if permitted and return it.
Definition: grid_helper.cpp:85
KIGFX::SNAP_INDICATOR m_viewSnapPoint
Definition: grid_helper.h:231
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:65
void updateSnapPoint(const TYPED_POINT2I &aPoint)
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:232
GRID_HELPER(TOOL_MANAGER *aToolMgr, int aConstructionLayer)
Definition: grid_helper.cpp:39
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:70
View item to draw debug items for anchors.
Definition: anchor_debug.h:45
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:311
void SetColor(const COLOR4D &aColor)
void SetPersistentColor(const COLOR4D &aColor)
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
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void SetSnapTypes(int aSnapTypes)
Set a mask of point types that this snap item represents.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:299
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:334
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1669
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:199
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:217
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition: view.cpp:1639
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1596
EDA_ITEM * Front() const
Definition: selection.h:172
void SetUpdateCallback(GFX_UPDATE_CALLBACK aCallback)
Set the callback to call when the construction geometry changes and a view may need updating.
Master controller class:
Definition: tool_manager.h:62
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:391
GRID_HELPER_GRIDS
Definition: grid_helper.h:42
bool m_EnableSnapAnchorsDebug
Enable snap anchors debug visualization.
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:54
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
VECTOR2I m_point
Definition: point_types.h:77
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691