KiCad PCB EDA Suite
prjconfig.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004-2015 Jean-Pierre Charras
5  * Copyright (C) 2004-2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
31 #include <wx/dir.h>
32 #include <wx/filename.h>
33 #include <wx/stdpaths.h>
34 #include <wx/msgdlg.h>
35 
36 #include <build_version.h>
37 #include <config_params.h>
38 #include <confirm.h>
39 #include <kiway.h>
40 #include <project.h>
42 
44 
45 #include "kicad.h"
46 #include "pgm_kicad.h"
47 #include "tree_project_frame.h"
48 
49 
50 #define SEP() wxFileName::GetPathSeparator()
51 
52 // Not really useful, provided to save/restore params in project config file,
53 // (Add them in s_KicadManagerParams if any)
54 // Used also to create new .pro files from the kicad.pro template file
55 // for new projects
56 #define GeneralGroupName wxT( "/general" )
57 
59 
60 
61 void KICAD_MANAGER_FRAME::LoadProject( const wxFileName& aProjectFileName )
62 {
63  // The project file should be valid by the time we get here or something has gone wrong.
64  if( !aProjectFileName.Exists() )
65  return;
66 
67  // Any open KIFACE's must be closed if they are not part of the new project.
68  // (We never want a KIWAY_PLAYER open on a KIWAY that isn't in the same project.)
69  // User is prompted here to close those KIWAY_PLAYERs:
70  if( !Kiway.PlayersClose( false ) )
71  return;
72 
73  SetTitle( wxString( "KiCad " ) + GetBuildVersion() );
74 
75  // Save the project file for the currently loaded project.
76  if( m_active_project )
77  Prj().ConfigLoad( PgmTop().SysSearch(), GeneralGroupName, s_KicadManagerParams );
78 
79  m_active_project = true;
80  ClearMsg();
81  SetProjectFileName( aProjectFileName.GetFullPath() );
82  Prj().ConfigLoad( PgmTop().SysSearch(), GeneralGroupName, s_KicadManagerParams );
83 
84  wxString title = GetTitle() + " " + aProjectFileName.GetFullPath();
85 
86  if( !aProjectFileName.IsDirWritable() )
87  title += _( " [Read Only]" );
88  else
89  SetMruPath( Prj().GetProjectPath() ); // Only set MRU path if we have write access. Why?
90 
91  SetTitle( title );
92 
93  UpdateFileHistory( aProjectFileName.GetFullPath(), &PgmTop().GetFileHistory() );
94 
96 
97  // Rebuild the list of watched paths.
98  // however this is possible only when the main loop event handler is running,
99  // so we use it to run the rebuild function.
100  wxCommandEvent cmd( wxEVT_COMMAND_MENU_SELECTED, ID_INIT_WATCHED_PATHS );
101 
102  wxPostEvent( this, cmd );
103 
104  PrintPrjInfo();
105 }
106 
107 
108 void KICAD_MANAGER_FRAME::CreateNewProject( const wxFileName& aProjectFileName )
109 {
110  wxCHECK_RET( aProjectFileName.DirExists() && aProjectFileName.IsDirWritable(),
111  "Project folder must exist and be writable to create a new project." );
112 
113  // Init project filename. This clears all elements from the project object.
114  SetProjectFileName( aProjectFileName.GetFullPath() );
115 
116  // Copy kicad.pro file from template folder.
117  if( !aProjectFileName.FileExists() )
118  {
119  wxString srcFileName = sys_search().FindValidPath( "kicad.pro" );
120 
121  // Create a minimal project (.pro) file if the template project file could not be copied.
122  if( !wxFileName::FileExists( srcFileName )
123  || !wxCopyFile( srcFileName, aProjectFileName.GetFullPath() ) )
124  {
125  Prj().ConfigSave( PgmTop().SysSearch(), GeneralGroupName, s_KicadManagerParams );
126  }
127  }
128 
129  // Ensure a "stub" for a schematic root sheet and a board exist.
130  // It will avoid messages from the schematic editor or the board editor to create a new file
131  // And forces the user to create main files under the right name for the project manager
132  wxFileName fn( aProjectFileName.GetFullPath() );
133  fn.SetExt( SchematicFileExtension );
134 
135  // If a <project>.sch file does not exist, create a "stub" file ( minimal schematic file )
136  if( !fn.FileExists() )
137  {
138  wxFile file( fn.GetFullPath(), wxFile::write );
139 
140  if( file.IsOpened() )
141  file.Write( wxT( "EESchema Schematic File Version 2\n"
142  "EELAYER 25 0\nEELAYER END\n$EndSCHEMATC\n" ) );
143 
144  // wxFile dtor will close the file
145  }
146 
147  // If a <project>.kicad_pcb or <project>.brd file does not exist,
148  // create a .kicad_pcb "stub" file
149  fn.SetExt( KiCadPcbFileExtension );
150  wxFileName leg_fn( fn );
151  leg_fn.SetExt( LegacyPcbFileExtension );
152 
153  if( !fn.FileExists() && !leg_fn.FileExists() )
154  {
155  wxFile file( fn.GetFullPath(), wxFile::write );
156 
157  if( file.IsOpened() )
158  file.Write( wxT( "(kicad_pcb (version 4) (host kicad \"dummy file\") )\n" ) );
159 
160  // wxFile dtor will close the file
161  }
162 }
163 
164 
165 void KICAD_MANAGER_FRAME::OnLoadProject( wxCommandEvent& event )
166 {
167  wxString default_dir = GetMruPath();
168  wxFileDialog dlg( this, _( "Open Existing Project" ), default_dir, wxEmptyString,
169  ProjectFileWildcard(), wxFD_OPEN | wxFD_FILE_MUST_EXIST );
170 
171  if( dlg.ShowModal() == wxID_CANCEL )
172  return;
173 
174  wxFileName pro( dlg.GetPath() );
175  pro.SetExt( ProjectFileExtension ); // enforce extension
176 
177  if( !pro.IsAbsolute() )
178  pro.MakeAbsolute();
179 
180  if( !pro.FileExists() )
181  return;
182 
183  LoadProject( pro );
184 }
185 
186 
188 class DIR_CHECKBOX : public wxPanel
189 {
190 public:
191  DIR_CHECKBOX( wxWindow* aParent )
192  : wxPanel( aParent )
193  {
194  m_cbCreateDir = new wxCheckBox( this, wxID_ANY,
195  _( "Create a new directory for the project" ) );
196  m_cbCreateDir->SetValue( true );
197 
198  wxBoxSizer* sizer = new wxBoxSizer( wxHORIZONTAL );
199  sizer->Add( m_cbCreateDir, 0, wxALL, 8 );
200 
201  SetSizerAndFit( sizer );
202  }
203 
204  bool CreateNewDir() const
205  {
206  return m_cbCreateDir->GetValue();
207  }
208 
209  static wxWindow* Create( wxWindow* aParent )
210  {
211  return new DIR_CHECKBOX( aParent );
212  }
213 
214 protected:
215  wxCheckBox* m_cbCreateDir;
216 };
217 
218 
219 void KICAD_MANAGER_FRAME::OnNewProject( wxCommandEvent& aEvent )
220 {
221  wxString default_dir = GetMruPath();
222  wxFileDialog dlg( this, _( "Create New Project" ), default_dir, wxEmptyString,
223  ProjectFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
224 
225  // Add a "Create a new directory" checkbox
226  dlg.SetExtraControlCreator( &DIR_CHECKBOX::Create );
227 
228  if( dlg.ShowModal() == wxID_CANCEL )
229  return;
230 
231  wxFileName pro( dlg.GetPath() );
232  pro.SetExt( ProjectFileExtension ); // enforce extension
233 
234  if( !pro.IsAbsolute() )
235  pro.MakeAbsolute();
236 
237  // Append a new directory with the same name of the project file.
238  if( static_cast<DIR_CHECKBOX*>( dlg.GetExtraControl() )->CreateNewDir() )
239  pro.AppendDir( pro.GetName() );
240 
241  // Check if the project directory is empty if it already exists.
242  wxDir directory( pro.GetPath() );
243 
244  if( !pro.DirExists() )
245  {
246  if( !pro.Mkdir() )
247  {
248  wxString msg;
249  msg.Printf( _( "Directory \"%s\" could not be created.\n\n"
250  "Please make sure you have write permissions and try again." ),
251  pro.GetPath() );
252  DisplayErrorMessage( this, msg );
253  return;
254  }
255  }
256  else if( directory.HasFiles() )
257  {
258  wxString msg = _( "The selected directory is not empty. It is recommended that you "
259  "create projects in their own empty directory.\n\nDo you "
260  "want to continue?" );
261 
262  if( !IsOK( this, msg ) )
263  return;
264  }
265 
266  CreateNewProject( pro );
267  LoadProject( pro );
268 }
269 
270 
272 {
273  wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
274  wxString title = _( "New Project Folder" );
275  wxDirDialog dlg( this, title, default_dir );
276 
277  if( dlg.ShowModal() == wxID_CANCEL )
278  return;
279 
280  // Builds the project .pro filename, from the new project folder name
281  wxFileName fn;
282  fn.AssignDir( dlg.GetPath() );
283  fn.SetName( dlg.GetPath().AfterLast( SEP() ) );
284  fn.SetExt( wxT( "pro" ) );
285 
286  wxChar sep[2] = { SEP(), 0 }; // nul terminated separator wxChar string.
287 
288  ClearMsg();
289 
291 
292  wxFileName templatePath;
293  wxString envStr;
294 
295 #ifndef __WXMAC__
296  wxGetEnv( wxT( "KICAD" ), &envStr );
297 
298  // Add a new tab for system templates
299  if( !envStr.empty() )
300  {
301  // user may or may not have including terminating separator.
302  if( !envStr.EndsWith( sep ) )
303  envStr += sep;
304 
305  templatePath = envStr + wxT( "template" ) + sep;
306  }
307  else
308  {
309  // The standard path should be in the share directory for kicad. As
310  // it is normal on Windows to only have the share directory and not
311  // the kicad sub-directory we fall back to that if the directory
312  // doesn't exist
313  templatePath = wxPathOnly( wxStandardPaths::Get().GetExecutablePath() ) +
314  sep + wxT( ".." ) + sep + wxT( "share" ) + sep + wxT( "kicad" ) +
315  sep + wxT( "template" ) + sep;
316 
317  if( !wxDirExists( templatePath.GetFullPath() ) )
318  {
319  templatePath = wxPathOnly( wxStandardPaths::Get().GetExecutablePath() ) + sep +
320  wxT( ".." ) + sep + wxT( "share" ) + sep + wxT( "template" ) + sep;
321  }
322  }
323 #else
324  // Use what is provided in the bundle data dir
325  templatePath = GetOSXKicadDataDir() + sep + wxT( "template" );
326 #endif
327 
328  ps->AddTemplatesPage( _( "System Templates" ), templatePath );
329 
330  // Add a new tab for user templates
331  wxFileName userPath = wxStandardPaths::Get().GetDocumentsDir() + sep + wxT( "kicad" ) +
332  sep + wxT( "template" ) + sep;
333 
334  ps->AddTemplatesPage( _( "User Templates" ), userPath );
335 
336  // Check to see if a custom template location is available and setup a
337  // new selection tab if there is.
338  envStr.clear();
339  wxGetEnv( wxT( "KICAD_PTEMPLATES" ), &envStr );
340 
341  if( !envStr.empty() )
342  {
343  if( !envStr.EndsWith( sep ) )
344  envStr += sep;
345 
346  wxFileName envPath = envStr;
347 
348  ps->AddTemplatesPage( _( "Portable Templates" ), envPath );
349  }
350 
351  // Show the project template selector dialog
352  if( ps->ShowModal() != wxID_OK )
353  return;
354 
355  if( ps->GetSelectedTemplate() == NULL )
356  {
357  wxMessageBox( _( "No project template was selected. Cannot generate new project." ),
358  _( "Error" ),
359  wxOK | wxICON_ERROR,
360  this );
361 
362  return;
363  }
364 
365  // Make sure the user has write permissions to the base path.
366  wxFileName prjPath = fn;
367 
368  while( !prjPath.DirExists() )
369  prjPath.RemoveLastDir();
370 
371  if( !prjPath.IsDirWritable() )
372  {
373  wxString msg;
374 
375  msg.Printf( _( "Cannot write to folder \"%s\"." ), prjPath.GetPath() );
376  wxMessageDialog msgDlg( this, msg, _( "Error!" ), wxICON_ERROR | wxOK | wxCENTER );
377  msgDlg.SetExtendedMessage( _( "Please check your access permissions to this folder "
378  "and try again." ) );
379  msgDlg.ShowModal();
380  return;
381  }
382 
383  // Make sure we are not overwriting anything in the destination folder.
384  std::vector< wxFileName > destFiles;
385 
386  if( ps->GetSelectedTemplate()->GetDestinationFiles( fn, destFiles ) )
387  {
388  std::vector< wxFileName > overwrittenFiles;
389 
390  for( const auto& file : destFiles )
391  {
392  if( file.FileExists() )
393  overwrittenFiles.push_back( file );
394  }
395 
396  if( !overwrittenFiles.empty() )
397  {
398  wxString extendedMsg = _( "Overwriting files:" ) + "\n";
399 
400  for( const auto& file : overwrittenFiles )
401  {
402  extendedMsg += "\n" + file.GetFullName();
403  }
404 
405  wxMessageDialog owDlg( this,
406  _( "Are you sure you want to overwrite files in "
407  "the destination folder?" ),
408  _( "Warning!" ),
409  wxYES_NO | wxICON_QUESTION | wxCENTER );
410  owDlg.SetExtendedMessage( extendedMsg );
411  owDlg.SetYesNoLabels( _( "Overwrite" ), _( "Do Not Overwrite" ) );
412 
413  if( owDlg.ShowModal() == wxID_NO )
414  return;
415  }
416  }
417 
418  wxString errorMsg;
419 
420  // The selected template widget contains the template we're attempting to use to
421  // create a project
422  if( !ps->GetSelectedTemplate()->CreateProject( fn, &errorMsg ) )
423  {
424  wxMessageDialog createDlg( this,
425  _( "A problem occurred creating new project from template!" ),
426  _( "Template Error" ),
427  wxOK | wxICON_ERROR );
428 
429  if( !errorMsg.empty() )
430  createDlg.SetExtendedMessage( errorMsg );
431 
432  createDlg.ShowModal();
433  return;
434  }
435 
436  CreateNewProject( fn.GetFullPath() );
437  LoadProject( fn );
438 }
439 
440 
441 void KICAD_MANAGER_FRAME::OnSaveProject( wxCommandEvent& event )
442 {
443  if( !wxIsWritable( GetProjectFileName() ) )
444  return;
445 
446  Prj().ConfigSave( PgmTop().SysSearch(), GeneralGroupName, s_KicadManagerParams );
447 }
448 
449 
450 void KICAD_MANAGER_FRAME::OnUpdateRequiresProject( wxUpdateUIEvent& event )
451 {
452  event.Enable( m_active_project );
453 }
A list of parameters type.
VTBL_ENTRY bool PlayersClose(bool doForce)
Function PlayersClose calls the KIWAY_PLAYER::Close( bool force ) function on all the windows and if ...
Definition: kiway.cpp:373
VTBL_ENTRY void ConfigSave(const SEARCH_STACK &aSList, const wxString &aGroupName, const PARAM_CFG_ARRAY &aParams, const wxString &aFileName=wxEmptyString)
Function ConfigSave saves the current "project" parameters into the wxConfigBase* derivative...
Definition: project.cpp:316
void OnNewProject(wxCommandEvent &aEvent)
Definition: prjconfig.cpp:219
void SetMruPath(const wxString &aPath)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:199
PARAM_CFG_ARRAY s_KicadManagerParams
Definition: prjconfig.cpp:58
This file is part of the common library.
Helper widget to select whether a new directory should be created for a project
Definition: prjconfig.cpp:188
PROJECT_TEMPLATE * GetSelectedTemplate()
const wxString ProjectFileExtension
PROJECT & Prj()
Definition: kicad.cpp:270
const wxString LegacyPcbFileExtension
#define SEP()
Definition: prjconfig.cpp:50
The common library.
void OnLoadProject(wxCommandEvent &event)
Load an exiting project (.pro) file.
Definition: prjconfig.cpp:165
void UpdateFileHistory(const wxString &FullFileName, wxFileHistory *aFileHistory=NULL)
Function UpdateFileHistory Updates the list of recently opened files.
void OnUpdateRequiresProject(wxUpdateUIEvent &event)
Definition: prjconfig.cpp:450
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
KICAD_MANAGER_FRAME is the KiCad main frame.
DIR_CHECKBOX(wxWindow *aParent)
Definition: prjconfig.cpp:191
The common library.
void OnCreateProjectFromTemplate(wxCommandEvent &event)
Creates a new project folder, copy a template into this new folder.
Definition: prjconfig.cpp:271
void ReCreateTreePrj()
Create or modify the tree showing project file names.
PGM_KICAD & PgmTop()
Definition: kicad.cpp:71
wxCheckBox * m_cbCreateDir
Definition: prjconfig.cpp:215
#define GeneralGroupName
Definition: prjconfig.cpp:56
wxString ProjectFileWildcard()
const wxString GetProjectFileName()
Definition: mainframe.cpp:147
void SetProjectFileName(const wxString &aFullProjectProFileName)
Definition: mainframe.cpp:135
size_t GetDestinationFiles(const wxFileName &aNewProjectPath, std::vector< wxFileName > &aDestFiles)
Fetch the list of destination files to be copied when the new project is created. ...
bool CreateProject(wxFileName &aNewProjectPath, wxString *aErrorMsg=nullptr)
Copies and renames all template files to create a new project.
TREE_PROJECT_FRAME * m_LeftWin
Definition: kicad.h:304
static wxWindow * Create(wxWindow *aParent)
Definition: prjconfig.cpp:209
void AddTemplatesPage(const wxString &aTitle, wxFileName &aPath)
Add a new page with aTitle, populated with templates from aPath.
const wxString KiCadPcbFileExtension
bool CreateNewDir() const
Definition: prjconfig.cpp:204
VTBL_ENTRY bool ConfigLoad(const SEARCH_STACK &aSearchS, const wxString &aGroupName, const PARAM_CFG_ARRAY &aParams, const wxString &aForeignConfigFileName=wxEmptyString)
Function ConfigLoad reads a subset of parameters from the "project" file.
Definition: project.cpp:351
void LoadProject(const wxFileName &aProjectFileName)
Definition: prjconfig.cpp:61
bool m_active_project
Definition: kicad.h:314
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:71
void CreateNewProject(const wxFileName &aProjectFileName)
Creates a new project by setting up and initial project, schematic, and board files.
Definition: prjconfig.cpp:108
wxFileHistory & GetFileHistory()
Definition: pgm_kicad.h:57
wxString GetMruPath() const
const SEARCH_STACK & sys_search() override
Function sys_search returns a SEARCH_STACK pertaining to entire program, and is overloaded in KICAD_M...
Definition: mainframe.cpp:189
void ClearMsg()
Erase the text panel.
Definition: mainframe.cpp:529
void PrintPrjInfo()
Prints the current working directory name and the projet name on the text panel.
Definition: mainframe.cpp:549
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:233
const wxString SchematicFileExtension
void OnSaveProject(wxCommandEvent &event)
Save the project (.pro) file containing the top level configuration parameters.
Definition: prjconfig.cpp:441
KIWAY Kiway