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-2020 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 <dialog_helpers.h>
28 
29 #include <board.h>
30 #include <footprint.h>
31 
32 #include <pcbnew.h>
33 #include <pcbnew_id.h>
34 #include "footprint_wizard_frame.h"
37 #include <base_units.h>
38 #include <widgets/wx_grid.h>
39 #include <wx/listbox.h>
40 #include <wx/msgdlg.h>
41 
42 #include <tool/tool_manager.h>
43 
45 {
46  wxString msg;
47  int page;
48 
49  switch( event.GetId() )
50  {
52  page = m_pageList->GetSelection() + 1;
53 
54  if( (int)m_pageList->GetCount() <= page )
55  page = m_pageList->GetCount() - 1;
56 
57  m_pageList->SetSelection( page, true );
58  ClickOnPageList( event );
59  break;
60 
62  page = m_pageList->GetSelection() - 1;
63 
64  if( page < 0 )
65  page = 0;
66 
67  m_pageList->SetSelection( page, true );
68  ClickOnPageList( event );
69  break;
70 
71  default:
72  wxFAIL_MSG( wxString::Format(
73  "FOOTPRINT_WIZARD_FRAME::Process_Special_Functions error: id = %d",
74  event.GetId() ) );
75  break;
76  }
77 }
78 
79 
80 /* Displays the name of the current opened library in the caption */
82 {
83  wxString msg;
84 
85  msg = _( "Footprint Wizard" );
86  msg << wxT( " [" );
87 
88  if( !m_wizardName.IsEmpty() )
89  msg << m_wizardName;
90  else
91  msg += _( "no wizard selected" );
92 
93  msg << wxT( "]" );
94 
95  SetTitle( msg );
96 }
97 
98 
100 {
101  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
102 
103  if( !footprintWizard )
104  return;
105 
107 
108  // Delete the current footprint
110 
111  // Creates the footprint
112  wxString msg;
113  FOOTPRINT* footprint = footprintWizard->GetFootprint( &msg );
114  DisplayBuildMessage( msg );
115 
116  if( footprint )
117  {
118  // Add the object to board
119  GetBoard()->Add( footprint, ADD_MODE::APPEND );
120  footprint->SetPosition( wxPoint( 0, 0 ) );
121  }
122 
123  updateView();
124  GetCanvas()->Refresh();
125 }
126 
127 
129 {
130  m_buildMessageBox->SetValue( aMessage );
131 }
132 
133 
135 {
136  if( m_wizardName.Length() == 0 )
137  return NULL;
138 
140 
141  if( !footprintWizard )
142  {
143  wxMessageBox( _( "Couldn't reload footprint wizard" ) );
144  return NULL;
145  }
146 
147  return footprintWizard;
148 }
149 
150 
152 {
154 
155  if( footprintWizard && m_modal_ret_val )
156  {
157  wxString msg;
158  FOOTPRINT* footprint = footprintWizard->GetFootprint( &msg );
159  DisplayBuildMessage( msg );
160 
161  return footprint;
162  }
163 
164  return NULL;
165 }
166 
167 
169 {
170  DIALOG_FOOTPRINT_WIZARD_LIST wizardSelector( this );
171 
172  if( wizardSelector.ShowModal() != wxID_OK )
173  return;
174 
175  FOOTPRINT_WIZARD* footprintWizard = wizardSelector.GetWizard();
176 
177  if( footprintWizard )
178  {
179  m_wizardName = footprintWizard->GetName();
180  m_wizardDescription = footprintWizard->GetDescription();
181 
182  footprintWizard->ResetParameters();
183  }
184  else
185  {
186  m_wizardName.Empty();
187  m_wizardDescription.Empty();
188  }
189 
190  ReloadFootprint();
191  Zoom_Automatique( false );
195 }
196 
197 
198 void FOOTPRINT_WIZARD_FRAME::SelectCurrentWizard( wxCommandEvent& event )
199 {
201  updateView();
202 }
203 
204 void FOOTPRINT_WIZARD_FRAME::DefaultParameters( wxCommandEvent& event )
205 {
206  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
207 
208  if ( footprintWizard == NULL )
209  return;
210 
211  footprintWizard->ResetParameters();
212 
213  // Reload
215  ReloadFootprint();
217 }
218 
219 // This is a flag to avoid reentering of ParametersUpdated
220 // that can happen in some cases
221 static bool lock_update_prms = false;
222 
224 {
225  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
226 
227  if( !footprintWizard )
228  return;
229 
230  if( m_parameterGridPage < 0 )
231  return;
232 
233  if( lock_update_prms )
234  return;
235 
236  wxArrayString prmValues = footprintWizard->GetParameterValues( m_parameterGridPage );
237  wxArrayString ptList = footprintWizard->GetParameterTypes( m_parameterGridPage );
238 
239  bool has_changed = false;
240  int count = m_parameterGrid->GetNumberRows();
241 
242  for( int prm_id = 0; prm_id < count; ++prm_id )
243  {
244  wxString value = m_parameterGrid->GetCellValue( prm_id, WIZ_COL_VALUE );
245 
246  if( prmValues[prm_id] != value )
247  {
248  has_changed = true;
249  prmValues[prm_id] = value;
250  }
251  }
252 
253  if( has_changed )
254  {
255  wxString res = footprintWizard->SetParameterValues( m_parameterGridPage, prmValues );
256 
257  if( !res.IsEmpty() )
258  wxMessageBox( res );
259 
260  ReloadFootprint();
262 
263  // The python script can have modified some other parameters.
264  // So rebuild the current parameter list with new values, just in case.
265  //
266  // On wxWidgets 3.0.5, ReCreateParameterList() generates a EVT_GRID_CMD_CELL_CHANGED
267  // that call ParametersUpdated() and creating an infinite loop
268  // Note also it happens **only for languages using a comma** instead of a point
269  // for floating point separator
270  // It does not happen on wxWidgets 3.1.4
271  //
272  // So lock the next call.
273  lock_update_prms = true;
275  }
276 
277  // unlock ParametersUpdated() now the update is finished
278  lock_update_prms = false;
279 }
280 
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:606
void ReCreatePageList()
Create or recreate the list of parameter pages for the current wizard.
#define NULL
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.
Helper dialog and control classes.
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Reset all tools (i.e.
#define _(s)
FOOTPRINT_WIZARD * GetMyWizard()
Reloads the wizard by name.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
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:156
virtual wxString GetName()=0
void SelectFootprintWizard()
Show the list of footprint wizards available into the system.
void DeleteAllFootprints()
Remove all footprints from the deque and free the memory associated with them.
Definition: board.cpp:858
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:1442
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