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>
38 #include <project/project_file.h>
41 
42 
45 {
46  m_Parent = aParent;
47 
48  PROJECT& prj = Prj();
49  SetTitle( wxString::Format( _( "Project file: \"%s\"" ), 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  PROJECT_FILE& project = Prj().GetProjectFile();
64 
65  if( !project.m_EquivalenceFiles.empty() )
66  {
67  wxArrayString arr;
68 
69  for( const auto& entry : project.m_EquivalenceFiles )
70  arr.Add( entry );
71 
72  m_ListEquiv->InsertItems( arr, 0 );
73  }
74 
75  if( getEnvVarCount() < 2 )
76  m_gridEnvVars->AppendRows(2 - getEnvVarCount() );
77 
78  wxString evValue;
79  int row = 0;
80 
81  m_gridEnvVars->SetCellValue( row++, 0, PROJECT_VAR_NAME );
82  m_gridEnvVars->SetCellValue( row, 0, FP_LIB_TABLE::GlobalPathEnvVariableName() );
83 
84  for( row = 0; row < getEnvVarCount(); row++ )
85  {
86  if( wxGetEnv( m_gridEnvVars->GetCellValue( row, 0 ), &evValue ) )
87  m_gridEnvVars->SetCellValue( row, 1, evValue );
88  }
89 
90  m_gridEnvVars->AutoSizeColumns();
91 
92 }
93 
94 
95 void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
96 {
97  wxString editorname = Pgm().GetEditorName();
98 
99  if( editorname.IsEmpty() )
100  {
101  wxMessageBox( _( "No editor defined in KiCad. Please choose it." ) );
102  return;
103  }
104 
105  wxArrayInt selections;
106  m_ListEquiv->GetSelections( selections );
107 
108  wxString fullFileNames, tmp;
109 
110  for( unsigned ii = 0; ii < selections.GetCount(); ii++ )
111  {
112  tmp = m_ListEquiv->GetString( selections[ii] );
113  fullFileNames << wxT( " \"" ) << wxExpandEnvVars( tmp ) << wxT( "\"" );
114  m_ListChanged = true;
115  }
116 
117  ExecuteFile( this, editorname, fullFileNames );
118 }
119 
120 
121 void DIALOG_CONFIG_EQUFILES::OnOkClick( wxCommandEvent& event )
122 {
123  // Save new equ file list if the files list was modified
124  if( m_ListChanged )
125  {
126  PROJECT_FILE& project = Prj().GetProjectFile();
127 
128  // Recreate equ list
129  project.m_EquivalenceFiles.clear();
130 
131  for( unsigned ii = 0; ii < m_ListEquiv->GetCount(); ii++ )
132  project.m_EquivalenceFiles.emplace_back( m_ListEquiv->GetString( ii ) );
133 
134  wxCommandEvent evt( ID_SAVE_PROJECT );
135  Pgm().GetSettingsManager().SaveProject();
136  }
137 
138  EndModal( wxID_OK );
139 }
140 
141 
142 void DIALOG_CONFIG_EQUFILES::OnCloseWindow( wxCloseEvent& event )
143 {
144  EndModal( wxID_CANCEL );
145 }
146 
147 
148 void DIALOG_CONFIG_EQUFILES::OnButtonMoveUp( wxCommandEvent& event )
149 {
150  wxArrayInt selections;
151 
152  m_ListEquiv->GetSelections( selections );
153 
154  if ( selections.GetCount() <= 0 ) // No selection.
155  return;
156 
157  if( selections[0] == 0 ) // The first lib is selected. cannot move up it
158  return;
159 
160  wxArrayString libnames = m_ListEquiv->GetStrings();
161 
162  for( size_t ii = 0; ii < selections.GetCount(); ii++ )
163  {
164  int jj = selections[ii];
165  std::swap( libnames[jj], libnames[jj-1] );
166  }
167 
168  m_ListEquiv->Set( libnames );
169 
170  // Reselect previously selected names
171  for( size_t ii = 0; ii < selections.GetCount(); ii++ )
172  {
173  int jj = selections[ii];
174  m_ListEquiv->SetSelection( jj-1 );
175  }
176 
177  m_ListChanged = true;
178 }
179 
180 
181 void DIALOG_CONFIG_EQUFILES::OnButtonMoveDown( wxCommandEvent& event )
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 void DIALOG_CONFIG_EQUFILES::OnAddFiles( wxCommandEvent& event )
233 {
234  wxString equFilename;
235  wxFileName fn;
236 
237  wxListBox* list = m_ListEquiv;
238 
239  // Get a default path to open the file dialog:
240  wxString libpath;
241  wxArrayInt selectedRows = m_gridEnvVars->GetSelectedRows();
242 
243  int row = selectedRows.GetCount() ? selectedRows[0] :
244  m_gridEnvVars->GetGridCursorRow();
245 
246  libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
247 
248  wxFileDialog FilesDialog( this, _( "Footprint Association File" ), libpath,
249  wxEmptyString, EquFileWildcard(),
250  wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );
251 
252  if( FilesDialog.ShowModal() != wxID_OK )
253  return;
254 
255  wxArrayString Filenames;
256  FilesDialog.GetPaths( Filenames );
257 
258  for( unsigned jj = 0; jj < Filenames.GetCount(); jj++ )
259  {
260  fn = Filenames[jj];
261  equFilename.Empty();
262 
263  if( isPathRelativeAllowed() ) // try to use relative path
264  {
265  for( row = 0; row < getEnvVarCount(); row++ )
266  {
267  libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
268 
269  if( fn.MakeRelativeTo( libpath ) )
270  {
271  equFilename.Printf( wxT( "${%s}%c%s" ),
272  m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 0 ) ),
273  fn.GetPathSeparator(), fn.GetFullPath() );
274  break;
275  }
276  }
277  }
278 
279  if( equFilename.IsEmpty() )
280  equFilename = Filenames[jj];
281 
282  // Add or insert new library name, if not already in list
283  if( list->FindString( equFilename, fn.IsCaseSensitive() ) == wxNOT_FOUND )
284  {
285  m_ListChanged = true;
286  equFilename.Replace( wxT( "\\" ), wxT( "/" ) ); // Use unix separators only.
287  list->Append( equFilename );
288  }
289  else
290  {
291  wxString msg;
292  msg.Printf( _( "File \"%s\" already exists in list" ), equFilename.GetData() );
293  DisplayError( this, msg );
294  }
295  }
296 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:240
PROJECT holds project specific data.
Definition: project.h:63
This file is part of the common library TODO brief description.
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
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:102
PROJECT_FILE is the backing store for a PROJECT, in JSON format.
Definition: project_file.h:62
wxString EquFileWildcard()
void OnCloseWindow(wxCloseEvent &event) override
static const wxString GlobalPathEnvVariableName()
Function GlobalPathEnvVarVariableName.
VTBL_ENTRY PROJECT_FILE & GetProjectFile() const
Definition: project.h:143
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void OnButtonMoveUp(wxCommandEvent &event) override
std::vector< wxString > m_EquivalenceFiles
CvPcb params.
Definition: project_file.h:150
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:116
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
see class PGM_BASE
Class DIALOG_CONFIG_EQUFILES_BASE.
#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:165
void OnRemoveFiles(wxCommandEvent &event) override
The CvPcb application main window.