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 <fctsys.h>
27 #include <gr_basic.h>
28 #include <pcb_edit_frame.h>
29 #include <dialog_helpers.h>
30 
31 #include <class_board.h>
32 #include <class_module.h>
33 
34 #include <pcbnew.h>
35 #include <pcbnew_id.h>
36 #include "footprint_wizard_frame.h"
39 #include <base_units.h>
40 #include <widgets/wx_grid.h>
41 
42 #include <tool/tool_manager.h>
43 
45 {
46  wxString msg;
47  int page;
48 
49  switch( event.GetId() )
50  {
52  m_pageList->SetSelection( m_pageList->GetSelection() + 1, true );
53  ClickOnPageList( event );
54  break;
55 
57  page = m_pageList->GetSelection() - 1;
58 
59  if( page < 0 )
60  page = 0;
61 
62  m_pageList->SetSelection( page, true );
63  ClickOnPageList( event );
64  break;
65 
66  default:
67  wxFAIL_MSG( wxString::Format(
68  "FOOTPRINT_WIZARD_FRAME::Process_Special_Functions error: id = %d",
69  event.GetId() ) );
70  break;
71  }
72 }
73 
74 
75 /* Displays the name of the current opened library in the caption */
77 {
78  wxString msg;
79 
80  msg = _( "Footprint Wizard" );
81  msg << wxT( " [" );
82 
83  if( !m_wizardName.IsEmpty() )
84  msg << m_wizardName;
85  else
86  msg += _( "no wizard selected" );
87 
88  msg << wxT( "]" );
89 
90  SetTitle( msg );
91 }
92 
93 
95 {
96  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
97 
98  if( !footprintWizard )
99  return;
100 
102 
103  // Delete the current footprint
105 
106  // Creates the module
107  wxString msg;
108  MODULE* module = footprintWizard->GetFootprint( &msg );
109  DisplayBuildMessage( msg );
110 
111  if( module )
112  {
113  // Add the object to board
114  GetBoard()->Add( module, ADD_MODE::APPEND );
115  module->SetPosition( wxPoint( 0, 0 ) );
116  }
117 
118  updateView();
119  GetCanvas()->Refresh();
120 }
121 
122 
124 {
125  m_buildMessageBox->SetValue( aMessage );
126 }
127 
128 
130 {
131  if( m_wizardName.Length() == 0 )
132  return NULL;
133 
135 
136  if( !footprintWizard )
137  {
138  wxMessageBox( _( "Couldn't reload footprint wizard" ) );
139  return NULL;
140  }
141 
142  return footprintWizard;
143 }
144 
145 
147 {
149 
150  if( footprintWizard && m_modal_ret_val )
151  {
152  wxString msg;
153  MODULE * footprint = footprintWizard->GetFootprint( &msg );
154  DisplayBuildMessage( msg );
155 
156  return footprint;
157  }
158 
159  return NULL;
160 }
161 
162 
164 {
165  DIALOG_FOOTPRINT_WIZARD_LIST wizardSelector( this );
166 
167  if( wizardSelector.ShowModal() != wxID_OK )
168  return;
169 
170  FOOTPRINT_WIZARD* footprintWizard = wizardSelector.GetWizard();
171 
172  if( footprintWizard )
173  {
174  m_wizardName = footprintWizard->GetName();
175  m_wizardDescription = footprintWizard->GetDescription();
176 
177  footprintWizard->ResetParameters();
178  }
179  else
180  {
181  m_wizardName.Empty();
182  m_wizardDescription.Empty();
183  }
184 
185  ReloadFootprint();
186  Zoom_Automatique( false );
190 }
191 
192 
193 void FOOTPRINT_WIZARD_FRAME::SelectCurrentWizard( wxCommandEvent& event )
194 {
196  updateView();
197 }
198 
199 void FOOTPRINT_WIZARD_FRAME::DefaultParameters( wxCommandEvent& event )
200 {
201  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
202 
203  if ( footprintWizard == NULL )
204  return;
205 
206  footprintWizard->ResetParameters();
207 
208  // Reload
210  ReloadFootprint();
212 }
213 
214 // This is a flag to avoid reentering of ParametersUpdated
215 // that can happen in some cases
216 static bool lock_update_prms = false;
217 
219 {
220  FOOTPRINT_WIZARD* footprintWizard = GetMyWizard();
221 
222  if( !footprintWizard )
223  return;
224 
225  if( m_parameterGridPage < 0 )
226  return;
227 
228  if( lock_update_prms )
229  return;
230 
231  wxArrayString prmValues = footprintWizard->GetParameterValues( m_parameterGridPage );
232  wxArrayString ptList = footprintWizard->GetParameterTypes( m_parameterGridPage );
233 
234  bool has_changed = false;
235  int count = m_parameterGrid->GetNumberRows();
236 
237  for( int prm_id = 0; prm_id < count; ++prm_id )
238  {
239  wxString value = m_parameterGrid->GetCellValue( prm_id, WIZ_COL_VALUE );
240 
241  if( prmValues[prm_id] != value )
242  {
243  has_changed = true;
244  prmValues[prm_id] = value;
245  }
246  }
247 
248  if( has_changed )
249  {
250  wxString res = footprintWizard->SetParameterValues( m_parameterGridPage, prmValues );
251 
252  if( !res.IsEmpty() )
253  wxMessageBox( res );
254 
255  ReloadFootprint();
257 
258  // The python script can have modified some other parameters.
259  // So rebuild the current parameter list with new values, just in case.
260  //
261  // On wxWidgets 3.0.5, ReCreateParameterList() generates a EVT_GRID_CMD_CELL_CHANGED
262  // that call ParametersUpdated() and creating an infinite loop
263  // Note also it happens **only for languages using a comma** instead of a point
264  // for floating point separator
265  // It does not happen on wxWidgets 3.1.4
266  //
267  // So lock the next call.
268  lock_update_prms = true;
270  }
271 
272  // unlock ParametersUpdated() now the update is finished
273  lock_update_prms = false;
274 }
275 
bool m_modal_ret_val
Definition: kiway_player.h:205
wxString m_wizardName
name of the current wizard
void DisplayWizardInfos()
Function DisplayWizardInfos Shows all the details about the current wizard.
Model changes (required full reload)
Definition: tool_base.h:82
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 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()
Function ReloadFootprint Reloads the current footprint.
void ClickOnPageList(wxCommandEvent &event)
void ReCreateParameterList()
Function ReCreateParameterList Creates 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.
void ReCreatePageList()
Function ReCreatePageList Creates or recreates the list of parameter pages for the current wizard.
#define NULL
virtual wxString GetDescription()=0
Function GetDescription.
void ParametersUpdated(wxGridEvent &event)
Function ParametersUpdated Update the footprint python parameters values from the values in grid.
Definition of file extensions used in Kicad.
void DisplayBuildMessage(wxString &aMessage)
Function DisplayBuildMessages Display the message generated by the python build footprint script.
Helper dialog and control classes.
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Function ResetTools() Resets all tools (i.e.
void SetPosition(const wxPoint &aPos) override
FOOTPRINT_WIZARD * GetMyWizard()
Function 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)
virtual MODULE * GetFootprint(wxString *aMessage)=0
Function GetModule This method builds the module itself and returns it to the caller function.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:50
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
#define _(s)
Definition: 3d_actions.cpp:33
virtual wxString GetName()=0
Function GetName.
void SelectFootprintWizard()
Function SelectFootprintWizard Shows the list of footprint wizards available into the system.
void DeleteAllModules()
Removes all modules from the deque and frees the memory associated with them.
Definition: class_board.h:317
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 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