You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

944 lines
30 KiB

5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2019 CERN
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <advanced_config.h>
  26. #include <string_utils.h>
  27. #include <pgm_base.h>
  28. #include <tool/tool_manager.h>
  29. #include <tool/library_editor_control.h>
  30. #include <tools/pcb_actions.h>
  31. #include <eda_doc.h>
  32. #include <footprint_edit_frame.h>
  33. #include <generate_footprint_info.h>
  34. #include <pcbnew_id.h>
  35. #include <confirm.h>
  36. #include <kidialog.h>
  37. #include <wx/filename.h>
  38. #include <wildcards_and_files_ext.h>
  39. #include <launch_ext.h> // To default when file manager setting is empty
  40. #include <gestfich.h> // To open with a text editor
  41. #include <widgets/wx_infobar.h>
  42. #include <footprint.h>
  43. #include <pad.h>
  44. #include <pcb_group.h>
  45. #include <zone.h>
  46. #include <fp_lib_table.h>
  47. #include <dialogs/dialog_cleanup_graphics.h>
  48. #include <dialogs/dialog_footprint_checker.h>
  49. #include <footprint_wizard_frame.h>
  50. #include <kiway.h>
  51. #include <project_pcb.h>
  52. #include <view/view_controls.h>
  53. #include <memory>
  54. #include "footprint_editor_control.h"
  55. FOOTPRINT_EDITOR_CONTROL::FOOTPRINT_EDITOR_CONTROL() :
  56. PCB_TOOL_BASE( "pcbnew.ModuleEditor" ),
  57. m_frame( nullptr ),
  58. m_checkerDialog( nullptr )
  59. {
  60. }
  61. void FOOTPRINT_EDITOR_CONTROL::Reset( RESET_REASON aReason )
  62. {
  63. m_frame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
  64. if( m_checkerDialog )
  65. DestroyCheckerDialog();
  66. }
  67. bool FOOTPRINT_EDITOR_CONTROL::Init()
  68. {
  69. LIBRARY_EDITOR_CONTROL* libraryTreeTool = m_toolMgr->GetTool<LIBRARY_EDITOR_CONTROL>();
  70. // Build a context menu for the footprint tree
  71. //
  72. CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
  73. auto libSelectedCondition =
  74. [ this ]( const SELECTION& aSel )
  75. {
  76. LIB_ID sel = m_frame->GetLibTree()->GetSelectedLibId();
  77. return !sel.GetLibNickname().empty() && sel.GetLibItemName().empty();
  78. };
  79. // The libInferredCondition allows you to do things like New Symbol and Paste with a
  80. // symbol selected (in other words, when we know the library context even if the library
  81. // itself isn't selected.
  82. auto libInferredCondition =
  83. [ this ]( const SELECTION& aSel )
  84. {
  85. LIB_ID sel = m_frame->GetLibTree()->GetSelectedLibId();
  86. return !sel.GetLibNickname().empty();
  87. };
  88. auto fpSelectedCondition =
  89. [ this ]( const SELECTION& aSel )
  90. {
  91. LIB_ID sel = m_frame->GetLibTree()->GetSelectedLibId();
  92. return !sel.GetLibNickname().empty() && !sel.GetLibItemName().empty();
  93. };
  94. auto fpExportCondition =
  95. [ this ]( const SELECTION& aSel )
  96. {
  97. FOOTPRINT* fp = m_frame->GetBoard()->GetFirstFootprint();
  98. return fp != nullptr;
  99. };
  100. auto canOpenExternally =
  101. [ this ]( const SELECTION& aSel )
  102. {
  103. // The option is shown if the editor has no current edits,
  104. // dumb/simple guard against opening a new file that does not exist on disk
  105. bool ret = !m_frame->IsContentModified();
  106. return ret;
  107. };
  108. // clang-format off
  109. ctxMenu.AddItem( PCB_ACTIONS::newFootprint, libSelectedCondition, 10 );
  110. ctxMenu.AddItem( PCB_ACTIONS::createFootprint, libSelectedCondition, 10 );
  111. ctxMenu.AddSeparator( 10 );
  112. ctxMenu.AddItem( ACTIONS::save, SELECTION_CONDITIONS::ShowAlways, 10 );
  113. ctxMenu.AddItem( ACTIONS::saveAs, libSelectedCondition || fpSelectedCondition, 10 );
  114. ctxMenu.AddItem( ACTIONS::revert, libSelectedCondition || libInferredCondition, 10 );
  115. ctxMenu.AddSeparator( 10 );
  116. ctxMenu.AddItem( PCB_ACTIONS::cutFootprint, fpSelectedCondition, 10 );
  117. ctxMenu.AddItem( PCB_ACTIONS::copyFootprint, fpSelectedCondition, 10 );
  118. ctxMenu.AddItem( PCB_ACTIONS::pasteFootprint, libInferredCondition, 10 );
  119. ctxMenu.AddItem( PCB_ACTIONS::duplicateFootprint, fpSelectedCondition, 10 );
  120. ctxMenu.AddItem( PCB_ACTIONS::renameFootprint, fpSelectedCondition, 10 );
  121. ctxMenu.AddItem( PCB_ACTIONS::deleteFootprint, fpSelectedCondition, 10 );
  122. ctxMenu.AddSeparator( 100 );
  123. ctxMenu.AddItem( PCB_ACTIONS::importFootprint, libInferredCondition, 100 );
  124. ctxMenu.AddItem( PCB_ACTIONS::exportFootprint, fpExportCondition, 100 );
  125. if( ADVANCED_CFG::GetCfg().m_EnableLibWithText )
  126. {
  127. ctxMenu.AddSeparator( 200 );
  128. ctxMenu.AddItem( ACTIONS::openWithTextEditor, canOpenExternally && fpSelectedCondition, 200 );
  129. }
  130. if( ADVANCED_CFG::GetCfg().m_EnableLibDir )
  131. {
  132. ctxMenu.AddSeparator( 200 );
  133. ctxMenu.AddItem( ACTIONS::openDirectory, canOpenExternally && ( libSelectedCondition || fpSelectedCondition ), 200 );
  134. }
  135. // clang-format on
  136. libraryTreeTool->AddContextMenuItems( &ctxMenu );
  137. return true;
  138. }
  139. void FOOTPRINT_EDITOR_CONTROL::tryToSaveFootprintInLibrary( FOOTPRINT& aFootprint,
  140. const LIB_ID& aTargetLib )
  141. {
  142. const wxString libraryName = aTargetLib.GetUniStringLibNickname();
  143. if( aTargetLib.GetLibNickname().empty() )
  144. {
  145. // Do nothing - the footprint will need to be saved manually to assign
  146. // to a library.
  147. }
  148. else
  149. {
  150. FP_LIB_TABLE& libTable = *PROJECT_PCB::PcbFootprintLibs( &m_frame->Prj() );
  151. if( !libTable.IsFootprintLibWritable( libraryName ) )
  152. {
  153. // If the library is not writeable, we'll give the user a
  154. // footprint not in a library. But add a warning to let them know
  155. // they didn't quite get what they wanted.
  156. m_frame->ShowInfoBarWarning(
  157. wxString::Format(
  158. _( "The footprint could not be added to the selected library ('%s'). "
  159. "This library is read-only." ),
  160. libraryName ),
  161. false );
  162. // And the footprint will need to be saved manually
  163. }
  164. else
  165. {
  166. // Go ahead and save it to the library
  167. LIB_ID fpid = aFootprint.GetFPID();
  168. fpid.SetLibNickname( aTargetLib.GetLibNickname() );
  169. aFootprint.SetFPID( fpid );
  170. m_frame->SaveFootprint( &aFootprint );
  171. m_frame->ClearModify();
  172. }
  173. }
  174. }
  175. int FOOTPRINT_EDITOR_CONTROL::NewFootprint( const TOOL_EVENT& aEvent )
  176. {
  177. const LIB_ID selected = m_frame->GetTargetFPID();
  178. const wxString libraryName = selected.GetUniStringLibNickname();
  179. FOOTPRINT* newFootprint = m_frame->CreateNewFootprint( wxEmptyString, libraryName );
  180. if( !newFootprint )
  181. return 0;
  182. if( !m_frame->Clear_Pcb( true ) )
  183. return 0;
  184. canvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  185. m_frame->AddFootprintToBoard( newFootprint );
  186. // Initialize data relative to nets and netclasses (for a new footprint the defaults are
  187. // used). This is mandatory to handle and draw pads.
  188. board()->BuildListOfNets();
  189. newFootprint->SetPosition( VECTOR2I( 0, 0 ) );
  190. newFootprint->ClearFlags();
  191. m_frame->Zoom_Automatique( false );
  192. m_frame->GetScreen()->SetContentModified();
  193. tryToSaveFootprintInLibrary( *newFootprint, selected );
  194. m_frame->UpdateView();
  195. m_frame->Update3DView( true, true );
  196. m_frame->SyncLibraryTree( false );
  197. return 0;
  198. }
  199. int FOOTPRINT_EDITOR_CONTROL::CreateFootprint( const TOOL_EVENT& aEvent )
  200. {
  201. LIB_ID selected = m_frame->GetLibTree()->GetSelectedLibId();
  202. wxString libraryName = selected.GetUniStringLibNickname();
  203. if( m_frame->IsContentModified() )
  204. {
  205. if( !HandleUnsavedChanges( m_frame, _( "The current footprint has been modified. "
  206. "Save changes?" ),
  207. [&]() -> bool
  208. {
  209. return m_frame->SaveFootprint( footprint() );
  210. } ) )
  211. {
  212. return 0;
  213. }
  214. }
  215. if( KIWAY_PLAYER* frame = m_frame->Kiway().Player( FRAME_FOOTPRINT_WIZARD, true, m_frame ) )
  216. {
  217. FOOTPRINT_WIZARD_FRAME* wizard = static_cast<FOOTPRINT_WIZARD_FRAME*>( frame );
  218. if( wizard->ShowModal( nullptr, m_frame ) )
  219. {
  220. // Creates the new footprint from python script wizard
  221. FOOTPRINT* newFootprint = wizard->GetBuiltFootprint();
  222. if( newFootprint ) // i.e. if create footprint command is OK
  223. {
  224. m_frame->Clear_Pcb( false );
  225. canvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  226. // Add the new object to board
  227. m_frame->AddFootprintToBoard( newFootprint );
  228. // Initialize data relative to nets and netclasses (for a new footprint the
  229. // defaults are used). This is mandatory to handle and draw pads.
  230. board()->BuildListOfNets();
  231. newFootprint->SetPosition( VECTOR2I( 0, 0 ) );
  232. newFootprint->ClearFlags();
  233. m_frame->Zoom_Automatique( false );
  234. m_frame->GetScreen()->SetContentModified();
  235. m_frame->OnModify();
  236. tryToSaveFootprintInLibrary( *newFootprint, selected );
  237. m_frame->UpdateView();
  238. canvas()->Refresh();
  239. m_frame->Update3DView( true, true );
  240. m_frame->SyncLibraryTree( false );
  241. }
  242. }
  243. wizard->Destroy();
  244. }
  245. return 0;
  246. }
  247. int FOOTPRINT_EDITOR_CONTROL::Save( const TOOL_EVENT& aEvent )
  248. {
  249. if( !footprint() ) // no loaded footprint
  250. return 0;
  251. if( m_frame->GetTargetFPID() == m_frame->GetLoadedFPID() )
  252. {
  253. if( m_frame->SaveFootprint( footprint() ) )
  254. {
  255. view()->Update( footprint() );
  256. canvas()->ForceRefresh();
  257. m_frame->ClearModify();
  258. m_frame->UpdateTitle();
  259. }
  260. }
  261. m_frame->RefreshLibraryTree();
  262. return 0;
  263. }
  264. int FOOTPRINT_EDITOR_CONTROL::SaveAs( const TOOL_EVENT& aEvent )
  265. {
  266. if( m_frame->GetTargetFPID().GetLibItemName().empty() )
  267. {
  268. // Save Library As
  269. const wxString& src_libNickname = m_frame->GetTargetFPID().GetLibNickname();
  270. wxString src_libFullName = PROJECT_PCB::PcbFootprintLibs( &m_frame->Prj() )->GetFullURI( src_libNickname );
  271. if( m_frame->SaveLibraryAs( src_libFullName ) )
  272. m_frame->SyncLibraryTree( true );
  273. }
  274. else if( m_frame->GetTargetFPID() == m_frame->GetLoadedFPID() )
  275. {
  276. // Save Footprint As
  277. if( footprint() && m_frame->SaveFootprintAs( footprint() ) )
  278. {
  279. view()->Update( footprint() );
  280. m_frame->ClearModify();
  281. // Get rid of the save-will-update-board-only (or any other dismissable warning)
  282. WX_INFOBAR* infobar = m_frame->GetInfoBar();
  283. if( infobar->IsShownOnScreen() && infobar->HasCloseButton() )
  284. infobar->Dismiss();
  285. canvas()->ForceRefresh();
  286. m_frame->SyncLibraryTree( true );
  287. }
  288. }
  289. else
  290. {
  291. // Save Selected Footprint As
  292. FOOTPRINT* footprint = m_frame->LoadFootprint( m_frame->GetTargetFPID() );
  293. if( footprint && m_frame->SaveFootprintAs( footprint ) )
  294. {
  295. m_frame->SyncLibraryTree( true );
  296. m_frame->FocusOnLibID( footprint->GetFPID() );
  297. }
  298. }
  299. m_frame->RefreshLibraryTree();
  300. return 0;
  301. }
  302. int FOOTPRINT_EDITOR_CONTROL::Revert( const TOOL_EVENT& aEvent )
  303. {
  304. getEditFrame<FOOTPRINT_EDIT_FRAME>()->RevertFootprint();
  305. return 0;
  306. }
  307. int FOOTPRINT_EDITOR_CONTROL::CutCopyFootprint( const TOOL_EVENT& aEvent )
  308. {
  309. LIB_ID fpID = m_frame->GetLibTree()->GetSelectedLibId();
  310. if( fpID == m_frame->GetLoadedFPID() )
  311. {
  312. m_copiedFootprint = std::make_unique<FOOTPRINT>( *m_frame->GetBoard()->GetFirstFootprint() );
  313. m_copiedFootprint->SetParent( nullptr );
  314. }
  315. else
  316. {
  317. m_copiedFootprint.reset( m_frame->LoadFootprint( fpID ) );
  318. }
  319. if( aEvent.IsAction( &PCB_ACTIONS::cutFootprint ) )
  320. DeleteFootprint( aEvent );
  321. return 0;
  322. }
  323. int FOOTPRINT_EDITOR_CONTROL::PasteFootprint( const TOOL_EVENT& aEvent )
  324. {
  325. if( m_copiedFootprint && !m_frame->GetLibTree()->GetSelectedLibId().GetLibNickname().empty() )
  326. {
  327. wxString newLib = m_frame->GetLibTree()->GetSelectedLibId().GetLibNickname();
  328. wxString newName = m_copiedFootprint->GetFPID().GetLibItemName();
  329. while( PROJECT_PCB::PcbFootprintLibs( &m_frame->Prj() )->FootprintExists( newLib, newName ) )
  330. newName += _( "_copy" );
  331. m_copiedFootprint->SetFPID( LIB_ID( newLib, newName ) );
  332. m_frame->SaveFootprintInLibrary( m_copiedFootprint.get(), newLib );
  333. m_frame->SyncLibraryTree( true );
  334. m_frame->LoadFootprintFromLibrary( m_copiedFootprint->GetFPID() );
  335. m_frame->FocusOnLibID( m_copiedFootprint->GetFPID() );
  336. m_frame->RefreshLibraryTree();
  337. }
  338. return 0;
  339. }
  340. int FOOTPRINT_EDITOR_CONTROL::DuplicateFootprint( const TOOL_EVENT& aEvent )
  341. {
  342. LIB_ID fpID = m_frame->GetLibTree()->GetSelectedLibId();
  343. FOOTPRINT* footprint;
  344. if( fpID == m_frame->GetLoadedFPID() )
  345. footprint = new FOOTPRINT( *m_frame->GetBoard()->GetFirstFootprint() );
  346. else
  347. footprint = m_frame->LoadFootprint( m_frame->GetTargetFPID() );
  348. if( footprint && m_frame->DuplicateFootprint( footprint ) )
  349. {
  350. m_frame->SyncLibraryTree( true );
  351. m_frame->LoadFootprintFromLibrary( footprint->GetFPID() );
  352. m_frame->FocusOnLibID( footprint->GetFPID() );
  353. m_frame->RefreshLibraryTree();
  354. }
  355. return 0;
  356. }
  357. int FOOTPRINT_EDITOR_CONTROL::RenameFootprint( const TOOL_EVENT& aEvent )
  358. {
  359. LIBRARY_EDITOR_CONTROL* libTool = m_toolMgr->GetTool<LIBRARY_EDITOR_CONTROL>();
  360. FP_LIB_TABLE* tbl = PROJECT_PCB::PcbFootprintLibs( &m_frame->Prj() );
  361. LIB_ID fpID = m_frame->GetLibTree()->GetSelectedLibId();
  362. wxString libraryName = fpID.GetLibNickname();
  363. wxString oldName = fpID.GetLibItemName();
  364. wxString newName;
  365. wxString msg;
  366. if( !libTool->RenameLibrary( _( "Change Footprint Name" ), oldName,
  367. [&]( const wxString& aNewName )
  368. {
  369. newName = aNewName;
  370. if( newName.IsEmpty() )
  371. {
  372. wxMessageBox( _( "Footprint must have a name." ) );
  373. return false;
  374. }
  375. // If no change, accept it without prompting
  376. if( oldName != newName && tbl->FootprintExists( libraryName, newName ) )
  377. {
  378. msg = wxString::Format( _( "Footprint '%s' already exists in library '%s'." ),
  379. newName, libraryName );
  380. KIDIALOG errorDlg( m_frame, msg, _( "Confirmation" ),
  381. wxOK | wxCANCEL | wxICON_WARNING );
  382. errorDlg.SetOKLabel( _( "Overwrite" ) );
  383. return errorDlg.ShowModal() == wxID_OK;
  384. }
  385. return true;
  386. } ) )
  387. {
  388. return 0; // cancelled by user
  389. }
  390. if( newName == oldName )
  391. return 0;
  392. FOOTPRINT* footprint = nullptr;
  393. if( fpID == m_frame->GetLoadedFPID() )
  394. {
  395. footprint = m_frame->GetBoard()->GetFirstFootprint();
  396. if( footprint )
  397. {
  398. footprint->SetFPID( LIB_ID( libraryName, newName ) );
  399. if( footprint->GetValue() == oldName )
  400. footprint->SetValue( newName );
  401. m_frame->OnModify();
  402. m_frame->UpdateView();
  403. }
  404. }
  405. else
  406. {
  407. footprint = m_frame->LoadFootprint( fpID );
  408. if( footprint )
  409. {
  410. try
  411. {
  412. footprint->SetFPID( LIB_ID( libraryName, newName ) );
  413. if( footprint->GetValue() == oldName )
  414. footprint->SetValue( newName );
  415. m_frame->SaveFootprintInLibrary( footprint, libraryName );
  416. PROJECT_PCB::PcbFootprintLibs( &m_frame->Prj() )->FootprintDelete( libraryName, oldName );
  417. }
  418. catch( const IO_ERROR& ioe )
  419. {
  420. DisplayErrorMessage( m_frame, _( "Error renaming footprint" ), ioe.What() );
  421. }
  422. catch( ... )
  423. {
  424. // Best efforts...
  425. }
  426. }
  427. }
  428. wxDataViewItem treeItem = m_frame->GetLibTreeAdapter()->FindItem( fpID );
  429. m_frame->UpdateLibraryTree( treeItem, footprint );
  430. m_frame->FocusOnLibID( LIB_ID( libraryName, newName ) );
  431. return 0;
  432. }
  433. int FOOTPRINT_EDITOR_CONTROL::DeleteFootprint( const TOOL_EVENT& aEvent )
  434. {
  435. FOOTPRINT_EDIT_FRAME* frame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
  436. if( frame->DeleteFootprintFromLibrary( frame->GetTargetFPID(), true ) )
  437. {
  438. if( frame->GetTargetFPID() == frame->GetLoadedFPID() )
  439. frame->Clear_Pcb( false );
  440. frame->SyncLibraryTree( true );
  441. }
  442. return 0;
  443. }
  444. int FOOTPRINT_EDITOR_CONTROL::ImportFootprint( const TOOL_EVENT& aEvent )
  445. {
  446. bool is_last_fp_from_brd = m_frame->IsCurrentFPFromBoard();
  447. if( !m_frame->Clear_Pcb( true ) )
  448. return -1; // this command is aborted
  449. getViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  450. m_frame->ImportFootprint();
  451. if( m_frame->GetBoard()->GetFirstFootprint() )
  452. m_frame->GetBoard()->GetFirstFootprint()->ClearFlags();
  453. frame()->ClearUndoRedoList();
  454. // Update the save items if needed.
  455. if( is_last_fp_from_brd )
  456. {
  457. m_frame->ReCreateMenuBar();
  458. m_frame->ReCreateHToolbar();
  459. }
  460. m_toolMgr->RunAction( ACTIONS::zoomFitScreen );
  461. m_frame->OnModify();
  462. return 0;
  463. }
  464. int FOOTPRINT_EDITOR_CONTROL::ExportFootprint( const TOOL_EVENT& aEvent )
  465. {
  466. if( FOOTPRINT* fp = m_frame->GetBoard()->GetFirstFootprint() )
  467. m_frame->ExportFootprint( fp );
  468. return 0;
  469. }
  470. int FOOTPRINT_EDITOR_CONTROL::OpenDirectory( const TOOL_EVENT& aEvent )
  471. {
  472. // No check for multi selection since the context menu option must be hidden in that case
  473. FP_LIB_TABLE* globalTable = dynamic_cast<FP_LIB_TABLE*>( &GFootprintTable );
  474. FP_LIB_TABLE* projectTable = PROJECT_PCB::PcbFootprintLibs( &m_frame->Prj() );
  475. LIB_ID libId = m_frame->GetTargetFPID();
  476. wxString libName = libId.GetLibNickname();
  477. wxString libItemName = libId.GetLibItemName();
  478. wxString path = wxEmptyString;
  479. for( FP_LIB_TABLE* table : { globalTable, projectTable } )
  480. {
  481. if( !table )
  482. break;
  483. try
  484. {
  485. path = table->FindRow( libName, true )->GetFullURI( true );
  486. }
  487. catch( IO_ERROR& )
  488. {
  489. // Do nothing: libName can be not found in globalTable if libName is in projectTable
  490. }
  491. if( !path.IsEmpty() )
  492. break;
  493. }
  494. wxString fileExt = wxEmptyString;
  495. // If selection is footprint
  496. if( !libItemName.IsEmpty() )
  497. fileExt = FILEEXT::KiCadFootprintFileExtension;
  498. wxFileName fileName( path, libItemName, fileExt );
  499. COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
  500. wxString explCommand = cfg->m_System.file_explorer;
  501. if( explCommand.IsEmpty() )
  502. {
  503. path = fileName.GetFullPath().BeforeLast( wxFileName::GetPathSeparator() );
  504. if( !path.IsEmpty() && wxDirExists( path ) )
  505. LaunchExternal( path );
  506. return 0;
  507. }
  508. if( !explCommand.EndsWith( "%F" ) )
  509. {
  510. wxMessageBox( _( "Missing/malformed file explorer argument '%F' in common settings." ) );
  511. return 0;
  512. }
  513. wxString escapedFilePath = fileName.GetFullPath();
  514. escapedFilePath.Replace( wxS( "\"" ), wxS( "_" ) );
  515. wxString fileArg = wxEmptyString;
  516. fileArg << '"' << escapedFilePath << '"';
  517. explCommand.Replace( wxT( "%F" ), fileArg );
  518. if( !explCommand.IsEmpty() )
  519. wxExecute( explCommand );
  520. return 0;
  521. }
  522. int FOOTPRINT_EDITOR_CONTROL::OpenWithTextEditor( const TOOL_EVENT& aEvent )
  523. {
  524. wxString fullEditorName = Pgm().GetTextEditor();
  525. if( fullEditorName.IsEmpty() )
  526. {
  527. wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
  528. return 0;
  529. }
  530. // No check for multi selection since the context menu option must be hidden in that case
  531. FP_LIB_TABLE* globalTable = dynamic_cast<FP_LIB_TABLE*>( &GFootprintTable );
  532. FP_LIB_TABLE* projectTable = PROJECT_PCB::PcbFootprintLibs( &m_frame->Prj() );
  533. LIB_ID libId = m_frame->GetLibTree()->GetSelectedLibId();
  534. wxString libName = libId.GetLibNickname();
  535. wxString libItemName = wxEmptyString;
  536. for( FP_LIB_TABLE* table : { globalTable, projectTable } )
  537. {
  538. if( !table )
  539. break;
  540. try
  541. {
  542. libItemName = table->FindRow( libName, true )->GetFullURI( true );
  543. }
  544. catch( IO_ERROR& )
  545. {
  546. // Do nothing: libName can be not found in globalTable if libName is in projectTable
  547. }
  548. if( !libItemName.IsEmpty() )
  549. break;
  550. }
  551. libItemName << wxFileName::GetPathSeparator();
  552. libItemName << libId.GetLibItemName();
  553. libItemName << '.' + FILEEXT::KiCadFootprintFileExtension;
  554. if( !wxFileName::FileExists( libItemName ) )
  555. return 0;
  556. ExecuteFile( fullEditorName, libItemName.wc_str(), nullptr, false );
  557. return 0;
  558. }
  559. int FOOTPRINT_EDITOR_CONTROL::ShowDatasheet( const TOOL_EVENT& aEvent )
  560. {
  561. if( FOOTPRINT* footprint = m_frame->GetBoard()->GetFirstFootprint() )
  562. {
  563. std::optional<wxString> url = GetFootprintDocumentationURL( *footprint );
  564. if( !url.has_value() )
  565. {
  566. frame()->ShowInfoBarMsg( _( "No datasheet found in the footprint." ) );
  567. }
  568. else
  569. {
  570. // Only absolute URLs are supported
  571. SEARCH_STACK* searchStack = nullptr;
  572. GetAssociatedDocument( m_frame, *url, &m_frame->Prj(), searchStack, footprint );
  573. }
  574. }
  575. return 0;
  576. }
  577. int FOOTPRINT_EDITOR_CONTROL::EditFootprint( const TOOL_EVENT& aEvent )
  578. {
  579. m_frame->LoadFootprintFromLibrary( m_frame->GetLibTree()->GetSelectedLibId() );
  580. return 0;
  581. }
  582. int FOOTPRINT_EDITOR_CONTROL::EditLibraryFootprint( const TOOL_EVENT& aEvent )
  583. {
  584. FOOTPRINT* footprint = m_frame->GetBoard()->GetFirstFootprint();
  585. if( !footprint || !m_frame->IsCurrentFPFromBoard() )
  586. {
  587. wxBell();
  588. return 0;
  589. }
  590. m_frame->LoadFootprintFromLibrary( footprint->GetFPID() );
  591. if( !m_frame->IsLibraryTreeShown() )
  592. m_frame->ToggleLibraryTree();
  593. return 0;
  594. }
  595. int FOOTPRINT_EDITOR_CONTROL::ToggleLayersManager( const TOOL_EVENT& aEvent )
  596. {
  597. m_frame->ToggleLayersManager();
  598. return 0;
  599. }
  600. int FOOTPRINT_EDITOR_CONTROL::ToggleProperties( const TOOL_EVENT& aEvent )
  601. {
  602. m_frame->ToggleProperties();
  603. return 0;
  604. }
  605. int FOOTPRINT_EDITOR_CONTROL::Properties( const TOOL_EVENT& aEvent )
  606. {
  607. if( FOOTPRINT* footprint = m_frame->GetBoard()->GetFirstFootprint() )
  608. {
  609. getEditFrame<FOOTPRINT_EDIT_FRAME>()->OnEditItemRequest( footprint );
  610. m_frame->GetCanvas()->Refresh();
  611. }
  612. return 0;
  613. }
  614. int FOOTPRINT_EDITOR_CONTROL::DefaultPadProperties( const TOOL_EVENT& aEvent )
  615. {
  616. getEditFrame<FOOTPRINT_EDIT_FRAME>()->ShowPadPropertiesDialog( nullptr );
  617. return 0;
  618. }
  619. int FOOTPRINT_EDITOR_CONTROL::CleanupGraphics( const TOOL_EVENT& aEvent )
  620. {
  621. FOOTPRINT_EDIT_FRAME* editFrame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
  622. DIALOG_CLEANUP_GRAPHICS dlg( editFrame, true );
  623. dlg.ShowModal();
  624. return 0;
  625. }
  626. int FOOTPRINT_EDITOR_CONTROL::CheckFootprint( const TOOL_EVENT& aEvent )
  627. {
  628. if( !m_checkerDialog )
  629. {
  630. m_checkerDialog = new DIALOG_FOOTPRINT_CHECKER( m_frame );
  631. m_checkerDialog->Show( true );
  632. }
  633. else // The dialog is just not visible (because the user has double clicked on an error item)
  634. {
  635. m_checkerDialog->Show( true );
  636. }
  637. return 0;
  638. }
  639. void FOOTPRINT_EDITOR_CONTROL::CrossProbe( const PCB_MARKER* aMarker )
  640. {
  641. if( !m_checkerDialog )
  642. m_checkerDialog = new DIALOG_FOOTPRINT_CHECKER( m_frame );
  643. if( !m_checkerDialog->IsShownOnScreen() )
  644. m_checkerDialog->Show( true );
  645. m_checkerDialog->SelectMarker( aMarker );
  646. }
  647. void FOOTPRINT_EDITOR_CONTROL::DestroyCheckerDialog()
  648. {
  649. if( m_checkerDialog )
  650. {
  651. m_checkerDialog->Destroy();
  652. m_checkerDialog = nullptr;
  653. }
  654. }
  655. int FOOTPRINT_EDITOR_CONTROL::RepairFootprint( const TOOL_EVENT& aEvent )
  656. {
  657. FOOTPRINT* footprint = board()->Footprints().front();
  658. int errors = 0;
  659. wxString details;
  660. // Repair duplicate IDs and missing nets.
  661. std::set<KIID> ids;
  662. int duplicates = 0;
  663. auto processItem =
  664. [&]( EDA_ITEM* aItem )
  665. {
  666. if( ids.count( aItem->m_Uuid ) )
  667. {
  668. duplicates++;
  669. const_cast<KIID&>( aItem->m_Uuid ) = KIID();
  670. }
  671. ids.insert( aItem->m_Uuid );
  672. };
  673. // Footprint IDs are the most important, so give them the first crack at "claiming" a
  674. // particular KIID.
  675. processItem( footprint );
  676. // After that the principal use is for DRC marker pointers, which are most likely to pads.
  677. for( PAD* pad : footprint->Pads() )
  678. processItem( pad );
  679. // From here out I don't think order matters much.
  680. processItem( &footprint->Reference() );
  681. processItem( &footprint->Value() );
  682. for( BOARD_ITEM* item : footprint->GraphicalItems() )
  683. processItem( item );
  684. for( ZONE* zone : footprint->Zones() )
  685. processItem( zone );
  686. for( PCB_GROUP* group : footprint->Groups() )
  687. processItem( group );
  688. if( duplicates )
  689. {
  690. errors += duplicates;
  691. details += wxString::Format( _( "%d duplicate IDs replaced.\n" ), duplicates );
  692. }
  693. if( errors )
  694. {
  695. m_frame->OnModify();
  696. wxString msg = wxString::Format( _( "%d potential problems repaired." ), errors );
  697. DisplayInfoMessage( m_frame, msg, details );
  698. }
  699. else
  700. {
  701. DisplayInfoMessage( m_frame, _( "No footprint problems found." ) );
  702. }
  703. return 0;
  704. }
  705. void FOOTPRINT_EDITOR_CONTROL::setTransitions()
  706. {
  707. // clang-format off
  708. Go( &FOOTPRINT_EDITOR_CONTROL::NewFootprint, PCB_ACTIONS::newFootprint.MakeEvent() );
  709. Go( &FOOTPRINT_EDITOR_CONTROL::CreateFootprint, PCB_ACTIONS::createFootprint.MakeEvent() );
  710. Go( &FOOTPRINT_EDITOR_CONTROL::Save, ACTIONS::save.MakeEvent() );
  711. Go( &FOOTPRINT_EDITOR_CONTROL::SaveAs, ACTIONS::saveAs.MakeEvent() );
  712. Go( &FOOTPRINT_EDITOR_CONTROL::Revert, ACTIONS::revert.MakeEvent() );
  713. Go( &FOOTPRINT_EDITOR_CONTROL::DuplicateFootprint, PCB_ACTIONS::duplicateFootprint.MakeEvent() );
  714. Go( &FOOTPRINT_EDITOR_CONTROL::RenameFootprint, PCB_ACTIONS::renameFootprint.MakeEvent() );
  715. Go( &FOOTPRINT_EDITOR_CONTROL::DeleteFootprint, PCB_ACTIONS::deleteFootprint.MakeEvent() );
  716. Go( &FOOTPRINT_EDITOR_CONTROL::EditFootprint, PCB_ACTIONS::editFootprint.MakeEvent() );
  717. Go( &FOOTPRINT_EDITOR_CONTROL::EditLibraryFootprint, PCB_ACTIONS::editLibFpInFpEditor.MakeEvent() );
  718. Go( &FOOTPRINT_EDITOR_CONTROL::CutCopyFootprint, PCB_ACTIONS::cutFootprint.MakeEvent() );
  719. Go( &FOOTPRINT_EDITOR_CONTROL::CutCopyFootprint, PCB_ACTIONS::copyFootprint.MakeEvent() );
  720. Go( &FOOTPRINT_EDITOR_CONTROL::PasteFootprint, PCB_ACTIONS::pasteFootprint.MakeEvent() );
  721. Go( &FOOTPRINT_EDITOR_CONTROL::ImportFootprint, PCB_ACTIONS::importFootprint.MakeEvent() );
  722. Go( &FOOTPRINT_EDITOR_CONTROL::ExportFootprint, PCB_ACTIONS::exportFootprint.MakeEvent() );
  723. Go( &FOOTPRINT_EDITOR_CONTROL::OpenWithTextEditor, ACTIONS::openWithTextEditor.MakeEvent() );
  724. Go( &FOOTPRINT_EDITOR_CONTROL::OpenDirectory, ACTIONS::openDirectory.MakeEvent() );
  725. Go( &FOOTPRINT_EDITOR_CONTROL::ShowDatasheet, ACTIONS::showDatasheet.MakeEvent() );
  726. Go( &FOOTPRINT_EDITOR_CONTROL::EditTextAndGraphics, PCB_ACTIONS::editTextAndGraphics.MakeEvent() );
  727. Go( &FOOTPRINT_EDITOR_CONTROL::CleanupGraphics, PCB_ACTIONS::cleanupGraphics.MakeEvent() );
  728. Go( &FOOTPRINT_EDITOR_CONTROL::CheckFootprint, PCB_ACTIONS::checkFootprint.MakeEvent() );
  729. Go( &FOOTPRINT_EDITOR_CONTROL::RepairFootprint, PCB_ACTIONS::repairFootprint.MakeEvent() );
  730. Go( &FOOTPRINT_EDITOR_CONTROL::Properties, PCB_ACTIONS::footprintProperties.MakeEvent() );
  731. Go( &FOOTPRINT_EDITOR_CONTROL::DefaultPadProperties, PCB_ACTIONS::defaultPadProperties.MakeEvent() );
  732. Go( &FOOTPRINT_EDITOR_CONTROL::ToggleLayersManager, PCB_ACTIONS::showLayersManager.MakeEvent() );
  733. Go( &FOOTPRINT_EDITOR_CONTROL::ToggleProperties, ACTIONS::showProperties.MakeEvent() );
  734. // clang-format on
  735. }