KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_snapping.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 The 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 3
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <algorithm>
21
22#include <config_map.h>
24
25
26std::vector<MAGNETIC_OPTIONS> MagneticSnapOptions( bool aRouting )
27{
28 if( aRouting )
29 {
32 }
33
35}
36
37
38int MagneticSnapIndex( const std::vector<MAGNETIC_OPTIONS>& aOptions, MAGNETIC_OPTIONS aValue )
39{
40 auto it = std::find( aOptions.begin(), aOptions.end(), aValue );
41
42 if( it == aOptions.end() )
43 it = std::find( aOptions.begin(), aOptions.end(), MAGNETIC_OPTIONS::NO_EFFECT );
44
45 return it == aOptions.end() ? 0 : static_cast<int>( std::distance( aOptions.begin(), it ) );
46}
47
48
49MAGNETIC_OPTIONS MagneticSnapValue( const std::vector<MAGNETIC_OPTIONS>& aOptions, int aIndex )
50{
51 if( aIndex < 0 || aIndex >= static_cast<int>( aOptions.size() ) )
53
54 return aOptions[aIndex];
55}
56
57
58static wxString magneticOptionLabel( MAGNETIC_OPTIONS aOption )
59{
60 switch( aOption )
61 {
62 case MAGNETIC_OPTIONS::NO_EFFECT: return _( "Never" );
63 case MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL: return _( "When routing tracks" );
64 case MAGNETIC_OPTIONS::CAPTURE_ALWAYS: return _( "Always" );
65 }
66
67 return wxEmptyString;
68}
69
70
71static void fillMagneticChoice( wxChoice* aChoice, const std::vector<MAGNETIC_OPTIONS>& aOptions )
72{
73 for( MAGNETIC_OPTIONS option : aOptions )
74 aChoice->Append( magneticOptionLabel( option ) );
75}
76
77
78// The m_gridSnapChoice entries, in the form's order.
80 // clang-format off
81 { KIGFX::GRID_SNAPPING::ALWAYS, 0 }, // Default
84 // clang-format on
85};
86
87
88PANEL_SNAPPING::PANEL_SNAPPING( wxWindow* aParent, FRAME_T aFrameType, KIGFX::GRID_SNAPPING* aGridSnap,
89 SNAP_INFERENCE_SETTINGS* aInference, MAGNETIC_SETTINGS* aMagnetics,
90 std::function<VALUES()> aDefaults ) :
91 PANEL_SNAPPING_BASE( aParent ),
92 m_routing( aFrameType == FRAME_PCB_EDITOR ),
93 m_gridSnap( aGridSnap ),
94 m_inference( aInference ),
95 m_magnetics( aMagnetics ),
96 m_defaults( std::move( aDefaults ) )
97{
98 wxASSERT( gridSnapModeMap.size() == static_cast<size_t>( m_gridSnapChoice->GetCount() ) );
99
103
107
108 m_objectSnappingLabel->Show( m_magnetics != nullptr );
109 m_objectSnappingLine->Show( m_magnetics != nullptr );
110 m_sizerObjectSnapping->ShowItems( m_magnetics != nullptr );
111
112 // Only the editors that route tracks have tracks to snap to.
115
116 m_snapInferenceLabel->Show( m_inference != nullptr );
117 m_snapInferenceLine->Show( m_inference != nullptr );
118 m_sizerSnapInference->ShowItems( m_inference != nullptr );
119
120 Layout();
121}
122
123
124void PANEL_SNAPPING::load( const VALUES& aValues )
125{
127
128 if( m_magnetics )
129 {
130 m_padSnapChoice->SetSelection( MagneticSnapIndex( m_padOptions, aValues.magnetics.pads ) );
131
132 if( m_routing )
134
137 m_graphicsSnapChoice->SetSelection( MagneticSnapIndex( m_graphicsOptions, graphics ) );
138
139 m_snapAllLayers->SetValue( aValues.magnetics.allLayers );
140 }
141
142 if( m_inference )
143 {
144 m_snapObjectGeometry->SetValue( aValues.inference.objectGeometry );
146 m_snapTangentNormal->SetValue( aValues.inference.tangentNormal );
148 }
149}
150
151
153{
154 VALUES values;
155 values.gridSnap = *m_gridSnap;
156
157 if( m_magnetics )
158 values.magnetics = *m_magnetics;
159
160 if( m_inference )
161 values.inference = *m_inference;
162
163 load( values );
164
165 return true;
166}
167
168
170{
172
173 if( m_magnetics )
174 {
175 m_magnetics->pads = MagneticSnapValue( m_padOptions, m_padSnapChoice->GetSelection() );
176
177 if( m_routing )
179
182 m_magnetics->allLayers = m_snapAllLayers->GetValue();
183 }
184
185 if( m_inference )
186 {
187 m_inference->objectGeometry = m_snapObjectGeometry->GetValue();
188 m_inference->constructionExtensions = m_snapConstructionExtensions->GetValue();
189 m_inference->tangentNormal = m_snapTangentNormal->GetValue();
190 m_inference->alignmentDistribution = m_snapAlignmentDistribution->GetValue();
191 }
192
193 return true;
194}
195
196
198{
199 load( m_defaults() );
200}
PANEL_SNAPPING_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
wxStaticText * m_objectSnappingLabel
wxBoxSizer * m_sizerSnapInference
wxCheckBox * m_snapAlignmentDistribution
wxBoxSizer * m_sizerObjectSnapping
wxCheckBox * m_snapConstructionExtensions
wxStaticText * m_trackSnapLabel
wxStaticText * m_snapInferenceLabel
wxStaticLine * m_objectSnappingLine
wxCheckBox * m_snapTangentNormal
wxStaticLine * m_snapInferenceLine
wxCheckBox * m_snapObjectGeometry
std::function< VALUES()> m_defaults
void ResetPanel() override
Reset the contents of this panel.
bool TransferDataToWindow() override
PANEL_SNAPPING(wxWindow *aParent, FRAME_T aFrameType, KIGFX::GRID_SNAPPING *aGridSnap, SNAP_INFERENCE_SETTINGS *aInference, MAGNETIC_SETTINGS *aMagnetics, std::function< VALUES()> aDefaults)
bool TransferDataFromWindow() override
std::vector< MAGNETIC_OPTIONS > m_padOptions
SNAP_INFERENCE_SETTINGS * m_inference
MAGNETIC_SETTINGS * m_magnetics
void load(const VALUES &aValues)
std::vector< MAGNETIC_OPTIONS > m_trackOptions
KIGFX::GRID_SNAPPING * m_gridSnap
std::vector< MAGNETIC_OPTIONS > m_graphicsOptions
#define _(s)
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
std::vector< std::pair< T, long > > CFG_MAP
A config value table is a list of native values (usually enums) to a different set of values,...
Definition config_map.h:47
static long GetConfigForVal(const MAP &aMap, CFG_NATIVE_VAL< MAP > aVal)
Get the mapped config value (the one to write to file, or use in an index) from the given native (pro...
Definition config_map.h:67
static CFG_NATIVE_VAL< MAP > GetValFromConfig(const MAP &aMap, long aConf)
Get the native value corresponding to the config value (read from file or UI, probably) and find it i...
Definition config_map.h:94
STL namespace.
static const UTIL::CFG_MAP< KIGFX::GRID_SNAPPING > gridSnapModeMap
int MagneticSnapIndex(const std::vector< MAGNETIC_OPTIONS > &aOptions, MAGNETIC_OPTIONS aValue)
The position of aValue in aOptions, falling back to the "never" entry for a value the editor does not...
static wxString magneticOptionLabel(MAGNETIC_OPTIONS aOption)
std::vector< MAGNETIC_OPTIONS > MagneticSnapOptions(bool aRouting)
The object snapping modes an editor offers.
static void fillMagneticChoice(wxChoice *aChoice, const std::vector< MAGNETIC_OPTIONS > &aOptions)
MAGNETIC_OPTIONS MagneticSnapValue(const std::vector< MAGNETIC_OPTIONS > &aOptions, int aIndex)
The mode at aIndex, or "never" if the index is out of range.
int MagneticSnapIndex(const std::vector< MAGNETIC_OPTIONS > &aOptions, MAGNETIC_OPTIONS aValue)
The position of aValue in aOptions, falling back to the "never" entry for a value the editor does not...
std::vector< MAGNETIC_OPTIONS > MagneticSnapOptions(bool aRouting)
The object snapping modes an editor offers.
MAGNETIC_OPTIONS MagneticSnapValue(const std::vector< MAGNETIC_OPTIONS > &aOptions, int aIndex)
The mode at aIndex, or "never" if the index is out of range.
MAGNETIC_OPTIONS
MAGNETIC_OPTIONS tracks
MAGNETIC_OPTIONS pads
One editor's snapping settings by value, so the page can also be filled from a settings object that i...
KIGFX::GRID_SNAPPING gridSnap
SNAP_INFERENCE_SETTINGS inference
MAGNETIC_SETTINGS magnetics