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.

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