KiCad PCB EDA Suite
dialog_config_equfiles.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
9  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <pgm_base.h>
30 #include <confirm.h>
31 #include <gestfich.h>
32 #include <id.h>
33 #include <project.h> // For PROJECT_VAR_NAME definition
34 #include <fp_lib_table.h> // For KISYSMOD definition
35 
36 #include <cvpcb_mainframe.h>
37 #include <dialog_config_equfiles.h>
39 
40 
43 {
44  m_Parent = aParent;
45 
46  PROJECT& prj = Prj();
47  SetTitle( wxString::Format( _( "Project file: \"%s\"" ),
48  GetChars( prj.GetProjectFullName() ) ) );
49 
50  Init( );
51 
52  GetSizer()->SetSizeHints( this );
53  Center();
54 }
55 
56 
58 {
59  m_sdbSizerOK->SetDefault();
60  m_ListChanged = false;
61 
62  if( !m_Parent->m_EquFilesNames.IsEmpty() )
63  m_ListEquiv->InsertItems( m_Parent->m_EquFilesNames, 0 );
64 
65  if( getEnvVarCount() < 2 )
66  m_gridEnvVars->AppendRows(2 - getEnvVarCount() );
67 
68  wxString evValue;
69  int row = 0;
70 
71  m_gridEnvVars->SetCellValue( row++, 0, PROJECT_VAR_NAME );
72  m_gridEnvVars->SetCellValue( row, 0, FP_LIB_TABLE::GlobalPathEnvVariableName() );
73 
74  for( row = 0; row < getEnvVarCount(); row++ )
75  {
76  if( wxGetEnv( m_gridEnvVars->GetCellValue( row, 0 ), &evValue ) )
77  m_gridEnvVars->SetCellValue( row, 1, evValue );
78  }
79 
80  m_gridEnvVars->AutoSizeColumns();
81 
82 }
83 
84 
85 void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
86 {
87  wxString editorname = Pgm().GetEditorName();
88 
89  if( editorname.IsEmpty() )
90  {
91  wxMessageBox( _( "No editor defined in KiCad. Please choose it." ) );
92  return;
93  }
94 
95  wxArrayInt selections;
96  m_ListEquiv->GetSelections( selections );
97 
98  wxString fullFileNames, tmp;
99 
100  for( unsigned ii = 0; ii < selections.GetCount(); ii++ )
101  {
102  tmp = m_ListEquiv->GetString( selections[ii] );
103  fullFileNames << wxT( " \"" ) << wxExpandEnvVars( tmp ) << wxT( "\"" );
104  m_ListChanged = true;
105  }
106 
107  ExecuteFile( this, editorname, fullFileNames );
108 }
109 
110 
111 void DIALOG_CONFIG_EQUFILES::OnOkClick( wxCommandEvent& event )
112 {
113  // Save new equ file list if the files list was modified
114  if( m_ListChanged )
115  {
116  // Recreate equ list
117  m_Parent->m_EquFilesNames.Clear();
118 
119  for( unsigned ii = 0; ii < m_ListEquiv->GetCount(); ii++ )
120  m_Parent->m_EquFilesNames.Add( m_ListEquiv->GetString( ii ) );
121 
122  wxCommandEvent evt( ID_SAVE_PROJECT );
124  }
125 
126  EndModal( wxID_OK );
127 }
128 
129 
130 void DIALOG_CONFIG_EQUFILES::OnCloseWindow( wxCloseEvent& event )
131 {
132  EndModal( wxID_CANCEL );
133 }
134 
135 
136 void DIALOG_CONFIG_EQUFILES::OnButtonMoveUp( wxCommandEvent& event )
137 {
138  wxArrayInt selections;
139 
140  m_ListEquiv->GetSelections( selections );
141 
142  if ( selections.GetCount() <= 0 ) // No selection.
143  return;
144 
145  if( selections[0] == 0 ) // The first lib is selected. cannot move up it
146  return;
147 
148  wxArrayString libnames = m_ListEquiv->GetStrings();
149 
150  for( size_t ii = 0; ii < selections.GetCount(); ii++ )
151  {
152  int jj = selections[ii];
153  std::swap( libnames[jj], libnames[jj-1] );
154  }
155 
156  m_ListEquiv->Set( libnames );
157 
158  // Reselect previously selected names
159  for( size_t ii = 0; ii < selections.GetCount(); ii++ )
160  {
161  int jj = selections[ii];
162  m_ListEquiv->SetSelection( jj-1 );
163  }
164 
165  m_ListChanged = true;
166 }
167 
168 
169 void DIALOG_CONFIG_EQUFILES::OnButtonMoveDown( wxCommandEvent& event )
170 {
171  wxArrayInt selections;
172  m_ListEquiv->GetSelections( selections );
173 
174  if ( selections.GetCount() <= 0 ) // No selection.
175  return;
176 
177  // The last lib is selected. cannot move down it
178  if( selections.Last() == int( m_ListEquiv->GetCount()-1 ) )
179  return;
180 
181  wxArrayString libnames = m_ListEquiv->GetStrings();
182 
183  for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
184  {
185  int jj = selections[ii];
186  std::swap( libnames[jj], libnames[jj+1] );
187  }
188 
189  m_ListEquiv->Set( libnames );
190 
191  // Reselect previously selected names
192  for( size_t ii = 0; ii < selections.GetCount(); ii++ )
193  {
194  int jj = selections[ii];
195  m_ListEquiv->SetSelection( jj+1 );
196  }
197 
198  m_ListChanged = true;
199 }
200 
201 
202 /* Remove a library to the library list.
203  * The real list (g_LibName_List) is not changed, so the change can be canceled
204  */
205 void DIALOG_CONFIG_EQUFILES::OnRemoveFiles( wxCommandEvent& event )
206 {
207  wxArrayInt selections;
208  m_ListEquiv->GetSelections( selections );
209 
210  std::sort( selections.begin(), selections.end() );
211 
212  for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
213  {
214  m_ListEquiv->Delete( selections[ii] );
215  m_ListChanged = true;
216  }
217 }
218 
219 
220 void DIALOG_CONFIG_EQUFILES::OnAddFiles( wxCommandEvent& event )
221 {
222  wxString equFilename;
223  wxFileName fn;
224 
225  wxListBox* list = m_ListEquiv;
226 
227  // Get a default path to open the file dialog:
228  wxString libpath;
229  wxArrayInt selectedRows = m_gridEnvVars->GetSelectedRows();
230 
231  int row = selectedRows.GetCount() ? selectedRows[0] :
232  m_gridEnvVars->GetGridCursorRow();
233 
234  libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
235 
236  wxFileDialog FilesDialog( this, _( "Footprint Association File" ), libpath,
237  wxEmptyString, EquFileWildcard(),
238  wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );
239 
240  if( FilesDialog.ShowModal() != wxID_OK )
241  return;
242 
243  wxArrayString Filenames;
244  FilesDialog.GetPaths( Filenames );
245 
246  for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
247  {
248  fn = Filenames[jj];
249  equFilename.Empty();
250 
251  if( isPathRelativeAllowed() ) // try to use relative path
252  {
253  for( row = 0; row < getEnvVarCount(); row++ )
254  {
255  libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
256 
257  if( fn.MakeRelativeTo( libpath ) )
258  {
259  equFilename.Printf( wxT( "${%s}%c%s" ),
260  GetChars( m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 0 ) ) ),
261  fn.GetPathSeparator(),
262  GetChars( fn.GetFullPath() ) );
263  break;
264  }
265  }
266  }
267 
268  if( equFilename.IsEmpty() )
269  equFilename = Filenames[jj];
270 
271  // Add or insert new library name, if not already in list
272  if( list->FindString( equFilename, fn.IsCaseSensitive() ) == wxNOT_FOUND )
273  {
274  m_ListChanged = true;
275  equFilename.Replace( wxT( "\\" ), wxT( "/" ) ); // Use unix separators only.
276  list->Append( equFilename );
277  }
278  else
279  {
280  wxString msg;
281  msg.Printf( _( "File \"%s\" already exists in list" ), equFilename.GetData() );
282  DisplayError( this, msg );
283  }
284  }
285 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:103
PROJECT holds project specific data.
Definition: project.h:59
This file is part of the common library TODO brief description.
wxArrayString m_EquFilesNames
This file is part of the common library.
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:38
void OnEditEquFile(wxCommandEvent &event) override
void OnAddFiles(wxCommandEvent &event) override
wxString EquFileWildcard()
void OnCloseWindow(wxCloseEvent &event) override
static const wxString GlobalPathEnvVariableName()
Function GlobalPathEnvVarVariableName.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void OnButtonMoveUp(wxCommandEvent &event) override
Definition of file extensions used in Kicad.
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:96
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:101
see class PGM_BASE
void SaveProjectFile()
Function SaveProjectFile Saves the CvPcb configuration parameter from the project (....
Definition: cfg.cpp:61
Class DIALOG_CONFIG_EQUFILES_BASE.
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:205
#define _(s)
Definition: 3d_actions.cpp:33
void OnButtonMoveDown(wxCommandEvent &event) override
void OnOkClick(wxCommandEvent &event) override
DIALOG_CONFIG_EQUFILES(CVPCB_MAINFRAME *parent)
int ExecuteFile(wxWindow *frame, const wxString &ExecFile, const wxString &param, wxProcess *callback)
Function ExecuteFile calls the executable file ExecFile with the command line parameters param.
Definition: gestfich.cpp:174
void OnRemoveFiles(wxCommandEvent &event) override
The CvPcb application main window.