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 {
274 
275  wxFileName templatePath;
276  wxString envStr;
277 
278  // KiCad system template path.
279  ENV_VAR_MAP_CITER it = Pgm().GetLocalEnvVariables().find( "KICAD_TEMPLATE_DIR" );
280 
281  if( it != Pgm().GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString )
282  {
283  templatePath.AssignDir( it->second.GetValue() );
284  ps->AddTemplatesPage( _( "System Templates" ), templatePath );
285  }
286 
287  // User template path.
288  it = Pgm().GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
289 
290  if( it != Pgm().GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString )
291  {
292  templatePath.AssignDir( it->second.GetValue() );
293  ps->AddTemplatesPage( _( "User Templates" ), templatePath );
294  }
295 
296  // Show the project template selector dialog
297  if( ps->ShowModal() != wxID_OK )
298  return;
299 
300  if( ps->GetSelectedTemplate() == NULL )
301  {
302  wxMessageBox( _( "No project template was selected. Cannot generate new project." ),
303  _( "Error" ),
304  wxOK | wxICON_ERROR,
305  this );
306 
307  return;
308  }
309 
310  // Get project destination folder and project file name.
311  wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
312  wxString title = _( "New Project Folder" );
313  wxDirDialog dlg( this, title, default_dir );
314 
315  if( dlg.ShowModal() == wxID_CANCEL )
316  return;
317 
318  // Builds the project .pro filename, from the new project folder name
319  wxFileName fn;
320  fn.AssignDir( dlg.GetPath() );
321  fn.SetName( dlg.GetPath().AfterLast( SEP() ) );
322  fn.SetExt( "pro" );
323 
324  // Make sure the user has write permissions to the project path.
325  wxFileName prjPath = fn;
326 
327  while( !prjPath.DirExists() )
328  prjPath.RemoveLastDir();
329 
330  if( !prjPath.IsDirWritable() )
331  {
332  wxString msg;
333 
334  msg.Printf( _( "Cannot write to folder \"%s\"." ), prjPath.GetPath() );
335  wxMessageDialog msgDlg( this, msg, _( "Error!" ), wxICON_ERROR | wxOK | wxCENTER );
336  msgDlg.SetExtendedMessage( _( "Please check your access permissions to this folder "
337  "and try again." ) );
338  msgDlg.ShowModal();
339  return;
340  }
341 
342  ClearMsg();
343 
344  // Make sure we are not overwriting anything in the destination folder.
345  std::vector< wxFileName > destFiles;
346 
347  if( ps->GetSelectedTemplate()->GetDestinationFiles( fn, destFiles ) )
348  {
349  std::vector< wxFileName > overwrittenFiles;
350 
351  for( const auto& file : destFiles )
352  {
353  if( file.FileExists() )
354  overwrittenFiles.push_back( file );
355  }
356 
357  if( !overwrittenFiles.empty() )
358  {
359  wxString extendedMsg = _( "Overwriting files:" ) + "\n";
360 
361  for( const auto& file : overwrittenFiles )
362  {
363  extendedMsg += "\n" + file.GetFullName();
364  }
365 
366  wxMessageDialog owDlg( this,
367  _( "Are you sure you want to overwrite files in "
368  "the destination folder?" ),
369  _( "Warning!" ),
370  wxYES_NO | wxICON_QUESTION | wxCENTER );
371  owDlg.SetExtendedMessage( extendedMsg );
372  owDlg.SetYesNoLabels( _( "Overwrite" ), _( "Do Not Overwrite" ) );
373 
374  if( owDlg.ShowModal() == wxID_NO )
375  return;
376  }
377  }
378 
379  wxString errorMsg;
380 
381  // The selected template widget contains the template we're attempting to use to
382  // create a project
383  if( !ps->GetSelectedTemplate()->CreateProject( fn, &errorMsg ) )
384  {
385  wxMessageDialog createDlg( this,
386  _( "A problem occurred creating new project from template!" ),
387  _( "Template Error" ),
388  wxOK | wxICON_ERROR );
389 
390  if( !errorMsg.empty() )
391  createDlg.SetExtendedMessage( errorMsg );
392 
393  createDlg.ShowModal();
394  return;
395  }
396 
397  CreateNewProject( fn.GetFullPath() );
398  LoadProject( fn );
399 }
400 
401 
402 void KICAD_MANAGER_FRAME::OnSaveProject( wxCommandEvent& event )
403 {
404  if( !wxIsWritable( GetProjectFileName() ) )
405  return;
406 
407  Prj().ConfigSave( PgmTop().SysSearch(), GeneralGroupName, s_KicadManagerParams );
408 }
409 
410 
411 void KICAD_MANAGER_FRAME::OnUpdateRequiresProject( wxUpdateUIEvent& event )
412 {
413  event.Enable( m_active_project );
414 }
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:318
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:287
const wxString LegacyPcbFileExtension
#define SEP()
Definition: prjconfig.cpp:50
The common library.
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
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.
VTBL_ENTRY const ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.h:270
void OnUpdateRequiresProject(wxUpdateUIEvent &event)
Definition: prjconfig.cpp:411
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
KICAD_MANAGER_FRAME is the KiCad main frame.
std::map< wxString, ENV_VAR_ITEM >::const_iterator ENV_VAR_MAP_CITER
Definition: pgm_base.h:89
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:72
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:353
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:402
KIWAY Kiway