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.

2012 lines
64 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
7 months 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
2 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 The 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 <gal/graphics_abstraction_layer.h>
  35. #include <kidialog.h>
  36. #include <kiface_base.h>
  37. #include <kiplatform/app.h>
  38. #include <kiway_express.h>
  39. #include <symbol_edit_frame.h>
  40. #include <lib_symbol_library_manager.h>
  41. #include <symbol_editor/symbol_editor_settings.h>
  42. #include <paths.h>
  43. #include <pgm_base.h>
  44. #include <project_sch.h>
  45. #include <sch_painter.h>
  46. #include <sch_view.h>
  47. #include <settings/settings_manager.h>
  48. #include <symbol_lib_table.h>
  49. #include <toolbars_symbol_editor.h>
  50. #include <tool/action_manager.h>
  51. #include <tool/action_toolbar.h>
  52. #include <tool/common_control.h>
  53. #include <tool/common_tools.h>
  54. #include <tool/editor_conditions.h>
  55. #include <tool/embed_tool.h>
  56. #include <tool/library_editor_control.h>
  57. #include <tool/picker_tool.h>
  58. #include <tool/properties_tool.h>
  59. #include <tool/selection.h>
  60. #include <tool/tool_dispatcher.h>
  61. #include <tool/tool_manager.h>
  62. #include <tool/zoom_tool.h>
  63. #include <tools/ee_actions.h>
  64. #include <tools/ee_inspection_tool.h>
  65. #include <tools/ee_point_editor.h>
  66. #include <tools/ee_grid_helper.h>
  67. #include <tools/ee_selection_tool.h>
  68. #include <tools/symbol_editor_control.h>
  69. #include <tools/symbol_editor_drawing_tools.h>
  70. #include <tools/symbol_editor_edit_tool.h>
  71. #include <tools/symbol_editor_move_tool.h>
  72. #include <tools/symbol_editor_pin_tool.h>
  73. #include <view/view_controls.h>
  74. #include <widgets/app_progress_dialog.h>
  75. #include <widgets/wx_infobar.h>
  76. #include <widgets/wx_progress_reporters.h>
  77. #include <widgets/panel_sch_selection_filter.h>
  78. #include <widgets/sch_properties_panel.h>
  79. #include <widgets/symbol_tree_pane.h>
  80. #include <widgets/wx_aui_utils.h>
  81. #include <wildcards_and_files_ext.h>
  82. #include <panel_sym_lib_table.h>
  83. #include <string_utils.h>
  84. #include <wx/msgdlg.h>
  85. #include <wx/log.h>
  86. bool SYMBOL_EDIT_FRAME::m_showDeMorgan = false;
  87. BEGIN_EVENT_TABLE( SYMBOL_EDIT_FRAME, SCH_BASE_FRAME )
  88. EVT_COMBOBOX( ID_LIBEDIT_SELECT_UNIT_NUMBER, SYMBOL_EDIT_FRAME::OnSelectUnit )
  89. // menubar commands
  90. EVT_MENU( wxID_EXIT, SYMBOL_EDIT_FRAME::OnExitKiCad )
  91. EVT_MENU( wxID_CLOSE, SYMBOL_EDIT_FRAME::CloseWindow )
  92. // Update user interface elements.
  93. EVT_UPDATE_UI( ID_LIBEDIT_SELECT_UNIT_NUMBER, SYMBOL_EDIT_FRAME::OnUpdateUnitNumber )
  94. // Drop files event
  95. EVT_DROP_FILES( SYMBOL_EDIT_FRAME::OnDropFiles )
  96. END_EVENT_TABLE()
  97. SYMBOL_EDIT_FRAME::SYMBOL_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
  98. SCH_BASE_FRAME( aKiway, aParent, FRAME_SCH_SYMBOL_EDITOR, _( "Library Editor" ),
  99. wxDefaultPosition, wxDefaultSize, KICAD_DEFAULT_DRAWFRAME_STYLE,
  100. LIB_EDIT_FRAME_NAME ),
  101. m_unitSelectBox( nullptr ),
  102. m_isSymbolFromSchematic( false )
  103. {
  104. SetShowDeMorgan( false );
  105. m_SyncPinEdit = false;
  106. m_symbol = nullptr;
  107. m_treePane = nullptr;
  108. m_libMgr = nullptr;
  109. m_unit = 1;
  110. m_bodyStyle = 1;
  111. m_aboutTitle = _HKI( "KiCad Symbol Editor" );
  112. wxIcon icon;
  113. wxIconBundle icon_bundle;
  114. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_libedit, 48 ) );
  115. icon_bundle.AddIcon( icon );
  116. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_libedit, 256 ) );
  117. icon_bundle.AddIcon( icon );
  118. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_libedit, 128 ) );
  119. icon_bundle.AddIcon( icon );
  120. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_libedit_32 ) );
  121. icon_bundle.AddIcon( icon );
  122. icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_libedit_16 ) );
  123. icon_bundle.AddIcon( icon );
  124. SetIcons( icon_bundle );
  125. m_settings = Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" );
  126. LoadSettings( m_settings );
  127. m_libMgr = new LIB_SYMBOL_LIBRARY_MANAGER( *this );
  128. bool loadingCancelled = false;
  129. {
  130. // Preload libraries before using SyncLibraries the first time, as the preload is
  131. // multi-threaded
  132. WX_PROGRESS_REPORTER reporter( this, _( "Loading Symbol Libraries" ),
  133. m_libMgr->GetLibraryCount(), true );
  134. m_libMgr->Preload( reporter );
  135. loadingCancelled = reporter.IsCancelled();
  136. wxSafeYield();
  137. }
  138. SyncLibraries( false, loadingCancelled );
  139. m_treePane = new SYMBOL_TREE_PANE( this, m_libMgr );
  140. m_treePane->GetLibTree()->SetSortMode( (LIB_TREE_MODEL_ADAPTER::SORT_MODE) m_settings->m_LibrarySortMode );
  141. resolveCanvasType();
  142. SwitchCanvas( m_canvasType );
  143. // Ensure axis are always drawn
  144. KIGFX::GAL_DISPLAY_OPTIONS& gal_opts = GetGalDisplayOptions();
  145. gal_opts.m_axesEnabled = true;
  146. m_dummyScreen = new SCH_SCREEN();
  147. SetScreen( m_dummyScreen );
  148. GetScreen()->m_Center = true;
  149. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  150. GetRenderSettings()->LoadColors( GetColorSettings() );
  151. GetRenderSettings()->m_IsSymbolEditor = true;
  152. GetCanvas()->GetGAL()->SetAxesColor( m_colorSettings->GetColor( LAYER_SCHEMATIC_GRID_AXES ) );
  153. setupTools();
  154. setupUIConditions();
  155. ReCreateMenuBar();
  156. m_toolbarSettings = Pgm().GetSettingsManager().GetToolbarSettings<SYMBOL_EDIT_TOOLBAR_SETTINGS>( "symbol_editor-toolbars" );
  157. configureToolbars();
  158. RecreateToolbars();
  159. UpdateTitle();
  160. UpdateSymbolMsgPanelInfo();
  161. RebuildSymbolUnitsList();
  162. m_propertiesPanel = new SCH_PROPERTIES_PANEL( this, this );
  163. m_propertiesPanel->SetSplitterProportion( m_settings->m_AuiPanels.properties_splitter );
  164. m_selectionFilterPanel = new PANEL_SCH_SELECTION_FILTER( this );
  165. m_auimgr.SetManagedWindow( this );
  166. CreateInfoBar();
  167. // Rows; layers 4 - 6
  168. m_auimgr.AddPane( m_tbTopMain, EDA_PANE().HToolbar().Name( "TopMainToolbar" )
  169. .Top().Layer( 6 ) );
  170. m_auimgr.AddPane( m_messagePanel, EDA_PANE().Messages().Name( "MsgPanel" )
  171. .Bottom().Layer( 6 ) );
  172. // Columns; layers 1 - 3
  173. m_auimgr.AddPane( m_treePane, EDA_PANE().Palette().Name( "LibraryTree" )
  174. .Left().Layer( 3 )
  175. .TopDockable( false ).BottomDockable( false )
  176. .Caption( _( "Libraries" ) )
  177. .MinSize( FromDIP( 250 ), -1 ).BestSize( FromDIP( 250 ), -1 ) );
  178. m_auimgr.AddPane( m_propertiesPanel, defaultPropertiesPaneInfo( this ) );
  179. // Show or hide m_propertiesPanel depending on current settings:
  180. wxAuiPaneInfo& propertiesPaneInfo = m_auimgr.GetPane( PropertiesPaneName() );
  181. m_auimgr.AddPane( m_selectionFilterPanel, defaultSchSelectionFilterPaneInfo( this ) );
  182. wxAuiPaneInfo& selectionFilterPane = m_auimgr.GetPane( wxS( "SelectionFilter" ) );
  183. // The selection filter doesn't need to grow in the vertical direction when docked
  184. selectionFilterPane.dock_proportion = 0;
  185. propertiesPaneInfo.Show( m_settings->m_AuiPanels.show_properties );
  186. updateSelectionFilterVisbility();
  187. m_auimgr.AddPane( m_tbLeft, EDA_PANE().VToolbar().Name( "LeftToolbar" )
  188. .Left().Layer( 2 ) );
  189. m_auimgr.AddPane( m_tbRight, EDA_PANE().VToolbar().Name( "RightToolbar" )
  190. .Right().Layer( 2 ) );
  191. // Center
  192. m_auimgr.AddPane( GetCanvas(), wxAuiPaneInfo().Name( "DrawFrame" )
  193. .CentrePane() );
  194. FinishAUIInitialization();
  195. // Can't put this in LoadSettings, because it has to be called before setupTools :/
  196. EE_SELECTION_TOOL* selTool = GetToolManager()->GetTool<EE_SELECTION_TOOL>();
  197. selTool->GetFilter() = GetSettings()->m_SelectionFilter;
  198. if( m_settings->m_LibWidth > 0 )
  199. SetAuiPaneSize( m_auimgr, m_auimgr.GetPane( "LibraryTree" ), m_settings->m_LibWidth, -1 );
  200. Raise();
  201. Show( true );
  202. SyncView();
  203. GetCanvas()->GetView()->UseDrawPriority( true );
  204. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  205. setupUnits( m_settings );
  206. // Set the working/draw area size to display a symbol to a reasonable value:
  207. // A 600mm x 600mm with a origin at the area center looks like a large working area
  208. double max_size_x = schIUScale.mmToIU( 600 );
  209. double max_size_y = schIUScale.mmToIU( 600 );
  210. BOX2D bbox;
  211. bbox.SetOrigin( -max_size_x /2, -max_size_y/2 );
  212. bbox.SetSize( max_size_x, max_size_y );
  213. GetCanvas()->GetView()->SetBoundary( bbox );
  214. m_toolManager->RunAction( ACTIONS::zoomFitScreen );
  215. m_acceptedExts.emplace( FILEEXT::KiCadSymbolLibFileExtension, &ACTIONS::ddAddLibrary );
  216. DragAcceptFiles( true );
  217. KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Library changes are unsaved" ) );
  218. // Catch unhandled accelerator command characters that were no handled by the library tree
  219. // panel.
  220. Bind( wxEVT_CHAR, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
  221. Bind( wxEVT_CHAR_HOOK, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
  222. // Ensure the window is on top
  223. Raise();
  224. if( loadingCancelled )
  225. ShowInfoBarWarning( _( "Symbol library loading was cancelled by user." ) );
  226. }
  227. SYMBOL_EDIT_FRAME::~SYMBOL_EDIT_FRAME()
  228. {
  229. // Shutdown all running tools
  230. if( m_toolManager )
  231. m_toolManager->ShutdownAllTools();
  232. setSymWatcher( nullptr );
  233. if( IsSymbolFromSchematic() )
  234. {
  235. delete m_symbol;
  236. m_symbol = nullptr;
  237. SCH_SCREEN* screen = GetScreen();
  238. delete screen;
  239. m_isSymbolFromSchematic = false;
  240. }
  241. // current screen is destroyed in EDA_DRAW_FRAME
  242. SetScreen( m_dummyScreen );
  243. SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
  244. SYMBOL_EDITOR_SETTINGS* cfg = mgr.GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" );
  245. if( cfg )
  246. mgr.Save( cfg );
  247. delete m_libMgr;
  248. }
  249. void SYMBOL_EDIT_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  250. {
  251. wxCHECK_RET( m_settings, "Call to SYMBOL_EDIT_FRAME::LoadSettings with null m_boardAdapter" );
  252. SCH_BASE_FRAME::LoadSettings( GetSettings() );
  253. GetRenderSettings()->m_ShowPinsElectricalType = m_settings->m_ShowPinElectricalType;
  254. GetRenderSettings()->m_ShowHiddenPins = m_settings->m_ShowHiddenPins;
  255. GetRenderSettings()->m_ShowHiddenFields = m_settings->m_ShowHiddenFields;
  256. GetRenderSettings()->m_ShowPinAltIcons = m_settings->m_ShowPinAltIcons;
  257. GetRenderSettings()->SetDefaultFont( wxEmptyString );
  258. }
  259. void SYMBOL_EDIT_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
  260. {
  261. wxCHECK_RET( m_settings, "Call to SYMBOL_EDIT_FRAME::LoadSettings with null m_boardAdapter" );
  262. GetGalDisplayOptions().m_axesEnabled = true;
  263. SCH_BASE_FRAME::SaveSettings( GetSettings() );
  264. m_settings->m_ShowPinElectricalType = GetRenderSettings()->m_ShowPinsElectricalType;
  265. m_settings->m_ShowHiddenPins = GetRenderSettings()->m_ShowHiddenPins;
  266. m_settings->m_ShowHiddenFields = GetRenderSettings()->m_ShowHiddenFields;
  267. m_settings->m_ShowPinAltIcons = GetRenderSettings()->m_ShowPinAltIcons;
  268. m_settings->m_LibWidth = m_treePane->GetSize().x;
  269. m_settings->m_LibrarySortMode = GetLibTree()->GetSortMode();
  270. m_settings->m_AuiPanels.properties_splitter = m_propertiesPanel->SplitterProportion();
  271. bool prop_shown = m_auimgr.GetPane( PropertiesPaneName() ).IsShown();
  272. m_settings->m_AuiPanels.show_properties = prop_shown;
  273. EE_SELECTION_TOOL* selTool = GetToolManager()->GetTool<EE_SELECTION_TOOL>();
  274. m_settings->m_SelectionFilter = selTool->GetFilter();
  275. }
  276. APP_SETTINGS_BASE* SYMBOL_EDIT_FRAME::config() const
  277. {
  278. return static_cast<APP_SETTINGS_BASE*>( GetSettings() );
  279. }
  280. COLOR_SETTINGS* SYMBOL_EDIT_FRAME::GetColorSettings( bool aForceRefresh ) const
  281. {
  282. SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
  283. if( GetSettings()->m_UseEeschemaColorSettings )
  284. return mgr.GetColorSettings( mgr.GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" )->m_ColorTheme );
  285. else
  286. return mgr.GetColorSettings( GetSettings()->m_ColorTheme );
  287. }
  288. void SYMBOL_EDIT_FRAME::setupTools()
  289. {
  290. // Create the manager and dispatcher & route draw panel events to the dispatcher
  291. m_toolManager = new TOOL_MANAGER;
  292. m_toolManager->SetEnvironment( GetScreen(), GetCanvas()->GetView(),
  293. GetCanvas()->GetViewControls(), GetSettings(), this );
  294. m_actions = new EE_ACTIONS();
  295. m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager );
  296. // Register tools
  297. m_toolManager->RegisterTool( new COMMON_CONTROL );
  298. m_toolManager->RegisterTool( new COMMON_TOOLS );
  299. m_toolManager->RegisterTool( new ZOOM_TOOL );
  300. m_toolManager->RegisterTool( new EE_SELECTION_TOOL );
  301. m_toolManager->RegisterTool( new PICKER_TOOL );
  302. m_toolManager->RegisterTool( new EE_INSPECTION_TOOL );
  303. m_toolManager->RegisterTool( new SYMBOL_EDITOR_PIN_TOOL );
  304. m_toolManager->RegisterTool( new SYMBOL_EDITOR_DRAWING_TOOLS );
  305. m_toolManager->RegisterTool( new EE_POINT_EDITOR );
  306. m_toolManager->RegisterTool( new SYMBOL_EDITOR_MOVE_TOOL );
  307. m_toolManager->RegisterTool( new SYMBOL_EDITOR_EDIT_TOOL );
  308. m_toolManager->RegisterTool( new LIBRARY_EDITOR_CONTROL );
  309. m_toolManager->RegisterTool( new SYMBOL_EDITOR_CONTROL );
  310. m_toolManager->RegisterTool( new PROPERTIES_TOOL );
  311. m_toolManager->RegisterTool( new EMBED_TOOL );
  312. m_toolManager->InitTools();
  313. // Run the selection tool, it is supposed to be always active
  314. m_toolManager->InvokeTool( "eeschema.InteractiveSelection" );
  315. GetCanvas()->SetEventDispatcher( m_toolDispatcher );
  316. }
  317. void SYMBOL_EDIT_FRAME::setupUIConditions()
  318. {
  319. SCH_BASE_FRAME::setupUIConditions();
  320. ACTION_MANAGER* mgr = m_toolManager->GetActionManager();
  321. EDITOR_CONDITIONS cond( this );
  322. wxASSERT( mgr );
  323. #define ENABLE( x ) ACTION_CONDITIONS().Enable( x )
  324. #define CHECK( x ) ACTION_CONDITIONS().Check( x )
  325. auto haveSymbolCond =
  326. [this]( const SELECTION& )
  327. {
  328. return m_symbol;
  329. };
  330. auto isEditableCond =
  331. [this]( const SELECTION& )
  332. {
  333. // Only root symbols from the new s-expression libraries or the schematic
  334. // are editable.
  335. return IsSymbolEditable() && !IsSymbolAlias();
  336. };
  337. auto isEditableInAliasCond =
  338. [this]( const SELECTION& )
  339. {
  340. // Less restrictive than isEditableCond
  341. // Symbols fields (root symbols and aliases) from the new s-expression libraries
  342. // or in the schematic are editable.
  343. return IsSymbolEditable();
  344. };
  345. auto symbolModifiedCondition =
  346. [this]( const SELECTION& sel )
  347. {
  348. return m_libMgr && m_libMgr->IsSymbolModified( GetTargetLibId().GetLibItemName(),
  349. GetTargetLibId().GetLibNickname() );
  350. };
  351. auto libSelectedCondition =
  352. [this]( const SELECTION& sel )
  353. {
  354. return !GetTargetLibId().GetLibNickname().empty();
  355. };
  356. auto canEditProperties =
  357. [this]( const SELECTION& sel )
  358. {
  359. return m_symbol && ( !IsSymbolFromLegacyLibrary() || IsSymbolFromSchematic() );
  360. };
  361. auto saveSymbolAsCondition =
  362. [this]( const SELECTION& aSel )
  363. {
  364. return getTargetSymbol() != nullptr;
  365. };
  366. const auto isSymbolFromSchematicCond =
  367. [this]( const SELECTION& )
  368. {
  369. return IsSymbolFromSchematic();
  370. };
  371. // clang-format off
  372. mgr->SetConditions( ACTIONS::saveAll, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  373. mgr->SetConditions( ACTIONS::save, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  374. mgr->SetConditions( EE_ACTIONS::saveLibraryAs, ENABLE( libSelectedCondition ) );
  375. mgr->SetConditions( EE_ACTIONS::saveSymbolAs, ENABLE( saveSymbolAsCondition ) );
  376. mgr->SetConditions( EE_ACTIONS::saveSymbolCopyAs, ENABLE( saveSymbolAsCondition ) );
  377. mgr->SetConditions( EE_ACTIONS::newSymbol, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  378. mgr->SetConditions( EE_ACTIONS::importSymbol, ENABLE( SELECTION_CONDITIONS::ShowAlways ) );
  379. mgr->SetConditions( EE_ACTIONS::editLibSymbolWithLibEdit, ENABLE( isSymbolFromSchematicCond ) );
  380. mgr->SetConditions( ACTIONS::undo, ENABLE( haveSymbolCond && cond.UndoAvailable() ) );
  381. mgr->SetConditions( ACTIONS::redo, ENABLE( haveSymbolCond && cond.RedoAvailable() ) );
  382. mgr->SetConditions( ACTIONS::revert, ENABLE( symbolModifiedCondition ) );
  383. mgr->SetConditions( ACTIONS::toggleGrid, CHECK( cond.GridVisible() ) );
  384. mgr->SetConditions( ACTIONS::toggleGridOverrides, CHECK( cond.GridOverrides() ) );
  385. mgr->SetConditions( ACTIONS::toggleCursorStyle, CHECK( cond.FullscreenCursor() ) );
  386. mgr->SetConditions( ACTIONS::millimetersUnits, CHECK( cond.Units( EDA_UNITS::MM ) ) );
  387. mgr->SetConditions( ACTIONS::inchesUnits, CHECK( cond.Units( EDA_UNITS::INCH ) ) );
  388. mgr->SetConditions( ACTIONS::milsUnits, CHECK( cond.Units( EDA_UNITS::MILS ) ) );
  389. mgr->SetConditions( ACTIONS::cut, ENABLE( isEditableCond ) );
  390. mgr->SetConditions( ACTIONS::copy, ENABLE( haveSymbolCond ) );
  391. mgr->SetConditions( ACTIONS::copyAsText, ENABLE( haveSymbolCond ) );
  392. mgr->SetConditions( ACTIONS::paste, ENABLE( isEditableCond &&
  393. SELECTION_CONDITIONS::Idle && cond.NoActiveTool() ) );
  394. mgr->SetConditions( ACTIONS::doDelete, ENABLE( isEditableCond ) );
  395. mgr->SetConditions( ACTIONS::duplicate, ENABLE( isEditableCond ) );
  396. mgr->SetConditions( ACTIONS::selectAll, ENABLE( haveSymbolCond ) );
  397. mgr->SetConditions( ACTIONS::unselectAll, ENABLE( haveSymbolCond ) );
  398. // These actions in symbol editor when editing alias field rotations are allowed.
  399. mgr->SetConditions( EE_ACTIONS::rotateCW, ENABLE( isEditableInAliasCond ) );
  400. mgr->SetConditions( EE_ACTIONS::rotateCCW, ENABLE( isEditableInAliasCond ) );
  401. mgr->SetConditions( EE_ACTIONS::mirrorH, ENABLE( isEditableCond ) );
  402. mgr->SetConditions( EE_ACTIONS::mirrorV, ENABLE( isEditableCond ) );
  403. mgr->SetConditions( ACTIONS::zoomTool, CHECK( cond.CurrentTool( ACTIONS::zoomTool ) ) );
  404. mgr->SetConditions( ACTIONS::selectionTool, CHECK( cond.CurrentTool( ACTIONS::selectionTool ) ) );
  405. // clang-format on
  406. auto pinTypeCond =
  407. [this]( const SELECTION& )
  408. {
  409. return libeditconfig()->m_ShowPinElectricalType;
  410. };
  411. auto hiddenPinCond =
  412. [this]( const SELECTION& )
  413. {
  414. return libeditconfig()->m_ShowHiddenPins;
  415. };
  416. auto hiddenFieldCond =
  417. [this]( const SELECTION& )
  418. {
  419. return libeditconfig()->m_ShowHiddenFields;
  420. };
  421. auto showPinAltIconsCond =
  422. [this]( const SELECTION& )
  423. {
  424. return libeditconfig()->m_ShowPinAltIcons;
  425. };
  426. auto showLibraryTreeCond =
  427. [this]( const SELECTION& )
  428. {
  429. return IsLibraryTreeShown();
  430. };
  431. auto propertiesCond =
  432. [this] ( const SELECTION& )
  433. {
  434. return m_auimgr.GetPane( PropertiesPaneName() ).IsShown();
  435. };
  436. mgr->SetConditions( EE_ACTIONS::showElectricalTypes, CHECK( pinTypeCond ) );
  437. mgr->SetConditions( ACTIONS::toggleBoundingBoxes, CHECK( cond.BoundingBoxes() ) );
  438. mgr->SetConditions( ACTIONS::showLibraryTree, CHECK( showLibraryTreeCond ) );
  439. mgr->SetConditions( ACTIONS::showProperties, CHECK( propertiesCond ) );
  440. mgr->SetConditions( EE_ACTIONS::showHiddenPins, CHECK( hiddenPinCond ) );
  441. mgr->SetConditions( EE_ACTIONS::showHiddenFields, CHECK( hiddenFieldCond ) );
  442. mgr->SetConditions( EE_ACTIONS::togglePinAltIcons, CHECK( showPinAltIconsCond ) );
  443. auto demorganCond =
  444. [this]( const SELECTION& )
  445. {
  446. return GetShowDeMorgan();
  447. };
  448. auto demorganStandardCond =
  449. [this]( const SELECTION& )
  450. {
  451. return m_bodyStyle == BODY_STYLE::BASE;
  452. };
  453. auto demorganAlternateCond =
  454. [this]( const SELECTION& )
  455. {
  456. return m_bodyStyle == BODY_STYLE::DEMORGAN;
  457. };
  458. auto multiUnitModeCond =
  459. [this]( const SELECTION& )
  460. {
  461. return m_symbol && m_symbol->IsMulti() && !m_symbol->UnitsLocked();
  462. };
  463. auto hasMultipleUnitsCond =
  464. [this]( const SELECTION& )
  465. {
  466. return m_symbol && m_symbol->IsMulti();
  467. };
  468. auto syncedPinsModeCond =
  469. [this]( const SELECTION& )
  470. {
  471. return m_SyncPinEdit;
  472. };
  473. auto haveDatasheetCond =
  474. [this]( const SELECTION& )
  475. {
  476. return m_symbol && !m_symbol->GetDatasheetField().GetText().IsEmpty();
  477. };
  478. mgr->SetConditions( ACTIONS::showDatasheet, ENABLE( haveDatasheetCond ) );
  479. mgr->SetConditions( EE_ACTIONS::symbolProperties, ENABLE( canEditProperties && haveSymbolCond ) );
  480. mgr->SetConditions( EE_ACTIONS::runERC, ENABLE( haveSymbolCond ) );
  481. mgr->SetConditions( EE_ACTIONS::pinTable, ENABLE( isEditableCond && haveSymbolCond ) );
  482. mgr->SetConditions( EE_ACTIONS::showDeMorganStandard,
  483. ACTION_CONDITIONS().Enable( demorganCond ).Check( demorganStandardCond ) );
  484. mgr->SetConditions( EE_ACTIONS::showDeMorganAlternate,
  485. ACTION_CONDITIONS().Enable( demorganCond ).Check( demorganAlternateCond ) );
  486. mgr->SetConditions( EE_ACTIONS::toggleSyncedPinsMode,
  487. ACTION_CONDITIONS().Enable( multiUnitModeCond ).Check( syncedPinsModeCond ) );
  488. mgr->SetConditions( EE_ACTIONS::setUnitDisplayName,
  489. ACTION_CONDITIONS().Enable( isEditableCond && hasMultipleUnitsCond ) );
  490. // Only enable a tool if the symbol is edtable
  491. #define EDIT_TOOL( tool ) ACTION_CONDITIONS().Enable( isEditableCond ).Check( cond.CurrentTool( tool ) )
  492. mgr->SetConditions( ACTIONS::deleteTool, EDIT_TOOL( ACTIONS::deleteTool ) );
  493. mgr->SetConditions( EE_ACTIONS::placeSymbolPin, EDIT_TOOL( EE_ACTIONS::placeSymbolPin ) );
  494. mgr->SetConditions( EE_ACTIONS::placeSymbolText, EDIT_TOOL( EE_ACTIONS::placeSymbolText ) );
  495. mgr->SetConditions( EE_ACTIONS::drawSymbolTextBox, EDIT_TOOL( EE_ACTIONS::drawSymbolTextBox ) );
  496. mgr->SetConditions( EE_ACTIONS::drawRectangle, EDIT_TOOL( EE_ACTIONS::drawRectangle ) );
  497. mgr->SetConditions( EE_ACTIONS::drawCircle, EDIT_TOOL( EE_ACTIONS::drawCircle ) );
  498. mgr->SetConditions( EE_ACTIONS::drawArc, EDIT_TOOL( EE_ACTIONS::drawArc ) );
  499. mgr->SetConditions( EE_ACTIONS::drawBezier, EDIT_TOOL( EE_ACTIONS::drawBezier ) );
  500. mgr->SetConditions( EE_ACTIONS::drawSymbolLines, EDIT_TOOL( EE_ACTIONS::drawSymbolLines ) );
  501. mgr->SetConditions( EE_ACTIONS::drawSymbolPolygon, EDIT_TOOL( EE_ACTIONS::drawSymbolPolygon ) );
  502. mgr->SetConditions( EE_ACTIONS::placeSymbolAnchor, EDIT_TOOL( EE_ACTIONS::placeSymbolAnchor ) );
  503. mgr->SetConditions( EE_ACTIONS::importGraphics, EDIT_TOOL( EE_ACTIONS::importGraphics ) );
  504. #undef CHECK
  505. #undef ENABLE
  506. #undef EDIT_TOOL
  507. }
  508. bool SYMBOL_EDIT_FRAME::CanCloseSymbolFromSchematic( bool doClose )
  509. {
  510. if( IsContentModified() )
  511. {
  512. SCH_EDIT_FRAME* schframe = (SCH_EDIT_FRAME*) Kiway().Player( FRAME_SCH, false );
  513. wxString msg = _( "Save changes to '%s' before closing?" );
  514. switch( UnsavedChangesDialog( this, wxString::Format( msg, m_reference ), nullptr ) )
  515. {
  516. case wxID_YES:
  517. if( schframe && GetCurSymbol() ) // Should be always the case
  518. schframe->SaveSymbolToSchematic( *GetCurSymbol(), m_schematicSymbolUUID );
  519. break;
  520. case wxID_NO:
  521. break;
  522. default:
  523. case wxID_CANCEL:
  524. return false;
  525. }
  526. }
  527. if( doClose )
  528. {
  529. SetCurSymbol( nullptr, false );
  530. UpdateTitle();
  531. }
  532. return true;
  533. }
  534. bool SYMBOL_EDIT_FRAME::canCloseWindow( wxCloseEvent& aEvent )
  535. {
  536. // Shutdown blocks must be determined and vetoed as early as possible
  537. if( KIPLATFORM::APP::SupportsShutdownBlockReason()
  538. && aEvent.GetId() == wxEVT_QUERY_END_SESSION
  539. && IsContentModified() )
  540. {
  541. return false;
  542. }
  543. if( m_isSymbolFromSchematic && !CanCloseSymbolFromSchematic( false ) )
  544. return false;
  545. if( !saveAllLibraries( true ) )
  546. return false;
  547. // Save symbol tree column widths
  548. m_libMgr->GetAdapter()->SaveSettings();
  549. return true;
  550. }
  551. void SYMBOL_EDIT_FRAME::doCloseWindow()
  552. {
  553. Destroy();
  554. }
  555. void SYMBOL_EDIT_FRAME::RebuildSymbolUnitsList()
  556. {
  557. if( !m_unitSelectBox )
  558. return;
  559. if( m_unitSelectBox->GetCount() != 0 )
  560. m_unitSelectBox->Clear();
  561. if( !m_symbol || m_symbol->GetUnitCount() <= 1 )
  562. {
  563. m_unit = 1;
  564. m_unitSelectBox->Append( wxEmptyString );
  565. }
  566. else
  567. {
  568. for( int i = 0; i < m_symbol->GetUnitCount(); i++ )
  569. {
  570. wxString unitDisplayName = m_symbol->GetUnitDisplayName( i + 1 );
  571. m_unitSelectBox->Append( unitDisplayName );
  572. }
  573. }
  574. // Ensure the selected unit is compatible with the number of units of the current symbol:
  575. if( m_symbol && m_symbol->GetUnitCount() < m_unit )
  576. m_unit = 1;
  577. m_unitSelectBox->SetSelection(( m_unit > 0 ) ? m_unit - 1 : 0 );
  578. }
  579. void SYMBOL_EDIT_FRAME::ToggleProperties()
  580. {
  581. if( !m_propertiesPanel )
  582. return;
  583. bool show = !m_propertiesPanel->IsShownOnScreen();
  584. wxAuiPaneInfo& propertiesPaneInfo = m_auimgr.GetPane( PropertiesPaneName() );
  585. propertiesPaneInfo.Show( show );
  586. updateSelectionFilterVisbility();
  587. if( show )
  588. {
  589. SetAuiPaneSize( m_auimgr, propertiesPaneInfo,
  590. m_settings->m_AuiPanels.properties_panel_width, -1 );
  591. }
  592. else
  593. {
  594. m_settings->m_AuiPanels.properties_panel_width = m_propertiesPanel->GetSize().x;
  595. }
  596. m_auimgr.Update();
  597. Refresh();
  598. }
  599. void SYMBOL_EDIT_FRAME::ToggleLibraryTree()
  600. {
  601. wxAuiPaneInfo& treePane = m_auimgr.GetPane( m_treePane );
  602. treePane.Show( !IsLibraryTreeShown() );
  603. updateSelectionFilterVisbility();
  604. m_auimgr.Update();
  605. Refresh();
  606. }
  607. bool SYMBOL_EDIT_FRAME::IsLibraryTreeShown() const
  608. {
  609. return const_cast<wxAuiManager&>( m_auimgr ).GetPane( m_treePane ).IsShown();
  610. }
  611. void SYMBOL_EDIT_FRAME::FocusLibraryTreeInput()
  612. {
  613. GetLibTree()->FocusSearchFieldIfExists();
  614. }
  615. void SYMBOL_EDIT_FRAME::FreezeLibraryTree()
  616. {
  617. m_treePane->Freeze();
  618. m_libMgr->GetAdapter()->Freeze();
  619. }
  620. void SYMBOL_EDIT_FRAME::ThawLibraryTree()
  621. {
  622. m_libMgr->GetAdapter()->Thaw();
  623. m_treePane->Thaw();
  624. }
  625. void SYMBOL_EDIT_FRAME::OnExitKiCad( wxCommandEvent& event )
  626. {
  627. Kiway().OnKiCadExit();
  628. }
  629. void SYMBOL_EDIT_FRAME::OnUpdateUnitNumber( wxUpdateUIEvent& event )
  630. {
  631. event.Enable( m_symbol && m_symbol->GetUnitCount() > 1 );
  632. }
  633. void SYMBOL_EDIT_FRAME::OnSelectUnit( wxCommandEvent& event )
  634. {
  635. int i = event.GetSelection();
  636. if( i == wxNOT_FOUND )
  637. return;
  638. SetUnit( i + 1 );
  639. }
  640. bool SYMBOL_EDIT_FRAME::IsSymbolFromLegacyLibrary() const
  641. {
  642. if( m_symbol )
  643. {
  644. SYMBOL_LIB_TABLE_ROW* row = m_libMgr->GetLibrary( m_symbol->GetLibNickname() );
  645. if( row && row->GetType() == SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY ) )
  646. return true;
  647. }
  648. return false;
  649. }
  650. wxString SYMBOL_EDIT_FRAME::GetCurLib() const
  651. {
  652. wxString libNickname = Prj().GetRString( PROJECT::SCH_LIBEDIT_CUR_LIB );
  653. if( !libNickname.empty() )
  654. {
  655. if( !PROJECT_SCH::SchSymbolLibTable( &Prj() )->HasLibrary( libNickname ) )
  656. {
  657. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, wxEmptyString );
  658. libNickname = wxEmptyString;
  659. }
  660. }
  661. return libNickname;
  662. }
  663. wxString SYMBOL_EDIT_FRAME::SetCurLib( const wxString& aLibNickname )
  664. {
  665. wxString old = GetCurLib();
  666. if( aLibNickname.empty() || !PROJECT_SCH::SchSymbolLibTable( &Prj() )->HasLibrary( aLibNickname ) )
  667. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, wxEmptyString );
  668. else
  669. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, aLibNickname );
  670. return old;
  671. }
  672. void SYMBOL_EDIT_FRAME::SetCurSymbol( LIB_SYMBOL* aSymbol, bool aUpdateZoom )
  673. {
  674. wxCHECK( m_toolManager, /* void */ );
  675. m_toolManager->RunAction( EE_ACTIONS::clearSelection );
  676. GetCanvas()->GetView()->Clear();
  677. delete m_symbol;
  678. m_symbol = aSymbol;
  679. // select the current symbol in the tree widget
  680. if( !IsSymbolFromSchematic() && m_symbol )
  681. GetLibTree()->SelectLibId( m_symbol->GetLibId() );
  682. else
  683. GetLibTree()->Unselect();
  684. wxString symbolName;
  685. wxString libName;
  686. if( m_symbol )
  687. {
  688. symbolName = m_symbol->GetName();
  689. libName = UnescapeString( m_symbol->GetLibId().GetLibNickname() );
  690. }
  691. // retain in case this wxFrame is re-opened later on the same PROJECT
  692. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_SYMBOL, symbolName );
  693. // Ensure synchronized pin edit can be enabled only symbols with interchangeable units
  694. m_SyncPinEdit = aSymbol && aSymbol->IsRoot() && aSymbol->IsMulti() && !aSymbol->UnitsLocked();
  695. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  696. GetRenderSettings()->m_ShowUnit = m_unit;
  697. GetRenderSettings()->m_ShowBodyStyle = m_bodyStyle;
  698. GetRenderSettings()->m_ShowDisabled = IsSymbolFromLegacyLibrary() && !IsSymbolFromSchematic();
  699. GetRenderSettings()->m_ShowGraphicsDisabled = IsSymbolAlias() && !IsSymbolFromSchematic();
  700. GetCanvas()->DisplaySymbol( m_symbol );
  701. GetCanvas()->GetView()->HideDrawingSheet();
  702. GetCanvas()->GetView()->ClearHiddenFlags();
  703. if( aUpdateZoom )
  704. m_toolManager->RunAction( ACTIONS::zoomFitScreen );
  705. GetCanvas()->Refresh();
  706. WX_INFOBAR& infobar = *GetInfoBar();
  707. infobar.RemoveAllButtons();
  708. wxArrayString msgs;
  709. int infobarFlags = wxICON_INFORMATION;
  710. if( IsSymbolFromSchematic() )
  711. {
  712. msgs.push_back( wxString::Format( _( "Editing symbol %s from schematic. Saving will "
  713. "update the schematic only." ),
  714. m_reference ) );
  715. wxString link = wxString::Format( _( "Open symbol from library %s" ), libName );
  716. wxHyperlinkCtrl* button = new wxHyperlinkCtrl( &infobar, wxID_ANY, link, wxEmptyString );
  717. button->Bind( wxEVT_COMMAND_HYPERLINK, std::function<void( wxHyperlinkEvent& aEvent )>(
  718. [this, symbolName, libName]( wxHyperlinkEvent& aEvent )
  719. {
  720. GetToolManager()->RunAction( EE_ACTIONS::editLibSymbolWithLibEdit );
  721. } ) );
  722. infobar.AddButton( button );
  723. }
  724. else if( IsSymbolFromLegacyLibrary() )
  725. {
  726. msgs.push_back( _( "Symbols in legacy libraries are not editable. Use Manage Symbol "
  727. "Libraries to migrate to current format." ) );
  728. wxString link = _( "Manage symbol libraries" );
  729. wxHyperlinkCtrl* button = new wxHyperlinkCtrl( &infobar, wxID_ANY, link, wxEmptyString );
  730. button->Bind( wxEVT_COMMAND_HYPERLINK, std::function<void( wxHyperlinkEvent& aEvent )>(
  731. [this]( wxHyperlinkEvent& aEvent )
  732. {
  733. InvokeSchEditSymbolLibTable( &Kiway(), this );
  734. } ) );
  735. infobar.AddButton( button );
  736. }
  737. else if( IsSymbolAlias() )
  738. {
  739. msgs.push_back( wxString::Format( _( "Symbol %s is a derived symbol. Symbol graphics will "
  740. "not be editable." ),
  741. UnescapeString( symbolName ) ) );
  742. // Don't assume the parent symbol shared pointer is still valid.
  743. if( std::shared_ptr<LIB_SYMBOL> rootSymbol = m_symbol->GetRootSymbol() )
  744. {
  745. int unit = GetUnit();
  746. int bodyStyle = GetBodyStyle();
  747. wxString rootSymbolName = rootSymbol->GetName();
  748. wxString link = wxString::Format( _( "Open %s" ), UnescapeString( rootSymbolName ) );
  749. wxHyperlinkCtrl* button = new wxHyperlinkCtrl( &infobar, wxID_ANY, link,
  750. wxEmptyString );
  751. button->Bind( wxEVT_COMMAND_HYPERLINK, std::function<void( wxHyperlinkEvent& aEvent )>(
  752. [this, rootSymbolName, unit, bodyStyle]( wxHyperlinkEvent& aEvent )
  753. {
  754. LoadSymbolFromCurrentLib( rootSymbolName, unit, bodyStyle );
  755. } ) );
  756. infobar.AddButton( button );
  757. }
  758. }
  759. if( m_symbol
  760. && !IsSymbolFromSchematic()
  761. && m_libMgr->IsLibraryReadOnly( m_symbol->GetLibId().GetFullLibraryName() ) )
  762. {
  763. msgs.push_back( _( "Library is read-only. Changes cannot be saved to this library." ) );
  764. wxString link = wxString::Format( _( "Create an editable copy" ) );
  765. wxHyperlinkCtrl* button = new wxHyperlinkCtrl( &infobar, wxID_ANY, link, wxEmptyString );
  766. button->Bind( wxEVT_COMMAND_HYPERLINK, std::function<void( wxHyperlinkEvent& aEvent )>(
  767. [this, symbolName, libName]( wxHyperlinkEvent& aEvent )
  768. {
  769. wxString msg = wxString::Format( _( "Create an editable copy of the symbol or "
  770. "the entire library (%s)?" ),
  771. libName );
  772. KIDIALOG errorDlg( this, msg, _( "Select type of item to save" ),
  773. wxYES_NO | wxCANCEL | wxICON_QUESTION );
  774. // These buttons are in a weird order(?)
  775. errorDlg.SetYesNoCancelLabels( _( "Copy symbol" ), _( "Cancel" ),
  776. _( "Copy library" ) );
  777. int choice = errorDlg.ShowModal();
  778. switch( choice )
  779. {
  780. case wxID_YES:
  781. SaveSymbolCopyAs( true );
  782. break;
  783. case wxID_CANCEL:
  784. SaveLibraryAs();
  785. break;
  786. default:
  787. // Do nothing
  788. break;
  789. }
  790. } ) );
  791. infobar.AddButton( button );
  792. }
  793. if( msgs.empty() )
  794. {
  795. infobar.Dismiss();
  796. }
  797. else
  798. {
  799. wxString msg = wxJoin( msgs, '\n', '\0' );
  800. infobar.ShowMessage( msg, infobarFlags );
  801. }
  802. }
  803. LIB_SYMBOL_LIBRARY_MANAGER& SYMBOL_EDIT_FRAME::GetLibManager()
  804. {
  805. wxASSERT( m_libMgr );
  806. return *m_libMgr;
  807. }
  808. void SYMBOL_EDIT_FRAME::OnModify()
  809. {
  810. EDA_BASE_FRAME::OnModify();
  811. GetScreen()->SetContentModified();
  812. m_autoSaveRequired = true;
  813. if( !IsSymbolFromSchematic() )
  814. storeCurrentSymbol();
  815. GetLibTree()->RefreshLibTree();
  816. if( !GetTitle().StartsWith( "*" ) )
  817. UpdateTitle();
  818. }
  819. void SYMBOL_EDIT_FRAME::SetUnit( int aUnit )
  820. {
  821. wxCHECK( aUnit > 0 && aUnit <= GetCurSymbol()->GetUnitCount(), /* void*/ );
  822. if( m_unit == aUnit )
  823. return;
  824. m_toolManager->RunAction( ACTIONS::cancelInteractive );
  825. m_toolManager->RunAction( EE_ACTIONS::clearSelection );
  826. m_unit = aUnit;
  827. if( m_unitSelectBox->GetSelection() != ( m_unit - 1 ) )
  828. m_unitSelectBox->SetSelection( m_unit - 1 );
  829. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  830. RebuildView();
  831. UpdateSymbolMsgPanelInfo();
  832. }
  833. bool SYMBOL_EDIT_FRAME::SynchronizePins()
  834. {
  835. return m_SyncPinEdit && m_symbol && m_symbol->IsMulti() && !m_symbol->UnitsLocked();
  836. }
  837. wxString SYMBOL_EDIT_FRAME::AddLibraryFile( bool aCreateNew )
  838. {
  839. // Select the target library table (global/project)
  840. SYMBOL_LIB_TABLE* libTable = SelectSymLibTable();
  841. if( !libTable )
  842. return wxEmptyString;
  843. wxFileName fn = m_libMgr->GetUniqueLibraryName();
  844. if( !LibraryFileBrowser( !aCreateNew, fn, FILEEXT::KiCadSymbolLibFileWildcard(),
  845. FILEEXT::KiCadSymbolLibFileExtension, false,
  846. ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() ),
  847. PATHS::GetDefaultUserSymbolsPath() ) )
  848. {
  849. return wxEmptyString;
  850. }
  851. wxString libName = fn.GetName();
  852. if( libName.IsEmpty() )
  853. return wxEmptyString;
  854. if( m_libMgr->LibraryExists( libName ) )
  855. {
  856. DisplayError( this, wxString::Format( _( "Library '%s' already exists." ), libName ) );
  857. return wxEmptyString;
  858. }
  859. if( aCreateNew )
  860. {
  861. if( !m_libMgr->CreateLibrary( fn.GetFullPath(), *libTable ) )
  862. {
  863. DisplayError( this, wxString::Format( _( "Could not create the library file '%s'.\n"
  864. "Make sure you have write permissions and "
  865. "try again." ),
  866. fn.GetFullPath() ) );
  867. return wxEmptyString;
  868. }
  869. }
  870. else
  871. {
  872. if( !m_libMgr->AddLibrary( fn.GetFullPath(), *libTable ) )
  873. {
  874. DisplayError( this, _( "Could not open the library file." ) );
  875. return wxEmptyString;
  876. }
  877. }
  878. bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
  879. saveSymbolLibTables( globalTable, !globalTable );
  880. std::string packet = fn.GetFullPath().ToStdString();
  881. this->Kiway().ExpressMail( FRAME_SCH_SYMBOL_EDITOR, MAIL_LIB_EDIT, packet );
  882. return fn.GetFullPath();
  883. }
  884. void SYMBOL_EDIT_FRAME::DdAddLibrary( wxString aLibFile )
  885. {
  886. // Select the target library table (global/project)
  887. SYMBOL_LIB_TABLE* libTable = SelectSymLibTable();
  888. if( !libTable )
  889. return;
  890. wxFileName fn = wxFileName( aLibFile );
  891. wxString libName = fn.GetName();
  892. if( libName.IsEmpty() )
  893. return;
  894. if( m_libMgr->LibraryExists( libName ) )
  895. {
  896. DisplayError( this, wxString::Format( _( "Library '%s' already exists." ), libName ) );
  897. return;
  898. }
  899. if( !m_libMgr->AddLibrary( fn.GetFullPath(), *libTable ) )
  900. {
  901. DisplayError( this, _( "Could not open the library file." ) );
  902. return;
  903. }
  904. bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
  905. saveSymbolLibTables( globalTable, !globalTable );
  906. std::string packet = fn.GetFullPath().ToStdString();
  907. this->Kiway().ExpressMail( FRAME_SCH_SYMBOL_EDITOR, MAIL_LIB_EDIT, packet );
  908. }
  909. LIB_ID SYMBOL_EDIT_FRAME::GetTreeLIBID( int* aUnit ) const
  910. {
  911. return GetLibTree()->GetSelectedLibId( aUnit );
  912. }
  913. int SYMBOL_EDIT_FRAME::GetTreeSelectionCount() const
  914. {
  915. return GetLibTree()->GetSelectionCount();
  916. }
  917. int SYMBOL_EDIT_FRAME::GetTreeLIBIDs( std::vector<LIB_ID>& aSelection ) const
  918. {
  919. return GetLibTree()->GetSelectedLibIds( aSelection );
  920. }
  921. LIB_SYMBOL* SYMBOL_EDIT_FRAME::getTargetSymbol() const
  922. {
  923. if( IsLibraryTreeShown() )
  924. {
  925. LIB_ID libId = GetTreeLIBID();
  926. if( libId.IsValid() )
  927. return m_libMgr->GetSymbol( libId.GetLibItemName(), libId.GetLibNickname() );
  928. }
  929. return m_symbol;
  930. }
  931. LIB_ID SYMBOL_EDIT_FRAME::GetTargetLibId() const
  932. {
  933. LIB_ID id;
  934. if( IsLibraryTreeShown() )
  935. id = GetTreeLIBID();
  936. if( id.GetLibNickname().empty() && m_symbol )
  937. id = m_symbol->GetLibId();
  938. return id;
  939. }
  940. std::vector<LIB_ID> SYMBOL_EDIT_FRAME::GetSelectedLibIds() const
  941. {
  942. std::vector<LIB_ID> ids;
  943. GetTreeLIBIDs( ids );
  944. return ids;
  945. }
  946. wxString SYMBOL_EDIT_FRAME::getTargetLib() const
  947. {
  948. return GetTargetLibId().GetLibNickname();
  949. }
  950. void SYMBOL_EDIT_FRAME::SyncLibraries( bool aShowProgress, bool aPreloadCancelled,
  951. const wxString& aForceRefresh )
  952. {
  953. LIB_ID selected;
  954. if( m_treePane )
  955. selected = GetLibTree()->GetSelectedLibId();
  956. if( aShowProgress )
  957. {
  958. APP_PROGRESS_DIALOG progressDlg( _( "Loading Symbol Libraries" ), wxEmptyString,
  959. m_libMgr->GetAdapter()->GetLibrariesCount(), this );
  960. m_libMgr->Sync( aForceRefresh,
  961. [&]( int progress, int max, const wxString& libName )
  962. {
  963. progressDlg.Update( progress, wxString::Format( _( "Loading library '%s'..." ),
  964. libName ) );
  965. } );
  966. }
  967. else if( !aPreloadCancelled )
  968. {
  969. m_libMgr->Sync( aForceRefresh,
  970. [&]( int progress, int max, const wxString& libName )
  971. {
  972. } );
  973. }
  974. if( m_treePane )
  975. {
  976. wxDataViewItem found;
  977. if( selected.IsValid() )
  978. {
  979. // Check if the previously selected item is still valid,
  980. // if not - it has to be unselected to prevent crash
  981. found = m_libMgr->GetAdapter()->FindItem( selected );
  982. if( !found )
  983. GetLibTree()->Unselect();
  984. }
  985. GetLibTree()->Regenerate( true );
  986. // Try to select the parent library, in case the symbol is not found
  987. if( !found && selected.IsValid() )
  988. {
  989. selected.SetLibItemName( "" );
  990. found = m_libMgr->GetAdapter()->FindItem( selected );
  991. if( found )
  992. GetLibTree()->SelectLibId( selected );
  993. }
  994. // If no selection, see if there's a current symbol to centre
  995. if( !selected.IsValid() && m_symbol )
  996. {
  997. LIB_ID current( GetCurLib(), m_symbol->GetName() );
  998. GetLibTree()->CenterLibId( current );
  999. }
  1000. }
  1001. }
  1002. void SYMBOL_EDIT_FRAME::RefreshLibraryTree()
  1003. {
  1004. GetLibTree()->RefreshLibTree();
  1005. }
  1006. void SYMBOL_EDIT_FRAME::FocusOnLibId( const LIB_ID& aLibID )
  1007. {
  1008. GetLibTree()->SelectLibId( aLibID );
  1009. }
  1010. void SYMBOL_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem, LIB_SYMBOL* aSymbol )
  1011. {
  1012. if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
  1013. // from file therefore not yet in tree.
  1014. {
  1015. static_cast<LIB_TREE_NODE_ITEM*>( aTreeItem.GetID() )->Update( aSymbol );
  1016. GetLibTree()->RefreshLibTree();
  1017. }
  1018. }
  1019. bool SYMBOL_EDIT_FRAME::backupFile( const wxFileName& aOriginalFile, const wxString& aBackupExt )
  1020. {
  1021. if( aOriginalFile.FileExists() )
  1022. {
  1023. wxFileName backupFileName( aOriginalFile );
  1024. backupFileName.SetExt( aBackupExt );
  1025. if( backupFileName.FileExists() )
  1026. wxRemoveFile( backupFileName.GetFullPath() );
  1027. if( !wxCopyFile( aOriginalFile.GetFullPath(), backupFileName.GetFullPath() ) )
  1028. {
  1029. DisplayError( this, wxString::Format( _( "Failed to save backup to '%s'." ),
  1030. backupFileName.GetFullPath() ) );
  1031. return false;
  1032. }
  1033. }
  1034. return true;
  1035. }
  1036. void SYMBOL_EDIT_FRAME::storeCurrentSymbol()
  1037. {
  1038. if( m_symbol && !GetCurLib().IsEmpty() && GetScreen()->IsContentModified() )
  1039. m_libMgr->UpdateSymbol( m_symbol, GetCurLib() ); // UpdateSymbol() makes a copy
  1040. }
  1041. bool SYMBOL_EDIT_FRAME::IsCurrentSymbol( const LIB_ID& aLibId ) const
  1042. {
  1043. // This will return the root symbol of any alias
  1044. LIB_SYMBOL* symbol = m_libMgr->GetBufferedSymbol( aLibId.GetLibItemName(),
  1045. aLibId.GetLibNickname() );
  1046. // Now we can compare the libId of the current symbol and the root symbol
  1047. return ( symbol && m_symbol && symbol->GetLibId() == m_symbol->GetLibId() );
  1048. }
  1049. void SYMBOL_EDIT_FRAME::emptyScreen()
  1050. {
  1051. GetLibTree()->Unselect();
  1052. SetCurLib( wxEmptyString );
  1053. SetCurSymbol( nullptr, false );
  1054. SetScreen( m_dummyScreen );
  1055. ClearUndoRedoList();
  1056. m_toolManager->RunAction( ACTIONS::zoomFitScreen );
  1057. Refresh();
  1058. }
  1059. void SYMBOL_EDIT_FRAME::CommonSettingsChanged( int aFlags )
  1060. {
  1061. SCH_BASE_FRAME::CommonSettingsChanged( aFlags );
  1062. SETTINGS_MANAGER* mgr = GetSettingsManager();
  1063. SYMBOL_EDITOR_SETTINGS* cfg = mgr->GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" );
  1064. GetRenderSettings()->m_ShowPinsElectricalType = cfg->m_ShowPinElectricalType;
  1065. GetRenderSettings()->m_ShowHiddenPins = cfg->m_ShowHiddenPins;
  1066. GetRenderSettings()->m_ShowHiddenFields = cfg->m_ShowHiddenFields;
  1067. GetRenderSettings()->m_ShowPinAltIcons = cfg->m_ShowPinAltIcons;
  1068. GetGalDisplayOptions().ReadWindowSettings( cfg->m_Window );
  1069. if( m_symbol )
  1070. m_symbol->ClearCaches();
  1071. GetCanvas()->ForceRefresh();
  1072. GetCanvas()->GetView()->UpdateAllItems( KIGFX::ALL );
  1073. GetCanvas()->Refresh();
  1074. RecreateToolbars();
  1075. if( aFlags & ENVVARS_CHANGED )
  1076. SyncLibraries( true );
  1077. Layout();
  1078. SendSizeEvent();
  1079. }
  1080. void SYMBOL_EDIT_FRAME::ShowChangedLanguage()
  1081. {
  1082. // call my base class
  1083. SCH_BASE_FRAME::ShowChangedLanguage();
  1084. // tooltips in toolbars
  1085. RecreateToolbars();
  1086. // For some obscure reason, the AUI manager hides the first modified pane.
  1087. // So force show panes
  1088. wxAuiPaneInfo& tree_pane_info = m_auimgr.GetPane( m_treePane );
  1089. bool tree_shown = tree_pane_info.IsShown();
  1090. tree_pane_info.Caption( _( "Libraries" ) );
  1091. tree_pane_info.Show( tree_shown );
  1092. m_auimgr.Update();
  1093. GetLibTree()->ShowChangedLanguage();
  1094. // status bar
  1095. UpdateMsgPanel();
  1096. if( GetRenderSettings()->m_ShowPinsElectricalType )
  1097. {
  1098. GetCanvas()->GetView()->UpdateAllItems( KIGFX::ALL );
  1099. GetCanvas()->Refresh();
  1100. }
  1101. UpdateTitle();
  1102. }
  1103. void SYMBOL_EDIT_FRAME::SetScreen( BASE_SCREEN* aScreen )
  1104. {
  1105. SCH_BASE_FRAME::SetScreen( aScreen );
  1106. // Let tools add things to the view if necessary
  1107. if( m_toolManager )
  1108. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  1109. }
  1110. void SYMBOL_EDIT_FRAME::RebuildView()
  1111. {
  1112. GetRenderSettings()->m_ShowUnit = m_unit;
  1113. GetRenderSettings()->m_ShowBodyStyle = m_bodyStyle;
  1114. GetRenderSettings()->m_ShowDisabled = IsSymbolFromLegacyLibrary() && !IsSymbolFromSchematic();
  1115. GetRenderSettings()->m_ShowGraphicsDisabled = IsSymbolAlias() && !IsSymbolFromSchematic();
  1116. GetCanvas()->DisplaySymbol( m_symbol );
  1117. GetCanvas()->GetView()->HideDrawingSheet();
  1118. GetCanvas()->GetView()->ClearHiddenFlags();
  1119. // Let tools add things to the view if necessary
  1120. if( m_toolManager )
  1121. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  1122. GetCanvas()->GetView()->UpdateAllItems( KIGFX::ALL );
  1123. GetCanvas()->Refresh();
  1124. }
  1125. void SYMBOL_EDIT_FRAME::HardRedraw()
  1126. {
  1127. SyncLibraries( true );
  1128. if( m_symbol )
  1129. {
  1130. EE_SELECTION_TOOL* selectionTool = m_toolManager->GetTool<EE_SELECTION_TOOL>();
  1131. EE_SELECTION& selection = selectionTool->GetSelection();
  1132. for( SCH_ITEM& item : m_symbol->GetDrawItems() )
  1133. {
  1134. if( !alg::contains( selection, &item ) )
  1135. item.ClearSelected();
  1136. else
  1137. item.SetSelected();
  1138. }
  1139. m_symbol->ClearCaches();
  1140. }
  1141. RebuildView();
  1142. }
  1143. const BOX2I SYMBOL_EDIT_FRAME::GetDocumentExtents( bool aIncludeAllVisible ) const
  1144. {
  1145. if( !m_symbol )
  1146. {
  1147. // Gives a reasonable drawing area size
  1148. int width = schIUScale.mmToIU( 50 );
  1149. int height = schIUScale.mmToIU( 30 );
  1150. return BOX2I( VECTOR2I( -width/2, -height/2 ), VECTOR2I( width, height ) );
  1151. }
  1152. else
  1153. {
  1154. return m_symbol->Flatten()->GetUnitBoundingBox( m_unit, m_bodyStyle );
  1155. }
  1156. }
  1157. void SYMBOL_EDIT_FRAME::FocusOnItem( SCH_ITEM* aItem )
  1158. {
  1159. static KIID lastBrightenedItemID( niluuid );
  1160. SCH_ITEM* lastItem = nullptr;
  1161. if( m_symbol )
  1162. {
  1163. for( SCH_PIN* pin : m_symbol->GetPins() )
  1164. {
  1165. if( pin->m_Uuid == lastBrightenedItemID )
  1166. lastItem = pin;
  1167. }
  1168. std::vector<SCH_FIELD*> fields;
  1169. m_symbol->GetFields( fields );
  1170. for( SCH_FIELD* field : fields )
  1171. {
  1172. if( field->m_Uuid == lastBrightenedItemID )
  1173. lastItem = field;
  1174. }
  1175. }
  1176. if( lastItem && lastItem != aItem )
  1177. {
  1178. lastItem->ClearBrightened();
  1179. UpdateItem( lastItem );
  1180. lastBrightenedItemID = niluuid;
  1181. }
  1182. if( aItem )
  1183. {
  1184. if( !aItem->IsBrightened() )
  1185. {
  1186. aItem->SetBrightened();
  1187. UpdateItem( aItem );
  1188. lastBrightenedItemID = aItem->m_Uuid;
  1189. }
  1190. FocusOnLocation( VECTOR2I( aItem->GetFocusPosition().x, -aItem->GetFocusPosition().y ) );
  1191. }
  1192. }
  1193. void SYMBOL_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  1194. {
  1195. const std::string& payload = mail.GetPayload();
  1196. switch( mail.Command() )
  1197. {
  1198. case MAIL_LIB_EDIT:
  1199. if( !payload.empty() )
  1200. {
  1201. wxString libFileName( payload );
  1202. wxString libNickname;
  1203. wxString msg;
  1204. SYMBOL_LIB_TABLE* libTable = PROJECT_SCH::SchSymbolLibTable( &Prj() );
  1205. const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( libFileName );
  1206. if( !libTableRow )
  1207. {
  1208. msg.Printf( _( "The current configuration does not include the symbol library '%s'." ),
  1209. libFileName );
  1210. msg += wxS( "\n" ) + _( "Use Manage Symbol Libraries to edit the configuration." );
  1211. DisplayErrorMessage( this, _( "Library not found in symbol library table." ), msg );
  1212. break;
  1213. }
  1214. libNickname = libTableRow->GetNickName();
  1215. if( !libTable->HasLibrary( libNickname, true ) )
  1216. {
  1217. msg.Printf( _( "The symbol library '%s' is not enabled in the current configuration." ),
  1218. UnescapeString( libNickname ) );
  1219. msg += wxS( "\n" ) + _( "Use Manage Symbol Libraries to edit the configuration." );
  1220. DisplayErrorMessage( this, _( "Symbol library not enabled." ), msg );
  1221. break;
  1222. }
  1223. SetCurLib( libNickname );
  1224. if( m_treePane )
  1225. {
  1226. LIB_ID id( libNickname, wxEmptyString );
  1227. GetLibTree()->SelectLibId( id );
  1228. GetLibTree()->ExpandLibId( id );
  1229. GetLibTree()->CenterLibId( id );
  1230. }
  1231. }
  1232. break;
  1233. case MAIL_RELOAD_LIB:
  1234. {
  1235. wxString currentLib = GetCurLib();
  1236. SYMBOL_LIB_TABLE* libTable = PROJECT_SCH::SchSymbolLibTable( &Prj() );
  1237. FreezeLibraryTree();
  1238. // Check if the currently selected symbol library been removed or disabled.
  1239. if( !currentLib.empty() && libTable && !libTable->HasLibrary( currentLib, true ) )
  1240. {
  1241. SetCurLib( wxEmptyString );
  1242. emptyScreen();
  1243. }
  1244. SyncLibraries( true );
  1245. ThawLibraryTree();
  1246. RefreshLibraryTree();
  1247. break;
  1248. }
  1249. case MAIL_REFRESH_SYMBOL:
  1250. {
  1251. SYMBOL_LIB_TABLE* tbl = PROJECT_SCH::SchSymbolLibTable( &Prj() );
  1252. LIB_SYMBOL* symbol = GetCurSymbol();
  1253. wxLogTrace( "KICAD_LIB_WATCH", "Received refresh symbol request for %s",
  1254. payload );
  1255. if( !tbl || !symbol )
  1256. break;
  1257. wxString libName = symbol->GetLibId().GetLibNickname();
  1258. const SYMBOL_LIB_TABLE_ROW* row = tbl->FindRow( libName );
  1259. if( !row )
  1260. return;
  1261. wxFileName libfullname( row->GetFullURI( true ) );
  1262. wxFileName changedLib( mail.GetPayload() );
  1263. wxLogTrace( "KICAD_LIB_WATCH",
  1264. "Received refresh symbol request for %s, current symbols is %s",
  1265. changedLib.GetFullPath(), libfullname.GetFullPath() );
  1266. if( changedLib == libfullname )
  1267. {
  1268. wxLogTrace( "KICAD_LIB_WATCH", "Refreshing symbol %s", symbol->GetName() );
  1269. SetScreen( m_dummyScreen ); // UpdateLibraryBuffer will destroy the old screen
  1270. m_libMgr->UpdateLibraryBuffer( libName );
  1271. LIB_SYMBOL* lib_symbol = m_libMgr->GetBufferedSymbol( symbol->GetName(), libName );
  1272. wxCHECK2_MSG( lib_symbol, break, wxString::Format( "Symbol %s not found in library %s",
  1273. symbol->GetName(), libName ) );
  1274. // The buffered screen for the symbol
  1275. SCH_SCREEN* symbol_screen = m_libMgr->GetScreen( lib_symbol->GetName(), libName );
  1276. SetScreen( symbol_screen );
  1277. SetCurSymbol( new LIB_SYMBOL( *lib_symbol ), false );
  1278. RebuildSymbolUnitsList();
  1279. SetShowDeMorgan( GetCurSymbol()->HasAlternateBodyStyle() );
  1280. if( m_toolManager )
  1281. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  1282. }
  1283. break;
  1284. }
  1285. default:
  1286. ;
  1287. }
  1288. }
  1289. std::unique_ptr<GRID_HELPER> SYMBOL_EDIT_FRAME::MakeGridHelper()
  1290. {
  1291. return std::make_unique<EE_GRID_HELPER>( m_toolManager );
  1292. }
  1293. void SYMBOL_EDIT_FRAME::SwitchCanvas( EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType )
  1294. {
  1295. // switches currently used canvas ( Cairo / OpenGL):
  1296. SCH_BASE_FRAME::SwitchCanvas( aCanvasType );
  1297. // Set options specific to symbol editor (axies are always enabled):
  1298. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  1299. GetCanvas()->GetGAL()->SetAxesColor( m_colorSettings->GetColor( LAYER_SCHEMATIC_GRID_AXES ) );
  1300. }
  1301. bool SYMBOL_EDIT_FRAME::HasLibModifications() const
  1302. {
  1303. wxCHECK( m_libMgr, false );
  1304. return m_libMgr->HasModifications();
  1305. }
  1306. bool SYMBOL_EDIT_FRAME::IsContentModified() const
  1307. {
  1308. wxCHECK( m_libMgr, false );
  1309. // Test if the currently edited symbol is modified
  1310. if( GetScreen() && GetScreen()->IsContentModified() && GetCurSymbol() )
  1311. return true;
  1312. // Test if any library has been modified
  1313. for( const wxString& libName : m_libMgr->GetLibraryNames() )
  1314. {
  1315. if( m_libMgr->IsLibraryModified( libName ) && !m_libMgr->IsLibraryReadOnly( libName ) )
  1316. return true;
  1317. }
  1318. return false;
  1319. }
  1320. void SYMBOL_EDIT_FRAME::ClearUndoORRedoList( UNDO_REDO_LIST whichList, int aItemCount )
  1321. {
  1322. if( aItemCount == 0 )
  1323. return;
  1324. UNDO_REDO_CONTAINER& list = ( whichList == UNDO_LIST ) ? m_undoList : m_redoList;
  1325. if( aItemCount < 0 )
  1326. {
  1327. list.ClearCommandList();
  1328. }
  1329. else
  1330. {
  1331. for( int ii = 0; ii < aItemCount; ii++ )
  1332. {
  1333. if( list.m_CommandsList.size() == 0 )
  1334. break;
  1335. PICKED_ITEMS_LIST* curr_cmd = list.m_CommandsList[0];
  1336. list.m_CommandsList.erase( list.m_CommandsList.begin() );
  1337. curr_cmd->ClearListAndDeleteItems( []( EDA_ITEM* aItem )
  1338. {
  1339. delete aItem;
  1340. } );
  1341. delete curr_cmd; // Delete command
  1342. }
  1343. }
  1344. }
  1345. SELECTION& SYMBOL_EDIT_FRAME::GetCurrentSelection()
  1346. {
  1347. return m_toolManager->GetTool<EE_SELECTION_TOOL>()->GetSelection();
  1348. }
  1349. void SYMBOL_EDIT_FRAME::LoadSymbolFromSchematic( SCH_SYMBOL* aSymbol )
  1350. {
  1351. std::unique_ptr<LIB_SYMBOL> symbol = aSymbol->GetLibSymbolRef()->Flatten();
  1352. wxCHECK( symbol, /* void */ );
  1353. symbol->SetLibId( aSymbol->GetLibId() );
  1354. // Take in account the symbol orientation and mirroring. to calculate the field
  1355. // positions in symbol editor (i.e. no rotation, no mirroring)
  1356. int orientation = aSymbol->GetOrientation() & ~( SYM_MIRROR_X | SYM_MIRROR_Y );
  1357. int mirror = aSymbol->GetOrientation() & ( SYM_MIRROR_X | SYM_MIRROR_Y );
  1358. std::vector<SCH_FIELD> fullSetOfFields;
  1359. for( const SCH_FIELD& field : aSymbol->GetFields() )
  1360. {
  1361. VECTOR2I pos = field.GetPosition() - aSymbol->GetPosition();
  1362. SCH_FIELD libField( symbol.get(), field.GetId() );
  1363. libField = field;
  1364. // The inverse transform is mirroring before, rotate after
  1365. switch( mirror )
  1366. {
  1367. case SYM_MIRROR_X: pos.y = -pos.y; break;
  1368. case SYM_MIRROR_Y: pos.x = -pos.x; break;
  1369. default: break;
  1370. }
  1371. switch( orientation )
  1372. {
  1373. case SYM_ORIENT_90:
  1374. std::swap( pos.x, pos.y );
  1375. pos.x = - pos.x;
  1376. break;
  1377. case SYM_ORIENT_270:
  1378. std::swap( pos.x, pos.y );
  1379. pos.y = - pos.y;
  1380. break;
  1381. case SYM_ORIENT_180:
  1382. pos.x = - pos.x;
  1383. pos.y = - pos.y;
  1384. break;
  1385. default:
  1386. break;
  1387. }
  1388. libField.SetPosition( pos );
  1389. fullSetOfFields.emplace_back( std::move( libField ) );
  1390. }
  1391. symbol->SetFields( fullSetOfFields );
  1392. if( m_symbol )
  1393. SetCurSymbol( nullptr, false );
  1394. m_isSymbolFromSchematic = true;
  1395. m_schematicSymbolUUID = aSymbol->m_Uuid;
  1396. m_reference = symbol->GetReferenceField().GetText();
  1397. m_unit = std::max( 1, aSymbol->GetUnit() );
  1398. m_bodyStyle = std::max( 1, aSymbol->GetBodyStyle() );
  1399. // Optimize default edit options for this symbol
  1400. // Usually if units are locked, graphic items are specific to each unit
  1401. // and if units are interchangeable, graphic items are common to units
  1402. SYMBOL_EDITOR_DRAWING_TOOLS* tools = GetToolManager()->GetTool<SYMBOL_EDITOR_DRAWING_TOOLS>();
  1403. tools->SetDrawSpecificUnit( symbol->UnitsLocked() );
  1404. // The buffered screen for the symbol
  1405. SCH_SCREEN* tmpScreen = new SCH_SCREEN();
  1406. SetScreen( tmpScreen );
  1407. SetCurSymbol( symbol.release(), true );
  1408. setSymWatcher( nullptr );
  1409. ReCreateMenuBar();
  1410. RecreateToolbars();
  1411. if( IsLibraryTreeShown() )
  1412. ToggleLibraryTree();
  1413. UpdateTitle();
  1414. RebuildSymbolUnitsList();
  1415. SetShowDeMorgan( GetCurSymbol()->HasAlternateBodyStyle() );
  1416. UpdateSymbolMsgPanelInfo();
  1417. // Let tools add things to the view if necessary
  1418. if( m_toolManager )
  1419. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  1420. GetCanvas()->GetView()->UpdateAllItems( KIGFX::ALL );
  1421. GetCanvas()->Refresh();
  1422. }
  1423. bool SYMBOL_EDIT_FRAME::addLibTableEntry( const wxString& aLibFile, TABLE_SCOPE aScope )
  1424. {
  1425. wxFileName fn = aLibFile;
  1426. wxFileName libTableFileName( Prj().GetProjectPath(),
  1427. SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
  1428. wxString libNickname = fn.GetName();
  1429. SYMBOL_LIB_TABLE* libTable = PROJECT_SCH::SchSymbolLibTable( &Prj() );
  1430. const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
  1431. if( libTable->HasLibrary( libNickname ) )
  1432. {
  1433. wxString tmp;
  1434. int suffix = 1;
  1435. while( libTable->HasLibrary( libNickname ) )
  1436. {
  1437. tmp.Printf( "%s%d", fn.GetName(), suffix );
  1438. libNickname = tmp;
  1439. suffix += 1;
  1440. }
  1441. }
  1442. SYMBOL_LIB_TABLE_ROW* row = new SYMBOL_LIB_TABLE_ROW();
  1443. row->SetNickName( libNickname );
  1444. wxString normalizedPath = NormalizePath( aLibFile, &envVars, Prj().GetProjectPath() );
  1445. if( aScope == GLOBAL_LIB_TABLE )
  1446. {
  1447. libTable = &SYMBOL_LIB_TABLE::GetGlobalLibTable();
  1448. libTableFileName = SYMBOL_LIB_TABLE::GetGlobalTableFileName();
  1449. // We cannot normalize against the current project path when saving to global table.
  1450. normalizedPath = NormalizePath( aLibFile, &envVars, wxEmptyString );
  1451. }
  1452. row->SetFullURI( normalizedPath );
  1453. wxCHECK( libTable->InsertRow( row ), false );
  1454. try
  1455. {
  1456. libTable->Save( libTableFileName.GetFullPath() );
  1457. }
  1458. catch( const IO_ERROR& ioe )
  1459. {
  1460. wxString msg = aScope == GLOBAL_LIB_TABLE ? _( "Error saving global library table." )
  1461. : _( "Error saving project library table." );
  1462. wxMessageDialog dlg( this, msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
  1463. dlg.SetExtendedMessage( ioe.What() );
  1464. dlg.ShowModal();
  1465. return false;
  1466. }
  1467. return true;
  1468. }
  1469. bool SYMBOL_EDIT_FRAME::replaceLibTableEntry( const wxString& aLibNickname,
  1470. const wxString& aLibFile )
  1471. {
  1472. // Check the global library table first because checking the project library table
  1473. // checks the global library table as well due to library chaining.
  1474. bool isGlobalTable = true;
  1475. wxFileName libTableFileName = SYMBOL_LIB_TABLE::GetGlobalTableFileName();;
  1476. const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
  1477. SYMBOL_LIB_TABLE* libTable = &SYMBOL_LIB_TABLE::GetGlobalLibTable();
  1478. SYMBOL_LIB_TABLE_ROW* row = libTable->FindRow( aLibNickname );
  1479. if( !row )
  1480. {
  1481. libTableFileName.SetPath( Prj().GetProjectPath() );
  1482. libTableFileName.SetName( SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
  1483. libTable = PROJECT_SCH::SchSymbolLibTable( &Prj() );
  1484. isGlobalTable = false;
  1485. row = libTable->FindRow( aLibNickname );
  1486. }
  1487. wxCHECK( row, false );
  1488. wxString projectPath;
  1489. if( !isGlobalTable )
  1490. projectPath = Prj().GetProjectPath();
  1491. wxString normalizedPath = NormalizePath( aLibFile, &envVars, projectPath );
  1492. row->SetFullURI( normalizedPath );
  1493. row->SetType( "KiCad" );
  1494. try
  1495. {
  1496. libTable->Save( libTableFileName.GetFullPath() );
  1497. }
  1498. catch( const IO_ERROR& ioe )
  1499. {
  1500. wxString msg = isGlobalTable ? _( "Error saving global library table." )
  1501. : _( "Error saving project library table." );
  1502. wxMessageDialog dlg( this, msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
  1503. dlg.SetExtendedMessage( ioe.What() );
  1504. dlg.ShowModal();
  1505. return false;
  1506. }
  1507. return true;
  1508. }
  1509. bool SYMBOL_EDIT_FRAME::IsSymbolAlias() const
  1510. {
  1511. return m_symbol && !m_symbol->IsRoot();
  1512. }
  1513. bool SYMBOL_EDIT_FRAME::IsSymbolEditable() const
  1514. {
  1515. return m_symbol && ( !IsSymbolFromLegacyLibrary() || IsSymbolFromSchematic() );
  1516. }
  1517. void SYMBOL_EDIT_FRAME::UpdateItem( EDA_ITEM* aItem, bool isAddOrDelete, bool aUpdateRtree )
  1518. {
  1519. SCH_BASE_FRAME::UpdateItem( aItem, isAddOrDelete, aUpdateRtree );
  1520. if( EDA_TEXT* eda_text = dynamic_cast<EDA_TEXT*>( aItem ) )
  1521. {
  1522. eda_text->ClearBoundingBoxCache();
  1523. eda_text->ClearRenderCache();
  1524. }
  1525. }
  1526. void SYMBOL_EDIT_FRAME::updateSelectionFilterVisbility()
  1527. {
  1528. wxAuiPaneInfo& treePane = m_auimgr.GetPane( m_treePane );
  1529. wxAuiPaneInfo& propertiesPane = m_auimgr.GetPane( PropertiesPaneName() );
  1530. wxAuiPaneInfo& selectionFilterPane = m_auimgr.GetPane( wxS( "SelectionFilter" ) );
  1531. // Don't give the selection filter its own visibility controls; instead show it if
  1532. // anything else is visible
  1533. bool showFilter = ( treePane.IsShown() && treePane.IsDocked() )
  1534. || ( propertiesPane.IsShown() && propertiesPane.IsDocked() );
  1535. selectionFilterPane.Show( showFilter );
  1536. }
  1537. bool SYMBOL_EDIT_FRAME::GetShowInvisibleFields()
  1538. {
  1539. // Returns the current render option for invisible fields
  1540. return libeditconfig()->m_ShowHiddenFields;
  1541. }
  1542. bool SYMBOL_EDIT_FRAME::GetShowInvisiblePins()
  1543. {
  1544. // Returns the current render option for invisible pins
  1545. return libeditconfig()->m_ShowHiddenPins;
  1546. }