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.

1010 lines
34 KiB

7 years ago
7 years ago
7 years ago
2 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 years ago
2 years ago
2 years ago
5 years ago
7 years ago
7 years ago
  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
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include "tools/symbol_editor_control.h"
  25. #include <advanced_config.h>
  26. #include <bitmaps/bitmap_types.h>
  27. #include <confirm.h>
  28. #include <dialogs/dialog_lib_fields.h>
  29. #include <gestfich.h> // To open with a text editor
  30. #include <kidialog.h>
  31. #include <kiway.h>
  32. #include <launch_ext.h> // To default when file manager setting is empty
  33. #include <lib_symbol_library_manager.h>
  34. #include <pgm_base.h>
  35. #include <sch_painter.h>
  36. #include <string_utils.h>
  37. #include <symbol_editor/symbol_editor_settings.h>
  38. #include <symbol_lib_table.h>
  39. #include <symbol_tree_model_adapter.h>
  40. #include <symbol_viewer_frame.h>
  41. #include <tool/library_editor_control.h>
  42. #include <tool/tool_manager.h>
  43. #include <tools/sch_actions.h>
  44. #include <wildcards_and_files_ext.h>
  45. #include <wx/filedlg.h>
  46. bool SYMBOL_EDITOR_CONTROL::Init()
  47. {
  48. m_frame = getEditFrame<SCH_BASE_FRAME>();
  49. m_selectionTool = m_toolMgr->GetTool<SCH_SELECTION_TOOL>();
  50. m_isSymbolEditor = m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR );
  51. if( m_isSymbolEditor )
  52. {
  53. LIBRARY_EDITOR_CONTROL* libraryTreeTool = m_toolMgr->GetTool<LIBRARY_EDITOR_CONTROL>();
  54. CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
  55. auto libSelectedCondition =
  56. [this]( const SELECTION& aSel )
  57. {
  58. if( SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>() )
  59. {
  60. LIB_ID sel = editFrame->GetTreeLIBID();
  61. return !sel.GetLibNickname().empty() && sel.GetLibItemName().empty();
  62. }
  63. return false;
  64. };
  65. // The libInferredCondition allows you to do things like New Symbol and Paste with a
  66. // symbol selected (in other words, when we know the library context even if the library
  67. // itself isn't selected.
  68. auto libInferredCondition =
  69. [this]( const SELECTION& aSel )
  70. {
  71. if( SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>() )
  72. {
  73. LIB_ID sel = editFrame->GetTreeLIBID();
  74. return !sel.GetLibNickname().empty();
  75. }
  76. return false;
  77. };
  78. auto symbolSelectedCondition =
  79. [this]( const SELECTION& aSel )
  80. {
  81. if( SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>() )
  82. {
  83. LIB_ID sel = editFrame->GetTargetLibId();
  84. return !sel.GetLibNickname().empty() && !sel.GetLibItemName().empty();
  85. }
  86. return false;
  87. };
  88. /* not used, but used to be used
  89. auto multiSelectedCondition =
  90. [this]( const SELECTION& aSel )
  91. {
  92. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  93. return editFrame && editFrame->GetTreeSelectionCount() > 1;
  94. };
  95. */
  96. auto multiSymbolSelectedCondition =
  97. [this]( const SELECTION& aSel )
  98. {
  99. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  100. if( editFrame && editFrame->GetTreeSelectionCount() > 1 )
  101. {
  102. for( LIB_ID& sel : editFrame->GetSelectedLibIds() )
  103. {
  104. if( !sel.IsValid() )
  105. return false;
  106. }
  107. return true;
  108. }
  109. return false;
  110. };
  111. /* not used, yet
  112. auto multiLibrarySelectedCondition =
  113. [this]( const SELECTION& aSel )
  114. {
  115. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  116. if( editFrame && editFrame->GetTreeSelectionCount() > 1 )
  117. {
  118. for( LIB_ID& sel : editFrame->GetSelectedLibIds() )
  119. {
  120. if( sel.IsValid() )
  121. return false;
  122. }
  123. return true;
  124. }
  125. return false;
  126. };
  127. */
  128. auto canOpenExternally =
  129. [this]( const SELECTION& aSel )
  130. {
  131. // The option is shown if the lib has no current edits
  132. if( SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>() )
  133. {
  134. LIB_SYMBOL_LIBRARY_MANAGER& libMgr = editFrame->GetLibManager();
  135. wxString libName = editFrame->GetTargetLibId().GetLibNickname();
  136. return !libMgr.IsLibraryModified( libName );
  137. }
  138. return false;
  139. };
  140. auto librarySelectedCondition = [this]( const SELECTION& aSel ) -> bool
  141. {
  142. bool result = false;
  143. LIB_TREE* libTree = m_frame->GetLibTree();
  144. if( libTree )
  145. {
  146. std::vector<LIB_TREE_NODE*> selection;
  147. libTree->GetSelectedTreeNodes( selection );
  148. if( selection.size() == 1 )
  149. {
  150. const LIB_TREE_NODE* lib = selection[0];
  151. if( lib && lib->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
  152. {
  153. result = true;
  154. }
  155. }
  156. }
  157. return result;
  158. };
  159. // clang-format off
  160. ctxMenu.AddItem( SCH_ACTIONS::newSymbol, libInferredCondition, 10 );
  161. ctxMenu.AddItem( SCH_ACTIONS::deriveFromExistingSymbol, symbolSelectedCondition, 10 );
  162. ctxMenu.AddSeparator( 10 );
  163. ctxMenu.AddItem( ACTIONS::save, symbolSelectedCondition || libInferredCondition, 10 );
  164. ctxMenu.AddItem( SCH_ACTIONS::saveLibraryAs, libSelectedCondition, 10 );
  165. ctxMenu.AddItem( SCH_ACTIONS::saveSymbolAs, symbolSelectedCondition, 10 );
  166. ctxMenu.AddItem( SCH_ACTIONS::saveSymbolCopyAs, symbolSelectedCondition, 10 );
  167. ctxMenu.AddItem( ACTIONS::revert, symbolSelectedCondition || libInferredCondition, 10 );
  168. ctxMenu.AddSeparator( 10 );
  169. ctxMenu.AddItem( SCH_ACTIONS::cutSymbol, symbolSelectedCondition || multiSymbolSelectedCondition, 10 );
  170. ctxMenu.AddItem( SCH_ACTIONS::copySymbol, symbolSelectedCondition || multiSymbolSelectedCondition, 10 );
  171. ctxMenu.AddItem( SCH_ACTIONS::pasteSymbol, libInferredCondition, 10 );
  172. ctxMenu.AddItem( SCH_ACTIONS::duplicateSymbol, symbolSelectedCondition, 10 );
  173. ctxMenu.AddItem( SCH_ACTIONS::renameSymbol, symbolSelectedCondition, 10 );
  174. ctxMenu.AddItem( SCH_ACTIONS::deleteSymbol, symbolSelectedCondition || multiSymbolSelectedCondition, 10 );
  175. ctxMenu.AddSeparator( 100 );
  176. ctxMenu.AddItem( SCH_ACTIONS::importSymbol, libInferredCondition, 100 );
  177. ctxMenu.AddItem( SCH_ACTIONS::exportSymbol, symbolSelectedCondition );
  178. if( ADVANCED_CFG::GetCfg().m_EnableLibWithText )
  179. {
  180. ctxMenu.AddSeparator( 200 );
  181. ctxMenu.AddItem( ACTIONS::openWithTextEditor, canOpenExternally && ( symbolSelectedCondition || libSelectedCondition ), 200 );
  182. }
  183. if( ADVANCED_CFG::GetCfg().m_EnableLibDir )
  184. {
  185. ctxMenu.AddSeparator( 200 );
  186. ctxMenu.AddItem( ACTIONS::openDirectory, canOpenExternally && ( symbolSelectedCondition || libSelectedCondition ), 200 );
  187. }
  188. ctxMenu.AddItem( ACTIONS::showLibraryTable, librarySelectedCondition, 300 );
  189. libraryTreeTool->AddContextMenuItems( &ctxMenu );
  190. }
  191. // clang-format on
  192. return true;
  193. }
  194. int SYMBOL_EDITOR_CONTROL::AddLibrary( const TOOL_EVENT& aEvent )
  195. {
  196. bool createNew = aEvent.IsAction( &ACTIONS::newLibrary );
  197. if( m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
  198. static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->AddLibraryFile( createNew );
  199. return 0;
  200. }
  201. int SYMBOL_EDITOR_CONTROL::DdAddLibrary( const TOOL_EVENT& aEvent )
  202. {
  203. wxString libFile = *aEvent.Parameter<wxString*>();
  204. if( m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
  205. static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->DdAddLibrary( libFile );
  206. return 0;
  207. }
  208. int SYMBOL_EDITOR_CONTROL::EditSymbol( const TOOL_EVENT& aEvent )
  209. {
  210. if( !m_isSymbolEditor )
  211. return 0;
  212. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  213. int unit = 0;
  214. LIB_ID partId = editFrame->GetTreeLIBID( &unit );
  215. editFrame->LoadSymbol( partId.GetLibItemName(), partId.GetLibNickname(), unit );
  216. return 0;
  217. }
  218. int SYMBOL_EDITOR_CONTROL::EditLibrarySymbol( const TOOL_EVENT& aEvent )
  219. {
  220. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  221. const LIB_SYMBOL* symbol = editFrame->GetCurSymbol();
  222. if( !symbol || !editFrame->IsSymbolFromSchematic() )
  223. {
  224. wxBell();
  225. return 0;
  226. }
  227. const LIB_ID& libId = symbol->GetLibId();
  228. if( editFrame->LoadSymbol( libId, editFrame->GetUnit(), editFrame->GetBodyStyle() ) )
  229. {
  230. if( !editFrame->IsLibraryTreeShown() )
  231. editFrame->ToggleLibraryTree();
  232. }
  233. else
  234. {
  235. const wxString libName = libId.GetLibNickname();
  236. const wxString symbolName = libId.GetLibItemName();
  237. DisplayError( editFrame,
  238. wxString::Format( _( "Failed to load symbol %s from "
  239. "library %s." ),
  240. UnescapeString( symbolName ), UnescapeString( libName ) ) );
  241. }
  242. return 0;
  243. }
  244. int SYMBOL_EDITOR_CONTROL::AddSymbol( const TOOL_EVENT& aEvent )
  245. {
  246. if( !m_isSymbolEditor )
  247. return 0;
  248. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  249. LIB_ID target = editFrame->GetTargetLibId();
  250. const wxString& libName = target.GetLibNickname();
  251. wxString msg;
  252. if( libName.IsEmpty() )
  253. {
  254. msg.Printf( _( "No symbol library selected." ) );
  255. m_frame->ShowInfoBarError( msg );
  256. return 0;
  257. }
  258. if( !editFrame->GetLibManager().LibraryExists( libName ) )
  259. {
  260. msg.Printf( _( "Symbol library '%s' not found." ), libName );
  261. m_frame->ShowInfoBarError( msg );
  262. return 0;
  263. }
  264. if( editFrame->GetLibManager().IsLibraryReadOnly( libName ) )
  265. {
  266. msg.Printf( _( "Symbol library '%s' is not writable." ), libName );
  267. m_frame->ShowInfoBarError( msg );
  268. return 0;
  269. }
  270. if( aEvent.IsAction( &SCH_ACTIONS::newSymbol ) )
  271. editFrame->CreateNewSymbol();
  272. else if( aEvent.IsAction( &SCH_ACTIONS::deriveFromExistingSymbol ) )
  273. editFrame->CreateNewSymbol( target.GetLibItemName() );
  274. else if( aEvent.IsAction( &SCH_ACTIONS::importSymbol ) )
  275. editFrame->ImportSymbol();
  276. return 0;
  277. }
  278. int SYMBOL_EDITOR_CONTROL::Save( const TOOL_EVENT& aEvt )
  279. {
  280. if( !m_isSymbolEditor )
  281. return 0;
  282. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  283. if( aEvt.IsAction( &SCH_ACTIONS::save ) )
  284. editFrame->Save();
  285. else if( aEvt.IsAction( &SCH_ACTIONS::saveLibraryAs ) )
  286. editFrame->SaveLibraryAs();
  287. else if( aEvt.IsAction( &SCH_ACTIONS::saveSymbolAs ) )
  288. editFrame->SaveSymbolCopyAs( true );
  289. else if( aEvt.IsAction( &SCH_ACTIONS::saveSymbolCopyAs ) )
  290. editFrame->SaveSymbolCopyAs( false );
  291. else if( aEvt.IsAction( &SCH_ACTIONS::saveAll ) )
  292. editFrame->SaveAll();
  293. return 0;
  294. }
  295. int SYMBOL_EDITOR_CONTROL::Revert( const TOOL_EVENT& aEvent )
  296. {
  297. if( m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
  298. static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->Revert();
  299. return 0;
  300. }
  301. int SYMBOL_EDITOR_CONTROL::OpenDirectory( const TOOL_EVENT& aEvent )
  302. {
  303. if( !m_isSymbolEditor )
  304. return 0;
  305. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  306. LIB_SYMBOL_LIBRARY_MANAGER& libMgr = editFrame->GetLibManager();
  307. LIB_ID libId = editFrame->GetTreeLIBID();
  308. wxString libName = libId.GetLibNickname();
  309. wxString libItemName = libMgr.GetLibrary( libName )->GetFullURI( true );
  310. wxFileName fileName( libItemName );
  311. wxString filePath = wxEmptyString;
  312. COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
  313. wxString explCommand = cfg->m_System.file_explorer;
  314. if( explCommand.IsEmpty() )
  315. {
  316. filePath = fileName.GetFullPath().BeforeLast( wxFileName::GetPathSeparator() );
  317. if( !filePath.IsEmpty() && wxDirExists( filePath ) )
  318. LaunchExternal( filePath );
  319. return 0;
  320. }
  321. if( !explCommand.EndsWith( "%F" ) )
  322. {
  323. wxMessageBox( _( "Missing/malformed file explorer argument '%F' in common settings." ) );
  324. return 0;
  325. }
  326. filePath = fileName.GetFullPath();
  327. filePath.Replace( wxS( "\"" ), wxS( "_" ) );
  328. wxString fileArg = '"' + filePath + '"';
  329. explCommand.Replace( wxT( "%F" ), fileArg );
  330. if( !explCommand.IsEmpty() )
  331. wxExecute( explCommand );
  332. return 0;
  333. }
  334. int SYMBOL_EDITOR_CONTROL::OpenWithTextEditor( const TOOL_EVENT& aEvent )
  335. {
  336. if( !m_isSymbolEditor )
  337. return 0;
  338. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  339. LIB_SYMBOL_LIBRARY_MANAGER& libMgr = editFrame->GetLibManager();
  340. wxString textEditorName = Pgm().GetTextEditor();
  341. if( textEditorName.IsEmpty() )
  342. {
  343. wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
  344. return 0;
  345. }
  346. LIB_ID libId = editFrame->GetTreeLIBID();
  347. wxString libName = libId.GetLibNickname();
  348. wxString tempFName = libMgr.GetLibrary( libName )->GetFullURI( true ).wc_str();
  349. if( !tempFName.IsEmpty() )
  350. ExecuteFile( textEditorName, tempFName, nullptr, false );
  351. return 0;
  352. }
  353. int SYMBOL_EDITOR_CONTROL::CutCopyDelete( const TOOL_EVENT& aEvt )
  354. {
  355. if( !m_isSymbolEditor )
  356. return 0;
  357. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  358. if( aEvt.IsAction( &SCH_ACTIONS::cutSymbol ) || aEvt.IsAction( &SCH_ACTIONS::copySymbol ) )
  359. editFrame->CopySymbolToClipboard();
  360. if( aEvt.IsAction( &SCH_ACTIONS::cutSymbol ) || aEvt.IsAction( &SCH_ACTIONS::deleteSymbol ) )
  361. {
  362. bool hasWritableLibs = false;
  363. wxString msg;
  364. for( LIB_ID& sel : editFrame->GetSelectedLibIds() )
  365. {
  366. const wxString& libName = sel.GetLibNickname();
  367. if( !editFrame->GetLibManager().LibraryExists( libName ) )
  368. msg.Printf( _( "Symbol library '%s' not found." ), libName );
  369. else if( editFrame->GetLibManager().IsLibraryReadOnly( libName ) )
  370. msg.Printf( _( "Symbol library '%s' is not writable." ), libName );
  371. else
  372. hasWritableLibs = true;
  373. }
  374. if( !msg.IsEmpty() )
  375. m_frame->ShowInfoBarError( msg );
  376. if( !hasWritableLibs )
  377. return 0;
  378. editFrame->DeleteSymbolFromLibrary();
  379. }
  380. return 0;
  381. }
  382. int SYMBOL_EDITOR_CONTROL::DuplicateSymbol( const TOOL_EVENT& aEvent )
  383. {
  384. if( !m_isSymbolEditor )
  385. return 0;
  386. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  387. LIB_ID sel = editFrame->GetTargetLibId();
  388. // DuplicateSymbol() is called to duplicate a symbol, or to paste a previously
  389. // saved symbol in clipboard
  390. bool isPasteAction = aEvent.IsAction( &SCH_ACTIONS::pasteSymbol );
  391. wxString msg;
  392. if( !sel.IsValid() && !isPasteAction )
  393. {
  394. // When duplicating a symbol, a source symbol must exists.
  395. msg.Printf( _( "No symbol selected" ) );
  396. m_frame->ShowInfoBarError( msg );
  397. return 0;
  398. }
  399. const wxString& libName = sel.GetLibNickname();
  400. if( !editFrame->GetLibManager().LibraryExists( libName ) )
  401. {
  402. msg.Printf( _( "Symbol library '%s' not found." ), libName );
  403. m_frame->ShowInfoBarError( msg );
  404. return 0;
  405. }
  406. if( editFrame->GetLibManager().IsLibraryReadOnly( libName ) )
  407. {
  408. msg.Printf( _( "Symbol library '%s' is not writable." ), libName );
  409. m_frame->ShowInfoBarError( msg );
  410. return 0;
  411. }
  412. editFrame->DuplicateSymbol( isPasteAction );
  413. return 0;
  414. }
  415. int SYMBOL_EDITOR_CONTROL::RenameSymbol( const TOOL_EVENT& aEvent )
  416. {
  417. if( !m_isSymbolEditor )
  418. return 0;
  419. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  420. LIB_SYMBOL_LIBRARY_MANAGER& libMgr = editFrame->GetLibManager();
  421. LIBRARY_EDITOR_CONTROL* libTool = m_toolMgr->GetTool<LIBRARY_EDITOR_CONTROL>();
  422. LIB_ID libId = editFrame->GetTreeLIBID();
  423. wxString libName = libId.GetLibNickname();
  424. wxString oldName = libId.GetLibItemName();
  425. wxString newName;
  426. wxString msg;
  427. if( !libMgr.LibraryExists( libName ) )
  428. return 0;
  429. if( !libTool->RenameLibrary( _( "Change Symbol Name" ), oldName,
  430. [&]( const wxString& aNewName )
  431. {
  432. newName = EscapeString( aNewName, CTX_LIBID );
  433. if( newName.IsEmpty() )
  434. {
  435. wxMessageBox( _( "Symbol must have a name." ) );
  436. return false;
  437. }
  438. // If no change, accept it without prompting
  439. if( newName != oldName && libMgr.SymbolExists( newName, libName ) )
  440. {
  441. msg = wxString::Format( _( "Symbol '%s' already exists in library '%s'." ),
  442. newName, libName );
  443. KIDIALOG errorDlg( m_frame, msg, _( "Confirmation" ),
  444. wxOK | wxCANCEL | wxICON_WARNING );
  445. errorDlg.SetOKLabel( _( "Overwrite" ) );
  446. return errorDlg.ShowModal() == wxID_OK;
  447. }
  448. return true;
  449. } ) )
  450. {
  451. return 0; // cancelled by user
  452. }
  453. if( newName == oldName )
  454. return 0;
  455. LIB_SYMBOL* libSymbol = libMgr.GetBufferedSymbol( oldName, libName );
  456. if( !libSymbol )
  457. return 0;
  458. // Renaming the current symbol
  459. const bool isCurrentSymbol = editFrame->IsCurrentSymbol( libId );
  460. bool overwritingCurrentSymbol = false;
  461. if( libMgr.SymbolExists( newName, libName ) )
  462. {
  463. // Overwriting the current symbol also need to update the open symbol
  464. LIB_SYMBOL* const overwrittenSymbol = libMgr.GetBufferedSymbol( newName, libName );
  465. overwritingCurrentSymbol = editFrame->IsCurrentSymbol( overwrittenSymbol->GetLibId() );
  466. libMgr.RemoveSymbol( newName, libName );
  467. }
  468. libSymbol->SetName( newName );
  469. if( libSymbol->GetFieldById( VALUE_FIELD )->GetText() == oldName )
  470. libSymbol->GetFieldById( VALUE_FIELD )->SetText( newName );
  471. libMgr.UpdateSymbolAfterRename( libSymbol, newName, libName );
  472. libMgr.SetSymbolModified( newName, libName );
  473. if( overwritingCurrentSymbol )
  474. {
  475. // We overwrite the old current symbol with the renamed one, so show
  476. // the renamed one now
  477. editFrame->SetCurSymbol( new LIB_SYMBOL( *libSymbol ), false );
  478. }
  479. else if( isCurrentSymbol && editFrame->GetCurSymbol() )
  480. {
  481. // Renamed the current symbol - follow it
  482. libSymbol = editFrame->GetCurSymbol();
  483. libSymbol->SetName( newName );
  484. if( libSymbol->GetFieldById( VALUE_FIELD )->GetText() == oldName )
  485. libSymbol->GetFieldById( VALUE_FIELD )->SetText( newName );
  486. editFrame->RebuildView();
  487. editFrame->OnModify();
  488. editFrame->UpdateTitle();
  489. // N.B. The view needs to be rebuilt first as the Symbol Properties change may
  490. // invalidate the view pointers by rebuilting the field table
  491. editFrame->UpdateMsgPanel();
  492. }
  493. wxDataViewItem treeItem = libMgr.GetAdapter()->FindItem( libId );
  494. editFrame->UpdateLibraryTree( treeItem, libSymbol );
  495. editFrame->FocusOnLibId( LIB_ID( libName, newName ) );
  496. return 0;
  497. }
  498. int SYMBOL_EDITOR_CONTROL::OnDeMorgan( const TOOL_EVENT& aEvent )
  499. {
  500. int bodyStyle = aEvent.IsAction( &SCH_ACTIONS::showDeMorganStandard ) ? BODY_STYLE::BASE
  501. : BODY_STYLE::DEMORGAN;
  502. if( m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
  503. {
  504. m_toolMgr->RunAction( ACTIONS::cancelInteractive );
  505. m_toolMgr->RunAction( SCH_ACTIONS::clearSelection );
  506. SYMBOL_EDIT_FRAME* symbolEditor = static_cast<SYMBOL_EDIT_FRAME*>( m_frame );
  507. symbolEditor->SetBodyStyle( bodyStyle );
  508. m_toolMgr->ResetTools( TOOL_BASE::MODEL_RELOAD );
  509. symbolEditor->RebuildView();
  510. }
  511. else if( m_frame->IsType( FRAME_SCH_VIEWER ) )
  512. {
  513. SYMBOL_VIEWER_FRAME* symbolViewer = static_cast<SYMBOL_VIEWER_FRAME*>( m_frame );
  514. symbolViewer->SetUnitAndBodyStyle( symbolViewer->GetUnit(), bodyStyle );
  515. }
  516. return 0;
  517. }
  518. int SYMBOL_EDITOR_CONTROL::ToggleProperties( const TOOL_EVENT& aEvent )
  519. {
  520. if( m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
  521. getEditFrame<SYMBOL_EDIT_FRAME>()->ToggleProperties();
  522. return 0;
  523. }
  524. int SYMBOL_EDITOR_CONTROL::ShowElectricalTypes( const TOOL_EVENT& aEvent )
  525. {
  526. SCH_RENDER_SETTINGS* renderSettings = m_frame->GetRenderSettings();
  527. renderSettings->m_ShowPinsElectricalType = !renderSettings->m_ShowPinsElectricalType;
  528. // Update canvas
  529. m_frame->GetCanvas()->GetView()->UpdateAllItems( KIGFX::REPAINT );
  530. m_frame->GetCanvas()->Refresh();
  531. return 0;
  532. }
  533. int SYMBOL_EDITOR_CONTROL::ShowPinNumbers( const TOOL_EVENT& aEvent )
  534. {
  535. SCH_RENDER_SETTINGS* renderSettings = m_frame->GetRenderSettings();
  536. renderSettings->m_ShowPinNumbers = !renderSettings->m_ShowPinNumbers;
  537. // Update canvas
  538. m_frame->GetCanvas()->GetView()->UpdateAllItems( KIGFX::REPAINT );
  539. m_frame->GetCanvas()->Refresh();
  540. return 0;
  541. }
  542. int SYMBOL_EDITOR_CONTROL::ToggleSyncedPinsMode( const TOOL_EVENT& aEvent )
  543. {
  544. if( !m_isSymbolEditor )
  545. return 0;
  546. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  547. editFrame->m_SyncPinEdit = !editFrame->m_SyncPinEdit;
  548. return 0;
  549. }
  550. int SYMBOL_EDITOR_CONTROL::ToggleHiddenPins( const TOOL_EVENT& aEvent )
  551. {
  552. if( !m_isSymbolEditor )
  553. return 0;
  554. SYMBOL_EDITOR_SETTINGS* cfg = m_frame->libeditconfig();
  555. cfg->m_ShowHiddenPins = !cfg->m_ShowHiddenPins;
  556. getEditFrame<SYMBOL_EDIT_FRAME>()->GetRenderSettings()->m_ShowHiddenPins =
  557. cfg->m_ShowHiddenPins;
  558. getView()->UpdateAllItems( KIGFX::REPAINT );
  559. m_frame->GetCanvas()->Refresh();
  560. return 0;
  561. }
  562. int SYMBOL_EDITOR_CONTROL::ToggleHiddenFields( const TOOL_EVENT& aEvent )
  563. {
  564. if( !m_isSymbolEditor )
  565. return 0;
  566. SYMBOL_EDITOR_SETTINGS* cfg = m_frame->libeditconfig();
  567. cfg->m_ShowHiddenFields = !cfg->m_ShowHiddenFields;
  568. // TODO: Why is this needed in symbol edit and not in schematic edit?
  569. getEditFrame<SYMBOL_EDIT_FRAME>()->GetRenderSettings()->m_ShowHiddenFields =
  570. cfg->m_ShowHiddenFields;
  571. getView()->UpdateAllItems( KIGFX::REPAINT );
  572. m_frame->GetCanvas()->Refresh();
  573. return 0;
  574. }
  575. int SYMBOL_EDITOR_CONTROL::TogglePinAltIcons( const TOOL_EVENT& aEvent )
  576. {
  577. if( !m_isSymbolEditor )
  578. return 0;
  579. SYMBOL_EDITOR_SETTINGS& cfg = *m_frame->libeditconfig();
  580. cfg.m_ShowPinAltIcons = !cfg.m_ShowPinAltIcons;
  581. m_frame->GetRenderSettings()->m_ShowPinAltIcons = cfg.m_ShowPinAltIcons;
  582. getView()->UpdateAllItems( KIGFX::REPAINT );
  583. m_frame->GetCanvas()->Refresh();
  584. return 0;
  585. }
  586. int SYMBOL_EDITOR_CONTROL::ExportView( const TOOL_EVENT& aEvent )
  587. {
  588. if( !m_isSymbolEditor )
  589. return 0;
  590. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  591. LIB_SYMBOL* symbol = editFrame->GetCurSymbol();
  592. if( !symbol )
  593. {
  594. wxMessageBox( _( "No symbol to export" ) );
  595. return 0;
  596. }
  597. wxFileName fn( symbol->GetName() );
  598. fn.SetExt( "png" );
  599. wxString projectPath = wxPathOnly( m_frame->Prj().GetProjectFullName() );
  600. wxFileDialog dlg( editFrame, _( "Export View as PNG" ), projectPath, fn.GetFullName(),
  601. FILEEXT::PngFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  602. if( dlg.ShowModal() == wxID_OK && !dlg.GetPath().IsEmpty() )
  603. {
  604. // calling wxYield is mandatory under Linux, after closing the file selector dialog
  605. // to refresh the screen before creating the PNG or JPEG image from screen
  606. wxYield();
  607. if( !editFrame->SaveCanvasImageToFile( dlg.GetPath(), BITMAP_TYPE::PNG ) )
  608. {
  609. wxMessageBox( wxString::Format( _( "Can't save file '%s'." ), dlg.GetPath() ) );
  610. }
  611. }
  612. return 0;
  613. }
  614. int SYMBOL_EDITOR_CONTROL::ExportSymbol( const TOOL_EVENT& aEvent )
  615. {
  616. if( m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
  617. static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->ExportSymbol();
  618. return 0;
  619. }
  620. int SYMBOL_EDITOR_CONTROL::ExportSymbolAsSVG( const TOOL_EVENT& aEvent )
  621. {
  622. if( !m_isSymbolEditor )
  623. return 0;
  624. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  625. LIB_SYMBOL* symbol = editFrame->GetCurSymbol();
  626. if( !symbol )
  627. {
  628. wxMessageBox( _( "No symbol to export" ) );
  629. return 0;
  630. }
  631. wxFileName fn( symbol->GetName() );
  632. fn.SetExt( FILEEXT::SVGFileExtension );
  633. wxString pro_dir = wxPathOnly( m_frame->Prj().GetProjectFullName() );
  634. wxString fullFileName = wxFileSelector( _( "SVG File Name" ), pro_dir, fn.GetFullName(),
  635. FILEEXT::SVGFileExtension, FILEEXT::SVGFileWildcard(),
  636. wxFD_SAVE,
  637. m_frame );
  638. if( !fullFileName.IsEmpty() )
  639. {
  640. PAGE_INFO pageSave = editFrame->GetScreen()->GetPageSettings();
  641. PAGE_INFO pageTemp = pageSave;
  642. BOX2I symbolBBox = symbol->GetUnitBoundingBox( editFrame->GetUnit(),
  643. editFrame->GetBodyStyle(), false );
  644. // Add a small margin (10% of size)to the plot bounding box
  645. symbolBBox.Inflate( symbolBBox.GetSize().x * 0.1, symbolBBox.GetSize().y * 0.1 );
  646. pageTemp.SetWidthMils( schIUScale.IUToMils( symbolBBox.GetSize().x ) );
  647. pageTemp.SetHeightMils( schIUScale.IUToMils( symbolBBox.GetSize().y ) );
  648. // Add an offet to plot the symbol centered on the page.
  649. VECTOR2I plot_offset = symbolBBox.GetOrigin();
  650. editFrame->GetScreen()->SetPageSettings( pageTemp );
  651. editFrame->SVGPlotSymbol( fullFileName, -plot_offset );
  652. editFrame->GetScreen()->SetPageSettings( pageSave );
  653. }
  654. return 0;
  655. }
  656. int SYMBOL_EDITOR_CONTROL::AddSymbolToSchematic( const TOOL_EVENT& aEvent )
  657. {
  658. LIB_SYMBOL* libSymbol = nullptr;
  659. LIB_ID libId;
  660. int unit, bodyStyle;
  661. if( m_isSymbolEditor )
  662. {
  663. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  664. libSymbol = editFrame->GetCurSymbol();
  665. unit = editFrame->GetUnit();
  666. bodyStyle = editFrame->GetBodyStyle();
  667. if( libSymbol )
  668. libId = libSymbol->GetLibId();
  669. }
  670. else
  671. {
  672. SYMBOL_VIEWER_FRAME* viewerFrame = getEditFrame<SYMBOL_VIEWER_FRAME>();
  673. libSymbol = viewerFrame->GetSelectedSymbol();
  674. unit = viewerFrame->GetUnit();
  675. bodyStyle = viewerFrame->GetBodyStyle();
  676. if( libSymbol )
  677. libId = libSymbol->GetLibId();
  678. }
  679. if( libSymbol )
  680. {
  681. SCH_EDIT_FRAME* schframe = (SCH_EDIT_FRAME*) m_frame->Kiway().Player( FRAME_SCH, false );
  682. if( !schframe ) // happens when the schematic editor is not active (or closed)
  683. {
  684. DisplayErrorMessage( m_frame, _( "No schematic currently open." ) );
  685. return 0;
  686. }
  687. wxWindow* blocking_dialog = schframe->Kiway().GetBlockingDialog();
  688. if( blocking_dialog )
  689. {
  690. blocking_dialog->Raise();
  691. wxBell();
  692. return 0;
  693. }
  694. SCH_SYMBOL* symbol = new SCH_SYMBOL( *libSymbol, libId, &schframe->GetCurrentSheet(),
  695. unit, bodyStyle );
  696. symbol->SetParent( schframe->GetScreen() );
  697. if( schframe->eeconfig()->m_AutoplaceFields.enable )
  698. {
  699. // Not placed yet, so pass a nullptr screen reference
  700. symbol->AutoplaceFields( nullptr, AUTOPLACE_AUTO );
  701. }
  702. schframe->Raise();
  703. schframe->GetToolManager()->PostAction( SCH_ACTIONS::placeSymbol,
  704. SCH_ACTIONS::PLACE_SYMBOL_PARAMS{ symbol, true } );
  705. }
  706. return 0;
  707. }
  708. int SYMBOL_EDITOR_CONTROL::ChangeUnit( const TOOL_EVENT& aEvent )
  709. {
  710. if( !m_isSymbolEditor )
  711. return 0;
  712. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  713. const int deltaUnit = aEvent.Parameter<int>();
  714. const int nUnits = editFrame->GetCurSymbol()->GetUnitCount();
  715. const int newUnit = ( ( editFrame->GetUnit() - 1 + deltaUnit + nUnits ) % nUnits ) + 1;
  716. editFrame->SetUnit( newUnit );
  717. return 0;
  718. }
  719. int SYMBOL_EDITOR_CONTROL::ShowLibraryTable( const TOOL_EVENT& aEvent )
  720. {
  721. SYMBOL_EDIT_FRAME* editFrame = getEditFrame<SYMBOL_EDIT_FRAME>();
  722. wxString libName = editFrame->GetTreeLIBID().GetLibNickname();
  723. // Get all symbol names from the library manager
  724. wxArrayString symbolNames;
  725. editFrame->GetLibManager().GetSymbolNames( libName, symbolNames );
  726. DIALOG_LIB_FIELDS dlg( editFrame, libName, symbolNames );
  727. dlg.SetTitle( _( "Library Fields" ) );
  728. dlg.ShowModal();
  729. return 0;
  730. }
  731. void SYMBOL_EDITOR_CONTROL::setTransitions()
  732. {
  733. // clang-format off
  734. Go( &SYMBOL_EDITOR_CONTROL::AddLibrary, ACTIONS::newLibrary.MakeEvent() );
  735. Go( &SYMBOL_EDITOR_CONTROL::AddLibrary, ACTIONS::addLibrary.MakeEvent() );
  736. Go( &SYMBOL_EDITOR_CONTROL::AddSymbol, SCH_ACTIONS::newSymbol.MakeEvent() );
  737. Go( &SYMBOL_EDITOR_CONTROL::AddSymbol, SCH_ACTIONS::deriveFromExistingSymbol.MakeEvent() );
  738. Go( &SYMBOL_EDITOR_CONTROL::AddSymbol, SCH_ACTIONS::importSymbol.MakeEvent() );
  739. Go( &SYMBOL_EDITOR_CONTROL::EditSymbol, SCH_ACTIONS::editSymbol.MakeEvent() );
  740. Go( &SYMBOL_EDITOR_CONTROL::EditLibrarySymbol, SCH_ACTIONS::editLibSymbolWithLibEdit.MakeEvent() );
  741. Go( &SYMBOL_EDITOR_CONTROL::DdAddLibrary, ACTIONS::ddAddLibrary.MakeEvent() );
  742. Go( &SYMBOL_EDITOR_CONTROL::Save, ACTIONS::save.MakeEvent() );
  743. Go( &SYMBOL_EDITOR_CONTROL::Save, SCH_ACTIONS::saveLibraryAs.MakeEvent() );
  744. Go( &SYMBOL_EDITOR_CONTROL::Save, SCH_ACTIONS::saveSymbolAs.MakeEvent() );
  745. Go( &SYMBOL_EDITOR_CONTROL::Save, SCH_ACTIONS::saveSymbolCopyAs.MakeEvent() );
  746. Go( &SYMBOL_EDITOR_CONTROL::Save, ACTIONS::saveAll.MakeEvent() );
  747. Go( &SYMBOL_EDITOR_CONTROL::Revert, ACTIONS::revert.MakeEvent() );
  748. Go( &SYMBOL_EDITOR_CONTROL::DuplicateSymbol, SCH_ACTIONS::duplicateSymbol.MakeEvent() );
  749. Go( &SYMBOL_EDITOR_CONTROL::RenameSymbol, SCH_ACTIONS::renameSymbol.MakeEvent() );
  750. Go( &SYMBOL_EDITOR_CONTROL::CutCopyDelete, SCH_ACTIONS::deleteSymbol.MakeEvent() );
  751. Go( &SYMBOL_EDITOR_CONTROL::CutCopyDelete, SCH_ACTIONS::cutSymbol.MakeEvent() );
  752. Go( &SYMBOL_EDITOR_CONTROL::CutCopyDelete, SCH_ACTIONS::copySymbol.MakeEvent() );
  753. Go( &SYMBOL_EDITOR_CONTROL::DuplicateSymbol, SCH_ACTIONS::pasteSymbol.MakeEvent() );
  754. Go( &SYMBOL_EDITOR_CONTROL::ExportSymbol, SCH_ACTIONS::exportSymbol.MakeEvent() );
  755. Go( &SYMBOL_EDITOR_CONTROL::OpenWithTextEditor, ACTIONS::openWithTextEditor.MakeEvent() );
  756. Go( &SYMBOL_EDITOR_CONTROL::OpenDirectory, ACTIONS::openDirectory.MakeEvent() );
  757. Go( &SYMBOL_EDITOR_CONTROL::ExportView, SCH_ACTIONS::exportSymbolView.MakeEvent() );
  758. Go( &SYMBOL_EDITOR_CONTROL::ExportSymbolAsSVG, SCH_ACTIONS::exportSymbolAsSVG.MakeEvent() );
  759. Go( &SYMBOL_EDITOR_CONTROL::AddSymbolToSchematic, SCH_ACTIONS::addSymbolToSchematic.MakeEvent() );
  760. Go( &SYMBOL_EDITOR_CONTROL::OnDeMorgan, SCH_ACTIONS::showDeMorganStandard.MakeEvent() );
  761. Go( &SYMBOL_EDITOR_CONTROL::OnDeMorgan, SCH_ACTIONS::showDeMorganAlternate.MakeEvent() );
  762. Go( &SYMBOL_EDITOR_CONTROL::ShowElectricalTypes, SCH_ACTIONS::showElectricalTypes.MakeEvent() );
  763. Go( &SYMBOL_EDITOR_CONTROL::ShowPinNumbers, SCH_ACTIONS::showPinNumbers.MakeEvent() );
  764. Go( &SYMBOL_EDITOR_CONTROL::ToggleSyncedPinsMode, SCH_ACTIONS::toggleSyncedPinsMode.MakeEvent() );
  765. Go( &SYMBOL_EDITOR_CONTROL::ToggleProperties, ACTIONS::showProperties.MakeEvent() );
  766. Go( &SYMBOL_EDITOR_CONTROL::ToggleHiddenPins, SCH_ACTIONS::showHiddenPins.MakeEvent() );
  767. Go( &SYMBOL_EDITOR_CONTROL::ToggleHiddenFields, SCH_ACTIONS::showHiddenFields.MakeEvent() );
  768. Go( &SYMBOL_EDITOR_CONTROL::TogglePinAltIcons, SCH_ACTIONS::togglePinAltIcons.MakeEvent() );
  769. Go( &SYMBOL_EDITOR_CONTROL::ShowLibraryTable, ACTIONS::showLibraryTable.MakeEvent() );
  770. Go( &SYMBOL_EDITOR_CONTROL::ChangeUnit, SCH_ACTIONS::previousUnit.MakeEvent() );
  771. Go( &SYMBOL_EDITOR_CONTROL::ChangeUnit, SCH_ACTIONS::nextUnit.MakeEvent() );
  772. // clang-format on
  773. }