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