KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
kicad_manager_control.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) 2019 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22#include <env_vars.h>
23#include <executable_names.h>
24#include <pgm_base.h>
25#include <pgm_kicad.h>
26#include <policy_keys.h>
27#include <kiway.h>
28#include <kicad_manager_frame.h>
29#include <kiplatform/policy.h>
30#include <kiplatform/secrets.h>
31#include <confirm.h>
32#include <kidialog.h>
37#include <tool/selection.h>
38#include <tool/tool_event.h>
45#include <gestfich.h>
46#include <paths.h>
47#include <wx/dir.h>
48#include <wx/filedlg.h>
50#include "dialog_pcm.h"
52#include <project_tree_pane.h>
53#include <project_tree.h>
54#include <launch_ext.h>
55
57
59 TOOL_INTERACTIVE( "kicad.Control" ),
60 m_frame( nullptr )
61{
62}
63
64
66{
67 m_frame = getEditFrame<KICAD_MANAGER_FRAME>();
68}
69
70
71wxFileName KICAD_MANAGER_CONTROL::newProjectDirectory( wxString* aFileName, bool isRepo )
72{
73 wxString default_filename = aFileName ? *aFileName : wxString();
74
75 wxString default_dir = m_frame->GetMruPath();
76 wxFileDialog dlg( m_frame, _( "Create New Project" ), default_dir, default_filename,
77 ( isRepo ? wxString( "" ) : FILEEXT::ProjectFileWildcard() ),
78 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
79
80 // Add a "Create a new directory" checkbox
81 FILEDLG_NEW_PROJECT newProjectHook;
82 dlg.SetCustomizeHook( newProjectHook );
83
84 if( dlg.ShowModal() == wxID_CANCEL )
85 return wxFileName();
86
87 wxFileName pro( dlg.GetPath() );
88
89 // wxFileName automatically extracts an extension. But if it isn't
90 // a .pro extension, we should keep it as part of the filename
91 if( !pro.GetExt().IsEmpty() && pro.GetExt().ToStdString() != FILEEXT::ProjectFileExtension )
92 pro.SetName( pro.GetName() + wxT( "." ) + pro.GetExt() );
93
94 pro.SetExt( FILEEXT::ProjectFileExtension ); // enforce extension
95
96 if( !pro.IsAbsolute() )
97 pro.MakeAbsolute();
98
99 // Append a new directory with the same name of the project file.
100 bool createNewDir = false;
101
102 createNewDir = newProjectHook.GetCreateNewDir();
103
104 if( createNewDir )
105 pro.AppendDir( pro.GetName() );
106
107 // Check if the project directory is empty if it already exists.
108 wxDir directory( pro.GetPath() );
109
110 if( !pro.DirExists() )
111 {
112 if( !pro.Mkdir() )
113 {
114 wxString msg;
115 msg.Printf( _( "Folder '%s' could not be created.\n\n"
116 "Make sure you have write permissions and try again." ),
117 pro.GetPath() );
119 return wxFileName();
120 }
121 }
122 else if( directory.HasFiles() )
123 {
124 wxString msg = _( "The selected folder is not empty. It is recommended that you "
125 "create projects in their own empty folder.\n\n"
126 "Do you want to continue?" );
127
128 if( !IsOK( m_frame, msg ) )
129 return wxFileName();
130 }
131
132 return pro;
133}
134
135
137{
138
139 wxFileName pro = newProjectDirectory();
140
141 if( !pro.IsOk() )
142 return -1;
143
145 m_frame->LoadProject( pro );
146
147 return 0;
148}
149
150
152{
153 DIALOG_GIT_REPOSITORY dlg( m_frame, nullptr );
154
155 dlg.SetTitle( _( "Clone Project from Git Repository" ) );
156
157 int ret = dlg.ShowModal();
158
159 if( ret != wxID_OK )
160 return -1;
161
162 wxString project_name = dlg.GetRepoName();
163 wxFileName pro = newProjectDirectory( &project_name, true );
164
165 if( !pro.IsOk() )
166 return -1;
167
168 PROJECT_TREE_PANE *pane = static_cast<PROJECT_TREE_PANE*>( m_frame->GetToolCanvas() );
169
170
171 GIT_CLONE_HANDLER cloneHandler( pane->m_TreeProject->GitCommon() );
172
173 cloneHandler.SetRemote( dlg.GetFullURL() );
174 cloneHandler.SetClonePath( pro.GetPath() );
175 cloneHandler.SetUsername( dlg.GetUsername() );
176 cloneHandler.SetPassword( dlg.GetPassword() );
177 cloneHandler.SetSSHKey( dlg.GetRepoSSHPath() );
178
179 cloneHandler.SetProgressReporter( std::make_unique<WX_PROGRESS_REPORTER>( m_frame, _( "Cloning Repository" ), 1 ) );
180
181 if( !cloneHandler.PerformClone() )
182 {
183 DisplayErrorMessage( m_frame, cloneHandler.GetErrorString() );
184 return -1;
185 }
186
187 std::vector<wxString> projects = cloneHandler.GetProjectDirs();
188
189 if( projects.empty() )
190 {
191 DisplayErrorMessage( m_frame, _( "No project files were found in the repository." ) );
192 return -1;
193 }
194
195 // Currently, we pick the first project file we find in the repository.
196 // TODO: Look into spare checkout to allow the user to pick a partial repository
197 wxString dest = pro.GetPath() + wxFileName::GetPathSeparator() + projects.front();
198 m_frame->LoadProject( dest );
199
203
207 Prj().GetLocalSettings().m_GitRepoType = "https";
208 else
209 Prj().GetLocalSettings().m_GitRepoType = "local";
210
211 return 0;
212}
213
214
216{
217 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
218 wxFileDialog dlg( m_frame, _( "Create New Jobset" ), default_dir, wxEmptyString,
220 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
221
222 if( dlg.ShowModal() == wxID_CANCEL )
223 return -1;
224
225 wxFileName jobsetFn( dlg.GetPath() );
226
227 m_frame->OpenJobsFile( jobsetFn.GetFullPath(), true );
228
229 return 0;
230}
231
232
234{
236 KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
237 std::map<wxString, wxFileName> titleDirMap;
238
239 wxFileName templatePath;
240
241 // KiCad system template path.
242 std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( Pgm().GetLocalEnvVariables(),
243 wxT( "TEMPLATE_DIR" ) );
244
245 if( v && !v->IsEmpty() )
246 {
247 templatePath.AssignDir( *v );
248 titleDirMap.emplace( _( "System Templates" ), templatePath );
249 }
250
251 // User template path.
252 ENV_VAR_MAP_CITER it = Pgm().GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
253
254 if( it != Pgm().GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString )
255 {
256 templatePath.AssignDir( it->second.GetValue() );
257 titleDirMap.emplace( _( "User Templates" ), templatePath );
258 }
259
261 settings->m_TemplateWindowSize, titleDirMap );
262
263 // Show the project template selector dialog
264 int result = ps.ShowModal();
265
266 settings->m_TemplateWindowPos = ps.GetPosition();
267 settings->m_TemplateWindowSize = ps.GetSize();
268
269 if( result != wxID_OK )
270 return -1;
271
272 if( !ps.GetSelectedTemplate() )
273 {
274 wxMessageBox( _( "No project template was selected. Cannot generate new project." ),
275 _( "Error" ), wxOK | wxICON_ERROR, m_frame );
276
277 return -1;
278 }
279
280 // Get project destination folder and project file name.
281 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
282 wxString title = _( "New Project Folder" );
283 wxFileDialog dlg( m_frame, title, default_dir, wxEmptyString, FILEEXT::ProjectFileWildcard(),
284 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
285
286 // Add a "Create a new directory" checkbox
287 FILEDLG_NEW_PROJECT newProjectHook;
288 dlg.SetCustomizeHook( newProjectHook );
289
290 if( dlg.ShowModal() == wxID_CANCEL )
291 return -1;
292
293 wxFileName fn( dlg.GetPath() );
294
295 // wxFileName automatically extracts an extension. But if it isn't a .kicad_pro extension,
296 // we should keep it as part of the filename
297 if( !fn.GetExt().IsEmpty() && fn.GetExt().ToStdString() != FILEEXT::ProjectFileExtension )
298 fn.SetName( fn.GetName() + wxT( "." ) + fn.GetExt() );
299
301
302 if( !fn.IsAbsolute() )
303 fn.MakeAbsolute();
304
305 bool createNewDir = false;
306 createNewDir = newProjectHook.GetCreateNewDir();
307
308 // Append a new directory with the same name of the project file.
309 if( createNewDir )
310 fn.AppendDir( fn.GetName() );
311
312 // Check if the project directory is empty if it already exists.
313
314 if( !fn.DirExists() )
315 {
316 if( !fn.Mkdir() )
317 {
318 wxString msg;
319 msg.Printf( _( "Folder '%s' could not be created.\n\n"
320 "Make sure you have write permissions and try again." ),
321 fn.GetPath() );
323 return -1;
324 }
325 }
326
327 if( !fn.IsDirWritable() )
328 {
329 wxString msg;
330
331 msg.Printf( _( "Insufficient permissions to write to folder '%s'." ), fn.GetPath() );
332 wxMessageDialog msgDlg( m_frame, msg, _( "Error" ), wxICON_ERROR | wxOK | wxCENTER );
333 msgDlg.ShowModal();
334 return -1;
335 }
336
337 // Make sure we are not overwriting anything in the destination folder.
338 std::vector< wxFileName > destFiles;
339
340 if( ps.GetSelectedTemplate()->GetDestinationFiles( fn, destFiles ) )
341 {
342 std::vector<wxFileName> overwrittenFiles;
343
344 for( const wxFileName& file : destFiles )
345 {
346 if( file.FileExists() )
347 overwrittenFiles.push_back( file );
348 }
349
350 if( !overwrittenFiles.empty() )
351 {
352 wxString extendedMsg = _( "Overwriting files:" ) + "\n";
353
354 for( const wxFileName& file : overwrittenFiles )
355 extendedMsg += "\n" + file.GetFullName();
356
357 KIDIALOG msgDlg( m_frame,
358 _( "Similar files already exist in the destination folder." ),
359 _( "Confirmation" ),
360 wxOK | wxCANCEL | wxICON_WARNING );
361 msgDlg.SetExtendedMessage( extendedMsg );
362 msgDlg.SetOKLabel( _( "Overwrite" ) );
363 msgDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
364
365 if( msgDlg.ShowModal() == wxID_CANCEL )
366 return -1;
367 }
368 }
369
370 wxString errorMsg;
371
372 // The selected template widget contains the template we're attempting to use to
373 // create a project
374 if( !ps.GetSelectedTemplate()->CreateProject( fn, &errorMsg ) )
375 {
376 wxMessageDialog createDlg( m_frame,
377 _( "A problem occurred creating new project from template." ),
378 _( "Error" ),
379 wxOK | wxICON_ERROR );
380
381 if( !errorMsg.empty() )
382 createDlg.SetExtendedMessage( errorMsg );
383
384 createDlg.ShowModal();
385 return -1;
386 }
387
388 m_frame->CreateNewProject( fn.GetFullPath() );
389 m_frame->LoadProject( fn );
390 return 0;
391}
392
393
394int KICAD_MANAGER_CONTROL::openProject( const wxString& aDefaultDir )
395{
396 wxString wildcard = FILEEXT::AllProjectFilesWildcard()
399
400 wxFileDialog dlg( m_frame, _( "Open Existing Project" ), aDefaultDir, wxEmptyString, wildcard,
401 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
402
403 if( dlg.ShowModal() == wxID_CANCEL )
404 return -1;
405
406 wxFileName pro( dlg.GetPath() );
407
408 if( !pro.IsAbsolute() )
409 pro.MakeAbsolute();
410
411 if( !pro.FileExists() )
412 return -1;
413
414 m_frame->LoadProject( pro );
415
416 return 0;
417}
418
419
421{
423}
424
425
427{
428 return openProject( m_frame->GetMruPath() );
429}
430
431
433{
434 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
435 wxFileDialog dlg( m_frame, _( "Open Jobset" ), default_dir, wxEmptyString,
436 FILEEXT::JobsetFileWildcard(), wxFD_OPEN | wxFD_FILE_MUST_EXIST );
437
438 if( dlg.ShowModal() == wxID_CANCEL )
439 return -1;
440
441 wxFileName jobsetFn( dlg.GetPath() );
442
443 m_frame->OpenJobsFile( jobsetFn.GetFullPath(), true );
444
445 return 0;
446}
447
448
450{
451 m_frame->CloseProject( true );
452 return 0;
453}
454
455
457{
458 if( aEvent.Parameter<wxString*>() )
459 m_frame->LoadProject( wxFileName( *aEvent.Parameter<wxString*>() ) );
460 return 0;
461}
462
463
465{
466 wxFileName fileName = m_frame->GetProjectFileName();
467
468 fileName.SetExt( FILEEXT::ArchiveFileExtension );
469
470 wxFileDialog dlg( m_frame, _( "Archive Project Files" ),
471 fileName.GetPath(), fileName.GetFullName(),
472 FILEEXT::ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
473
474 if( dlg.ShowModal() == wxID_CANCEL )
475 return 0;
476
477 wxFileName zipFile = dlg.GetPath();
478
479 wxString currdirname = fileName.GetPathWithSep();
480 wxDir dir( currdirname );
481
482 if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
483 return 0;
484
485 STATUSBAR_REPORTER reporter( m_frame->GetStatusBar(), 1 );
486 PROJECT_ARCHIVER archiver;
487
488 archiver.Archive( currdirname, zipFile.GetFullPath(), reporter, true, true );
489 return 0;
490}
491
492
494{
496 return 0;
497}
498
499
501{
502 // Open project directory in host OS's file explorer
503 LaunchExternal( Prj().GetProjectPath() );
504 return 0;
505}
506
507
509{
510 if( aEvent.Parameter<wxString*>() )
511 wxExecute( *aEvent.Parameter<wxString*>(), wxEXEC_ASYNC );
512 return 0;
513}
514
515class SAVE_AS_TRAVERSER : public wxDirTraverser
516{
517public:
519 const wxString& aSrcProjectDirPath,
520 const wxString& aSrcProjectName,
521 const wxString& aNewProjectDirPath,
522 const wxString& aNewProjectName ) :
523 m_frame( aFrame ),
524 m_projectDirPath( aSrcProjectDirPath ),
525 m_projectName( aSrcProjectName ),
526 m_newProjectDirPath( aNewProjectDirPath ),
527 m_newProjectName( aNewProjectName )
528 {
529 }
530
531 virtual wxDirTraverseResult OnFile( const wxString& aSrcFilePath ) override
532 {
533 // Recursion guard for a Save As to a location inside the source project.
534 if( aSrcFilePath.StartsWith( m_newProjectDirPath + wxFileName::GetPathSeparator() ) )
535 return wxDIR_CONTINUE;
536
537 wxFileName destFile( aSrcFilePath );
538 wxString ext = destFile.GetExt();
539 bool atRoot = destFile.GetPath() == m_projectDirPath;
540
544 {
545 wxString destPath = destFile.GetPath();
546
547 if( destPath.StartsWith( m_projectDirPath ) )
548 {
549 destPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
550 destFile.SetPath( destPath );
551 }
552
553 if( destFile.GetName() == m_projectName )
554 {
555 destFile.SetName( m_newProjectName );
556
557 if( atRoot && ext != FILEEXT::ProjectLocalSettingsFileExtension )
558 m_newProjectFile = destFile;
559 }
560
562 {
563 // All paths in the settings file are relative so we can just do a straight copy
564 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), m_errors );
565 }
566 else if( ext == FILEEXT::ProjectFileExtension )
567 {
568 PROJECT_FILE projectFile( aSrcFilePath );
569 projectFile.LoadFromFile();
570 projectFile.SaveAs( destFile.GetPath(), destFile.GetName() );
571 }
573 {
574 PROJECT_LOCAL_SETTINGS projectLocalSettings( nullptr, aSrcFilePath );
575 projectLocalSettings.LoadFromFile();
576 projectLocalSettings.SaveAs( destFile.GetPath(), destFile.GetName() );
577 }
578 }
588 || destFile.GetName() == FILEEXT::SymbolLibraryTableFileName )
589 {
592 m_newProjectName, aSrcFilePath, m_errors );
593 }
594 else if( ext == FILEEXT::KiCadPcbFileExtension
600 || destFile.GetName() == FILEEXT::FootprintLibraryTableFileName )
601 {
604 m_newProjectName, aSrcFilePath, m_errors );
605 }
606 else if( ext == FILEEXT::DrawingSheetFileExtension )
607 {
610 m_newProjectName, aSrcFilePath, m_errors );
611 }
612 else if( ext == FILEEXT::GerberJobFileExtension
615 {
618 m_newProjectName, aSrcFilePath, m_errors );
619 }
620 else if( destFile.GetName().StartsWith( FILEEXT::LockFilePrefix )
622 {
623 // Ignore lock files
624 }
625 else
626 {
627 // Everything we don't recognize just gets a straight copy.
628 wxString destPath = destFile.GetPathWithSep();
629 wxString destName = destFile.GetName();
630 wxUniChar pathSep = wxFileName::GetPathSeparator();
631
632 wxString srcProjectFootprintLib = pathSep + m_projectName + ".pretty" + pathSep;
633 wxString newProjectFootprintLib = pathSep + m_newProjectName + ".pretty" + pathSep;
634
635 if( destPath.StartsWith( m_projectDirPath ) )
636 destPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
637
638 destPath.Replace( srcProjectFootprintLib, newProjectFootprintLib, true );
639
640 if( destName == m_projectName && ext != wxT( "zip" ) /* don't rename archives */ )
641 destFile.SetName( m_newProjectName );
642
643 destFile.SetPath( destPath );
644
645 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), m_errors );
646 }
647
648 return wxDIR_CONTINUE;
649 }
650
651 virtual wxDirTraverseResult OnDir( const wxString& aSrcDirPath ) override
652 {
653 // Recursion guard for a Save As to a location inside the source project.
654 if( aSrcDirPath.StartsWith( m_newProjectDirPath ) )
655 return wxDIR_CONTINUE;
656
657 wxFileName destDir( aSrcDirPath );
658 wxString destDirPath = destDir.GetPathWithSep();
659 wxUniChar pathSep = wxFileName::GetPathSeparator();
660
661 if( destDirPath.StartsWith( m_projectDirPath + pathSep )
662 || destDirPath.StartsWith( m_projectDirPath + PROJECT_BACKUPS_DIR_SUFFIX ) )
663 {
664 destDirPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
665 destDir.SetPath( destDirPath );
666 }
667
668 if( destDir.GetName() == m_projectName )
669 {
670 if( destDir.GetExt() == "pretty" )
671 destDir.SetName( m_newProjectName );
672#if 0
673 // WAYNE STAMBAUGH TODO:
674 // If we end up with a symbol equivalent to ".pretty" we'll want to handle it here....
675 else if( destDir.GetExt() == "sym_lib_dir_extension" )
676 destDir.SetName( m_newProjectName );
677#endif
678 }
679
680 if( !wxMkdir( destDir.GetFullPath() ) )
681 {
682 wxString msg;
683
684 if( !m_errors.empty() )
685 m_errors += "\n";
686
687 msg.Printf( _( "Cannot copy folder '%s'." ), destDir.GetFullPath() );
688 m_errors += msg;
689 }
690
691 return wxDIR_CONTINUE;
692 }
693
694 wxString GetErrors() { return m_errors; }
695
696 wxFileName GetNewProjectFile() { return m_newProjectFile; }
697
698private:
700
705
707 wxString m_errors;
708};
709
710
712{
713 wxString msg;
714
715 wxFileName currentProjectFile( Prj().GetProjectFullName() );
716 wxString currentProjectDirPath = currentProjectFile.GetPath();
717 wxString currentProjectName = Prj().GetProjectName();
718
719 wxString default_dir = m_frame->GetMruPath();
720
721 Prj().GetProjectFile().SaveToFile( currentProjectDirPath );
722 Prj().GetLocalSettings().SaveToFile( currentProjectDirPath );
723
724 if( default_dir == currentProjectDirPath
725 || default_dir == currentProjectDirPath + wxFileName::GetPathSeparator() )
726 {
727 // Don't start within the current project
728 wxFileName default_dir_fn( default_dir );
729 default_dir_fn.RemoveLastDir();
730 default_dir = default_dir_fn.GetPath();
731 }
732
733 wxFileDialog dlg( m_frame, _( "Save Project To" ), default_dir, wxEmptyString, wxEmptyString,
734 wxFD_SAVE );
735
736 if( dlg.ShowModal() == wxID_CANCEL )
737 return -1;
738
739 wxFileName newProjectDir( dlg.GetPath(), wxEmptyString );
740
741 if( !newProjectDir.IsAbsolute() )
742 newProjectDir.MakeAbsolute();
743
744 if( wxDirExists( newProjectDir.GetFullPath() ) )
745 {
746 msg.Printf( _( "'%s' already exists." ), newProjectDir.GetFullPath() );
748 return -1;
749 }
750
751 if( !wxMkdir( newProjectDir.GetFullPath() ) )
752 {
753 msg.Printf( _( "Folder '%s' could not be created.\n\n"
754 "Please make sure you have write permissions and try again." ),
755 newProjectDir.GetPath() );
757 return -1;
758 }
759
760 if( !newProjectDir.IsDirWritable() )
761 {
762 msg.Printf( _( "Insufficient permissions to write to folder '%s'." ),
763 newProjectDir.GetFullPath() );
764 wxMessageDialog msgDlg( m_frame, msg, _( "Error!" ), wxICON_ERROR | wxOK | wxCENTER );
765 msgDlg.ShowModal();
766 return -1;
767 }
768
769 const wxString& newProjectDirPath = newProjectDir.GetFullPath();
770 const wxString& newProjectName = newProjectDir.GetDirs().Last();
771 wxDir currentProjectDir( currentProjectDirPath );
772
773 SAVE_AS_TRAVERSER traverser( m_frame, currentProjectDirPath, currentProjectName,
774 newProjectDirPath, newProjectName );
775
776 currentProjectDir.Traverse( traverser );
777
778 if( !traverser.GetErrors().empty() )
779 DisplayErrorMessage( m_frame, traverser.GetErrors() );
780
781 if( !traverser.GetNewProjectFile().FileExists() )
783
784 m_frame->LoadProject( traverser.GetNewProjectFile() );
785
786 return 0;
787}
788
789
791{
793 return 0;
794}
795
796
798{
799 ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
800 CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
801 SELECTION dummySel;
802
803 if( conditionalMenu )
804 conditionalMenu->Evaluate( dummySel );
805
806 if( actionMenu )
807 actionMenu->UpdateAll();
808
809 return 0;
810}
811
812
814{
815 FRAME_T playerType = aEvent.Parameter<FRAME_T>();
816 KIWAY_PLAYER* player;
817
818 if( playerType == FRAME_SCH && !m_frame->IsProjectActive() )
819 {
820 DisplayInfoMessage( m_frame, _( "Create (or open) a project to edit a schematic." ),
821 wxEmptyString );
822 return -1;
823 }
824 else if( playerType == FRAME_PCB_EDITOR && !m_frame->IsProjectActive() )
825 {
826 DisplayInfoMessage( m_frame, _( "Create (or open) a project to edit a pcb." ),
827 wxEmptyString );
828 return -1;
829 }
830
831 // Prevent multiple KIWAY_PLAYER loading at one time
832 if( !m_loading.try_lock() )
833 return -1;
834
835 const std::lock_guard<std::mutex> lock( m_loading, std::adopt_lock );
836
837 try
838 {
839 player = m_frame->Kiway().Player( playerType, true );
840 }
841 catch( const IO_ERROR& err )
842 {
843 wxLogError( _( "Application failed to load:\n" ) + err.What() );
844 return -1;
845 }
846
847 if ( !player )
848 {
849 wxLogError( _( "Application cannot start." ) );
850 return -1;
851 }
852
853 if( !player->IsVisible() ) // A hidden frame might not have the document loaded.
854 {
855 wxString filepath;
856
857 if( playerType == FRAME_SCH )
858 {
859 wxFileName kicad_schematic( m_frame->SchFileName() );
860 wxFileName legacy_schematic( m_frame->SchLegacyFileName() );
861
862 if( !legacy_schematic.FileExists() || kicad_schematic.FileExists() )
863 filepath = kicad_schematic.GetFullPath();
864 else
865 filepath = legacy_schematic.GetFullPath();
866 }
867 else if( playerType == FRAME_PCB_EDITOR )
868 {
869 wxFileName kicad_board( m_frame->PcbFileName() );
870 wxFileName legacy_board( m_frame->PcbLegacyFileName() );
871
872 if( !legacy_board.FileExists() || kicad_board.FileExists() )
873 filepath = kicad_board.GetFullPath();
874 else
875 filepath = legacy_board.GetFullPath();
876 }
877
878 if( !filepath.IsEmpty() )
879 {
880 std::vector<wxString> file_list{ filepath };
881
882 if( !player->OpenProjectFiles( file_list ) )
883 {
884 player->Destroy();
885 return -1;
886 }
887 }
888
889 wxBusyCursor busy;
890 player->Show( true );
891 }
892
893 // Needed on Windows, other platforms do not use it, but it creates no issue
894 if( player->IsIconized() )
895 player->Iconize( false );
896
897 player->Raise();
898
899 // Raising the window does not set the focus on Linux. This should work on
900 // any platform.
901 if( wxWindow::FindFocus() != player )
902 player->SetFocus();
903
904 // Save window state to disk now. Don't wait around for a crash.
905 if( Pgm().GetCommonSettings()->m_Session.remember_open_files
906 && !player->GetCurrentFileName().IsEmpty()
907 && Prj().GetLocalSettings().ShouldAutoSave() )
908 {
909 wxFileName rfn( player->GetCurrentFileName() );
910 rfn.MakeRelativeTo( Prj().GetProjectPath() );
911
912 WINDOW_SETTINGS windowSettings;
913 player->SaveWindowSettings( &windowSettings );
914
915 Prj().GetLocalSettings().SaveFileState( rfn.GetFullPath(), &windowSettings, true );
916 Prj().GetLocalSettings().SaveToFile( Prj().GetProjectPath() );
917 }
918
919 return 0;
920}
921
922
923class TERMINATE_HANDLER : public wxProcess
924{
925public:
926 TERMINATE_HANDLER( const wxString& appName )
927 { }
928
929 void OnTerminate( int pid, int status ) override
930 {
931 delete this;
932 }
933};
934
935
937{
938 wxString execFile;
939 wxString param;
940
942 execFile = GERBVIEW_EXE;
944 execFile = BITMAPCONVERTER_EXE;
946 execFile = PCB_CALCULATOR_EXE;
948 execFile = PL_EDITOR_EXE;
950 execFile = Pgm().GetTextEditor();
952 execFile = EESCHEMA_EXE;
954 execFile = PCBNEW_EXE;
955 else
956 wxFAIL_MSG( "Execute(): unexpected request" );
957
958 if( execFile.IsEmpty() )
959 return 0;
960
961 if( aEvent.Parameter<wxString*>() )
962 param = *aEvent.Parameter<wxString*>();
964 param = m_frame->Prj().GetProjectPath();
965
966 TERMINATE_HANDLER* callback = new TERMINATE_HANDLER( execFile );
967
968 long pid = ExecuteFile( execFile, param, callback );
969
970 if( pid > 0 )
971 {
972#ifdef __WXMAC__
973 wxString script = wxString::Format( wxS( "tell application \"System Events\"\n"
974 " set frontmost of the first process whose unix id is %l to true\n"
975 "end tell" ), pid );
976
977 // This non-parameterized use of wxExecute is fine because script is not derived
978 // from user input.
979 wxExecute( wxString::Format( "osascript -e '%s'", script ) );
980#endif
981 }
982 else
983 {
984 delete callback;
985 }
986
987 return 0;
988}
989
990
992{
995 {
996 // policy disables the plugin manager
997 return 0;
998 }
999
1000 // For some reason, after a click or a double click the bitmap button calling
1001 // PCM keeps the focus althougt the focus was not set to this button.
1002 // This hack force removing the focus from this button
1003 m_frame->SetFocus();
1004 wxSafeYield();
1005
1006 if( !m_frame->GetPcm() )
1007 m_frame->CreatePCM();
1008
1009 DIALOG_PCM pcm( m_frame, m_frame->GetPcm() );
1010 pcm.ShowModal();
1011
1012 const std::unordered_set<PCM_PACKAGE_TYPE>& changed = pcm.GetChangedPackageTypes();
1013
1014 if( changed.count( PCM_PACKAGE_TYPE::PT_PLUGIN ) || changed.count( PCM_PACKAGE_TYPE::PT_FAB ) )
1015 {
1016 std::string payload = "";
1018 }
1019
1021 KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
1022
1023 if( changed.count( PCM_PACKAGE_TYPE::PT_LIBRARY )
1024 && ( settings->m_PcmLibAutoAdd || settings->m_PcmLibAutoRemove ) )
1025 {
1026 // Reset project tables
1028 Prj().SetElem( PROJECT::ELEM::FPTBL, nullptr );
1030
1031 KIWAY& kiway = m_frame->Kiway();
1032
1033 // Reset state containing global lib tables
1034 if( KIFACE* kiface = kiway.KiFACE( KIWAY::FACE_SCH, false ) )
1035 kiface->Reset();
1036
1037 if( KIFACE* kiface = kiway.KiFACE( KIWAY::FACE_PCB, false ) )
1038 kiface->Reset();
1039
1040 // Reload lib tables
1041 std::string payload = "";
1042
1045 kiway.ExpressMail( FRAME_CVPCB, MAIL_RELOAD_LIB, payload );
1047 kiway.ExpressMail( FRAME_SCH_VIEWER, MAIL_RELOAD_LIB, payload );
1048 }
1049
1050 if( changed.count( PCM_PACKAGE_TYPE::PT_COLORTHEME ) )
1052
1053 return 0;
1054}
1055
1056
1058{
1070
1074
1077
1087
1090
1092}
static TOOL_ACTION zoomRedraw
Definition: actions.h:124
static TOOL_ACTION saveAs
Definition: actions.h:52
static TOOL_ACTION updateMenu
Definition: actions.h:224
Define the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
void UpdateAll()
Run update handlers for the menu and its submenus.
const wxString & GetFullURL() const
KIGIT_COMMON::GIT_CONN_TYPE GetRepoType() const
wxString GetRepoSSHPath() const
wxString GetUsername() const
wxString GetRepoName() const
wxString GetPassword() const
Implementing pcm main dialog.
Definition: dialog_pcm.h:38
const std::unordered_set< PCM_PACKAGE_TYPE > & GetChangedPackageTypes() const
Definition: dialog_pcm.h:81
int ShowModal() override
PROJECT_TEMPLATE * GetSelectedTemplate()
wxString GetMruPath() const
bool GetCreateNewDir() const
void SetRemote(const wxString &aRemote)
void SetClonePath(const wxString &aPath)
void SetProgressReporter(std::unique_ptr< WX_PROGRESS_REPORTER > aProgressReporter)
Definition: git_progress.h:40
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
static TOOL_ACTION viewDroppedGerbers
static TOOL_ACTION openDemoProject
static TOOL_ACTION editPCB
static TOOL_ACTION unarchiveProject
static TOOL_ACTION loadProject
static TOOL_ACTION editOtherPCB
static TOOL_ACTION newProject
static TOOL_ACTION editOtherSch
static TOOL_ACTION editSchematic
static TOOL_ACTION openTextEditor
static TOOL_ACTION archiveProject
static TOOL_ACTION openProject
static TOOL_ACTION closeProject
static TOOL_ACTION convertImage
static TOOL_ACTION editDrawingSheet
static TOOL_ACTION openProjectDirectory
static TOOL_ACTION openJobsetFile
static TOOL_ACTION newJobsetFile
static TOOL_ACTION editFootprints
static TOOL_ACTION showPluginManager
static TOOL_ACTION showCalculator
static TOOL_ACTION viewGerbers
static TOOL_ACTION newFromRepository
static TOOL_ACTION newFromTemplate
static TOOL_ACTION editSymbols
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
int OpenProject(const TOOL_EVENT &aEvent)
int NewJobsetFile(const TOOL_EVENT &aEvent)
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
int OpenDemoProject(const TOOL_EVENT &aEvent)
int CloseProject(const TOOL_EVENT &aEvent)
int ArchiveProject(const TOOL_EVENT &aEvent)
int SaveProjectAs(const TOOL_EVENT &aEvent)
int NewProject(const TOOL_EVENT &aEvent)
int UnarchiveProject(const TOOL_EVENT &aEvent)
int ViewDroppedViewers(const TOOL_EVENT &aEvent)
Imports a non kicad project from a sch/pcb dropped file.
int NewFromTemplate(const TOOL_EVENT &aEvent)
int ShowPluginManager(const TOOL_EVENT &aEvent)
Set up handlers for various events.
int UpdateMenu(const TOOL_EVENT &aEvent)
int OpenJobsetFile(const TOOL_EVENT &aEvent)
wxFileName newProjectDirectory(wxString *aFileName=nullptr, bool isRepo=false)
int NewFromRepository(const TOOL_EVENT &aEvent)
int LoadProject(const TOOL_EVENT &aEvent)
KICAD_MANAGER_FRAME * m_frame
< Pointer to the currently used edit/draw frame.
int ExploreProject(const TOOL_EVENT &aEvent)
int ShowPlayer(const TOOL_EVENT &aEvent)
int Refresh(const TOOL_EVENT &aEvent)
int openProject(const wxString &aDefaultDir)
int Execute(const TOOL_EVENT &aEvent)
The main KiCad project manager frame.
void CreateNewProject(const wxFileName &aProjectFileName, bool aCreateStubFiles=true)
Creates a new project by setting up and initial project, schematic, and board files.
const wxString SchLegacyFileName()
wxWindow * GetToolCanvas() const override
Canvas access.
const wxString GetProjectFileName() const
const wxString SchFileName()
void OpenJobsFile(const wxFileName &aFileName, bool aCreate=false, bool aResaveProjectPreferences=true)
void LoadProject(const wxFileName &aProjectFileName)
std::shared_ptr< PLUGIN_CONTENT_MANAGER > GetPcm()
const wxString PcbLegacyFileName()
bool CloseProject(bool aSave)
Closes the project, and saves it if aSave is true;.
const wxString PcbFileName()
wxSize m_TemplateWindowSize
wxPoint m_TemplateWindowPos
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h:43
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition: kidialog.cpp:51
int ShowModal() override
Definition: kidialog.cpp:95
virtual void Reset() override
Reloads global state.
Definition: kiface_base.h:55
wxString GetErrorString()
void SetPassword(const wxString &aPassword)
Set the password.
std::vector< wxString > GetProjectDirs()
Get a list of project directories.
void SetUsername(const wxString &aUsername)
Set the username.
void SetSSHKey(const wxString &aSSHKey)
Set the SSH key.
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:55
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:65
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:285
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:406
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:201
@ FACE_SCH
eeschema DSO
Definition: kiway.h:292
@ FACE_PL_EDITOR
Definition: kiway.h:296
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:293
@ FACE_GERBVIEW
Definition: kiway.h:295
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:527
static wxString GetStockDemosPath()
Gets the stock (install) demos path.
Definition: paths.cpp:413
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.cpp:933
virtual const wxString & GetTextEditor(bool aCanShowFileChooser=true)
Return the path to the preferred text editor application.
Definition: pgm_base.cpp:196
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
static bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Create an archive of the project.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:73
bool SaveAs(const wxString &aDirectory, const wxString &aFile)
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
The project local settings are things that are attached to a particular project, but also might be pa...
bool SaveAs(const wxString &aDirectory, const wxString &aFile)
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
void SaveFileState(const wxString &aFileName, const WINDOW_SETTINGS *aWindowCfg, bool aOpen)
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.
PROJECT_TREE_PANE Window to display the tree files.
PROJECT_TREE * m_TreeProject
KIGIT_COMMON * GitCommon() const
Definition: project_tree.h:63
virtual void SetElem(PROJECT::ELEM aIndex, _ELEM *aElem)
Definition: project.cpp:359
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:146
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:158
virtual PROJECT_LOCAL_SETTINGS & GetLocalSettings() const
Definition: project.h:209
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:203
KICAD_MANAGER_FRAME * m_frame
virtual wxDirTraverseResult OnFile(const wxString &aSrcFilePath) override
SAVE_AS_TRAVERSER(KICAD_MANAGER_FRAME *aFrame, const wxString &aSrcProjectDirPath, const wxString &aSrcProjectName, const wxString &aNewProjectDirPath, const wxString &aNewProjectName)
virtual wxDirTraverseResult OnDir(const wxString &aSrcDirPath) override
T * GetAppSettings(const wxString &aFilename)
Return a handle to the a given settings by type.
void ReloadColorSettings()
Re-scan the color themes directory, reloading any changes it finds.
A wrapper for reporting to a specific text location in a statusbar.
Definition: reporter.h:294
void OnTerminate(int pid, int status) override
TERMINATE_HANDLER(const wxString &appName)
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:168
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:82
T Parameter() const
Return a parameter assigned to the event.
Definition: tool_event.h:465
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:249
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:221
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
This file is part of the common library.
#define _(s)
Functions related to environment variables, including help functions.
KiCad executable names.
const wxString EESCHEMA_EXE
const wxString GERBVIEW_EXE
const wxString PL_EDITOR_EXE
const wxString BITMAPCONVERTER_EXE
const wxString PCBNEW_EXE
const wxString PCB_CALCULATOR_EXE
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:33
@ FRAME_PCB_EDITOR
Definition: frame_type.h:42
@ FRAME_SCH_SYMBOL_EDITOR
Definition: frame_type.h:35
@ FRAME_FOOTPRINT_VIEWER
Definition: frame_type.h:45
@ FRAME_SCH_VIEWER
Definition: frame_type.h:36
@ FRAME_SCH
Definition: frame_type.h:34
@ FRAME_FOOTPRINT_EDITOR
Definition: frame_type.h:43
@ FRAME_CVPCB
Definition: frame_type.h:52
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:290
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
Call the executable file aEditorName with the parameter aFileName.
Definition: gestfich.cpp:143
static const std::string LegacySchematicFileExtension
static const std::string NetlistFileExtension
static const std::string SymbolLibraryTableFileName
static const std::string GerberJobFileExtension
static const std::string LockFileExtension
static const std::string ProjectFileExtension
static const std::string LegacyPcbFileExtension
static const std::string SchematicSymbolFileExtension
static const std::string LegacyProjectFileExtension
static const std::string ProjectLocalSettingsFileExtension
static const std::string KiCadSchematicFileExtension
static const std::string LegacySymbolLibFileExtension
static const std::string LockFilePrefix
static const std::string KiCadSymbolLibFileExtension
static const std::string FootprintLibraryTableFileName
static const std::string DrawingSheetFileExtension
static const std::string BackupFileSuffix
static const std::string LegacyFootprintLibPathExtension
static const std::string LegacySymbolDocumentFileExtension
static const std::string FootprintAssignmentFileExtension
static const std::string DrillFileExtension
static const std::string KiCadFootprintFileExtension
static const std::string ArchiveFileExtension
static const std::string KiCadPcbFileExtension
static wxString ProjectFileWildcard()
static bool IsGerberFileExtension(const wxString &ext)
static wxString JobsetFileWildcard()
static wxString LegacyProjectFileWildcard()
static wxString AllProjectFilesWildcard()
static wxString ZipFileWildcard()
std::map< wxString, ENV_VAR_ITEM >::const_iterator ENV_VAR_MAP_CITER
PROJECT & Prj()
Definition: kicad.cpp:597
This file is part of the common library.
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
Definition: launch_ext.cpp:25
@ MAIL_RELOAD_PLUGINS
Definition: mail_type.h:58
@ MAIL_RELOAD_LIB
Definition: mail_type.h:57
KICOMMON_API std::optional< wxString > GetVersionedEnvVarValue(const std::map< wxString, ENV_VAR_ITEM > &aMap, const wxString &aBaseName)
Attempt to retrieve the value of a versioned environment variable, such as KICAD8_TEMPLATE_DIR.
Definition: env_vars.cpp:92
PBOOL GetPolicyBool(const wxString &aKey)
Definition: unix/policy.cpp:26
bool StoreSecret(const wxString &aService, const wxString &aKey, const wxString &aSecret)
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1071
see class PGM_BASE
#define POLICY_KEY_PCM
Definition: policy_keys.h:31
#define PROJECT_BACKUPS_DIR_SUFFIX
Project settings path will be <projectname> + this.
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:152
virtual void SaveFileAs(const wxString &srcProjectBasePath, const wxString &srcProjectName, const wxString &newProjectBasePath, const wxString &newProjectName, const wxString &srcFilePath, wxString &aErrors)
Saving a file under a different name is delegated to the various KIFACEs because the project doesn't ...
Definition: kiway.h:217
Store the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:74
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
VECTOR2I end
Definition of file extensions used in Kicad.