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 
40 #include <tool/tool_manager.h>
41 
43 {
44  wxString msg;
45  int page;
46 
47  switch( event.GetId() )
48  {
50  page = m_pageList->GetSelection() + 1;
51 
52  if( (int)m_pageList->GetCount() <= page )
53  page = m_pageList->GetCount() - 1;
54 
55  m_pageList->SetSelection( page, true );
56  ClickOnPageList( event );
57  break;
58 
60  page = m_pageList->GetSelection() - 1;
61 
62  if( page < 0 )
63  page = 0;
64 
65  m_pageList->SetSelection( page, true );
66  ClickOnPageList( event );
67  break;
68 
69  default:
70  wxFAIL_MSG( wxString::Format(
71  "FOOTPRINT_WIZARD_FRAME::Process_Special_Functions error: id = %d",
72  event.GetId() ) );
73  break;
74  }
75 }
76 
77 
78 /* Displays the name of the current opened library in the caption */
80 {
81  wxString msg;
82 
83  msg = _( "Footprint Wizard" );
84  msg << wxT( " [" );
85 
86  if( !m_wizardName.IsEmpty() )
87  msg << m_wizardName;
88  else
89  msg += _( "no wizard selected" );
90 
91  msg << wxT( "]" );
92 
93  SetTitle( msg );
94 }
95 
96 
98 {
99  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
100 
101  if( !footprintWizard )
102  return;
103 
105 
106  // Delete the current footprint
108 
109  // Creates the footprint
110  wxString msg;
111  FOOTPRINT* footprint = footprintWizard->GetFootprint( &msg );
112  DisplayBuildMessage( msg );
113 
114  if( footprint )
115  {
116  // Add the object to board
117  GetBoard()->Add( footprint, ADD_MODE::APPEND );
118  footprint->SetPosition( wxPoint( 0, 0 ) );
119  }
120 
121  updateView();
122  GetCanvas()->Refresh();
123 }
124 
125 
127 {
128  m_buildMessageBox->SetValue( aMessage );
129 }
130 
131 
133 {
134  if( m_wizardName.Length() == 0 )
135  return NULL;
136 
138 
139  if( !footprintWizard )
140  {
141  wxMessageBox( _( "Couldn't reload footprint wizard" ) );
142  return NULL;
143  }
144 
145  return footprintWizard;
146 }
147 
148 
150 {
152 
153  if( footprintWizard && m_modal_ret_val )
154  {
155  wxString msg;
156  FOOTPRINT* footprint = footprintWizard->GetFootprint( &msg );
157  DisplayBuildMessage( msg );
158 
159  return footprint;
160  }
161 
162  return NULL;
163 }
164 
165 
167 {
168  DIALOG_FOOTPRINT_WIZARD_LIST wizardSelector( this );
169 
170  if( wizardSelector.ShowModal() != wxID_OK )
171  return;
172 
173  FOOTPRINT_WIZARD* footprintWizard = wizardSelector.GetWizard();
174 
175  if( footprintWizard )
176  {
177  m_wizardName = footprintWizard->GetName();
178  m_wizardDescription = footprintWizard->GetDescription();
179 
180  footprintWizard->ResetParameters();
181  }
182  else
183  {
184  m_wizardName.Empty();
185  m_wizardDescription.Empty();
186  }
187 
188  ReloadFootprint();
189  Zoom_Automatique( false );
193 }
194 
195 
196 void FOOTPRINT_WIZARD_FRAME::SelectCurrentWizard( wxCommandEvent& event )
197 {
199  updateView();
200 }
201 
202 void FOOTPRINT_WIZARD_FRAME::DefaultParameters( wxCommandEvent& event )
203 {
204  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
205 
206  if ( footprintWizard == NULL )
207  return;
208 
209  footprintWizard->ResetParameters();
210 
211  // Reload
213  ReloadFootprint();
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 
222 {
223  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
224 
225  if( !footprintWizard )
226  return;
227 
228  if( m_parameterGridPage < 0 )
229  return;
230 
231  if( lock_update_prms )
232  return;
233 
234  wxArrayString prmValues = footprintWizard->GetParameterValues( m_parameterGridPage );
235  wxArrayString ptList = footprintWizard->GetParameterTypes( m_parameterGridPage );
236 
237  bool has_changed = false;
238  int count = m_parameterGrid->GetNumberRows();
239 
240  for( int prm_id = 0; prm_id < count; ++prm_id )
241  {
242  wxString value = m_parameterGrid->GetCellValue( prm_id, WIZ_COL_VALUE );
243 
244  if( prmValues[prm_id] != value )
245  {
246  has_changed = true;
247  prmValues[prm_id] = value;
248  }
249  }
250 
251  if( has_changed )
252  {
253  wxString res = footprintWizard->SetParameterValues( m_parameterGridPage, prmValues );
254 
255  if( !res.IsEmpty() )
256  wxMessageBox( res );
257 
258  ReloadFootprint();
260 
261  // The python script can have modified some other parameters.
262  // So rebuild the current parameter list with new values, just in case.
263  //
264  // On wxWidgets 3.0.5, ReCreateParameterList() generates a EVT_GRID_CMD_CELL_CHANGED
265  // that call ParametersUpdated() and creating an infinite loop
266  // Note also it happens **only for languages using a comma** instead of a point
267  // for floating point separator
268  // It does not happen on wxWidgets 3.1.4
269  //
270  // So lock the next call.
271  lock_update_prms = true;
273  }
274 
275  // unlock ParametersUpdated() now the update is finished
276  lock_update_prms = false;
277 }
278 
bool m_modal_ret_val
Definition: kiway_player.h:199
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:81
Implementation of conversion functions that require both schematic and board internal units.
static FOOTPRINT_WIZARD * GetWizard(const wxString &aName)
Function GetWizard.
wxListBox * m_pageList
The list of pages.
virtual FOOTPRINT * GetFootprint(wxString *aMessage)=0
Function GetFootprint This method builds the footprint itself and returns it to the caller function.
virtual wxString SetParameterValues(int aPage, wxArrayString &aValues)=0
Function SetParameterValues.
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
Function GetParameterTypes.
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:563
void ReCreatePageList()
Create or recreate the list of parameter pages for the current wizard.
#define NULL
virtual wxString GetDescription()=0
Function GetDescription.
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.
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:157
#define _(s)
Definition: 3d_actions.cpp:33
virtual wxString GetName()=0
Function GetName.
void SelectFootprintWizard()
Show the list of footprint wizards available into the system.
void DeleteAllFootprints()
Removes all footprints from the deque and frees the memory associated with them.
Definition: board.h:388
void SelectCurrentWizard(wxCommandEvent &event)
virtual wxArrayString GetParameterValues(int aPage)=0
Function GetParameterValues.
void Process_Special_Functions(wxCommandEvent &event)
BOARD * GetBoard() const
static bool lock_update_prms
virtual void ResetParameters()=0
Function ResetParameters Reset all wizard parameters to default values.
void SetPosition(const wxPoint &aPos) override
Definition: footprint.cpp:1437
void updateView()
Rebuild the GAL view (reint tool manager, colors and drawings) must be run after any footprint change...
FOOTPRINT_WIZARD This is the parent class from where any footprint wizard class must derive.
wxString m_wizardDescription
description of the wizard