KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <kiplatform/ui.h>
32#include <confirm.h>
33#include <kidialog.h>
39#include <tool/selection.h>
40#include <tool/tool_event.h>
41#include <tool/tool_manager.h>
42#include <tool/common_control.h>
49#include <gestfich.h>
50#include <paths.h>
51#include <wx/dir.h>
52#include <wx/filedlg.h>
53#include <wx/ffile.h>
54#include "dialog_pcm.h"
56#include <project_tree_pane.h>
57#include <project_tree.h>
59#include <launch_ext.h>
60
62
64 TOOL_INTERACTIVE( "kicad.Control" ),
65 m_frame( nullptr ),
66 m_inShowPlayer( false )
67{
68}
69
70
75
76
77wxFileName KICAD_MANAGER_CONTROL::newProjectDirectory( wxString* aFileName, bool isRepo )
78{
79 wxString default_filename = aFileName ? *aFileName : wxString();
80
81 wxString default_dir = m_frame->GetMruPath();
82 wxFileDialog dlg( m_frame, _( "Create New Project" ), default_dir, default_filename,
83 ( isRepo ? wxString( "" ) : FILEEXT::ProjectFileWildcard() ),
84 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
85
86 dlg.AddShortcut( PATHS::GetDefaultUserProjectsPath() );
87
88 // Add a "Create a new directory" checkbox
89 FILEDLG_NEW_PROJECT newProjectHook;
90 dlg.SetCustomizeHook( newProjectHook );
91
93
94 if( dlg.ShowModal() == wxID_CANCEL )
95 return wxFileName();
96
97 wxFileName pro( dlg.GetPath() );
98
99 // wxFileName automatically extracts an extension. But if it isn't
100 // a .pro extension, we should keep it as part of the filename
101 if( !pro.GetExt().IsEmpty() && pro.GetExt().ToStdString() != FILEEXT::ProjectFileExtension )
102 pro.SetName( pro.GetName() + wxT( "." ) + pro.GetExt() );
103
104 pro.SetExt( FILEEXT::ProjectFileExtension ); // enforce extension
105
106 if( !pro.IsAbsolute() )
107 pro.MakeAbsolute();
108
109 // Append a new directory with the same name of the project file.
110 bool createNewDir = false;
111
112 createNewDir = newProjectHook.GetCreateNewDir();
113
114 if( createNewDir )
115 pro.AppendDir( pro.GetName() );
116
117 // Check if the project directory is empty if it already exists.
118 wxDir directory( pro.GetPath() );
119
120 if( !pro.DirExists() )
121 {
122 if( !pro.Mkdir() )
123 {
124 wxString msg;
125 msg.Printf( _( "Folder '%s' could not be created.\n\n"
126 "Make sure you have write permissions and try again." ),
127 pro.GetPath() );
129 return wxFileName();
130 }
131 }
132 else if( directory.HasFiles() )
133 {
134 wxString msg = _( "The selected folder is not empty. It is recommended that you "
135 "create projects in their own empty folder.\n\n"
136 "Do you want to continue?" );
137
138 if( !IsOK( m_frame, msg ) )
139 return wxFileName();
140 }
141
142 return pro;
143}
144
145
147{
148 ENV_VAR_MAP_CITER it = Pgm().GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
149
150 if( it == Pgm().GetLocalEnvVariables().end() || it->second.GetValue() == wxEmptyString )
151 return wxFileName();
152
153 wxFileName templatePath;
154 templatePath.AssignDir( it->second.GetValue() );
155 templatePath.AppendDir( "default" );
156
157 if( !templatePath.DirExists() && !templatePath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
158 return wxFileName();
159
160 wxFileName metaDir = templatePath;
161 metaDir.AppendDir( METADIR );
162
163 if( !metaDir.DirExists() && !metaDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
164 return wxFileName();
165
166 wxFileName infoFile = metaDir;
167 infoFile.SetFullName( METAFILE_INFO_HTML );
168
169 if( !infoFile.FileExists() )
170 {
171 wxFFile info( infoFile.GetFullPath(), wxT( "w" ) );
172
173 if( !info.IsOpened() )
174 return wxFileName();
175
176 info.Write( wxT( "<html><head><title>Default</title></head><body><h3>Default KiCad project template.</h3></body></html>" ) );
177 info.Close();
178 }
179
180 wxFileName proFile = templatePath;
181 proFile.SetFullName( wxT( "default.kicad_pro" ) );
182
183 if( !proFile.FileExists() )
184 {
185 wxFFile proj( proFile.GetFullPath(), wxT( "w" ) );
186
187 if( !proj.IsOpened() )
188 return wxFileName();
189
190 proj.Write( wxT( "{}" ) );
191 proj.Close();
192 }
193
194 if( infoFile.FileExists() && proFile.FileExists() )
195 return templatePath;
196 else
197 return wxFileName();
198}
199
201{
202 wxFileName defaultTemplate = ensureDefaultProjectTemplate();
203
204 if( !defaultTemplate.IsOk() )
205 {
206 wxFileName pro = newProjectDirectory();
207
208 if( !pro.IsOk() )
209 return -1;
210
211 m_frame->CreateNewProject( pro );
212 m_frame->LoadProject( pro );
213
214 return 0;
215 }
216
217 KICAD_SETTINGS* settings = GetAppSettings<KICAD_SETTINGS>( "kicad" );
218
219 wxString userTemplatesPath;
220 wxString systemTemplatesPath;
221
222 ENV_VAR_MAP_CITER itUser = Pgm().GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
223
224 if( itUser != Pgm().GetLocalEnvVariables().end() && itUser->second.GetValue() != wxEmptyString )
225 {
226 wxFileName templatePath;
227 templatePath.AssignDir( itUser->second.GetValue() );
228 templatePath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
229 userTemplatesPath = templatePath.GetFullPath();
230 }
231
232 std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( Pgm().GetLocalEnvVariables(),
233 wxT( "TEMPLATE_DIR" ) );
234
235 if( v && !v->IsEmpty() )
236 {
237 wxFileName templatePath;
238 templatePath.AssignDir( *v );
239 templatePath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
240 systemTemplatesPath = templatePath.GetFullPath();
241 }
242
243 // Use RunMainStack to show the dialog on the main stack instead of the coroutine stack.
244 // This is necessary because the template selector uses a WebView which triggers WebKit's
245 // JavaScript VM initialization. WebKit's stack validation fails on coroutine stacks.
246 int result = wxID_CANCEL;
247 wxString selectedTemplatePath;
248 wxPoint templateWindowPos;
249 wxSize templateWindowSize;
250 wxString projectToEdit;
251
253 [&]()
254 {
256 settings->m_TemplateWindowSize, userTemplatesPath,
257 systemTemplatesPath, settings->m_RecentTemplates );
258
259 result = ps.ShowModal();
260 templateWindowPos = ps.GetPosition();
261 templateWindowSize = ps.GetSize();
262 projectToEdit = ps.GetProjectToEdit();
263
265
266 if( templ )
267 {
268 wxFileName htmlFile = templ->GetHtmlFile();
269 htmlFile.RemoveLastDir();
270 selectedTemplatePath = htmlFile.GetPath();
271 }
272 } );
273
274 settings->m_TemplateWindowPos = templateWindowPos;
275 settings->m_TemplateWindowSize = templateWindowSize;
276
277 // Check if user wants to edit a template instead of creating new project
278 if( result == wxID_APPLY )
279 {
280 if( !projectToEdit.IsEmpty() && wxFileExists( projectToEdit ) )
281 {
282 m_frame->LoadProject( wxFileName( projectToEdit ) );
283 return 0;
284 }
285 }
286
287 if( result != wxID_OK )
288 return -1;
289
290 if( selectedTemplatePath.IsEmpty() )
291 {
292 wxMessageBox( _( "No project template was selected. Cannot generate new project." ), _( "Error" ),
293 wxOK | wxICON_ERROR, m_frame );
294
295 return -1;
296 }
297
298 // Recreate the template object from the saved path
299 PROJECT_TEMPLATE selectedTemplate( selectedTemplatePath );
300
301 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
302 wxString title = _( "New Project Folder" );
303 wxFileDialog dlg( m_frame, title, default_dir, wxEmptyString, FILEEXT::ProjectFileWildcard(),
304 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
305
306 dlg.AddShortcut( PATHS::GetDefaultUserProjectsPath() );
307
308 FILEDLG_NEW_PROJECT newProjectHook;
309 dlg.SetCustomizeHook( newProjectHook );
310
312
313 if( dlg.ShowModal() == wxID_CANCEL )
314 return -1;
315
316 wxFileName fn( dlg.GetPath() );
317
318 if( !fn.GetExt().IsEmpty() && fn.GetExt().ToStdString() != FILEEXT::ProjectFileExtension )
319 fn.SetName( fn.GetName() + wxT( "." ) + fn.GetExt() );
320
322
323 if( !fn.IsAbsolute() )
324 fn.MakeAbsolute();
325
326 bool createNewDir = false;
327 createNewDir = newProjectHook.GetCreateNewDir();
328
329 if( createNewDir )
330 fn.AppendDir( fn.GetName() );
331
332 if( !fn.DirExists() && !fn.Mkdir() )
333 {
334 DisplayErrorMessage( m_frame, wxString::Format( _( "Folder '%s' could not be created.\n\n"
335 "Make sure you have write permissions and try again." ),
336 fn.GetPath() ) );
337 return -1;
338 }
339
340 if( !fn.IsDirWritable() )
341 {
342 DisplayErrorMessage( m_frame, wxString::Format( _( "Insufficient permissions to write to folder '%s'." ),
343 fn.GetPath() ) );
344 return -1;
345 }
346
347 std::vector< wxFileName > destFiles;
348
349 if( selectedTemplate.GetDestinationFiles( fn, destFiles ) )
350 {
351 std::vector<wxFileName> overwrittenFiles;
352
353 for( const wxFileName& file : destFiles )
354 {
355 if( file.FileExists() )
356 overwrittenFiles.push_back( file );
357 }
358
359 if( !overwrittenFiles.empty() )
360 {
361 wxString extendedMsg = _( "Overwriting files:" ) + "\n";
362
363 for( const wxFileName& file : overwrittenFiles )
364 extendedMsg += "\n" + file.GetFullName();
365
366 KIDIALOG msgDlg( m_frame, _( "Similar files already exist in the destination folder." ),
367 _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
368 msgDlg.SetExtendedMessage( extendedMsg );
369 msgDlg.SetOKLabel( _( "Overwrite" ) );
370 msgDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
371
372 if( msgDlg.ShowModal() == wxID_CANCEL )
373 return -1;
374 }
375 }
376
377 wxString errorMsg;
378
379 if( !selectedTemplate.CreateProject( fn, &errorMsg ) )
380 {
381 DisplayErrorMessage( m_frame, _( "A problem occurred creating new project from template." ), errorMsg );
382 return -1;
383 }
384
385 // Update MRU list with the used template
386 wxFileName templateDir = selectedTemplate.GetHtmlFile();
387 templateDir.RemoveLastDir();
388 wxString templatePath = templateDir.GetPath();
389
390 settings->m_LastUsedTemplate = templatePath;
391
392 // Add to front of recent templates, remove duplicates, trim to 5
393 std::vector<wxString>& recentTemplates = settings->m_RecentTemplates;
394 recentTemplates.erase( std::remove( recentTemplates.begin(), recentTemplates.end(), templatePath ),
395 recentTemplates.end() );
396 recentTemplates.insert( recentTemplates.begin(), templatePath );
397
398 if( recentTemplates.size() > 5 )
399 recentTemplates.resize( 5 );
400
401 m_frame->CreateNewProject( fn.GetFullPath() );
402 m_frame->LoadProject( fn );
403 return 0;
404}
405
406
408{
409 DIALOG_GIT_REPOSITORY dlg( m_frame, nullptr );
410
411 dlg.SetTitle( _( "Clone Project from Git Repository" ) );
412
413 int ret = dlg.ShowModal();
414
415 if( ret != wxID_OK )
416 return -1;
417
418 wxString project_name = dlg.GetRepoName();
419 wxFileName pro = newProjectDirectory( &project_name, true );
420
421 if( !pro.IsOk() )
422 return -1;
423
424 PROJECT_TREE_PANE *pane = static_cast<PROJECT_TREE_PANE*>( m_frame->GetToolCanvas() );
425
426
427 GIT_CLONE_HANDLER cloneHandler( pane->m_TreeProject->GitCommon() );
428
429 cloneHandler.SetRemote( dlg.GetFullURL() );
430 cloneHandler.SetClonePath( pro.GetPath() );
431 cloneHandler.SetUsername( dlg.GetUsername() );
432 cloneHandler.SetPassword( dlg.GetPassword() );
433 cloneHandler.SetSSHKey( dlg.GetRepoSSHPath() );
434
435 cloneHandler.SetProgressReporter( std::make_unique<WX_PROGRESS_REPORTER>( m_frame, _( "Clone Repository" ), 1,
436 PR_NO_ABORT ) );
437
438 if( !cloneHandler.PerformClone() )
439 {
440 DisplayErrorMessage( m_frame, cloneHandler.GetErrorString() );
441 return -1;
442 }
443
444 std::vector<wxString> projects = cloneHandler.GetProjectDirs();
445
446 if( projects.empty() )
447 {
448 DisplayErrorMessage( m_frame, _( "No project files were found in the repository." ) );
449 return -1;
450 }
451
452 // Currently, we pick the first project file we find in the repository.
453 // TODO: Look into spare checkout to allow the user to pick a partial repository
454 wxString dest = pro.GetPath() + wxFileName::GetPathSeparator() + projects.front();
455 m_frame->LoadProject( dest );
456
460
464 Prj().GetLocalSettings().m_GitRepoType = "https";
465 else
466 Prj().GetLocalSettings().m_GitRepoType = "local";
467
468 return 0;
469}
470
471
473{
474 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
475 wxFileDialog dlg( m_frame, _( "Create New Jobset" ), default_dir, wxEmptyString, FILEEXT::JobsetFileWildcard(),
476 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
477
479
480 if( dlg.ShowModal() == wxID_CANCEL )
481 return -1;
482
483 wxFileName jobsetFn( dlg.GetPath() );
484
485 // Check if the file already exists
486 bool fileExists = wxFileExists( jobsetFn.GetFullPath() );
487
488 if( fileExists )
489 {
490 // Remove the existing file so that a new one can be created
491 if( !wxRemoveFile( jobsetFn.GetFullPath() ) )
492 {
493 return -1;
494 }
495 }
496
497 m_frame->OpenJobsFile( jobsetFn.GetFullPath(), true );
498
499 return 0;
500}
501
502
503
504
505int KICAD_MANAGER_CONTROL::openProject( const wxString& aDefaultDir )
506{
507 wxString wildcard = FILEEXT::AllProjectFilesWildcard()
510
511 wxFileDialog dlg( m_frame, _( "Open Existing Project" ), aDefaultDir, wxEmptyString, wildcard,
512 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
513
514 dlg.AddShortcut( PATHS::GetDefaultUserProjectsPath() );
515
517
518 if( dlg.ShowModal() == wxID_CANCEL )
519 return -1;
520
521 wxFileName pro( dlg.GetPath() );
522
523 if( !pro.IsAbsolute() )
524 pro.MakeAbsolute();
525
526 // You'd think wxFD_FILE_MUST_EXIST and the wild-cards would enforce these. Sentry
527 // indicates otherwise (at least on MSW).
528 if( !pro.Exists() || ( pro.GetExt() != FILEEXT::ProjectFileExtension
529 && pro.GetExt() != FILEEXT::LegacyProjectFileExtension ) )
530 {
531 return -1;
532 }
533
534 m_frame->LoadProject( pro );
535
536 return 0;
537}
538
539
544
545
547{
548 return openProject( m_frame->GetMruPath() );
549}
550
551
553{
554 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
555 wxFileDialog dlg( m_frame, _( "Open Jobset" ), default_dir, wxEmptyString, FILEEXT::JobsetFileWildcard(),
556 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
557
559
560 if( dlg.ShowModal() == wxID_CANCEL )
561 return -1;
562
563 wxFileName jobsetFn( dlg.GetPath() );
564
565 m_frame->OpenJobsFile( jobsetFn.GetFullPath(), true );
566
567 return 0;
568}
569
570
572{
573 m_frame->CloseProject( true );
574 return 0;
575}
576
577
579{
580 if( aEvent.Parameter<wxString*>() )
581 m_frame->LoadProject( wxFileName( *aEvent.Parameter<wxString*>() ) );
582 return 0;
583}
584
585
587{
588 wxFileName fileName = m_frame->GetProjectFileName();
589
590 fileName.SetExt( FILEEXT::ArchiveFileExtension );
591
592 wxFileDialog dlg( m_frame, _( "Archive Project Files" ), fileName.GetPath(), fileName.GetFullName(),
593 FILEEXT::ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
594
596
597 if( dlg.ShowModal() == wxID_CANCEL )
598 return 0;
599
600 wxFileName zipFile = dlg.GetPath();
601
602 wxString currdirname = fileName.GetPathWithSep();
603 wxDir dir( currdirname );
604
605 if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
606 return 0;
607
608 STATUSBAR_REPORTER reporter( m_frame->GetStatusBar(), 1 );
609 PROJECT_ARCHIVER archiver;
610
611 archiver.Archive( currdirname, zipFile.GetFullPath(), reporter, true, true );
612 return 0;
613}
614
615
617{
618 m_frame->UnarchiveFiles();
619 return 0;
620}
621
622
624{
625 // Open project directory in host OS's file explorer
626 LaunchExternal( Prj().GetProjectPath() );
627 return 0;
628}
629
631{
632 m_frame->RestoreLocalHistory();
633 return 0;
634}
635
636
638{
639 m_frame->ToggleLocalHistory();
640 return 0;
641}
642
643
645{
646 if( aEvent.Parameter<wxString*>() )
647 wxExecute( *aEvent.Parameter<wxString*>(), wxEXEC_ASYNC );
648
649 return 0;
650}
651
652
653
655{
656 wxString msg;
657
658 wxFileName currentProjectFile( Prj().GetProjectFullName() );
659 wxString currentProjectDirPath = currentProjectFile.GetPath();
660 wxString currentProjectName = Prj().GetProjectName();
661
662 wxString default_dir = m_frame->GetMruPath();
663
664 Prj().GetProjectFile().SaveToFile( currentProjectDirPath );
665 Prj().GetLocalSettings().SaveToFile( currentProjectDirPath );
666
667 if( default_dir == currentProjectDirPath
668 || default_dir == currentProjectDirPath + wxFileName::GetPathSeparator() )
669 {
670 // Don't start within the current project
671 wxFileName default_dir_fn( default_dir );
672 default_dir_fn.RemoveLastDir();
673 default_dir = default_dir_fn.GetPath();
674 }
675
676 wxFileDialog dlg( m_frame, _( "Save Project To" ), default_dir, wxEmptyString, wxEmptyString, wxFD_SAVE );
677
678 dlg.AddShortcut( PATHS::GetDefaultUserProjectsPath() );
679
681
682 if( dlg.ShowModal() == wxID_CANCEL )
683 return -1;
684
685 wxFileName newProjectDir( dlg.GetPath(), wxEmptyString );
686
687 if( !newProjectDir.IsAbsolute() )
688 newProjectDir.MakeAbsolute();
689
690 if( wxDirExists( newProjectDir.GetFullPath() ) )
691 {
692 msg.Printf( _( "'%s' already exists." ), newProjectDir.GetFullPath() );
694 return -1;
695 }
696
697 if( !wxMkdir( newProjectDir.GetFullPath() ) )
698 {
699 DisplayErrorMessage( m_frame, wxString::Format( _( "Folder '%s' could not be created.\n\n"
700 "Please make sure you have sufficient permissions." ),
701 newProjectDir.GetPath() ) );
702 return -1;
703 }
704
705 if( !newProjectDir.IsDirWritable() )
706 {
707 DisplayErrorMessage( m_frame, wxString::Format( _( "Insufficient permissions to write to folder '%s'." ),
708 newProjectDir.GetFullPath() ) );
709 return -1;
710 }
711
712 const wxString& newProjectDirPath = newProjectDir.GetFullPath();
713 const wxString& newProjectName = newProjectDir.GetDirs().Last();
714 wxDir currentProjectDir( currentProjectDirPath );
715
716 PROJECT_TREE_TRAVERSER traverser( m_frame, currentProjectDirPath, currentProjectName,
717 newProjectDirPath, newProjectName );
718
719 currentProjectDir.Traverse( traverser );
720
721 if( !traverser.GetErrors().empty() )
722 DisplayErrorMessage( m_frame, traverser.GetErrors() );
723
724 if( !traverser.GetNewProjectFile().FileExists() )
725 m_frame->CreateNewProject( traverser.GetNewProjectFile() );
726
727 m_frame->LoadProject( traverser.GetNewProjectFile() );
728
729 return 0;
730}
731
732
734{
735 m_frame->RefreshProjectTree();
736 return 0;
737}
738
739
741{
742 ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
743 CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
744 SELECTION dummySel;
745
746 if( conditionalMenu )
747 conditionalMenu->Evaluate( dummySel );
748
749 if( actionMenu )
750 actionMenu->UpdateAll();
751
752 return 0;
753}
754
755
757{
758 FRAME_T playerType = aEvent.Parameter<FRAME_T>();
759 KIWAY_PLAYER* player;
760
761 if( playerType == FRAME_SCH && !m_frame->IsProjectActive() )
762 {
763 DisplayInfoMessage( m_frame, _( "Create (or open) a project to edit a schematic." ), wxEmptyString );
764 return -1;
765 }
766 else if( playerType == FRAME_PCB_EDITOR && !m_frame->IsProjectActive() )
767 {
768 DisplayInfoMessage( m_frame, _( "Create (or open) a project to edit a pcb." ), wxEmptyString );
769 return -1;
770 }
771
772 if( m_inShowPlayer )
773 return -1;
774
776
777 try
778 {
779 player = m_frame->Kiway().Player( playerType, true );
780 }
781 catch( const IO_ERROR& err )
782 {
783 wxLogError( _( "Application failed to load:\n" ) + err.What() );
784 return -1;
785 }
786
787 if ( !player )
788 {
789 wxLogError( _( "Application cannot start." ) );
790 return -1;
791 }
792
793 if( !player->IsVisible() ) // A hidden frame might not have the document loaded.
794 {
795 wxString filepath;
796
797 if( playerType == FRAME_SCH )
798 {
799 wxFileName kicad_schematic( m_frame->SchFileName() );
800 wxFileName legacy_schematic( m_frame->SchLegacyFileName() );
801
802 if( !legacy_schematic.FileExists() || kicad_schematic.FileExists() )
803 filepath = kicad_schematic.GetFullPath();
804 else
805 filepath = legacy_schematic.GetFullPath();
806 }
807 else if( playerType == FRAME_PCB_EDITOR )
808 {
809 wxFileName kicad_board( m_frame->PcbFileName() );
810 wxFileName legacy_board( m_frame->PcbLegacyFileName() );
811
812 if( !legacy_board.FileExists() || kicad_board.FileExists() )
813 filepath = kicad_board.GetFullPath();
814 else
815 filepath = legacy_board.GetFullPath();
816 }
817
818 if( !filepath.IsEmpty() )
819 {
820 std::vector<wxString> file_list{ filepath };
821
822 if( !player->OpenProjectFiles( file_list ) )
823 {
824 player->Destroy();
825 return -1;
826 }
827 }
828
829 wxBusyCursor busy;
830 player->Show( true );
831 }
832
833 // Needed on Windows, other platforms do not use it, but it creates no issue
834 if( player->IsIconized() )
835 player->Iconize( false );
836
837 player->Raise();
838
839 // Raising the window does not set the focus on Linux. This should work on
840 // any platform.
841 if( wxWindow::FindFocus() != player )
842 player->SetFocus();
843
844 // Save window state to disk now. Don't wait around for a crash.
845 if( Pgm().GetCommonSettings()->m_Session.remember_open_files
846 && !player->GetCurrentFileName().IsEmpty()
847 && Prj().GetLocalSettings().ShouldAutoSave() )
848 {
849 wxFileName rfn( player->GetCurrentFileName() );
850 rfn.MakeRelativeTo( Prj().GetProjectPath() );
851
852 WINDOW_SETTINGS windowSettings;
853 player->SaveWindowSettings( &windowSettings );
854
855 Prj().GetLocalSettings().SaveFileState( rfn.GetFullPath(), &windowSettings, true );
856 Prj().GetLocalSettings().SaveToFile( Prj().GetProjectPath() );
857 }
858
859 return 0;
860}
861
862
864{
865 wxString execFile;
866 wxString param;
867
869 execFile = GERBVIEW_EXE;
871 execFile = BITMAPCONVERTER_EXE;
873 execFile = PCB_CALCULATOR_EXE;
875 execFile = PL_EDITOR_EXE;
877 execFile = Pgm().GetTextEditor();
879 execFile = EESCHEMA_EXE;
881 execFile = PCBNEW_EXE;
882 else
883 wxFAIL_MSG( "Execute(): unexpected request" );
884
885 if( execFile.IsEmpty() )
886 return 0;
887
888 if( aEvent.Parameter<wxString*>() )
889 param = *aEvent.Parameter<wxString*>();
890 else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::viewGerbers ) && m_frame->IsProjectActive() )
891 param = m_frame->Prj().GetProjectPath();
892
893 COMMON_CONTROL* commonControl = m_toolMgr->GetTool<COMMON_CONTROL>();
894 return commonControl->Execute( execFile, param );
895}
896
897
899{
901 {
902 // policy disables the plugin manager
903 return 0;
904 }
905
906 // For some reason, after a click or a double click the bitmap button calling
907 // PCM keeps the focus althougt the focus was not set to this button.
908 // This hack force removing the focus from this button
909 m_frame->SetFocus();
910 wxSafeYield();
911
912 if( !m_frame->GetPcm() )
913 m_frame->CreatePCM();
914
915 DIALOG_PCM pcm( m_frame, m_frame->GetPcm() );
916 pcm.ShowModal();
917
918 const std::unordered_set<PCM_PACKAGE_TYPE>& changed = pcm.GetChangedPackageTypes();
919
920 if( changed.count( PCM_PACKAGE_TYPE::PT_PLUGIN ) || changed.count( PCM_PACKAGE_TYPE::PT_FAB ) )
921 {
922 std::string payload = "";
923 m_frame->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_RELOAD_PLUGINS, payload );
924 }
925
926 KICAD_SETTINGS* settings = GetAppSettings<KICAD_SETTINGS>( "kicad" );
927
928 if( changed.count( PCM_PACKAGE_TYPE::PT_LIBRARY )
929 && ( settings->m_PcmLibAutoAdd || settings->m_PcmLibAutoRemove ) )
930 {
931 KIWAY& kiway = m_frame->Kiway();
932
933 // Reset state containing global lib tables
934 if( KIFACE* kiface = kiway.KiFACE( KIWAY::FACE_SCH, false ) )
935 kiface->Reset();
936
937 if( KIFACE* kiface = kiway.KiFACE( KIWAY::FACE_PCB, false ) )
938 kiface->Reset();
939
940 // Reload lib tables
941 std::string payload = "";
942
945 kiway.ExpressMail( FRAME_CVPCB, MAIL_RELOAD_LIB, payload );
948 }
949
950 if( changed.count( PCM_PACKAGE_TYPE::PT_COLORTHEME ) )
952
953 return 0;
954}
955
956
958{
969
975
978
988
991
993}
static TOOL_ACTION zoomRedraw
Definition actions.h:132
static TOOL_ACTION saveAs
Definition actions.h:59
static TOOL_ACTION updateMenu
Definition actions.h:270
Define the structure of a menu based on ACTIONs.
Definition action_menu.h:47
void UpdateAll()
Run update handlers for the menu and its submenus.
Handle actions that are shared between different applications.
int Execute(const TOOL_EVENT &aEvent)
const wxString & GetFullURL() const
KIGIT_COMMON::GIT_CONN_TYPE GetRepoType() const
wxString GetRepoSSHPath() 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
void SetRemote(const wxString &aRemote)
void SetClonePath(const wxString &aPath)
void SetProgressReporter(std::unique_ptr< WX_PROGRESS_REPORTER > aProgressReporter)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
static TOOL_ACTION viewDroppedGerbers
static TOOL_ACTION openDemoProject
static TOOL_ACTION unarchiveProject
static TOOL_ACTION loadProject
static TOOL_ACTION editOtherPCB
static TOOL_ACTION restoreLocalHistory
static TOOL_ACTION newProject
static TOOL_ACTION editOtherSch
static TOOL_ACTION showLocalHistory
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 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 ToggleLocalHistory(const TOOL_EVENT &aEvent)
int ArchiveProject(const TOOL_EVENT &aEvent)
bool m_inShowPlayer
Re-entrancy guard.
int SaveProjectAs(const TOOL_EVENT &aEvent)
int NewProject(const TOOL_EVENT &aEvent)
int RestoreLocalHistory(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 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)
std::vector< wxString > m_RecentTemplates
wxSize m_TemplateWindowSize
wxPoint m_TemplateWindowPos
wxString m_LastUsedTemplate
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition kidialog.h:42
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:55
int ShowModal() override
Definition kidialog.cpp:93
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.
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:295
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr, bool aFromOtherThread=false)
Send aPayload to aDestination from aSource.
Definition kiway.cpp:505
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition kiway.cpp:213
@ FACE_SCH
eeschema DSO
Definition kiway.h:302
@ FACE_PCB
pcbnew DSO
Definition kiway.h:303
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition paths.cpp:137
static wxString GetStockDemosPath()
Gets the stock (install) demos path.
Definition paths.cpp:439
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
Definition pgm_base.cpp:787
virtual const wxString & GetTextEditor(bool aCanShowFileChooser=true)
Return the path to the preferred text editor application.
Definition pgm_base.cpp:220
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:131
static bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Create an archive of the project.
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
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)
A class which provides project template functionality.
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.
wxFileName GetHtmlFile()
Get the full Html filename for the project template.
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
Traverser class to duplicate/copy project or template files with proper renaming.
wxFileName GetNewProjectFile() const
KIGIT_COMMON * GitCommon() const
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition project.cpp:189
virtual PROJECT_LOCAL_SETTINGS & GetLocalSettings() const
Definition project.h:209
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:203
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:359
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:186
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:78
Generic, UI-independent tool event.
Definition tool_event.h:171
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:473
void RunMainStack(std::function< void()> aFunc)
Call a function using the main stack.
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).
TOOL_INTERACTIVE(TOOL_ID aId, const std::string &aName)
Create a tool with given id & name.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:259
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:230
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:202
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
static const std::string ProjectFileExtension
static const std::string LegacyProjectFileExtension
static const std::string ArchiveFileExtension
static wxString ProjectFileWildcard()
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:642
static wxFileName ensureDefaultProjectTemplate()
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
@ 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:86
PBOOL GetPolicyBool(const wxString &aKey)
bool StoreSecret(const wxString &aService, const wxString &aKey, const wxString &aSecret)
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:435
@ PT_COLORTHEME
Definition pcm_data.h:48
@ PT_PLUGIN
Definition pcm_data.h:44
@ PT_LIBRARY
Definition pcm_data.h:46
@ PT_FAB
Definition pcm_data.h:45
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
#define POLICY_KEY_PCM
Definition policy_keys.h:31
#define METADIR
A directory which contains information about the project template and does not get copied.
#define METAFILE_INFO_HTML
A required html formatted file which contains information about the project template.
T * GetAppSettings(const char *aFilename)
Implement a participant in the KIWAY alchemy.
Definition kiway.h:156
Store the common settings that are saved and loaded for each window / frame.
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
VECTOR2I end
wxString result
Test unit parsing edge cases and error handling.
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:39
#define PR_NO_ABORT