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
23
24
25std::vector<MAGNETIC_OPTIONS> MagneticSnapOptions( bool aRouting )
26{
27 if( aRouting )
28 {
31 }
32
34}
35
36
37int MagneticSnapIndex( const std::vector<MAGNETIC_OPTIONS>& aOptions, MAGNETIC_OPTIONS aValue )
38{
39 auto it = std::find( aOptions.begin(), aOptions.end(), aValue );
40
41 if( it == aOptions.end() )
42 it = std::find( aOptions.begin(), aOptions.end(), MAGNETIC_OPTIONS::NO_EFFECT );
43
44 return it == aOptions.end() ? 0 : static_cast<int>( std::distance( aOptions.begin(), it ) );
45}
46
47
48MAGNETIC_OPTIONS MagneticSnapValue( const std::vector<MAGNETIC_OPTIONS>& aOptions, int aIndex )
49{
50 if( aIndex < 0 || aIndex >= static_cast<int>( aOptions.size() ) )
52
53 return aOptions[aIndex];
54}
55
56
57static wxString magneticOptionLabel( MAGNETIC_OPTIONS aOption )
58{
59 switch( aOption )
60 {
61 case MAGNETIC_OPTIONS::NO_EFFECT: return _( "Never" );
62 case MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL: return _( "When routing tracks" );
63 case MAGNETIC_OPTIONS::CAPTURE_ALWAYS: return _( "Always" );
64 }
65
66 return wxEmptyString;
67}
68
69
70static void fillMagneticChoice( wxChoice* aChoice, const std::vector<MAGNETIC_OPTIONS>& aOptions )
71{
72 for( MAGNETIC_OPTIONS option : aOptions )
73 aChoice->Append( magneticOptionLabel( option ) );
74}
75
76
77PANEL_SNAPPING::PANEL_SNAPPING( wxWindow* aParent, FRAME_T aFrameType, int* aGridSnap,
78 SNAP_INFERENCE_SETTINGS* aInference, MAGNETIC_SETTINGS* aMagnetics,
79 std::function<VALUES()> aDefaults ) :
80 PANEL_SNAPPING_BASE( aParent ),
81 m_routing( aFrameType == FRAME_PCB_EDITOR ),
82 m_gridSnap( aGridSnap ),
83 m_inference( aInference ),
84 m_magnetics( aMagnetics ),
85 m_defaults( std::move( aDefaults ) )
86{
90
94
95 m_objectSnappingLabel->Show( m_magnetics != nullptr );
96 m_objectSnappingLine->Show( m_magnetics != nullptr );
97 m_sizerObjectSnapping->ShowItems( m_magnetics != nullptr );
98
99 // Only the editors that route tracks have tracks to snap to.
102
103 m_snapInferenceLabel->Show( m_inference != nullptr );
104 m_snapInferenceLine->Show( m_inference != nullptr );
105 m_sizerSnapInference->ShowItems( m_inference != nullptr );
106
107 Layout();
108}
109
110
111void PANEL_SNAPPING::load( const VALUES& aValues )
112{
113 // The grid snapping mode is a bare int in the config, so a hand-edited file can name a mode
114 // that doesn't exist.
115 if( aValues.gridSnap >= 0 && aValues.gridSnap < static_cast<int>( m_gridSnapChoice->GetCount() ) )
116 m_gridSnapChoice->SetSelection( aValues.gridSnap );
117 else
118 m_gridSnapChoice->SetSelection( 0 );
119
120 if( m_magnetics )
121 {
122 m_padSnapChoice->SetSelection( MagneticSnapIndex( m_padOptions, aValues.magnetics.pads ) );
123
124 if( m_routing )
126
129 m_graphicsSnapChoice->SetSelection( MagneticSnapIndex( m_graphicsOptions, graphics ) );
130
131 m_snapAllLayers->SetValue( aValues.magnetics.allLayers );
132 }
133
134 if( m_inference )
135 {
136 m_snapObjectGeometry->SetValue( aValues.inference.objectGeometry );
138 m_snapTangentNormal->SetValue( aValues.inference.tangentNormal );
140 }
141}
142
143
145{
146 VALUES values;
147 values.gridSnap = *m_gridSnap;
148
149 if( m_magnetics )
150 values.magnetics = *m_magnetics;
151
152 if( m_inference )
153 values.inference = *m_inference;
154
155 load( values );
156
157 return true;
158}
159
160
162{
163 *m_gridSnap = m_gridSnapChoice->GetSelection();
164
165 if( m_magnetics )
166 {
167 m_magnetics->pads = MagneticSnapValue( m_padOptions, m_padSnapChoice->GetSelection() );
168
169 if( m_routing )
171
174 m_magnetics->allLayers = m_snapAllLayers->GetValue();
175 }
176
177 if( m_inference )
178 {
179 m_inference->objectGeometry = m_snapObjectGeometry->GetValue();
180 m_inference->constructionExtensions = m_snapConstructionExtensions->GetValue();
181 m_inference->tangentNormal = m_snapTangentNormal->GetValue();
182 m_inference->alignmentDistribution = m_snapAlignmentDistribution->GetValue();
183 }
184
185 return true;
186}
187
188
190{
191 load( m_defaults() );
192}
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
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
std::vector< MAGNETIC_OPTIONS > m_graphicsOptions
PANEL_SNAPPING(wxWindow *aParent, FRAME_T aFrameType, int *aGridSnap, SNAP_INFERENCE_SETTINGS *aInference, MAGNETIC_SETTINGS *aMagnetics, std::function< VALUES()> aDefaults)
#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
STL namespace.
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...
SNAP_INFERENCE_SETTINGS inference
MAGNETIC_SETTINGS magnetics