KiCad PCB EDA Suite
footprint_wizard_frame_functions.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) 2012 Miguel Angel Ajo Pelayo, miguelangel@nbee.es
5  * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
6  * Copyright (C) 2004-2021 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <pcb_edit_frame.h>
27 #include <board.h>
28 #include <footprint.h>
29 #include <pcbnew.h>
30 #include <pcbnew_id.h>
33 #include <base_units.h>
34 #include <widgets/wx_grid.h>
35 #include <wx/listbox.h>
36 #include <wx/msgdlg.h>
37 #include <tool/tool_manager.h>
38 #include "footprint_wizard_frame.h"
39 
40 
42 {
43  wxString msg;
44  int page;
45 
46  switch( event.GetId() )
47  {
49  page = m_pageList->GetSelection() + 1;
50 
51  if( (int)m_pageList->GetCount() <= page )
52  page = m_pageList->GetCount() - 1;
53 
54  m_pageList->SetSelection( page, true );
55  ClickOnPageList( event );
56  break;
57 
59  page = m_pageList->GetSelection() - 1;
60 
61  if( page < 0 )
62  page = 0;
63 
64  m_pageList->SetSelection( page, true );
65  ClickOnPageList( event );
66  break;
67 
68  default:
69  wxFAIL_MSG( wxString::Format(
70  "FOOTPRINT_WIZARD_FRAME::Process_Special_Functions error: id = %d",
71  event.GetId() ) );
72  break;
73  }
74 }
75 
76 
77 /* Displays the name of the current opened library in the caption */
79 {
80  wxString msg;
81 
82  msg = _( "Footprint Wizard" );
83  msg << wxT( " [" );
84 
85  if( !m_wizardName.IsEmpty() )
86  msg << m_wizardName;
87  else
88  msg += _( "no wizard selected" );
89 
90  msg << wxT( "]" );
91 
92  SetTitle( msg );
93 }
94 
95 
97 {
98  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
99 
100  if( !footprintWizard )
101  return;
102 
104 
105  // Delete the current footprint
107 
108  // Creates the footprint
109  wxString msg;
110  FOOTPRINT* footprint = footprintWizard->GetFootprint( &msg );
111  DisplayBuildMessage( msg );
112 
113  if( footprint )
114  {
115  // Add the object to board
116  GetBoard()->Add( footprint, ADD_MODE::APPEND );
117  footprint->SetPosition( wxPoint( 0, 0 ) );
118  }
119 
120  updateView();
121  GetCanvas()->Refresh();
122 }
123 
124 
126 {
127  m_buildMessageBox->SetValue( aMessage );
128 }
129 
130 
132 {
133  if( m_wizardName.Length() == 0 )
134  return nullptr;
135 
137 
138  if( !footprintWizard )
139  {
140  wxMessageBox( _( "Couldn't reload footprint wizard" ) );
141  return nullptr;
142  }
143 
144  return footprintWizard;
145 }
146 
147 
149 {
151 
152  if( footprintWizard && m_modal_ret_val )
153  {
154  wxString msg;
155  FOOTPRINT* footprint = footprintWizard->GetFootprint( &msg );
156  DisplayBuildMessage( msg );
157 
158  return footprint;
159  }
160 
161  return nullptr;
162 }
163 
164 
166 {
167  DIALOG_FOOTPRINT_WIZARD_LIST wizardSelector( this );
168 
169  if( wizardSelector.ShowModal() != wxID_OK )
170  return;
171 
172  FOOTPRINT_WIZARD* footprintWizard = wizardSelector.GetWizard();
173 
174  if( footprintWizard )
175  {
176  m_wizardName = footprintWizard->GetName();
177  m_wizardDescription = footprintWizard->GetDescription();
178 
179  footprintWizard->ResetParameters();
180  }
181  else
182  {
183  m_wizardName.Empty();
184  m_wizardDescription.Empty();
185  }
186 
187  ReloadFootprint();
188  Zoom_Automatique( false );
192 }
193 
194 
195 void FOOTPRINT_WIZARD_FRAME::SelectCurrentWizard( wxCommandEvent& event )
196 {
198  updateView();
199 }
200 
201 void FOOTPRINT_WIZARD_FRAME::DefaultParameters( wxCommandEvent& event )
202 {
203  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
204 
205  if ( footprintWizard == nullptr )
206  return;
207 
208  footprintWizard->ResetParameters();
209 
210  // Reload
212  ReloadFootprint();
214 }
215 
216 
217 // This is a flag to avoid reentering of ParametersUpdated
218 // that can happen in some cases
219 static bool lock_update_prms = false;
220 
221 
223 {
224  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
225 
226  if( !footprintWizard )
227  return;
228 
229  if( m_parameterGridPage < 0 )
230  return;
231 
232  if( lock_update_prms )
233  return;
234 
235  wxArrayString prmValues = footprintWizard->GetParameterValues( m_parameterGridPage );
236  wxArrayString ptList = footprintWizard->GetParameterTypes( m_parameterGridPage );
237 
238  bool has_changed = false;
239  int count = m_parameterGrid->GetNumberRows();
240 
241  for( int prm_id = 0; prm_id < count; ++prm_id )
242  {
243  wxString value = m_parameterGrid->GetCellValue( prm_id, WIZ_COL_VALUE );
244 
245  if( prmValues[prm_id] != value )
246  {
247  has_changed = true;
248  prmValues[prm_id] = value;
249  }
250  }
251 
252  if( has_changed )
253  {
254  wxString res = footprintWizard->SetParameterValues( m_parameterGridPage, prmValues );
255 
256  if( !res.IsEmpty() )
257  wxMessageBox( res );
258 
259  ReloadFootprint();
261 
262  // The python script can have modified some other parameters.
263  // So rebuild the current parameter list with new values, just in case.
264  //
265  // On wxWidgets 3.0.5, ReCreateParameterList() generates a EVT_GRID_CMD_CELL_CHANGED
266  // that call ParametersUpdated() and creating an infinite loop
267  // Note also it happens **only for languages using a comma** instead of a point
268  // for floating point separator
269  // It does not happen on wxWidgets 3.1.4
270  //
271  // So lock the next call.
272  lock_update_prms = true;
274  }
275 
276  // unlock ParametersUpdated() now the update is finished
277  lock_update_prms = false;
278 }
279 
bool m_modal_ret_val
Definition: kiway_player.h:216
wxString m_wizardName
name of the current wizard
void DisplayWizardInfos()
Show all the details about the current wizard.
Model changes (required full reload)
Definition: tool_base.h:80
Implementation of conversion functions that require both schematic and board internal units.
static FOOTPRINT_WIZARD * GetWizard(const wxString &aName)
wxListBox * m_pageList
The list of pages.
virtual FOOTPRINT * GetFootprint(wxString *aMessage)=0
Build the footprint itself and returns it to the caller function.
virtual wxString SetParameterValues(int aPage, wxArrayString &aValues)=0
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
int m_parameterGridPage
the page currently displayed by m_parameterGrid it is most of time the m_pageList selection,...
virtual wxArrayString GetParameterTypes(int aPage)=0
WX_GRID * m_parameterGrid
The list of parameters.
void ReloadFootprint()
Reload the current footprint.
void ClickOnPageList(wxCommandEvent &event)
void ReCreateParameterList()
Create the list of parameters for the current page.
virtual void Zoom_Automatique(bool aWarpPointer)
Redraw the screen with best zoom level and the best centering that shows all the page or the board.
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Adds an item to the container.
Definition: board.cpp:607
void ReCreatePageList()
Create or recreate the list of parameter pages for the current wizard.
virtual wxString GetDescription()=0
void ParametersUpdated(wxGridEvent &event)
Update the footprint python parameters values from the values in grid.
Definition of file extensions used in Kicad.
void DisplayBuildMessage(wxString &aMessage)
Display the message generated by the python build footprint script.
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Reset all tools (i.e.
#define _(s)
FOOTPRINT_WIZARD * GetMyWizard()
Reloads the wizard by name.
void DefaultParameters(wxCommandEvent &event)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:158
virtual wxString GetName()=0
void SelectFootprintWizard()
Show the list of footprint wizards available into the system.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
void DeleteAllFootprints()
Remove all footprints from the deque and free the memory associated with them.
Definition: board.cpp:859
void SelectCurrentWizard(wxCommandEvent &event)
virtual wxArrayString GetParameterValues(int aPage)=0
void Process_Special_Functions(wxCommandEvent &event)
BOARD * GetBoard() const
static bool lock_update_prms
virtual void ResetParameters()=0
Reset all wizard parameters to default values.
void SetPosition(const wxPoint &aPos) override
Definition: footprint.cpp:1499
void updateView()
Rebuild the GAL view (reint tool manager, colors and drawings) must be run after any footprint change...
The parent class from where any footprint wizard class must derive.
wxString m_wizardDescription
description of the wizard