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;
46 
47  PROJECT& prj = Prj();
48  SetTitle( wxString::Format( _( "Project file: \"%s\"" ),
49  GetChars( prj.GetProjectFullName() ) ) );
50 
51  Init( );
52 
53  GetSizer()->SetSizeHints( this );
54  Center();
55 }
56 
57 
59 {
60  m_sdbSizerOK->SetDefault();
61  m_ListChanged = false;
62 
63  if( !m_Parent->m_EquFilesNames.IsEmpty() )
64  m_ListEquiv->InsertItems( m_Parent->m_EquFilesNames, 0 );
65 
66  if( getEnvVarCount() < 2 )
67  m_gridEnvVars->AppendRows(2 - getEnvVarCount() );
68 
69  wxString evValue;
70  int row = 0;
71 
72  m_gridEnvVars->SetCellValue( row++, 0, PROJECT_VAR_NAME );
73  m_gridEnvVars->SetCellValue( row, 0, FP_LIB_TABLE::GlobalPathEnvVariableName() );
74 
75  for( row = 0; row < getEnvVarCount(); row++ )
76  {
77  if( wxGetEnv( m_gridEnvVars->GetCellValue( row, 0 ), &evValue ) )
78  m_gridEnvVars->SetCellValue( row, 1, evValue );
79  }
80 
81  m_gridEnvVars->AutoSizeColumns();
82 
83 }
84 
85 
86 void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
87 {
88  wxString editorname = Pgm().GetEditorName();
89 
90  if( editorname.IsEmpty() )
91  {
92  wxMessageBox( _( "No editor defined in KiCad. Please choose it." ) );
93  return;
94  }
95 
96  wxArrayInt selections;
97  m_ListEquiv->GetSelections( selections );
98 
99  wxString fullFileNames, tmp;
100 
101  for( unsigned ii = 0; ii < selections.GetCount(); ii++ )
102  {
103  tmp = m_ListEquiv->GetString( selections[ii] );
104  fullFileNames << wxT( " \"" ) << wxExpandEnvVars( tmp ) << wxT( "\"" );
105  m_ListChanged = true;
106  }
107 
108  ExecuteFile( this, editorname, fullFileNames );
109 }
110 
111 
112 void DIALOG_CONFIG_EQUFILES::OnOkClick( wxCommandEvent& event )
113 {
114  // Save new equ file list if the files list was modified
115  if( m_ListChanged )
116  {
117  // Recreate equ list
118  m_Parent->m_EquFilesNames.Clear();
119 
120  for( unsigned ii = 0; ii < m_ListEquiv->GetCount(); ii++ )
121  m_Parent->m_EquFilesNames.Add( m_ListEquiv->GetString( ii ) );
122 
123  wxCommandEvent evt( ID_SAVE_PROJECT );
125  }
126 
127  EndModal( wxID_OK );
128 }
129 
130 
131 void DIALOG_CONFIG_EQUFILES::OnCloseWindow( wxCloseEvent& event )
132 {
133  EndModal( wxID_CANCEL );
134 }
135 
136 
137 void DIALOG_CONFIG_EQUFILES::OnButtonMoveUp( wxCommandEvent& event )
138 {
139  wxArrayInt selections;
140 
141  m_ListEquiv->GetSelections( selections );
142 
143  if ( selections.GetCount() <= 0 ) // No selection.
144  return;
145 
146  if( selections[0] == 0 ) // The first lib is selected. cannot move up it
147  return;
148 
149  wxArrayString libnames = m_ListEquiv->GetStrings();
150 
151  for( size_t ii = 0; ii < selections.GetCount(); ii++ )
152  {
153  int jj = selections[ii];
154  std::swap( libnames[jj], libnames[jj-1] );
155  }
156 
157  m_ListEquiv->Set( libnames );
158 
159  // Reselect previously selected names
160  for( size_t ii = 0; ii < selections.GetCount(); ii++ )
161  {
162  int jj = selections[ii];
163  m_ListEquiv->SetSelection( jj-1 );
164  }
165 
166  m_ListChanged = true;
167 }
168 
169 
170 void DIALOG_CONFIG_EQUFILES::OnButtonMoveDown( wxCommandEvent& event )
171 {
172  wxArrayInt selections;
173  m_ListEquiv->GetSelections( selections );
174 
175  if ( selections.GetCount() <= 0 ) // No selection.
176  return;
177 
178  // The last lib is selected. cannot move down it
179  if( selections.Last() == int( m_ListEquiv->GetCount()-1 ) )
180  return;
181 
182  wxArrayString libnames = m_ListEquiv->GetStrings();
183 
184  for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
185  {
186  int jj = selections[ii];
187  std::swap( libnames[jj], libnames[jj+1] );
188  }
189 
190  m_ListEquiv->Set( libnames );
191 
192  // Reselect previously selected names
193  for( size_t ii = 0; ii < selections.GetCount(); ii++ )
194  {
195  int jj = selections[ii];
196  m_ListEquiv->SetSelection( jj+1 );
197  }
198 
199  m_ListChanged = true;
200 }
201 
202 
203 /* Remove a library to the library list.
204  * The real list (g_LibName_List) is not changed, so the change can be canceled
205  */
206 void DIALOG_CONFIG_EQUFILES::OnRemoveFiles( wxCommandEvent& event )
207 {
208  wxArrayInt selections;
209  m_ListEquiv->GetSelections( selections );
210 
211  std::sort( selections.begin(), selections.end() );
212 
213  for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
214  {
215  m_ListEquiv->Delete( selections[ii] );
216  m_ListChanged = true;
217  }
218 }
219 
220 
221 void DIALOG_CONFIG_EQUFILES::OnAddFiles( wxCommandEvent& event )
222 {
223  wxString equFilename;
224  wxFileName fn;
225 
226  wxListBox* list = m_ListEquiv;
227 
228  // Get a default path to open the file dialog:
229  wxString libpath;
230  wxArrayInt selectedRows = m_gridEnvVars->GetSelectedRows();
231 
232  int row = selectedRows.GetCount() ? selectedRows[0] :
233  m_gridEnvVars->GetGridCursorRow();
234 
235  libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
236 
237  wxFileDialog FilesDialog( this, _( "Footprint Association File" ), libpath,
238  wxEmptyString, EquFileWildcard(),
239  wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );
240 
241  if( FilesDialog.ShowModal() != wxID_OK )
242  return;
243 
244  wxArrayString Filenames;
245  FilesDialog.GetPaths( Filenames );
246 
247  for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
248  {
249  fn = Filenames[jj];
250  equFilename.Empty();
251 
252  if( isPathRelativeAllowed() ) // try to use relative path
253  {
254  for( row = 0; row < getEnvVarCount(); row++ )
255  {
256  libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
257 
258  if( fn.MakeRelativeTo( libpath ) )
259  {
260  equFilename.Printf( wxT( "${%s}%c%s" ),
261  GetChars( m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 0 ) ) ),
262  fn.GetPathSeparator(),
263  GetChars( fn.GetFullPath() ) );
264  break;
265  }
266  }
267  }
268 
269  if( equFilename.IsEmpty() )
270  equFilename = Filenames[jj];
271 
272  // Add or insert new library name, if not already in list
273  if( list->FindString( equFilename, fn.IsCaseSensitive() ) == wxNOT_FOUND )
274  {
275  m_ListChanged = true;
276  equFilename.Replace( wxT( "\\" ), wxT( "/" ) ); // Use unix separators only.
277  list->Append( equFilename );
278  }
279  else
280  {
281  wxString msg;
282  msg.Printf( _( "File \"%s\" already exists in list" ), equFilename.GetData() );
283  DisplayError( this, msg );
284  }
285  }
286 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
Class PROJECT holds project specific data.
Definition: project.h:58
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:37
void OnEditEquFile(wxCommandEvent &event) override
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
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
#define _(s)
VTBL_ENTRY wxConfigBase * CommonSettings() const
Definition: pgm_base.h:194
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
void OnButtonMoveDown(wxCommandEvent &event) override
void OnOkClick(wxCommandEvent &event) override
VTBL_ENTRY const wxString & GetEditorName(bool aCanShowFileChooser=true)
Return the preferred editor name.
Definition: pgm_base.cpp:161
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.