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.

1542 lines
47 KiB

Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
7 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) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 2004-2022 KiCad Developers, see AUTHORS.txt for contributors.
  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 <bitmaps.h>
  26. #include <wx/hyperlink.h>
  27. #include <base_screen.h>
  28. #include <symbol_library.h>
  29. #include <confirm.h>
  30. #include <core/kicad_algo.h>
  31. #include <eeschema_id.h>
  32. #include <eeschema_settings.h>
  33. #include <env_paths.h>
  34. #include <kiface_base.h>
  35. #include <kiplatform/app.h>
  36. #include <kiway_express.h>
  37. #include <symbol_edit_frame.h>
  38. #include <symbol_library_manager.h>
  39. #include <lib_text.h>
  40. #include <symbol_editor_settings.h>
  41. #include <paths.h>
  42. #include <pgm_base.h>
  43. #include <sch_painter.h>
  44. #include <sch_view.h>
  45. #include <settings/settings_manager.h>
  46. #include <symbol_lib_table.h>
  47. #include <tool/action_manager.h>
  48. #include <tool/action_toolbar.h>
  49. #include <tool/common_control.h>
  50. #include <tool/common_tools.h>
  51. #include <tool/editor_conditions.h>
  52. #include <tool/picker_tool.h>
  53. #include <tool/selection.h>
  54. #include <tool/tool_dispatcher.h>
  55. #include <tool/tool_manager.h>
  56. #include <tool/zoom_tool.h>
  57. #include <tools/ee_actions.h>
  58. #include <tools/ee_inspection_tool.h>
  59. #include <tools/ee_point_editor.h>
  60. #include <tools/ee_selection_tool.h>
  61. #include <tools/symbol_editor_control.h>
  62. #include <tools/symbol_editor_drawing_tools.h>
  63. #include <tools/symbol_editor_edit_tool.h>
  64. #include <tools/symbol_editor_move_tool.h>
  65. #include <tools/symbol_editor_pin_tool.h>
  66. #include <widgets/app_progress_dialog.h>
  67. #include <widgets/infobar.h>
  68. #include <widgets/lib_tree.h>
  69. #include <widgets/wx_progress_reporters.h>
  70. #include <widgets/symbol_tree_pane.h>
  71. #include <wildcards_and_files_ext.h>
  72. #include <panel_sym_lib_table.h>
  73. #include <wx/choicdlg.h>
  74. #include <string_utils.h>
  75. bool SYMBOL_EDIT_FRAME::m_showDeMorgan = false;
  76. BEGIN_EVENT_TABLE( SYMBOL_EDIT_FRAME, EDA_DRAW_FRAME )
  77. EVT_SIZE( SYMBOL_EDIT_FRAME::OnSize )
  78. EVT_COMBOBOX( ID_LIBEDIT_SELECT_UNIT_NUMBER, SYMBOL_EDIT_FRAME::OnSelectUnit )
  79. // menubar commands
  80. EVT_MENU( wxID_EXIT, SYMBOL_EDIT_FRAME::OnExitKiCad )
  81. EVT_MENU( wxID_CLOSE, SYMBOL_EDIT_FRAME::CloseWindow )
  82. EVT_MENU( ID_GRID_SETTINGS, SCH_BASE_FRAME::OnGridSettings )
  83. // Update user interface elements.
  84. EVT_UPDATE_UI( ID_LIBEDIT_SELECT_UNIT_NUMBER, SYMBOL_EDIT_FRAME::OnUpdateUnitNumber )
  85. END_EVENT_TABLE()
  86. SYMBOL_EDIT_FRAME::SYMBOL_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
  87. SCH_BASE_FRAME( aKiway, aParent, FRAME_SCH_SYMBOL_EDITOR, _( "Library Editor" ),
  88. wxDefaultPosition, wxDefaultSize, KICAD_DEFAULT_DRAWFRAME_STYLE,
  89. LIB_EDIT_FRAME_NAME ),
  90. m_unitSelectBox( nullptr ),
  91. m_isSymbolFromSchematic( false )
  92. {
  93. SetShowDeMorgan( false );
  94. m_SyncPinEdit = false;
  95. m_symbol = nullptr;
  96. m_treePane = nullptr;
  97. m_libMgr = nullptr;
  98. m_unit = 1;
  99. m_convert = 1;
  100. m_aboutTitle = _( "KiCad Symbol Editor" );
  101. wxIcon icon;
  102. wxIconBundle icon_bundle;
  103. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_libedit ) );
  104. icon_bundle.AddIcon( icon );
  105. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_libedit_32 ) );
  106. icon_bundle.AddIcon( icon );
  107. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_libedit_16 ) );
  108. icon_bundle.AddIcon( icon );
  109. SetIcons( icon_bundle );
  110. m_settings = Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
  111. LoadSettings( m_settings );
  112. m_libMgr = new SYMBOL_LIBRARY_MANAGER( *this );
  113. bool loadingCancelled = false;
  114. {
  115. // Preload libraries before using SyncLibraries the first time, as the preload is
  116. // multi-threaded
  117. WX_PROGRESS_REPORTER reporter( this, _( "Loading Symbol Libraries" ),
  118. m_libMgr->GetLibraryCount(), true );
  119. m_libMgr->Preload( reporter );
  120. loadingCancelled = reporter.IsCancelled();
  121. wxSafeYield();
  122. }
  123. SyncLibraries( false, loadingCancelled );
  124. m_treePane = new SYMBOL_TREE_PANE( this, m_libMgr );
  125. resolveCanvasType();
  126. SwitchCanvas( m_canvasType );
  127. // Ensure axis are always drawn
  128. KIGFX::GAL_DISPLAY_OPTIONS& gal_opts = GetGalDisplayOptions();
  129. gal_opts.m_axesEnabled = true;
  130. m_dummyScreen = new SCH_SCREEN();
  131. SetScreen( m_dummyScreen );
  132. GetScreen()->m_Center = true;
  133. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  134. GetRenderSettings()->LoadColors( GetColorSettings() );
  135. GetRenderSettings()->m_IsSymbolEditor = true;
  136. GetCanvas()->GetGAL()->SetAxesColor( m_colorSettings->GetColor( LAYER_SCHEMATIC_GRID_AXES ) );
  137. setupTools();
  138. setupUIConditions();
  139. ReCreateMenuBar();
  140. ReCreateHToolbar();
  141. ReCreateVToolbar();
  142. ReCreateOptToolbar();
  143. updateTitle();
  144. UpdateSymbolMsgPanelInfo();
  145. RebuildSymbolUnitsList();
  146. m_auimgr.SetManagedWindow( this );
  147. CreateInfoBar();
  148. m_auimgr.AddPane( m_mainToolBar, EDA_PANE().HToolbar().Name( "MainToolbar" )
  149. .Top().Layer( 6 ) );
  150. m_auimgr.AddPane( m_messagePanel, EDA_PANE().Messages().Name( "MsgPanel" )
  151. .Bottom().Layer( 6 ) );
  152. m_auimgr.AddPane( m_optionsToolBar, EDA_PANE().VToolbar().Name( "OptToolbar" )
  153. .Left().Layer( 3 ) );
  154. m_auimgr.AddPane( m_treePane, EDA_PANE().Palette().Name( "SymbolTree" )
  155. .Left().Layer( 2 )
  156. .Caption( _( "Libraries" ) )
  157. .MinSize( 250, -1 ).BestSize( 250, -1 ) );
  158. m_auimgr.AddPane( m_drawToolBar, EDA_PANE().VToolbar().Name( "ToolsToolbar" )
  159. .Right().Layer( 2 ) );
  160. m_auimgr.AddPane( GetCanvas(), wxAuiPaneInfo().Name( "DrawFrame" )
  161. .CentrePane() );
  162. FinishAUIInitialization();
  163. if( m_settings->m_LibWidth > 0 )
  164. {
  165. wxAuiPaneInfo& treePane = m_auimgr.GetPane( "SymbolTree" );
  166. // wxAUI hack: force width by setting MinSize() and then Fixed()
  167. // thanks to ZenJu http://trac.wxwidgets.org/ticket/13180
  168. treePane.MinSize( m_settings->m_LibWidth, -1 );
  169. treePane.Fixed();
  170. m_auimgr.Update();
  171. // now make it resizable again
  172. treePane.Resizable();
  173. m_auimgr.Update();
  174. // Note: DO NOT call m_auimgr.Update() anywhere after this; it will nuke the size
  175. // back to minimum.
  176. treePane.MinSize( 250, -1 );
  177. }
  178. Raise();
  179. Show( true );
  180. SyncView();
  181. GetCanvas()->GetView()->UseDrawPriority( true );
  182. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  183. setupUnits( m_settings );
  184. // Set the working/draw area size to display a symbol to a reasonable value:
  185. // A 600mm x 600mm with a origin at the area center looks like a large working area
  186. double max_size_x = Millimeter2iu( 600 );
  187. double max_size_y = Millimeter2iu( 600 );
  188. BOX2D bbox;
  189. bbox.SetOrigin( -max_size_x /2, -max_size_y/2 );
  190. bbox.SetSize( max_size_x, max_size_y );
  191. GetCanvas()->GetView()->SetBoundary( bbox );
  192. m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
  193. KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Library changes are unsaved" ) );
  194. // Catch unhandled accelerator command characters that were no handled by the library tree
  195. // panel.
  196. Bind( wxEVT_CHAR, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
  197. Bind( wxEVT_CHAR_HOOK, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
  198. // Ensure the window is on top
  199. Raise();
  200. if( loadingCancelled )
  201. ShowInfoBarWarning( _( "Symbol library loading was cancelled by user." ) );
  202. }
  203. SYMBOL_EDIT_FRAME::~SYMBOL_EDIT_FRAME()
  204. {
  205. // Shutdown all running tools
  206. if( m_toolManager )
  207. m_toolManager->ShutdownAllTools();
  208. if( IsSymbolFromSchematic() )
  209. {
  210. delete m_symbol;
  211. m_symbol = nullptr;
  212. SCH_SCREEN* screen = GetScreen();
  213. delete screen;
  214. m_isSymbolFromSchematic = false;
  215. }
  216. // current screen is destroyed in EDA_DRAW_FRAME
  217. SetScreen( m_dummyScreen );
  218. auto libedit = Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
  219. Pgm().GetSettingsManager().Save( libedit );
  220. delete m_libMgr;
  221. }
  222. void SYMBOL_EDIT_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  223. {
  224. wxCHECK_RET( m_settings, "Call to SYMBOL_EDIT_FRAME::LoadSettings with null m_boardAdapter" );
  225. SCH_BASE_FRAME::LoadSettings( GetSettings() );
  226. GetRenderSettings()->m_ShowPinsElectricalType = m_settings->m_ShowPinElectricalType;
  227. }
  228. void SYMBOL_EDIT_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
  229. {
  230. wxCHECK_RET( m_settings, "Call to SYMBOL_EDIT_FRAME::LoadSettings with null m_boardAdapter" );
  231. GetGalDisplayOptions().m_axesEnabled = true;
  232. SCH_BASE_FRAME::SaveSettings( GetSettings() );
  233. m_settings->m_ShowPinElectricalType = GetRenderSettings()->m_ShowPinsElectricalType;
  234. m_settings->m_LibWidth = m_treePane->GetSize().x;
  235. }
  236. APP_SETTINGS_BASE* SYMBOL_EDIT_FRAME::config() const
  237. {
  238. return static_cast<APP_SETTINGS_BASE*>( GetSettings() );
  239. }
  240. COLOR_SETTINGS* SYMBOL_EDIT_FRAME::GetColorSettings( bool aForceRefresh ) const
  241. {
  242. SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
  243. if( GetSettings()->m_UseEeschemaColorSettings )
  244. return mgr.GetColorSettings( mgr.GetAppSettings<EESCHEMA_SETTINGS>()->m_ColorTheme );
  245. else
  246. return mgr.GetColorSettings( GetSettings()->m_ColorTheme );
  247. }
  248. void SYMBOL_EDIT_FRAME::setupTools()
  249. {
  250. // Create the manager and dispatcher & route draw panel events to the dispatcher
  251. m_toolManager = new TOOL_MANAGER;
  252. m_toolManager->SetEnvironment( GetScreen(), GetCanvas()->GetView(),
  253. GetCanvas()->GetViewControls(), GetSettings(), this );
  254. m_actions = new EE_ACTIONS();
  255. m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager );
  256. // Register tools
  257. m_toolManager->RegisterTool( new COMMON_CONTROL );
  258. m_toolManager->RegisterTool( new COMMON_TOOLS );
  259. m_toolManager->RegisterTool( new ZOOM_TOOL );
  260. m_toolManager->RegisterTool( new EE_SELECTION_TOOL );
  261. m_toolManager->RegisterTool( new PICKER_TOOL );
  262. m_toolManager->RegisterTool( new EE_INSPECTION_TOOL );
  263. m_toolManager->RegisterTool( new SYMBOL_EDITOR_PIN_TOOL );
  264. m_toolManager->RegisterTool( new SYMBOL_EDITOR_DRAWING_TOOLS );
  265. m_toolManager->RegisterTool( new EE_POINT_EDITOR );
  266. m_toolManager->RegisterTool( new SYMBOL_EDITOR_MOVE_TOOL );
  267. m_toolManager->RegisterTool( new SYMBOL_EDITOR_EDIT_TOOL );
  268. m_toolManager->RegisterTool( new SYMBOL_EDITOR_CONTROL );
  269. m_toolManager->InitTools();
  270. // Run the selection tool, it is supposed to be always active
  271. m_toolManager->InvokeTool( "eeschema.InteractiveSelection" );
  272. GetCanvas()->SetEventDispatcher( m_toolDispatcher );
  273. }
  274. void SYMBOL_EDIT_FRAME::setupUIConditions()
  275. {
  276. SCH_BASE_FRAME::setupUIConditions();
  277. ACTION_MANAGER* mgr = m_toolManager->GetActionManager();
  278. EDITOR_CONDITIONS cond( this );
  279. wxASSERT( mgr );
  280. #define ENABLE( x ) ACTION_CONDITIONS().Enable( x )
  281. #define CHECK( x ) ACTION_CONDITIONS().Check( x )
  282. auto haveSymbolCond =
  283. [this]( const SELECTION& )
  284. {
  285. return m_symbol;
  286. };
  287. auto isEditableCond =
  288. [this]( const SELECTION& )
  289. {
  290. // Only root symbols from the new s-expression libraries or the schematic
  291. // are editable.
  292. return IsSymbolEditable() && !IsSymbolAlias();
  293. };
  294. auto symbolModifiedCondition =
  295. [this]( const SELECTION& sel )
  296. {
  297. return m_libMgr->IsSymbolModified( GetTargetLibId().GetLibItemName(),
  298. GetTargetLibId().GetLibNickname() );
  299. };
  300. auto libSelectedCondition =
  301. [this]( const SELECTION& sel )
  302. {
  303. return !GetTargetLibId().GetLibNickname().empty();
  304. };
  305. auto canEditProperties =
  306. [this]( const SELECTION& sel )
  307. {
  308. return m_symbol && ( !IsSymbolFromLegacyLibrary() || IsSymbolFromSchematic() );
  309. };
  310. auto saveSymbolAsCondition =
  311. [this]( const SELECTION& aSel )
  312. {
  313. return getTargetSymbol() != nullptr;
  314. };
  315. mgr->SetConditions( ACTIONS::saveAll, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  316. mgr->SetConditions( ACTIONS::save, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  317. mgr->SetConditions( EE_ACTIONS::saveLibraryAs, ENABLE( libSelectedCondition ) );
  318. mgr->SetConditions( EE_ACTIONS::saveSymbolAs, ENABLE( saveSymbolAsCondition ) );
  319. mgr->SetConditions( EE_ACTIONS::newSymbol, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  320. mgr->SetConditions( EE_ACTIONS::importSymbol, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  321. mgr->SetConditions( ACTIONS::undo, ENABLE( haveSymbolCond && cond.UndoAvailable() ) );
  322. mgr->SetConditions( ACTIONS::redo, ENABLE( haveSymbolCond && cond.RedoAvailable() ) );
  323. mgr->SetConditions( ACTIONS::revert, ENABLE( symbolModifiedCondition ) );
  324. mgr->SetConditions( ACTIONS::toggleGrid, CHECK( cond.GridVisible() ) );
  325. mgr->SetConditions( ACTIONS::toggleCursorStyle, CHECK( cond.FullscreenCursor() ) );
  326. mgr->SetConditions( ACTIONS::millimetersUnits, CHECK( cond.Units( EDA_UNITS::MILLIMETRES ) ) );
  327. mgr->SetConditions( ACTIONS::inchesUnits, CHECK( cond.Units( EDA_UNITS::INCHES ) ) );
  328. mgr->SetConditions( ACTIONS::milsUnits, CHECK( cond.Units( EDA_UNITS::MILS ) ) );
  329. mgr->SetConditions( ACTIONS::cut, ENABLE( isEditableCond ) );
  330. mgr->SetConditions( ACTIONS::copy, ENABLE( haveSymbolCond ) );
  331. mgr->SetConditions( ACTIONS::paste, ENABLE( isEditableCond && SELECTION_CONDITIONS::Idle ) );
  332. mgr->SetConditions( ACTIONS::doDelete, ENABLE( isEditableCond ) );
  333. mgr->SetConditions( ACTIONS::duplicate, ENABLE( isEditableCond ) );
  334. mgr->SetConditions( ACTIONS::selectAll, ENABLE( haveSymbolCond ) );
  335. mgr->SetConditions( EE_ACTIONS::rotateCW, ENABLE( isEditableCond ) );
  336. mgr->SetConditions( EE_ACTIONS::rotateCCW, ENABLE( isEditableCond ) );
  337. mgr->SetConditions( EE_ACTIONS::mirrorH, ENABLE( isEditableCond ) );
  338. mgr->SetConditions( EE_ACTIONS::mirrorV, ENABLE( isEditableCond ) );
  339. mgr->SetConditions( ACTIONS::zoomTool, CHECK( cond.CurrentTool( ACTIONS::zoomTool ) ) );
  340. mgr->SetConditions( ACTIONS::selectionTool, CHECK( cond.CurrentTool( ACTIONS::selectionTool ) ) );
  341. auto pinTypeCond =
  342. [this]( const SELECTION& )
  343. {
  344. return GetRenderSettings()->m_ShowPinsElectricalType;
  345. };
  346. auto showCompTreeCond =
  347. [this]( const SELECTION& )
  348. {
  349. return IsSymbolTreeShown();
  350. };
  351. mgr->SetConditions( EE_ACTIONS::showElectricalTypes, CHECK( pinTypeCond ) );
  352. mgr->SetConditions( ACTIONS::toggleBoundingBoxes, CHECK( cond.BoundingBoxes() ) );
  353. mgr->SetConditions( EE_ACTIONS::showSymbolTree, CHECK( showCompTreeCond ) );
  354. auto demorganCond =
  355. [this]( const SELECTION& )
  356. {
  357. return GetShowDeMorgan();
  358. };
  359. auto demorganStandardCond =
  360. [this]( const SELECTION& )
  361. {
  362. return m_convert == LIB_ITEM::LIB_CONVERT::BASE;
  363. };
  364. auto demorganAlternateCond =
  365. [this]( const SELECTION& )
  366. {
  367. return m_convert == LIB_ITEM::LIB_CONVERT::DEMORGAN;
  368. };
  369. auto multiUnitModeCond =
  370. [this]( const SELECTION& )
  371. {
  372. return m_symbol && m_symbol->IsMulti() && !m_symbol->UnitsLocked();
  373. };
  374. auto syncedPinsModeCond =
  375. [this]( const SELECTION& )
  376. {
  377. return m_SyncPinEdit;
  378. };
  379. auto haveDatasheetCond =
  380. [this]( const SELECTION& )
  381. {
  382. return m_symbol && !m_symbol->GetDatasheetField().GetText().IsEmpty();
  383. };
  384. mgr->SetConditions( EE_ACTIONS::showDatasheet, ENABLE( haveDatasheetCond ) );
  385. mgr->SetConditions( EE_ACTIONS::symbolProperties, ENABLE( canEditProperties && haveSymbolCond ) );
  386. mgr->SetConditions( EE_ACTIONS::runERC, ENABLE( haveSymbolCond ) );
  387. mgr->SetConditions( EE_ACTIONS::pinTable, ENABLE( isEditableCond && haveSymbolCond ) );
  388. mgr->SetConditions( EE_ACTIONS::showDeMorganStandard,
  389. ACTION_CONDITIONS().Enable( demorganCond ).Check( demorganStandardCond ) );
  390. mgr->SetConditions( EE_ACTIONS::showDeMorganAlternate,
  391. ACTION_CONDITIONS().Enable( demorganCond ).Check( demorganAlternateCond ) );
  392. mgr->SetConditions( EE_ACTIONS::toggleSyncedPinsMode,
  393. ACTION_CONDITIONS().Enable( multiUnitModeCond ).Check( syncedPinsModeCond ) );
  394. // Only enable a tool if the symbol is edtable
  395. #define EDIT_TOOL( tool ) ACTION_CONDITIONS().Enable( isEditableCond ).Check( cond.CurrentTool( tool ) )
  396. mgr->SetConditions( ACTIONS::deleteTool, EDIT_TOOL( ACTIONS::deleteTool ) );
  397. mgr->SetConditions( EE_ACTIONS::placeSymbolPin, EDIT_TOOL( EE_ACTIONS::placeSymbolPin ) );
  398. mgr->SetConditions( EE_ACTIONS::placeSymbolText, EDIT_TOOL( EE_ACTIONS::placeSymbolText ) );
  399. mgr->SetConditions( EE_ACTIONS::drawSymbolTextBox, EDIT_TOOL( EE_ACTIONS::drawSymbolTextBox ) );
  400. mgr->SetConditions( EE_ACTIONS::drawRectangle, EDIT_TOOL( EE_ACTIONS::drawRectangle ) );
  401. mgr->SetConditions( EE_ACTIONS::drawCircle, EDIT_TOOL( EE_ACTIONS::drawCircle ) );
  402. mgr->SetConditions( EE_ACTIONS::drawArc, EDIT_TOOL( EE_ACTIONS::drawArc ) );
  403. mgr->SetConditions( EE_ACTIONS::drawSymbolLines, EDIT_TOOL( EE_ACTIONS::drawSymbolLines ) );
  404. mgr->SetConditions( EE_ACTIONS::placeSymbolAnchor, EDIT_TOOL( EE_ACTIONS::placeSymbolAnchor ) );
  405. #undef CHECK
  406. #undef ENABLE
  407. #undef EDIT_TOOL
  408. }
  409. bool SYMBOL_EDIT_FRAME::CanCloseSymbolFromSchematic( bool doClose )
  410. {
  411. if( IsContentModified() )
  412. {
  413. SCH_EDIT_FRAME* schframe = (SCH_EDIT_FRAME*) Kiway().Player( FRAME_SCH, false );
  414. wxString msg = _( "Save changes to '%s' before closing?" );
  415. switch( UnsavedChangesDialog( this, wxString::Format( msg, m_reference ), nullptr ) )
  416. {
  417. case wxID_YES:
  418. if( schframe && GetCurSymbol() ) // Should be always the case
  419. schframe->SaveSymbolToSchematic( *GetCurSymbol(), m_schematicSymbolUUID );
  420. break;
  421. case wxID_NO:
  422. break;
  423. default:
  424. case wxID_CANCEL:
  425. return false;
  426. }
  427. }
  428. if( doClose )
  429. {
  430. GetInfoBar()->ShowMessageFor( wxEmptyString, 1 );
  431. SetCurSymbol( nullptr, false );
  432. updateTitle();
  433. }
  434. return true;
  435. }
  436. bool SYMBOL_EDIT_FRAME::canCloseWindow( wxCloseEvent& aEvent )
  437. {
  438. // Shutdown blocks must be determined and vetoed as early as possible
  439. if( KIPLATFORM::APP::SupportsShutdownBlockReason()
  440. && aEvent.GetId() == wxEVT_QUERY_END_SESSION
  441. && IsContentModified() )
  442. {
  443. return false;
  444. }
  445. if( m_isSymbolFromSchematic && !CanCloseSymbolFromSchematic( false ) )
  446. return false;
  447. if( !saveAllLibraries( true ) )
  448. return false;
  449. return true;
  450. }
  451. void SYMBOL_EDIT_FRAME::doCloseWindow()
  452. {
  453. Destroy();
  454. }
  455. void SYMBOL_EDIT_FRAME::RebuildSymbolUnitsList()
  456. {
  457. if( !m_unitSelectBox )
  458. return;
  459. if( m_unitSelectBox->GetCount() != 0 )
  460. m_unitSelectBox->Clear();
  461. if( !m_symbol || m_symbol->GetUnitCount() <= 1 )
  462. {
  463. m_unit = 1;
  464. m_unitSelectBox->Append( wxEmptyString );
  465. }
  466. else
  467. {
  468. for( int i = 0; i < m_symbol->GetUnitCount(); i++ )
  469. {
  470. wxString sub = LIB_SYMBOL::SubReference( i+1, false );
  471. wxString unit = wxString::Format( _( "Unit %s" ), sub );
  472. m_unitSelectBox->Append( unit );
  473. }
  474. }
  475. // Ensure the selected unit is compatible with the number of units of the current symbol:
  476. if( m_symbol && m_symbol->GetUnitCount() < m_unit )
  477. m_unit = 1;
  478. m_unitSelectBox->SetSelection(( m_unit > 0 ) ? m_unit - 1 : 0 );
  479. }
  480. void SYMBOL_EDIT_FRAME::OnToggleSymbolTree( wxCommandEvent& event )
  481. {
  482. wxAuiPaneInfo& treePane = m_auimgr.GetPane( m_treePane );
  483. treePane.Show( !IsSymbolTreeShown() );
  484. m_auimgr.Update();
  485. }
  486. bool SYMBOL_EDIT_FRAME::IsSymbolTreeShown() const
  487. {
  488. return const_cast<wxAuiManager&>( m_auimgr ).GetPane( m_treePane ).IsShown();
  489. }
  490. void SYMBOL_EDIT_FRAME::FreezeLibraryTree()
  491. {
  492. m_treePane->Freeze();
  493. m_libMgr->GetAdapter()->Freeze();
  494. }
  495. void SYMBOL_EDIT_FRAME::ThawLibraryTree()
  496. {
  497. m_libMgr->GetAdapter()->Thaw();
  498. m_treePane->Thaw();
  499. }
  500. void SYMBOL_EDIT_FRAME::OnExitKiCad( wxCommandEvent& event )
  501. {
  502. Kiway().OnKiCadExit();
  503. }
  504. void SYMBOL_EDIT_FRAME::OnUpdateUnitNumber( wxUpdateUIEvent& event )
  505. {
  506. event.Enable( m_symbol && m_symbol->GetUnitCount() > 1 );
  507. }
  508. void SYMBOL_EDIT_FRAME::OnSelectUnit( wxCommandEvent& event )
  509. {
  510. int i = event.GetSelection();
  511. if( ( i == wxNOT_FOUND ) || ( ( i + 1 ) == m_unit ) )
  512. return;
  513. m_toolManager->RunAction( ACTIONS::cancelInteractive, true );
  514. m_toolManager->RunAction( EE_ACTIONS::clearSelection, true );
  515. m_unit = i + 1;
  516. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  517. RebuildView();
  518. UpdateSymbolMsgPanelInfo();
  519. }
  520. bool SYMBOL_EDIT_FRAME::IsSymbolFromLegacyLibrary() const
  521. {
  522. if( m_symbol )
  523. {
  524. SYMBOL_LIB_TABLE_ROW* row = m_libMgr->GetLibrary( m_symbol->GetLibNickname() );
  525. if( row && row->GetType() == SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY ) )
  526. return true;
  527. }
  528. return false;
  529. }
  530. wxString SYMBOL_EDIT_FRAME::GetCurLib() const
  531. {
  532. wxString libNickname = Prj().GetRString( PROJECT::SCH_LIBEDIT_CUR_LIB );
  533. if( !libNickname.empty() )
  534. {
  535. if( !Prj().SchSymbolLibTable()->HasLibrary( libNickname ) )
  536. {
  537. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, wxEmptyString );
  538. libNickname = wxEmptyString;
  539. }
  540. }
  541. return libNickname;
  542. }
  543. wxString SYMBOL_EDIT_FRAME::SetCurLib( const wxString& aLibNickname )
  544. {
  545. wxString old = GetCurLib();
  546. if( aLibNickname.empty() || !Prj().SchSymbolLibTable()->HasLibrary( aLibNickname ) )
  547. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, wxEmptyString );
  548. else
  549. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, aLibNickname );
  550. return old;
  551. }
  552. void SYMBOL_EDIT_FRAME::SetCurSymbol( LIB_SYMBOL* aSymbol, bool aUpdateZoom )
  553. {
  554. m_toolManager->RunAction( EE_ACTIONS::clearSelection, true );
  555. GetCanvas()->GetView()->Clear();
  556. delete m_symbol;
  557. m_symbol = aSymbol;
  558. // select the current symbol in the tree widget
  559. if( !IsSymbolFromSchematic() && m_symbol )
  560. m_treePane->GetLibTree()->SelectLibId( m_symbol->GetLibId() );
  561. else
  562. m_treePane->GetLibTree()->Unselect();
  563. wxString symbolName = m_symbol ? m_symbol->GetName() : wxString();
  564. // retain in case this wxFrame is re-opened later on the same PROJECT
  565. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_SYMBOL, symbolName );
  566. // Ensure synchronized pin edit can be enabled only symbols with interchangeable units
  567. m_SyncPinEdit = aSymbol && aSymbol->IsRoot() && aSymbol->IsMulti() && !aSymbol->UnitsLocked();
  568. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  569. GetRenderSettings()->m_ShowUnit = m_unit;
  570. GetRenderSettings()->m_ShowConvert = m_convert;
  571. GetRenderSettings()->m_ShowDisabled = IsSymbolFromLegacyLibrary() && !IsSymbolFromSchematic();
  572. GetRenderSettings()->m_ShowGraphicsDisabled = IsSymbolAlias() && !IsSymbolFromSchematic();
  573. GetCanvas()->DisplaySymbol( m_symbol );
  574. GetCanvas()->GetView()->HideDrawingSheet();
  575. GetCanvas()->GetView()->ClearHiddenFlags();
  576. if( aUpdateZoom )
  577. m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
  578. GetCanvas()->Refresh();
  579. WX_INFOBAR* infobar = GetInfoBar();
  580. if( IsSymbolFromSchematic() )
  581. {
  582. wxString msg;
  583. msg.Printf( _( "Editing symbol %s from schematic. Saving will update the schematic "
  584. "only." ), m_reference );
  585. infobar->RemoveAllButtons();
  586. infobar->ShowMessage( msg, wxICON_INFORMATION );
  587. }
  588. else if( IsSymbolFromLegacyLibrary() )
  589. {
  590. wxHyperlinkCtrl* button = new wxHyperlinkCtrl( infobar, wxID_ANY,
  591. _( "Manage symbol libraries" ),
  592. wxEmptyString );
  593. button->Bind( wxEVT_COMMAND_HYPERLINK, std::function<void( wxHyperlinkEvent& aEvent )>(
  594. [&]( wxHyperlinkEvent& aEvent )
  595. {
  596. InvokeSchEditSymbolLibTable( &Kiway(), this );
  597. } ) );
  598. infobar->RemoveAllButtons();
  599. infobar->AddButton( button );
  600. infobar->ShowMessage( _( "Symbols in legacy libraries are not editable. Use Manage "
  601. "Symbol Libraries to migrate to current format." ),
  602. wxICON_INFORMATION );
  603. }
  604. else if( IsSymbolAlias() )
  605. {
  606. wxString parentSymbolName = m_symbol->GetParent().lock()->GetName();
  607. wxString msg;
  608. wxString link;
  609. msg.Printf( _( "Symbol %s is derived from %s. Symbol graphics will not be editable." ),
  610. UnescapeString( symbolName ),
  611. UnescapeString( parentSymbolName ) );
  612. link.Printf( _( "Open %s" ), UnescapeString( parentSymbolName ) );
  613. wxHyperlinkCtrl* button = new wxHyperlinkCtrl( infobar, wxID_ANY, link, wxEmptyString );
  614. button->Bind( wxEVT_COMMAND_HYPERLINK, std::function<void( wxHyperlinkEvent& aEvent )>(
  615. [&]( wxHyperlinkEvent& aEvent )
  616. {
  617. LoadSymbolFromCurrentLib( m_symbol->GetParent().lock()->GetName(),
  618. GetUnit(), GetConvert() );
  619. } ) );
  620. infobar->RemoveAllButtons();
  621. infobar->AddButton( button );
  622. infobar->ShowMessage( msg, wxICON_INFORMATION );
  623. }
  624. else
  625. {
  626. infobar->Dismiss();
  627. }
  628. }
  629. SYMBOL_LIBRARY_MANAGER& SYMBOL_EDIT_FRAME::GetLibManager()
  630. {
  631. wxASSERT( m_libMgr );
  632. return *m_libMgr;
  633. }
  634. void SYMBOL_EDIT_FRAME::OnModify()
  635. {
  636. GetScreen()->SetContentModified();
  637. if( !IsSymbolFromSchematic() )
  638. storeCurrentSymbol();
  639. m_treePane->GetLibTree()->RefreshLibTree();
  640. if( !GetTitle().StartsWith( "*" ) )
  641. updateTitle();
  642. }
  643. bool SYMBOL_EDIT_FRAME::SynchronizePins()
  644. {
  645. return m_SyncPinEdit && m_symbol && m_symbol->IsMulti() && !m_symbol->UnitsLocked();
  646. }
  647. wxString SYMBOL_EDIT_FRAME::AddLibraryFile( bool aCreateNew )
  648. {
  649. // Select the target library table (global/project)
  650. SYMBOL_LIB_TABLE* libTable = selectSymLibTable();
  651. if( !libTable )
  652. return wxEmptyString;
  653. wxFileName fn = m_libMgr->GetUniqueLibraryName();
  654. if( !LibraryFileBrowser( !aCreateNew, fn, KiCadSymbolLibFileWildcard(),
  655. KiCadSymbolLibFileExtension, false,
  656. ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() ),
  657. PATHS::GetDefaultUserSymbolsPath() ) )
  658. {
  659. return wxEmptyString;
  660. }
  661. wxString libName = fn.GetName();
  662. if( libName.IsEmpty() )
  663. return wxEmptyString;
  664. if( m_libMgr->LibraryExists( libName ) )
  665. {
  666. DisplayError( this, wxString::Format( _( "Library '%s' already exists." ), libName ) );
  667. return wxEmptyString;
  668. }
  669. if( aCreateNew )
  670. {
  671. if( !m_libMgr->CreateLibrary( fn.GetFullPath(), libTable ) )
  672. {
  673. DisplayError( this, wxString::Format( _( "Could not create the library file '%s'.\n"
  674. "Make sure you have write permissions and "
  675. "try again." ),
  676. fn.GetFullPath() ) );
  677. return wxEmptyString;
  678. }
  679. }
  680. else
  681. {
  682. if( !m_libMgr->AddLibrary( fn.GetFullPath(), libTable ) )
  683. {
  684. DisplayError( this, _( "Could not open the library file." ) );
  685. return wxEmptyString;
  686. }
  687. }
  688. bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
  689. saveSymbolLibTables( globalTable, !globalTable );
  690. std::string packet = fn.GetFullPath().ToStdString();
  691. this->Kiway().ExpressMail( FRAME_SCH_SYMBOL_EDITOR, MAIL_LIB_EDIT, packet );
  692. return fn.GetFullPath();
  693. }
  694. LIB_ID SYMBOL_EDIT_FRAME::GetTreeLIBID( int* aUnit ) const
  695. {
  696. return m_treePane->GetLibTree()->GetSelectedLibId( aUnit );
  697. }
  698. LIB_SYMBOL* SYMBOL_EDIT_FRAME::getTargetSymbol() const
  699. {
  700. LIB_ID libId = GetTreeLIBID();
  701. if( libId.IsValid() )
  702. {
  703. LIB_SYMBOL* alias = m_libMgr->GetAlias( libId.GetLibItemName(), libId.GetLibNickname() );
  704. return alias;
  705. }
  706. return m_symbol;
  707. }
  708. LIB_ID SYMBOL_EDIT_FRAME::GetTargetLibId() const
  709. {
  710. LIB_ID id;
  711. if( IsSymbolTreeShown() )
  712. id = GetTreeLIBID();
  713. if( id.GetLibNickname().empty() && m_symbol )
  714. id = m_symbol->GetLibId();
  715. return id;
  716. }
  717. LIB_TREE_NODE* SYMBOL_EDIT_FRAME::GetCurrentTreeNode() const
  718. {
  719. return m_treePane->GetLibTree()->GetCurrentTreeNode();
  720. }
  721. wxString SYMBOL_EDIT_FRAME::getTargetLib() const
  722. {
  723. return GetTargetLibId().GetLibNickname();
  724. }
  725. void SYMBOL_EDIT_FRAME::SyncLibraries( bool aShowProgress, bool aPreloadCancelled,
  726. const wxString& aForceRefresh )
  727. {
  728. LIB_ID selected;
  729. if( m_treePane )
  730. selected = m_treePane->GetLibTree()->GetSelectedLibId();
  731. if( aShowProgress )
  732. {
  733. APP_PROGRESS_DIALOG progressDlg( _( "Loading Symbol Libraries" ), wxEmptyString,
  734. m_libMgr->GetAdapter()->GetLibrariesCount(), this );
  735. m_libMgr->Sync( aForceRefresh,
  736. [&]( int progress, int max, const wxString& libName )
  737. {
  738. progressDlg.Update( progress, wxString::Format( _( "Loading library '%s'..." ),
  739. libName ) );
  740. } );
  741. }
  742. else if( !aPreloadCancelled )
  743. {
  744. m_libMgr->Sync( aForceRefresh,
  745. [&]( int progress, int max, const wxString& libName )
  746. {
  747. } );
  748. }
  749. if( m_treePane )
  750. {
  751. wxDataViewItem found;
  752. if( selected.IsValid() )
  753. {
  754. // Check if the previously selected item is still valid,
  755. // if not - it has to be unselected to prevent crash
  756. found = m_libMgr->GetAdapter()->FindItem( selected );
  757. if( !found )
  758. m_treePane->GetLibTree()->Unselect();
  759. }
  760. m_treePane->GetLibTree()->Regenerate( true );
  761. // Try to select the parent library, in case the symbol is not found
  762. if( !found && selected.IsValid() )
  763. {
  764. selected.SetLibItemName( "" );
  765. found = m_libMgr->GetAdapter()->FindItem( selected );
  766. if( found )
  767. m_treePane->GetLibTree()->SelectLibId( selected );
  768. }
  769. // If no selection, see if there's a current symbol to centre
  770. if( !selected.IsValid() && m_symbol )
  771. {
  772. LIB_ID current( GetCurLib(), m_symbol->GetName() );
  773. m_treePane->GetLibTree()->CenterLibId( current );
  774. }
  775. }
  776. }
  777. void SYMBOL_EDIT_FRAME::RegenerateLibraryTree()
  778. {
  779. LIB_ID target = GetTargetLibId();
  780. m_treePane->GetLibTree()->Regenerate( true );
  781. if( target.IsValid() )
  782. m_treePane->GetLibTree()->CenterLibId( target );
  783. }
  784. void SYMBOL_EDIT_FRAME::RefreshLibraryTree()
  785. {
  786. m_treePane->GetLibTree()->RefreshLibTree();
  787. }
  788. void SYMBOL_EDIT_FRAME::FocusOnLibId( const LIB_ID& aLibID )
  789. {
  790. m_treePane->GetLibTree()->SelectLibId( aLibID );
  791. }
  792. void SYMBOL_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem, LIB_SYMBOL* aSymbol )
  793. {
  794. if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
  795. // from file therefore not yet in tree.
  796. {
  797. static_cast<LIB_TREE_NODE_LIB_ID*>( aTreeItem.GetID() )->Update( aSymbol );
  798. m_treePane->GetLibTree()->RefreshLibTree();
  799. }
  800. }
  801. SYMBOL_LIB_TABLE* SYMBOL_EDIT_FRAME::selectSymLibTable( bool aOptional )
  802. {
  803. // If no project is loaded, always work with the global table
  804. if( Prj().IsNullProject() )
  805. {
  806. SYMBOL_LIB_TABLE* ret = &SYMBOL_LIB_TABLE::GetGlobalLibTable();
  807. if( aOptional )
  808. {
  809. wxMessageDialog dlg( this, _( "Add the library to the global library table?" ),
  810. _( "Add To Global Library Table" ), wxYES_NO );
  811. if( dlg.ShowModal() != wxID_OK )
  812. ret = nullptr;
  813. }
  814. return ret;
  815. }
  816. wxArrayString libTableNames;
  817. libTableNames.Add( _( "Global" ) );
  818. libTableNames.Add( _( "Project" ) );
  819. wxSingleChoiceDialog dlg( this, _( "Choose the Library Table to add the library to:" ),
  820. _( "Add To Library Table" ), libTableNames );
  821. if( aOptional )
  822. {
  823. dlg.FindWindow( wxID_CANCEL )->SetLabel( _( "Skip" ) );
  824. dlg.FindWindow( wxID_OK )->SetLabel( _( "Add" ) );
  825. }
  826. if( dlg.ShowModal() != wxID_OK )
  827. return nullptr;
  828. switch( dlg.GetSelection() )
  829. {
  830. case 0: return &SYMBOL_LIB_TABLE::GetGlobalLibTable();
  831. case 1: return Prj().SchSymbolLibTable();
  832. default: return nullptr;
  833. }
  834. }
  835. bool SYMBOL_EDIT_FRAME::backupFile( const wxFileName& aOriginalFile, const wxString& aBackupExt )
  836. {
  837. if( aOriginalFile.FileExists() )
  838. {
  839. wxFileName backupFileName( aOriginalFile );
  840. backupFileName.SetExt( aBackupExt );
  841. if( backupFileName.FileExists() )
  842. wxRemoveFile( backupFileName.GetFullPath() );
  843. if( !wxCopyFile( aOriginalFile.GetFullPath(), backupFileName.GetFullPath() ) )
  844. {
  845. DisplayError( this, wxString::Format( _( "Failed to save backup to '%s'." ),
  846. backupFileName.GetFullPath() ) );
  847. return false;
  848. }
  849. }
  850. return true;
  851. }
  852. void SYMBOL_EDIT_FRAME::storeCurrentSymbol()
  853. {
  854. if( m_symbol && !GetCurLib().IsEmpty() && GetScreen()->IsContentModified() )
  855. m_libMgr->UpdateSymbol( m_symbol, GetCurLib() ); // UpdateSymbol() makes a copy
  856. }
  857. bool SYMBOL_EDIT_FRAME::IsCurrentSymbol( const LIB_ID& aLibId ) const
  858. {
  859. // This will return the root symbol of any alias
  860. LIB_SYMBOL* symbol = m_libMgr->GetBufferedSymbol( aLibId.GetLibItemName(),
  861. aLibId.GetLibNickname() );
  862. // Now we can compare the libId of the current symbol and the root symbol
  863. return ( symbol && m_symbol && symbol->GetLibId() == m_symbol->GetLibId() );
  864. }
  865. void SYMBOL_EDIT_FRAME::emptyScreen()
  866. {
  867. m_treePane->GetLibTree()->Unselect();
  868. SetCurLib( wxEmptyString );
  869. SetCurSymbol( nullptr, false );
  870. SetScreen( m_dummyScreen );
  871. ClearUndoRedoList();
  872. m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
  873. Refresh();
  874. }
  875. void SYMBOL_EDIT_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
  876. {
  877. SCH_BASE_FRAME::CommonSettingsChanged( aEnvVarsChanged, aTextVarsChanged );
  878. SETTINGS_MANAGER* mgr = GetSettingsManager();
  879. SYMBOL_EDITOR_SETTINGS* cfg = mgr->GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
  880. GetRenderSettings()->m_ShowPinsElectricalType = cfg->m_ShowPinElectricalType;
  881. GetGalDisplayOptions().ReadWindowSettings( cfg->m_Window );
  882. if( m_symbol )
  883. m_symbol->ClearCaches();
  884. GetCanvas()->ForceRefresh();
  885. RecreateToolbars();
  886. if( aEnvVarsChanged )
  887. SyncLibraries( true );
  888. Layout();
  889. SendSizeEvent();
  890. }
  891. void SYMBOL_EDIT_FRAME::ShowChangedLanguage()
  892. {
  893. // call my base class
  894. SCH_BASE_FRAME::ShowChangedLanguage();
  895. // tooltips in toolbars
  896. RecreateToolbars();
  897. // status bar
  898. UpdateMsgPanel();
  899. if( GetRenderSettings()->m_ShowPinsElectricalType )
  900. {
  901. GetCanvas()->GetView()->UpdateAllItems( KIGFX::ALL );
  902. GetCanvas()->Refresh();
  903. }
  904. updateTitle();
  905. }
  906. void SYMBOL_EDIT_FRAME::SetScreen( BASE_SCREEN* aScreen )
  907. {
  908. SCH_BASE_FRAME::SetScreen( aScreen );
  909. }
  910. void SYMBOL_EDIT_FRAME::RebuildView()
  911. {
  912. GetRenderSettings()->m_ShowUnit = m_unit;
  913. GetRenderSettings()->m_ShowConvert = m_convert;
  914. GetRenderSettings()->m_ShowDisabled = IsSymbolFromLegacyLibrary() && !IsSymbolFromSchematic();
  915. GetRenderSettings()->m_ShowGraphicsDisabled = IsSymbolAlias() && !IsSymbolFromSchematic();
  916. GetCanvas()->DisplaySymbol( m_symbol );
  917. GetCanvas()->GetView()->HideDrawingSheet();
  918. GetCanvas()->GetView()->ClearHiddenFlags();
  919. GetCanvas()->Refresh();
  920. }
  921. void SYMBOL_EDIT_FRAME::HardRedraw()
  922. {
  923. SyncLibraries( true );
  924. if( m_symbol )
  925. {
  926. EE_SELECTION_TOOL* selectionTool = m_toolManager->GetTool<EE_SELECTION_TOOL>();
  927. EE_SELECTION& selection = selectionTool->GetSelection();
  928. for( LIB_ITEM& item : m_symbol->GetDrawItems() )
  929. {
  930. if( !alg::contains( selection, &item ) )
  931. item.ClearSelected();
  932. else
  933. item.SetSelected();
  934. }
  935. m_symbol->ClearCaches();
  936. }
  937. RebuildView();
  938. }
  939. const BOX2I SYMBOL_EDIT_FRAME::GetDocumentExtents( bool aIncludeAllVisible ) const
  940. {
  941. if( !m_symbol )
  942. {
  943. return BOX2I( VECTOR2I( Mils2iu( -100 ), Mils2iu( -100 ) ),
  944. VECTOR2I( Mils2iu( 200 ), Mils2iu( 200 ) ) );
  945. }
  946. else
  947. {
  948. EDA_RECT boundingBox = m_symbol->Flatten()->GetUnitBoundingBox( m_unit, m_convert );
  949. return BOX2I( boundingBox.GetOrigin(), VECTOR2I( boundingBox.GetWidth(),
  950. boundingBox.GetHeight() ) );
  951. }
  952. }
  953. void SYMBOL_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  954. {
  955. const std::string& payload = mail.GetPayload();
  956. switch( mail.Command() )
  957. {
  958. case MAIL_LIB_EDIT:
  959. if( !payload.empty() )
  960. {
  961. wxString libFileName( payload );
  962. wxString libNickname;
  963. wxString msg;
  964. SYMBOL_LIB_TABLE* libTable = Prj().SchSymbolLibTable();
  965. const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( libFileName );
  966. if( !libTableRow )
  967. {
  968. msg.Printf( _( "The current configuration does not include the library '%s'.\n"
  969. "Use Manage Symbol Libraries to edit the configuration." ),
  970. libFileName );
  971. DisplayErrorMessage( this, _( "Library not found in symbol library table." ), msg );
  972. break;
  973. }
  974. libNickname = libTableRow->GetNickName();
  975. if( !libTable->HasLibrary( libNickname, true ) )
  976. {
  977. msg.Printf( _( "The library '%s' is not enabled in the current configuration.\n"
  978. "Use Manage Symbol Libraries to edit the configuration." ),
  979. UnescapeString( libNickname ) );
  980. DisplayErrorMessage( this, _( "Symbol library not enabled." ), msg );
  981. break;
  982. }
  983. SetCurLib( libNickname );
  984. if( m_treePane )
  985. {
  986. LIB_ID id( libNickname, wxEmptyString );
  987. m_treePane->GetLibTree()->SelectLibId( id );
  988. m_treePane->GetLibTree()->ExpandLibId( id );
  989. m_treePane->GetLibTree()->CenterLibId( id );
  990. }
  991. }
  992. break;
  993. default:
  994. ;
  995. }
  996. }
  997. void SYMBOL_EDIT_FRAME::SwitchCanvas( EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType )
  998. {
  999. // switches currently used canvas ( Cairo / OpenGL):
  1000. SCH_BASE_FRAME::SwitchCanvas( aCanvasType );
  1001. // Set options specific to symbol editor (axies are always enabled):
  1002. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  1003. GetCanvas()->GetGAL()->SetAxesColor( m_colorSettings->GetColor( LAYER_SCHEMATIC_GRID_AXES ) );
  1004. }
  1005. bool SYMBOL_EDIT_FRAME::HasLibModifications() const
  1006. {
  1007. wxCHECK( m_libMgr, false );
  1008. return m_libMgr->HasModifications();
  1009. }
  1010. bool SYMBOL_EDIT_FRAME::IsContentModified() const
  1011. {
  1012. wxCHECK( m_libMgr, false );
  1013. // Test if the currently edited symbol is modified
  1014. if( GetScreen() && GetScreen()->IsContentModified() && GetCurSymbol() )
  1015. return true;
  1016. // Test if any library has been modified
  1017. for( const wxString& libName : m_libMgr->GetLibraryNames() )
  1018. {
  1019. if( m_libMgr->IsLibraryModified( libName ) && !m_libMgr->IsLibraryReadOnly( libName ) )
  1020. return true;
  1021. }
  1022. return false;
  1023. }
  1024. void SYMBOL_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
  1025. {
  1026. if( aItemCount == 0 )
  1027. return;
  1028. UNDO_REDO_CONTAINER& list = whichList == UNDO_LIST ? m_undoList : m_redoList;
  1029. for( PICKED_ITEMS_LIST* command : list.m_CommandsList )
  1030. {
  1031. command->ClearListAndDeleteItems();
  1032. delete command;
  1033. }
  1034. list.m_CommandsList.clear();
  1035. }
  1036. SELECTION& SYMBOL_EDIT_FRAME::GetCurrentSelection()
  1037. {
  1038. return m_toolManager->GetTool<EE_SELECTION_TOOL>()->GetSelection();
  1039. }
  1040. void SYMBOL_EDIT_FRAME::LoadSymbolFromSchematic( SCH_SYMBOL* aSymbol )
  1041. {
  1042. std::unique_ptr<LIB_SYMBOL> symbol = aSymbol->GetLibSymbolRef()->Flatten();
  1043. wxCHECK( symbol, /* void */ );
  1044. std::vector<LIB_FIELD> fullSetOfFields;
  1045. for( int i = 0; i < (int) aSymbol->GetFields().size(); ++i )
  1046. {
  1047. const SCH_FIELD& field = aSymbol->GetFields()[i];
  1048. VECTOR2I pos = field.GetPosition() - aSymbol->GetPosition();
  1049. LIB_FIELD libField( symbol.get(), field.GetId() );
  1050. if( i >= MANDATORY_FIELDS && !field.GetName( false ).IsEmpty() )
  1051. libField.SetName( field.GetName( false ) );
  1052. libField.SetText( field.GetText() );
  1053. libField.SetAttributes( field );
  1054. libField.SetPosition( wxPoint( pos.x, -pos.y ) );
  1055. fullSetOfFields.emplace_back( std::move( libField ) );
  1056. }
  1057. symbol->SetFields( fullSetOfFields );
  1058. if( m_symbol )
  1059. SetCurSymbol( nullptr, false );
  1060. m_isSymbolFromSchematic = true;
  1061. m_schematicSymbolUUID = aSymbol->m_Uuid;
  1062. m_reference = symbol->GetFieldById( REFERENCE_FIELD )->GetText();
  1063. m_unit = std::max( 1, aSymbol->GetUnit() );
  1064. m_convert = std::max( 1, aSymbol->GetConvert() );
  1065. // The buffered screen for the symbol
  1066. SCH_SCREEN* tmpScreen = new SCH_SCREEN();
  1067. SetScreen( tmpScreen );
  1068. SetCurSymbol( symbol.release(), true );
  1069. ReCreateMenuBar();
  1070. ReCreateHToolbar();
  1071. if( IsSymbolTreeShown() )
  1072. {
  1073. wxCommandEvent evt;
  1074. OnToggleSymbolTree( evt );
  1075. }
  1076. updateTitle();
  1077. RebuildSymbolUnitsList();
  1078. SetShowDeMorgan( GetCurSymbol()->HasConversion() );
  1079. UpdateSymbolMsgPanelInfo();
  1080. Refresh();
  1081. }
  1082. bool SYMBOL_EDIT_FRAME::addLibTableEntry( const wxString& aLibFile, TABLE_SCOPE aScope )
  1083. {
  1084. wxFileName fn = aLibFile;
  1085. wxFileName libTableFileName( Prj().GetProjectPath(),
  1086. SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
  1087. wxString libNickname = fn.GetName();
  1088. SYMBOL_LIB_TABLE* libTable = Prj().SchSymbolLibTable();
  1089. const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
  1090. if( libTable->HasLibrary( libNickname ) )
  1091. {
  1092. wxString tmp;
  1093. int suffix = 1;
  1094. while( libTable->HasLibrary( libNickname ) )
  1095. {
  1096. tmp.Printf( "%s%d", fn.GetName(), suffix );
  1097. libNickname = tmp;
  1098. suffix += 1;
  1099. }
  1100. }
  1101. SYMBOL_LIB_TABLE_ROW* row = new SYMBOL_LIB_TABLE_ROW();
  1102. row->SetNickName( libNickname );
  1103. wxString normalizedPath = NormalizePath( aLibFile, &envVars, Prj().GetProjectPath() );
  1104. if( aScope == GLOBAL_LIB_TABLE )
  1105. {
  1106. libTable = &SYMBOL_LIB_TABLE::GetGlobalLibTable();
  1107. libTableFileName = SYMBOL_LIB_TABLE::GetGlobalTableFileName();
  1108. // We cannot normalize against the current project path when saving to global table.
  1109. normalizedPath = NormalizePath( aLibFile, &envVars, wxEmptyString );
  1110. }
  1111. row->SetFullURI( normalizedPath );
  1112. wxCHECK( libTable->InsertRow( row ), false );
  1113. try
  1114. {
  1115. libTable->Save( libTableFileName.GetFullPath() );
  1116. }
  1117. catch( const IO_ERROR& ioe )
  1118. {
  1119. wxString msg = aScope == GLOBAL_LIB_TABLE ? _( "Error saving global library table." )
  1120. : _( "Error saving project library table." );
  1121. wxMessageDialog dlg( this, msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
  1122. dlg.SetExtendedMessage( ioe.What() );
  1123. dlg.ShowModal();
  1124. return false;
  1125. }
  1126. return true;
  1127. }
  1128. bool SYMBOL_EDIT_FRAME::replaceLibTableEntry( const wxString& aLibNickname,
  1129. const wxString& aLibFile )
  1130. {
  1131. // Check the global library table first because checking the project library table
  1132. // checks the global library table as well due to library chaining.
  1133. bool isGlobalTable = true;
  1134. wxFileName libTableFileName = SYMBOL_LIB_TABLE::GetGlobalTableFileName();;
  1135. const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
  1136. SYMBOL_LIB_TABLE* libTable = &SYMBOL_LIB_TABLE::GetGlobalLibTable();
  1137. SYMBOL_LIB_TABLE_ROW* row = libTable->FindRow( aLibNickname );
  1138. if( !row )
  1139. {
  1140. libTableFileName.SetPath( Prj().GetProjectPath() );
  1141. libTableFileName.SetName( SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
  1142. libTable = Prj().SchSymbolLibTable();
  1143. isGlobalTable = false;
  1144. row = libTable->FindRow( aLibNickname );
  1145. }
  1146. wxCHECK( row, false );
  1147. wxString projectPath;
  1148. if( !isGlobalTable )
  1149. projectPath = Prj().GetProjectPath();
  1150. wxString normalizedPath = NormalizePath( aLibFile, &envVars, projectPath );
  1151. row->SetFullURI( normalizedPath );
  1152. row->SetType( "KiCad" );
  1153. try
  1154. {
  1155. libTable->Save( libTableFileName.GetFullPath() );
  1156. }
  1157. catch( const IO_ERROR& ioe )
  1158. {
  1159. wxString msg = isGlobalTable ? _( "Error saving global library table." )
  1160. : _( "Error saving project library table." );
  1161. wxMessageDialog dlg( this, msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
  1162. dlg.SetExtendedMessage( ioe.What() );
  1163. dlg.ShowModal();
  1164. return false;
  1165. }
  1166. return true;
  1167. }
  1168. bool SYMBOL_EDIT_FRAME::IsSymbolAlias() const
  1169. {
  1170. return m_symbol && !m_symbol->IsRoot();
  1171. }
  1172. bool SYMBOL_EDIT_FRAME::IsSymbolEditable() const
  1173. {
  1174. return m_symbol && ( !IsSymbolFromLegacyLibrary() || IsSymbolFromSchematic() );
  1175. }
  1176. void SYMBOL_EDIT_FRAME::UpdateItem( EDA_ITEM* aItem, bool isAddOrDelete, bool aUpdateRtree )
  1177. {
  1178. SCH_BASE_FRAME::UpdateItem( aItem, isAddOrDelete, aUpdateRtree );
  1179. if( EDA_TEXT* eda_text = dynamic_cast<EDA_TEXT*>( aItem ) )
  1180. {
  1181. eda_text->ClearBoundingBoxCache();
  1182. eda_text->ClearRenderCache();
  1183. }
  1184. }