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