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>
38#include <tool/selection.h>
39#include <tool/tool_event.h>
40#include <tool/tool_manager.h>
41#include <tool/common_control.h>
48#include <gestfich.h>
49#include <paths.h>
50#include <wx/dir.h>
51#include <wx/filedlg.h>
52#include <wx/ffile.h>
53#include "dialog_pcm.h"
55#include <project_tree_pane.h>
56#include <project_tree.h>
58#include <launch_ext.h>
59
61
63 TOOL_INTERACTIVE( "kicad.Control" ),
64 m_frame( nullptr ),
65 m_inShowPlayer( false )
66{
67}
68
69
74
75
76wxFileName KICAD_MANAGER_CONTROL::newProjectDirectory( wxString* aFileName, bool isRepo )
77{
78 wxString default_filename = aFileName ? *aFileName : wxString();
79
80 wxString default_dir = m_frame->GetMruPath();
81 wxFileDialog dlg( m_frame, _( "Create New Project" ), default_dir, default_filename,
82 ( isRepo ? wxString( "" ) : FILEEXT::ProjectFileWildcard() ),
83 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
84
85 dlg.AddShortcut( PATHS::GetDefaultUserProjectsPath() );
86
87 // Add a "Create a new directory" checkbox
88 FILEDLG_NEW_PROJECT newProjectHook;
89 dlg.SetCustomizeHook( newProjectHook );
90
92
93 if( dlg.ShowModal() == wxID_CANCEL )
94 return wxFileName();
95
96 wxFileName pro( dlg.GetPath() );
97
98 // wxFileName automatically extracts an extension. But if it isn't
99 // a .pro extension, we should keep it as part of the filename
100 if( !pro.GetExt().IsEmpty() && pro.GetExt().ToStdString() != FILEEXT::ProjectFileExtension )
101 pro.SetName( pro.GetName() + wxT( "." ) + pro.GetExt() );
102
103 pro.SetExt( FILEEXT::ProjectFileExtension ); // enforce extension
104
105 if( !pro.IsAbsolute() )
106 pro.MakeAbsolute();
107
108 // Append a new directory with the same name of the project file.
109 bool createNewDir = false;
110
111 createNewDir = newProjectHook.GetCreateNewDir();
112
113 if( createNewDir )
114 pro.AppendDir( pro.GetName() );
115
116 // Check if the project directory is empty if it already exists.
117 wxDir directory( pro.GetPath() );
118
119 if( !pro.DirExists() )
120 {
121 if( !pro.Mkdir() )
122 {
123 wxString msg;
124 msg.Printf( _( "Folder '%s' could not be created.\n\n"
125 "Make sure you have write permissions and try again." ),
126 pro.GetPath() );
128 return wxFileName();
129 }
130 }
131 else if( directory.HasFiles() )
132 {
133 wxString msg = _( "The selected folder is not empty. It is recommended that you "
134 "create projects in their own empty folder.\n\n"
135 "Do you want to continue?" );
136
137 if( !IsOK( m_frame, msg ) )
138 return wxFileName();
139 }
140
141 return pro;
142}
143
144
146{
147 ENV_VAR_MAP_CITER it = Pgm().GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
148
149 if( it == Pgm().GetLocalEnvVariables().end() || it->second.GetValue() == wxEmptyString )
150 return wxFileName();
151
152 wxFileName templatePath;
153 templatePath.AssignDir( it->second.GetValue() );
154 templatePath.AppendDir( "default" );
155
156 if( templatePath.DirExists() )
157 return templatePath;
158
159 if( !templatePath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
160 return wxFileName();
161
162 wxFileName metaDir = templatePath;
163 metaDir.AppendDir( METADIR );
164
165 if( !metaDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
166 return wxFileName();
167
168 wxFileName infoFile = metaDir;
169 infoFile.SetFullName( METAFILE_INFO_HTML );
170 wxFFile info( infoFile.GetFullPath(), wxT( "w" ) );
171
172 if( !info.IsOpened() )
173 return wxFileName();
174
175 info.Write( wxT( "<html><head><title>Default</title></head><body></body></html>" ) );
176 info.Close();
177
178 wxFileName proFile = templatePath;
179 proFile.SetFullName( wxT( "default.kicad_pro" ) );
180 wxFFile proj( proFile.GetFullPath(), wxT( "w" ) );
181
182 if( !proj.IsOpened() )
183 return wxFileName();
184
185 proj.Write( wxT( "{}" ) );
186 proj.Close();
187
188 return templatePath;
189}
190
192{
193 wxFileName defaultTemplate = ensureDefaultProjectTemplate();
194
195 if( !defaultTemplate.IsOk() )
196 {
197 wxFileName pro = newProjectDirectory();
198
199 if( !pro.IsOk() )
200 return -1;
201
202 m_frame->CreateNewProject( pro );
203 m_frame->LoadProject( pro );
204
205 return 0;
206 }
207
208 KICAD_SETTINGS* settings = GetAppSettings<KICAD_SETTINGS>( "kicad" );
209
210 wxString userTemplatesPath;
211 wxString systemTemplatesPath;
212
213 ENV_VAR_MAP_CITER itUser = Pgm().GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
214
215 if( itUser != Pgm().GetLocalEnvVariables().end() && itUser->second.GetValue() != wxEmptyString )
216 {
217 wxFileName templatePath;
218 templatePath.AssignDir( itUser->second.GetValue() );
219 templatePath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
220 userTemplatesPath = templatePath.GetFullPath();
221 }
222
223 std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( Pgm().GetLocalEnvVariables(),
224 wxT( "TEMPLATE_DIR" ) );
225
226 if( v && !v->IsEmpty() )
227 {
228 wxFileName templatePath;
229 templatePath.AssignDir( *v );
230 templatePath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
231 systemTemplatesPath = templatePath.GetFullPath();
232 }
233
234 // Use RunMainStack to show the dialog on the main stack instead of the coroutine stack.
235 // This is necessary because the template selector uses a WebView which triggers WebKit's
236 // JavaScript VM initialization. WebKit's stack validation fails on coroutine stacks.
237 int result = wxID_CANCEL;
238 wxString selectedTemplatePath;
239 wxPoint templateWindowPos;
240 wxSize templateWindowSize;
241 wxString projectToEdit;
242
244 [&]()
245 {
247 settings->m_TemplateWindowSize, userTemplatesPath,
248 systemTemplatesPath, settings->m_RecentTemplates );
249
250 result = ps.ShowModal();
251 templateWindowPos = ps.GetPosition();
252 templateWindowSize = ps.GetSize();
253 projectToEdit = ps.GetProjectToEdit();
254
256
257 if( templ )
258 {
259 wxFileName htmlFile = templ->GetHtmlFile();
260 htmlFile.RemoveLastDir();
261 selectedTemplatePath = htmlFile.GetPath();
262 }
263 } );
264
265 settings->m_TemplateWindowPos = templateWindowPos;
266 settings->m_TemplateWindowSize = templateWindowSize;
267
268 // Check if user wants to edit a template instead of creating new project
269 if( result == wxID_APPLY )
270 {
271 if( !projectToEdit.IsEmpty() && wxFileExists( projectToEdit ) )
272 {
273 m_frame->LoadProject( wxFileName( projectToEdit ) );
274 return 0;
275 }
276 }
277
278 if( result != wxID_OK )
279 return -1;
280
281 if( selectedTemplatePath.IsEmpty() )
282 {
283 wxMessageBox( _( "No project template was selected. Cannot generate new project." ), _( "Error" ),
284 wxOK | wxICON_ERROR, m_frame );
285
286 return -1;
287 }
288
289 // Recreate the template object from the saved path
290 PROJECT_TEMPLATE selectedTemplate( selectedTemplatePath );
291
292 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
293 wxString title = _( "New Project Folder" );
294 wxFileDialog dlg( m_frame, title, default_dir, wxEmptyString, FILEEXT::ProjectFileWildcard(),
295 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
296
297 dlg.AddShortcut( PATHS::GetDefaultUserProjectsPath() );
298
299 FILEDLG_NEW_PROJECT newProjectHook;
300 dlg.SetCustomizeHook( newProjectHook );
301
303
304 if( dlg.ShowModal() == wxID_CANCEL )
305 return -1;
306
307 wxFileName fn( dlg.GetPath() );
308
309 if( !fn.GetExt().IsEmpty() && fn.GetExt().ToStdString() != FILEEXT::ProjectFileExtension )
310 fn.SetName( fn.GetName() + wxT( "." ) + fn.GetExt() );
311
313
314 if( !fn.IsAbsolute() )
315 fn.MakeAbsolute();
316
317 bool createNewDir = false;
318 createNewDir = newProjectHook.GetCreateNewDir();
319
320 if( createNewDir )
321 fn.AppendDir( fn.GetName() );
322
323 if( !fn.DirExists() && !fn.Mkdir() )
324 {
325 DisplayErrorMessage( m_frame, wxString::Format( _( "Folder '%s' could not be created.\n\n"
326 "Make sure you have write permissions and try again." ),
327 fn.GetPath() ) );
328 return -1;
329 }
330
331 if( !fn.IsDirWritable() )
332 {
333 DisplayErrorMessage( m_frame, wxString::Format( _( "Insufficient permissions to write to folder '%s'." ),
334 fn.GetPath() ) );
335 return -1;
336 }
337
338 std::vector< wxFileName > destFiles;
339
340 if( selectedTemplate.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, _( "Similar files already exist in the destination folder." ),
358 _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
359 msgDlg.SetExtendedMessage( extendedMsg );
360 msgDlg.SetOKLabel( _( "Overwrite" ) );
361 msgDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
362
363 if( msgDlg.ShowModal() == wxID_CANCEL )
364 return -1;
365 }
366 }
367
368 wxString errorMsg;
369
370 if( !selectedTemplate.CreateProject( fn, &errorMsg ) )
371 {
372 DisplayErrorMessage( m_frame, _( "A problem occurred creating new project from template." ), errorMsg );
373 return -1;
374 }
375
376 // Update MRU list with the used template
377 wxFileName templateDir = selectedTemplate.GetHtmlFile();
378 templateDir.RemoveLastDir();
379 wxString templatePath = templateDir.GetPath();
380
381 settings->m_LastUsedTemplate = templatePath;
382
383 // Add to front of recent templates, remove duplicates, trim to 5
384 std::vector<wxString>& recentTemplates = settings->m_RecentTemplates;
385 recentTemplates.erase( std::remove( recentTemplates.begin(), recentTemplates.end(), templatePath ),
386 recentTemplates.end() );
387 recentTemplates.insert( recentTemplates.begin(), templatePath );
388
389 if( recentTemplates.size() > 5 )
390 recentTemplates.resize( 5 );
391
392 m_frame->CreateNewProject( fn.GetFullPath() );
393 m_frame->LoadProject( fn );
394 return 0;
395}
396
397
399{
400 DIALOG_GIT_REPOSITORY dlg( m_frame, nullptr );
401
402 dlg.SetTitle( _( "Clone Project from Git Repository" ) );
403
404 int ret = dlg.ShowModal();
405
406 if( ret != wxID_OK )
407 return -1;
408
409 wxString project_name = dlg.GetRepoName();
410 wxFileName pro = newProjectDirectory( &project_name, true );
411
412 if( !pro.IsOk() )
413 return -1;
414
415 PROJECT_TREE_PANE *pane = static_cast<PROJECT_TREE_PANE*>( m_frame->GetToolCanvas() );
416
417
418 GIT_CLONE_HANDLER cloneHandler( pane->m_TreeProject->GitCommon() );
419
420 cloneHandler.SetRemote( dlg.GetFullURL() );
421 cloneHandler.SetClonePath( pro.GetPath() );
422 cloneHandler.SetUsername( dlg.GetUsername() );
423 cloneHandler.SetPassword( dlg.GetPassword() );
424 cloneHandler.SetSSHKey( dlg.GetRepoSSHPath() );
425
426 cloneHandler.SetProgressReporter( std::make_unique<WX_PROGRESS_REPORTER>( m_frame, _( "Clone Repository" ), 1,
427 PR_NO_ABORT ) );
428
429 if( !cloneHandler.PerformClone() )
430 {
431 DisplayErrorMessage( m_frame, cloneHandler.GetErrorString() );
432 return -1;
433 }
434
435 std::vector<wxString> projects = cloneHandler.GetProjectDirs();
436
437 if( projects.empty() )
438 {
439 DisplayErrorMessage( m_frame, _( "No project files were found in the repository." ) );
440 return -1;
441 }
442
443 // Currently, we pick the first project file we find in the repository.
444 // TODO: Look into spare checkout to allow the user to pick a partial repository
445 wxString dest = pro.GetPath() + wxFileName::GetPathSeparator() + projects.front();
446 m_frame->LoadProject( dest );
447
451
455 Prj().GetLocalSettings().m_GitRepoType = "https";
456 else
457 Prj().GetLocalSettings().m_GitRepoType = "local";
458
459 return 0;
460}
461
462
464{
465 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
466 wxFileDialog dlg( m_frame, _( "Create New Jobset" ), default_dir, wxEmptyString, FILEEXT::JobsetFileWildcard(),
467 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
468
470
471 if( dlg.ShowModal() == wxID_CANCEL )
472 return -1;
473
474 wxFileName jobsetFn( dlg.GetPath() );
475
476 // Check if the file already exists
477 bool fileExists = wxFileExists( jobsetFn.GetFullPath() );
478
479 if( fileExists )
480 {
481 // Remove the existing file so that a new one can be created
482 if( !wxRemoveFile( jobsetFn.GetFullPath() ) )
483 {
484 return -1;
485 }
486 }
487
488 m_frame->OpenJobsFile( jobsetFn.GetFullPath(), true );
489
490 return 0;
491}
492
493
494
495
496int KICAD_MANAGER_CONTROL::openProject( const wxString& aDefaultDir )
497{
498 wxString wildcard = FILEEXT::AllProjectFilesWildcard()
501
502 wxFileDialog dlg( m_frame, _( "Open Existing Project" ), aDefaultDir, wxEmptyString, wildcard,
503 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
504
505 dlg.AddShortcut( PATHS::GetDefaultUserProjectsPath() );
506
508
509 if( dlg.ShowModal() == wxID_CANCEL )
510 return -1;
511
512 wxFileName pro( dlg.GetPath() );
513
514 if( !pro.IsAbsolute() )
515 pro.MakeAbsolute();
516
517 // You'd think wxFD_FILE_MUST_EXIST and the wild-cards would enforce these. Sentry
518 // indicates otherwise (at least on MSW).
519 if( !pro.Exists() || ( pro.GetExt() != FILEEXT::ProjectFileExtension
520 && pro.GetExt() != FILEEXT::LegacyProjectFileExtension ) )
521 {
522 return -1;
523 }
524
525 m_frame->LoadProject( pro );
526
527 return 0;
528}
529
530
535
536
538{
539 return openProject( m_frame->GetMruPath() );
540}
541
542
544{
545 wxString default_dir = wxFileName( Prj().GetProjectFullName() ).GetPathWithSep();
546 wxFileDialog dlg( m_frame, _( "Open Jobset" ), default_dir, wxEmptyString, FILEEXT::JobsetFileWildcard(),
547 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
548
550
551 if( dlg.ShowModal() == wxID_CANCEL )
552 return -1;
553
554 wxFileName jobsetFn( dlg.GetPath() );
555
556 m_frame->OpenJobsFile( jobsetFn.GetFullPath(), true );
557
558 return 0;
559}
560
561
563{
564 m_frame->CloseProject( true );
565 return 0;
566}
567
568
570{
571 if( aEvent.Parameter<wxString*>() )
572 m_frame->LoadProject( wxFileName( *aEvent.Parameter<wxString*>() ) );
573 return 0;
574}
575
576
578{
579 wxFileName fileName = m_frame->GetProjectFileName();
580
581 fileName.SetExt( FILEEXT::ArchiveFileExtension );
582
583 wxFileDialog dlg( m_frame, _( "Archive Project Files" ), fileName.GetPath(), fileName.GetFullName(),
584 FILEEXT::ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
585
587
588 if( dlg.ShowModal() == wxID_CANCEL )
589 return 0;
590
591 wxFileName zipFile = dlg.GetPath();
592
593 wxString currdirname = fileName.GetPathWithSep();
594 wxDir dir( currdirname );
595
596 if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
597 return 0;
598
599 STATUSBAR_REPORTER reporter( m_frame->GetStatusBar(), 1 );
600 PROJECT_ARCHIVER archiver;
601
602 archiver.Archive( currdirname, zipFile.GetFullPath(), reporter, true, true );
603 return 0;
604}
605
606
608{
609 m_frame->UnarchiveFiles();
610 return 0;
611}
612
613
615{
616 // Open project directory in host OS's file explorer
617 LaunchExternal( Prj().GetProjectPath() );
618 return 0;
619}
620
622{
623 m_frame->RestoreLocalHistory();
624 return 0;
625}
626
627
629{
630 m_frame->ToggleLocalHistory();
631 return 0;
632}
633
634
636{
637 if( aEvent.Parameter<wxString*>() )
638 wxExecute( *aEvent.Parameter<wxString*>(), wxEXEC_ASYNC );
639
640 return 0;
641}
642
643
644
646{
647 wxString msg;
648
649 wxFileName currentProjectFile( Prj().GetProjectFullName() );
650 wxString currentProjectDirPath = currentProjectFile.GetPath();
651 wxString currentProjectName = Prj().GetProjectName();
652
653 wxString default_dir = m_frame->GetMruPath();
654
655 Prj().GetProjectFile().SaveToFile( currentProjectDirPath );
656 Prj().GetLocalSettings().SaveToFile( currentProjectDirPath );
657
658 if( default_dir == currentProjectDirPath
659 || default_dir == currentProjectDirPath + wxFileName::GetPathSeparator() )
660 {
661 // Don't start within the current project
662 wxFileName default_dir_fn( default_dir );
663 default_dir_fn.RemoveLastDir();
664 default_dir = default_dir_fn.GetPath();
665 }
666
667 wxFileDialog dlg( m_frame, _( "Save Project To" ), default_dir, wxEmptyString, wxEmptyString, wxFD_SAVE );
668
669 dlg.AddShortcut( PATHS::GetDefaultUserProjectsPath() );
670
672
673 if( dlg.ShowModal() == wxID_CANCEL )
674 return -1;
675
676 wxFileName newProjectDir( dlg.GetPath(), wxEmptyString );
677
678 if( !newProjectDir.IsAbsolute() )
679 newProjectDir.MakeAbsolute();
680
681 if( wxDirExists( newProjectDir.GetFullPath() ) )
682 {
683 msg.Printf( _( "'%s' already exists." ), newProjectDir.GetFullPath() );
685 return -1;
686 }
687
688 if( !wxMkdir( newProjectDir.GetFullPath() ) )
689 {
690 DisplayErrorMessage( m_frame, wxString::Format( _( "Folder '%s' could not be created.\n\n"
691 "Please make sure you have sufficient permissions." ),
692 newProjectDir.GetPath() ) );
693 return -1;
694 }
695
696 if( !newProjectDir.IsDirWritable() )
697 {
698 DisplayErrorMessage( m_frame, wxString::Format( _( "Insufficient permissions to write to folder '%s'." ),
699 newProjectDir.GetFullPath() ) );
700 return -1;
701 }
702
703 const wxString& newProjectDirPath = newProjectDir.GetFullPath();
704 const wxString& newProjectName = newProjectDir.GetDirs().Last();
705 wxDir currentProjectDir( currentProjectDirPath );
706
707 PROJECT_TREE_TRAVERSER traverser( m_frame, currentProjectDirPath, currentProjectName,
708 newProjectDirPath, newProjectName );
709
710 currentProjectDir.Traverse( traverser );
711
712 if( !traverser.GetErrors().empty() )
713 DisplayErrorMessage( m_frame, traverser.GetErrors() );
714
715 if( !traverser.GetNewProjectFile().FileExists() )
716 m_frame->CreateNewProject( traverser.GetNewProjectFile() );
717
718 m_frame->LoadProject( traverser.GetNewProjectFile() );
719
720 return 0;
721}
722
723
725{
726 m_frame->RefreshProjectTree();
727 return 0;
728}
729
730
732{
733 ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
734 CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
735 SELECTION dummySel;
736
737 if( conditionalMenu )
738 conditionalMenu->Evaluate( dummySel );
739
740 if( actionMenu )
741 actionMenu->UpdateAll();
742
743 return 0;
744}
745
746
748{
749 FRAME_T playerType = aEvent.Parameter<FRAME_T>();
750 KIWAY_PLAYER* player;
751
752 if( playerType == FRAME_SCH && !m_frame->IsProjectActive() )
753 {
754 DisplayInfoMessage( m_frame, _( "Create (or open) a project to edit a schematic." ), wxEmptyString );
755 return -1;
756 }
757 else if( playerType == FRAME_PCB_EDITOR && !m_frame->IsProjectActive() )
758 {
759 DisplayInfoMessage( m_frame, _( "Create (or open) a project to edit a pcb." ), wxEmptyString );
760 return -1;
761 }
762
763 if( m_inShowPlayer )
764 return -1;
765
767
768 try
769 {
770 player = m_frame->Kiway().Player( playerType, true );
771 }
772 catch( const IO_ERROR& err )
773 {
774 wxLogError( _( "Application failed to load:\n" ) + err.What() );
775 return -1;
776 }
777
778 if ( !player )
779 {
780 wxLogError( _( "Application cannot start." ) );
781 return -1;
782 }
783
784 if( !player->IsVisible() ) // A hidden frame might not have the document loaded.
785 {
786 wxString filepath;
787
788 if( playerType == FRAME_SCH )
789 {
790 wxFileName kicad_schematic( m_frame->SchFileName() );
791 wxFileName legacy_schematic( m_frame->SchLegacyFileName() );
792
793 if( !legacy_schematic.FileExists() || kicad_schematic.FileExists() )
794 filepath = kicad_schematic.GetFullPath();
795 else
796 filepath = legacy_schematic.GetFullPath();
797 }
798 else if( playerType == FRAME_PCB_EDITOR )
799 {
800 wxFileName kicad_board( m_frame->PcbFileName() );
801 wxFileName legacy_board( m_frame->PcbLegacyFileName() );
802
803 if( !legacy_board.FileExists() || kicad_board.FileExists() )
804 filepath = kicad_board.GetFullPath();
805 else
806 filepath = legacy_board.GetFullPath();
807 }
808
809 if( !filepath.IsEmpty() )
810 {
811 std::vector<wxString> file_list{ filepath };
812
813 if( !player->OpenProjectFiles( file_list ) )
814 {
815 player->Destroy();
816 return -1;
817 }
818 }
819
820 wxBusyCursor busy;
821 player->Show( true );
822 }
823
824 // Needed on Windows, other platforms do not use it, but it creates no issue
825 if( player->IsIconized() )
826 player->Iconize( false );
827
828 player->Raise();
829
830 // Raising the window does not set the focus on Linux. This should work on
831 // any platform.
832 if( wxWindow::FindFocus() != player )
833 player->SetFocus();
834
835 // Save window state to disk now. Don't wait around for a crash.
836 if( Pgm().GetCommonSettings()->m_Session.remember_open_files
837 && !player->GetCurrentFileName().IsEmpty()
838 && Prj().GetLocalSettings().ShouldAutoSave() )
839 {
840 wxFileName rfn( player->GetCurrentFileName() );
841 rfn.MakeRelativeTo( Prj().GetProjectPath() );
842
843 WINDOW_SETTINGS windowSettings;
844 player->SaveWindowSettings( &windowSettings );
845
846 Prj().GetLocalSettings().SaveFileState( rfn.GetFullPath(), &windowSettings, true );
847 Prj().GetLocalSettings().SaveToFile( Prj().GetProjectPath() );
848 }
849
850 return 0;
851}
852
853
855{
856 wxString execFile;
857 wxString param;
858
860 execFile = GERBVIEW_EXE;
862 execFile = BITMAPCONVERTER_EXE;
864 execFile = PCB_CALCULATOR_EXE;
866 execFile = PL_EDITOR_EXE;
868 execFile = Pgm().GetTextEditor();
870 execFile = EESCHEMA_EXE;
872 execFile = PCBNEW_EXE;
873 else
874 wxFAIL_MSG( "Execute(): unexpected request" );
875
876 if( execFile.IsEmpty() )
877 return 0;
878
879 if( aEvent.Parameter<wxString*>() )
880 param = *aEvent.Parameter<wxString*>();
881 else if( aEvent.IsAction( &KICAD_MANAGER_ACTIONS::viewGerbers ) && m_frame->IsProjectActive() )
882 param = m_frame->Prj().GetProjectPath();
883
884 COMMON_CONTROL* commonControl = m_toolMgr->GetTool<COMMON_CONTROL>();
885 return commonControl->Execute( execFile, param );
886}
887
888
890{
892 {
893 // policy disables the plugin manager
894 return 0;
895 }
896
897 // For some reason, after a click or a double click the bitmap button calling
898 // PCM keeps the focus althougt the focus was not set to this button.
899 // This hack force removing the focus from this button
900 m_frame->SetFocus();
901 wxSafeYield();
902
903 if( !m_frame->GetPcm() )
904 m_frame->CreatePCM();
905
906 DIALOG_PCM pcm( m_frame, m_frame->GetPcm() );
907 pcm.ShowModal();
908
909 const std::unordered_set<PCM_PACKAGE_TYPE>& changed = pcm.GetChangedPackageTypes();
910
911 if( changed.count( PCM_PACKAGE_TYPE::PT_PLUGIN ) || changed.count( PCM_PACKAGE_TYPE::PT_FAB ) )
912 {
913 std::string payload = "";
914 m_frame->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_RELOAD_PLUGINS, payload );
915 }
916
917 KICAD_SETTINGS* settings = GetAppSettings<KICAD_SETTINGS>( "kicad" );
918
919 if( changed.count( PCM_PACKAGE_TYPE::PT_LIBRARY )
920 && ( settings->m_PcmLibAutoAdd || settings->m_PcmLibAutoRemove ) )
921 {
922 KIWAY& kiway = m_frame->Kiway();
923
924 // Reset state containing global lib tables
925 if( KIFACE* kiface = kiway.KiFACE( KIWAY::FACE_SCH, false ) )
926 kiface->Reset();
927
928 if( KIFACE* kiface = kiway.KiFACE( KIWAY::FACE_PCB, false ) )
929 kiface->Reset();
930
931 // Reload lib tables
932 std::string payload = "";
933
936 kiway.ExpressMail( FRAME_CVPCB, MAIL_RELOAD_LIB, payload );
939 }
940
941 if( changed.count( PCM_PACKAGE_TYPE::PT_COLORTHEME ) )
943
944 return 0;
945}
946
947
949{
960
966
969
979
982
984}
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:294
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:507
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition kiway.cpp:206
@ FACE_SCH
eeschema DSO
Definition kiway.h:301
@ FACE_PCB
pcbnew DSO
Definition kiway.h:302
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:781
virtual const wxString & GetTextEditor(bool aCanShowFileChooser=true)
Return the path to the preferred text editor application.
Definition pgm_base.cpp:214
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:132
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:180
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:637
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:717
@ 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:155
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