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.

935 lines
27 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
7 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
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
7 years ago
7 years ago
7 years ago
7 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-2019 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 <base_screen.h>
  26. #include <class_library.h>
  27. #include <confirm.h>
  28. #include <eeschema_id.h>
  29. #include <eeschema_settings.h>
  30. #include <fctsys.h>
  31. #include <general.h>
  32. #include <kiface_i.h>
  33. #include <kiway_express.h>
  34. #include <lib_edit_frame.h>
  35. #include <lib_manager.h>
  36. #include <libedit_settings.h>
  37. #include <pgm_base.h>
  38. #include <sch_draw_panel.h>
  39. #include <sch_painter.h>
  40. #include <sch_view.h>
  41. #include <settings/settings_manager.h>
  42. #include <symbol_lib_table.h>
  43. #include <tool/action_toolbar.h>
  44. #include <tool/common_control.h>
  45. #include <tool/common_tools.h>
  46. #include <tool/picker_tool.h>
  47. #include <tool/tool_dispatcher.h>
  48. #include <tool/tool_manager.h>
  49. #include <tool/zoom_tool.h>
  50. #include <tools/ee_actions.h>
  51. #include <tools/ee_inspection_tool.h>
  52. #include <tools/ee_point_editor.h>
  53. #include <tools/ee_selection_tool.h>
  54. #include <tools/lib_control.h>
  55. #include <tools/lib_drawing_tools.h>
  56. #include <tools/lib_edit_tool.h>
  57. #include <tools/lib_move_tool.h>
  58. #include <tools/lib_pin_tool.h>
  59. #include <widgets/lib_tree.h>
  60. #include <widgets/symbol_tree_pane.h>
  61. #include <wildcards_and_files_ext.h>
  62. #include <wx/progdlg.h>
  63. bool LIB_EDIT_FRAME:: m_showDeMorgan = false;
  64. int LIB_EDIT_FRAME:: g_LastTextSize = -1;
  65. double LIB_EDIT_FRAME:: g_LastTextAngle = TEXT_ANGLE_HORIZ;
  66. int LIB_EDIT_FRAME:: g_LastLineWidth = 0;
  67. // these values are overridden when reading the config
  68. int LIB_EDIT_FRAME:: m_textPinNumDefaultSize = Mils2iu( DEFAULTPINNUMSIZE );
  69. int LIB_EDIT_FRAME:: m_textPinNameDefaultSize = Mils2iu( DEFAULTPINNAMESIZE );
  70. int LIB_EDIT_FRAME:: m_defaultPinLength = Mils2iu( DEFAULTPINLENGTH );
  71. FILL_T LIB_EDIT_FRAME:: g_LastFillStyle = NO_FILL;
  72. BEGIN_EVENT_TABLE( LIB_EDIT_FRAME, EDA_DRAW_FRAME )
  73. EVT_CLOSE( LIB_EDIT_FRAME::OnCloseWindow )
  74. EVT_SIZE( LIB_EDIT_FRAME::OnSize )
  75. EVT_COMBOBOX( ID_LIBEDIT_SELECT_PART_NUMBER, LIB_EDIT_FRAME::OnSelectUnit )
  76. // Right vertical toolbar.
  77. EVT_TOOL( ID_LIBEDIT_IMPORT_BODY_BUTT, LIB_EDIT_FRAME::OnImportBody )
  78. EVT_TOOL( ID_LIBEDIT_EXPORT_BODY_BUTT, LIB_EDIT_FRAME::OnExportBody )
  79. // menubar commands
  80. EVT_MENU( wxID_EXIT, LIB_EDIT_FRAME::OnExitKiCad )
  81. EVT_MENU( wxID_CLOSE, LIB_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_PART_NUMBER, LIB_EDIT_FRAME::OnUpdatePartNumber )
  85. END_EVENT_TABLE()
  86. LIB_EDIT_FRAME::LIB_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
  87. SCH_BASE_FRAME( aKiway, aParent, FRAME_SCH_LIB_EDITOR, _( "Library Editor" ),
  88. wxDefaultPosition, wxDefaultSize, KICAD_DEFAULT_DRAWFRAME_STYLE, LIB_EDIT_FRAME_NAME )
  89. {
  90. SetShowDeMorgan( false );
  91. m_DrawSpecificConvert = true;
  92. m_DrawSpecificUnit = false;
  93. m_SyncPinEdit = false;
  94. m_repeatPinStep = Mils2iu( DEFAULT_REPEAT_OFFSET_PIN );
  95. SetShowElectricalType( true );
  96. m_FrameSize = ConvertDialogToPixels( wxSize( 500, 350 ) ); // default in case of no prefs
  97. m_my_part = nullptr;
  98. m_treePane = nullptr;
  99. m_libMgr = nullptr;
  100. m_unit = 1;
  101. m_convert = 1;
  102. m_AboutTitle = "LibEdit";
  103. // Delayed initialization
  104. if( g_LastTextSize == -1 )
  105. g_LastTextSize = GetDefaultTextSize();
  106. // Initialize grid id to the default value 50 mils:
  107. m_LastGridSizeId = ID_POPUP_GRID_LEVEL_50 - ID_POPUP_GRID_LEVEL_1000;
  108. wxIcon icon;
  109. icon.CopyFromBitmap( KiBitmap( icon_libedit_xpm ) );
  110. SetIcon( icon );
  111. m_settings = Pgm().GetSettingsManager().GetAppSettings<LIBEDIT_SETTINGS>();
  112. LoadSettings( m_settings );
  113. // Ensure axis are always drawn
  114. KIGFX::GAL_DISPLAY_OPTIONS& gal_opts = GetGalDisplayOptions();
  115. gal_opts.m_axesEnabled = true;
  116. m_dummyScreen = new SCH_SCREEN( aKiway );
  117. SetScreen( m_dummyScreen );
  118. GetScreen()->m_Center = true;
  119. GetScreen()->SetMaxUndoItems( m_UndoRedoCountMax );
  120. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  121. SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
  122. setupTools();
  123. m_libMgr = new LIB_MANAGER( *this );
  124. SyncLibraries( true );
  125. m_treePane = new SYMBOL_TREE_PANE( this, m_libMgr );
  126. ReCreateMenuBar();
  127. ReCreateHToolbar();
  128. ReCreateVToolbar();
  129. ReCreateOptToolbar();
  130. InitExitKey();
  131. updateTitle();
  132. DisplayCmpDoc();
  133. RebuildSymbolUnitsList();
  134. m_auimgr.SetManagedWindow( this );
  135. m_auimgr.AddPane( m_mainToolBar, EDA_PANE().HToolbar().Name( "MainToolbar" ).Top().Layer(6) );
  136. m_auimgr.AddPane( m_messagePanel, EDA_PANE().Messages().Name( "MsgPanel" ).Bottom().Layer(6) );
  137. m_auimgr.AddPane( m_optionsToolBar, EDA_PANE().VToolbar().Name( "OptToolbar" ).Left().Layer(3) );
  138. m_auimgr.AddPane( m_treePane, EDA_PANE().Palette().Name( "ComponentTree" ).Left().Layer(1)
  139. .Caption( _( "Libraries" ) ).MinSize( 250, -1 )
  140. .BestSize( m_defaultLibWidth, -1 ).Resizable() );
  141. m_auimgr.AddPane( m_drawToolBar, EDA_PANE().VToolbar().Name( "ToolsToolbar" ).Right().Layer(1) );
  142. m_auimgr.AddPane( GetCanvas(), wxAuiPaneInfo().Name( "DrawFrame" ).CentrePane() );
  143. m_auimgr.Update();
  144. GetToolManager()->RunAction( "common.Control.gridPreset", true, m_LastGridSizeId );
  145. Raise();
  146. Show( true );
  147. SyncView();
  148. GetCanvas()->GetViewControls()->SetSnapping( true );
  149. GetCanvas()->GetView()->UseDrawPriority( true );
  150. GetCanvas()->GetGAL()->SetGridVisibility( IsGridVisible() );
  151. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  152. // Set the working/draw area size to display a symbol to a reasonable value:
  153. // A 600mm x 600mm with a origin at the area center looks like a large working area
  154. double max_size_x = Millimeter2iu( 600 );
  155. double max_size_y = Millimeter2iu( 600 );
  156. BOX2D bbox;
  157. bbox.SetOrigin( -max_size_x /2, -max_size_y/2 );
  158. bbox.SetSize( max_size_x, max_size_y );
  159. GetCanvas()->GetView()->SetBoundary( bbox );
  160. m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
  161. SetShutdownBlockReason( _( "Library changes are unsaved" ) );
  162. }
  163. LIB_EDIT_FRAME::~LIB_EDIT_FRAME()
  164. {
  165. // Shutdown all running tools
  166. if( m_toolManager )
  167. m_toolManager->ShutdownAllTools();
  168. // current screen is destroyed in EDA_DRAW_FRAME
  169. SetScreen( m_dummyScreen );
  170. auto libedit = Pgm().GetSettingsManager().GetAppSettings<LIBEDIT_SETTINGS>();
  171. Pgm().GetSettingsManager().Save( libedit );
  172. delete m_libMgr;
  173. }
  174. void LIB_EDIT_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
  175. {
  176. EDA_DRAW_FRAME::LoadSettings( aCfg );
  177. auto cfg = dynamic_cast<LIBEDIT_SETTINGS*>( aCfg );
  178. wxASSERT( cfg );
  179. SetDefaultLineThickness( Mils2iu( cfg->m_Defaults.line_width ) );
  180. SetDefaultPinLength( Mils2iu( cfg->m_Defaults.pin_length ) );
  181. m_textPinNameDefaultSize = Mils2iu( cfg->m_Defaults.pin_name_size );
  182. m_textPinNumDefaultSize = Mils2iu( cfg->m_Defaults.pin_num_size );
  183. SetRepeatDeltaLabel( cfg->m_Repeat.label_delta );
  184. SetRepeatPinStep( Mils2iu( cfg->m_Repeat.pin_step ) );
  185. SetRepeatStep( wxPoint( cfg->m_Repeat.x_step, cfg->m_Repeat.y_step ) );
  186. m_showPinElectricalTypeName = cfg->m_ShowPinElectricalType;
  187. m_defaultLibWidth = cfg->m_LibWidth;
  188. // TODO(JE) does libedit need its own TemplateFieldNames?
  189. auto ee_settings = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  190. wxASSERT( ee_settings );
  191. wxString templateFieldNames = ee_settings->m_Drawing.field_names;
  192. if( !templateFieldNames.IsEmpty() )
  193. {
  194. TEMPLATE_FIELDNAMES_LEXER lexer( TO_UTF8( templateFieldNames ) );
  195. try
  196. {
  197. m_templateFieldNames.Parse( &lexer );
  198. }
  199. catch( const IO_ERROR& DBG( e ) )
  200. {
  201. // @todo show error msg
  202. DBG( printf( "templatefieldnames parsing error: '%s'\n", TO_UTF8( e.What() ) ); )
  203. }
  204. }
  205. auto painter = static_cast<KIGFX::SCH_PAINTER*>( GetCanvas()->GetView()->GetPainter() );
  206. KIGFX::SCH_RENDER_SETTINGS* settings = painter->GetSettings();
  207. settings->m_ShowPinsElectricalType = m_showPinElectricalTypeName;
  208. // Hidden elements must be editable
  209. settings->m_ShowHiddenText = true;
  210. settings->m_ShowHiddenPins = true;
  211. settings->m_ShowUmbilicals = false;
  212. }
  213. void LIB_EDIT_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg)
  214. {
  215. // aCfg will be EESCHEMA_SETTINGS because that's the parent FACE
  216. // so we throw it away here and get our own settings
  217. auto cfg = Pgm().GetSettingsManager().GetAppSettings<LIBEDIT_SETTINGS>();
  218. EDA_DRAW_FRAME::SaveSettings( cfg );
  219. cfg->m_Defaults.line_width = Iu2Mils( GetDefaultLineThickness() );
  220. cfg->m_Defaults.pin_length = Iu2Mils( GetDefaultPinLength() );
  221. cfg->m_Defaults.pin_name_size = Iu2Mils( GetPinNameDefaultSize() );
  222. cfg->m_Defaults.pin_num_size = Iu2Mils( GetPinNumDefaultSize() );
  223. cfg->m_Repeat.label_delta = GetRepeatDeltaLabel();
  224. cfg->m_Repeat.pin_step = Iu2Mils( GetRepeatPinStep() );
  225. cfg->m_Repeat.x_step = Iu2Mils( GetRepeatStep().x );
  226. cfg->m_Repeat.y_step = Iu2Mils( GetRepeatStep().y );
  227. cfg->m_ShowPinElectricalType = GetShowElectricalType();
  228. cfg->m_LibWidth = m_treePane->GetSize().x;
  229. }
  230. void LIB_EDIT_FRAME::setupTools()
  231. {
  232. // Create the manager and dispatcher & route draw panel events to the dispatcher
  233. m_toolManager = new TOOL_MANAGER;
  234. m_toolManager->SetEnvironment( GetScreen(), GetCanvas()->GetView(),
  235. GetCanvas()->GetViewControls(), this );
  236. m_actions = new EE_ACTIONS();
  237. m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager, m_actions );
  238. // Register tools
  239. m_toolManager->RegisterTool( new COMMON_CONTROL );
  240. m_toolManager->RegisterTool( new COMMON_TOOLS );
  241. m_toolManager->RegisterTool( new ZOOM_TOOL );
  242. m_toolManager->RegisterTool( new EE_SELECTION_TOOL );
  243. m_toolManager->RegisterTool( new PICKER_TOOL );
  244. m_toolManager->RegisterTool( new EE_INSPECTION_TOOL );
  245. m_toolManager->RegisterTool( new LIB_PIN_TOOL );
  246. m_toolManager->RegisterTool( new LIB_DRAWING_TOOLS );
  247. m_toolManager->RegisterTool( new EE_POINT_EDITOR );
  248. m_toolManager->RegisterTool( new LIB_MOVE_TOOL );
  249. m_toolManager->RegisterTool( new LIB_EDIT_TOOL );
  250. m_toolManager->RegisterTool( new LIB_CONTROL );
  251. m_toolManager->InitTools();
  252. // Run the selection tool, it is supposed to be always active
  253. m_toolManager->InvokeTool( "eeschema.InteractiveSelection" );
  254. GetCanvas()->SetEventDispatcher( m_toolDispatcher );
  255. }
  256. void LIB_EDIT_FRAME::OnCloseWindow( wxCloseEvent& aEvent )
  257. {
  258. // Shutdown blocks must be determined and vetoed as early as possible
  259. if( SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION
  260. && IsContentModified() )
  261. {
  262. aEvent.Veto();
  263. return;
  264. }
  265. if( saveAllLibraries( true ) )
  266. Destroy();
  267. else
  268. aEvent.Veto();
  269. }
  270. void LIB_EDIT_FRAME::RebuildSymbolUnitsList()
  271. {
  272. if( !m_unitSelectBox )
  273. return;
  274. if( m_unitSelectBox->GetCount() != 0 )
  275. m_unitSelectBox->Clear();
  276. if( !m_my_part || m_my_part->GetUnitCount() <= 1 )
  277. {
  278. m_unit = 1;
  279. m_unitSelectBox->Append( wxEmptyString );
  280. }
  281. else
  282. {
  283. for( int i = 0; i < m_my_part->GetUnitCount(); i++ )
  284. {
  285. wxString sub = LIB_PART::SubReference( i+1, false );
  286. wxString unit = wxString::Format( _( "Unit %s" ), GetChars( sub ) );
  287. m_unitSelectBox->Append( unit );
  288. }
  289. }
  290. // Ensure the selected unit is compatible with the number of units of the current part:
  291. if( m_my_part && m_my_part->GetUnitCount() < m_unit )
  292. m_unit = 1;
  293. m_unitSelectBox->SetSelection(( m_unit > 0 ) ? m_unit - 1 : 0 );
  294. }
  295. void LIB_EDIT_FRAME::OnToggleSearchTree( wxCommandEvent& event )
  296. {
  297. auto& treePane = m_auimgr.GetPane( m_treePane );
  298. treePane.Show( !IsSearchTreeShown() );
  299. m_auimgr.Update();
  300. }
  301. bool LIB_EDIT_FRAME::IsSearchTreeShown()
  302. {
  303. return m_auimgr.GetPane( m_treePane ).IsShown();
  304. }
  305. void LIB_EDIT_FRAME::FreezeSearchTree()
  306. {
  307. m_libMgr->GetAdapter()->Freeze();
  308. }
  309. void LIB_EDIT_FRAME::ThawSearchTree()
  310. {
  311. m_libMgr->GetAdapter()->Thaw();
  312. }
  313. void LIB_EDIT_FRAME::OnExitKiCad( wxCommandEvent& event )
  314. {
  315. Kiway().OnKiCadExit();
  316. }
  317. void LIB_EDIT_FRAME::OnUpdatePartNumber( wxUpdateUIEvent& event )
  318. {
  319. if( !m_unitSelectBox )
  320. return;
  321. // Using the typical event.Enable() call doesn't seem to work with wxGTK
  322. // so use the pointer to alias combobox to directly enable or disable.
  323. m_unitSelectBox->Enable( m_my_part && m_my_part->GetUnitCount() > 1 );
  324. }
  325. void LIB_EDIT_FRAME::OnSelectUnit( wxCommandEvent& event )
  326. {
  327. int i = event.GetSelection();
  328. if( ( i == wxNOT_FOUND ) || ( ( i + 1 ) == m_unit ) )
  329. return;
  330. m_toolManager->RunAction( ACTIONS::cancelInteractive, true );
  331. m_toolManager->RunAction( EE_ACTIONS::clearSelection, true );
  332. m_unit = i + 1;
  333. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  334. RebuildView();
  335. }
  336. wxString LIB_EDIT_FRAME::GetCurLib() const
  337. {
  338. wxString libNickname = Prj().GetRString( PROJECT::SCH_LIBEDIT_CUR_LIB );
  339. if( !libNickname.empty() )
  340. {
  341. if( !Prj().SchSymbolLibTable()->HasLibrary( libNickname ) )
  342. {
  343. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, wxEmptyString );
  344. libNickname = wxEmptyString;
  345. }
  346. }
  347. return libNickname;
  348. }
  349. wxString LIB_EDIT_FRAME::SetCurLib( const wxString& aLibNickname )
  350. {
  351. wxString old = GetCurLib();
  352. if( aLibNickname.empty() || !Prj().SchSymbolLibTable()->HasLibrary( aLibNickname ) )
  353. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, wxEmptyString );
  354. else
  355. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_LIB, aLibNickname );
  356. m_libMgr->SetCurrentLib( aLibNickname );
  357. return old;
  358. }
  359. void LIB_EDIT_FRAME::SetCurPart( LIB_PART* aPart )
  360. {
  361. m_toolManager->RunAction( EE_ACTIONS::clearSelection, true );
  362. if( m_my_part )
  363. delete m_my_part;
  364. m_my_part = aPart;
  365. // select the current component in the tree widget
  366. if( m_my_part )
  367. {
  368. m_treePane->GetLibTree()->SelectLibId( m_my_part->GetLibId() );
  369. m_my_part->GetField( DATASHEET )->SetText( aPart->GetDocFileName() );
  370. }
  371. wxString partName = m_my_part ? m_my_part->GetName() : wxString();
  372. m_libMgr->SetCurrentPart( partName );
  373. // retain in case this wxFrame is re-opened later on the same PROJECT
  374. Prj().SetRString( PROJECT::SCH_LIBEDIT_CUR_PART, partName );
  375. // Ensure synchronized pin edit can be enabled only symbols with interchangeable units
  376. m_SyncPinEdit = aPart && aPart->IsRoot() && aPart->IsMulti() && !aPart->UnitsLocked();
  377. m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
  378. RebuildView();
  379. SyncLibraries( false );
  380. }
  381. LIB_MANAGER& LIB_EDIT_FRAME::GetLibManager()
  382. {
  383. wxASSERT( m_libMgr );
  384. return *m_libMgr;
  385. }
  386. void LIB_EDIT_FRAME::OnImportBody( wxCommandEvent& aEvent )
  387. {
  388. m_toolManager->DeactivateTool();
  389. LoadOneSymbol();
  390. m_drawToolBar->ToggleTool( ID_LIBEDIT_IMPORT_BODY_BUTT, false );
  391. }
  392. void LIB_EDIT_FRAME::OnExportBody( wxCommandEvent& aEvent )
  393. {
  394. m_toolManager->DeactivateTool();
  395. SaveOneSymbol();
  396. m_drawToolBar->ToggleTool( ID_LIBEDIT_EXPORT_BODY_BUTT, false );
  397. }
  398. void LIB_EDIT_FRAME::OnModify()
  399. {
  400. GetScreen()->SetModify();
  401. storeCurrentPart();
  402. m_treePane->GetLibTree()->RefreshLibTree();
  403. }
  404. bool LIB_EDIT_FRAME::SynchronizePins()
  405. {
  406. return m_SyncPinEdit && m_my_part && m_my_part->IsMulti() && !m_my_part->UnitsLocked();
  407. }
  408. void LIB_EDIT_FRAME::refreshSchematic()
  409. {
  410. // There may be no parent window so use KIWAY message to refresh the schematic editor
  411. // in case any symbols have changed.
  412. std::string dummyPayload;
  413. Kiway().ExpressMail( FRAME_SCH, MAIL_SCH_REFRESH, dummyPayload, this );
  414. }
  415. bool LIB_EDIT_FRAME::AddLibraryFile( bool aCreateNew )
  416. {
  417. wxFileName fn = m_libMgr->GetUniqueLibraryName();
  418. if( !LibraryFileBrowser( !aCreateNew, fn, SchematicLibraryFileWildcard(),
  419. SchematicLibraryFileExtension, false ) )
  420. {
  421. return false;
  422. }
  423. wxString libName = fn.GetName();
  424. if( libName.IsEmpty() )
  425. return false;
  426. if( m_libMgr->LibraryExists( libName ) )
  427. {
  428. DisplayError( this, wxString::Format( _( "Library \"%s\" already exists" ), libName ) );
  429. return false;
  430. }
  431. // Select the target library table (global/project)
  432. SYMBOL_LIB_TABLE* libTable = selectSymLibTable();
  433. if( !libTable )
  434. return false;
  435. if( aCreateNew )
  436. {
  437. if( !m_libMgr->CreateLibrary( fn.GetFullPath(), libTable ) )
  438. {
  439. DisplayError( this, wxString::Format( _( "Could not create the library file '%s'.\n"
  440. "Check write permission." ),
  441. fn.GetFullPath() ) );
  442. return false;
  443. }
  444. }
  445. else
  446. {
  447. if( !m_libMgr->AddLibrary( fn.GetFullPath(), libTable ) )
  448. {
  449. DisplayError( this, _( "Could not open the library file." ) );
  450. return false;
  451. }
  452. }
  453. bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
  454. saveSymbolLibTables( globalTable, !globalTable );
  455. return true;
  456. }
  457. LIB_ID LIB_EDIT_FRAME::GetTreeLIBID( int* aUnit ) const
  458. {
  459. return m_treePane->GetLibTree()->GetSelectedLibId( aUnit );
  460. }
  461. LIB_PART* LIB_EDIT_FRAME::getTargetPart() const
  462. {
  463. LIB_ID libId = GetTreeLIBID();
  464. if( libId.IsValid() )
  465. {
  466. LIB_PART* alias = m_libMgr->GetAlias( libId.GetLibItemName(), libId.GetLibNickname() );
  467. return alias;
  468. }
  469. return m_my_part;
  470. }
  471. LIB_ID LIB_EDIT_FRAME::getTargetLibId() const
  472. {
  473. LIB_ID id = GetTreeLIBID();
  474. if( id.GetLibNickname().empty() && m_my_part )
  475. id = m_my_part->GetLibId();
  476. return id;
  477. }
  478. LIB_TREE_NODE* LIB_EDIT_FRAME::GetCurrentTreeNode() const
  479. {
  480. return m_treePane->GetLibTree()->GetCurrentTreeNode();
  481. }
  482. wxString LIB_EDIT_FRAME::getTargetLib() const
  483. {
  484. return getTargetLibId().GetLibNickname();
  485. }
  486. void LIB_EDIT_FRAME::SyncLibraries( bool aShowProgress )
  487. {
  488. LIB_ID selected;
  489. if( m_treePane )
  490. selected = m_treePane->GetLibTree()->GetSelectedLibId();
  491. if( aShowProgress )
  492. {
  493. wxProgressDialog progressDlg( _( "Loading Symbol Libraries" ), wxEmptyString,
  494. m_libMgr->GetAdapter()->GetLibrariesCount(), this );
  495. m_libMgr->Sync( true, [&]( int progress, int max, const wxString& libName )
  496. {
  497. progressDlg.Update( progress, wxString::Format( _( "Loading library \"%s\"" ),
  498. libName ) );
  499. } );
  500. }
  501. else
  502. {
  503. m_libMgr->Sync( true );
  504. }
  505. if( m_treePane )
  506. {
  507. wxDataViewItem found;
  508. if( selected.IsValid() )
  509. {
  510. // Check if the previously selected item is still valid,
  511. // if not - it has to be unselected to prevent crash
  512. found = m_libMgr->GetAdapter()->FindItem( selected );
  513. if( !found )
  514. m_treePane->GetLibTree()->Unselect();
  515. }
  516. m_treePane->GetLibTree()->Regenerate( true );
  517. // Try to select the parent library, in case the part is not found
  518. if( !found && selected.IsValid() )
  519. {
  520. selected.SetLibItemName( "" );
  521. found = m_libMgr->GetAdapter()->FindItem( selected );
  522. if( found )
  523. m_treePane->GetLibTree()->SelectLibId( selected );
  524. }
  525. // If no selection, see if there's a current part to centre
  526. if( !selected.IsValid() && m_my_part )
  527. {
  528. LIB_ID current( GetCurLib(), m_my_part->GetName() );
  529. m_treePane->GetLibTree()->CenterLibId( current );
  530. }
  531. }
  532. }
  533. void LIB_EDIT_FRAME::RegenerateLibraryTree()
  534. {
  535. LIB_ID target = getTargetLibId();
  536. m_treePane->GetLibTree()->Regenerate( true );
  537. if( target.IsValid() )
  538. m_treePane->GetLibTree()->CenterLibId( target );
  539. }
  540. SYMBOL_LIB_TABLE* LIB_EDIT_FRAME::selectSymLibTable( bool aOptional )
  541. {
  542. wxArrayString libTableNames;
  543. libTableNames.Add( _( "Global" ) );
  544. libTableNames.Add( _( "Project" ) );
  545. wxSingleChoiceDialog dlg( this, _( "Choose the Library Table to add the library to:" ),
  546. _( "Add To Library Table" ), libTableNames );
  547. if( aOptional )
  548. {
  549. dlg.FindWindow( wxID_CANCEL )->SetLabel( _( "Skip" ) );
  550. dlg.FindWindow( wxID_OK )->SetLabel( _( "Add" ) );
  551. }
  552. if( dlg.ShowModal() != wxID_OK )
  553. return nullptr;
  554. switch( dlg.GetSelection() )
  555. {
  556. case 0: return &SYMBOL_LIB_TABLE::GetGlobalLibTable();
  557. case 1: return Prj().SchSymbolLibTable();
  558. default: return nullptr;
  559. }
  560. }
  561. bool LIB_EDIT_FRAME::backupFile( const wxFileName& aOriginalFile, const wxString& aBackupExt )
  562. {
  563. if( aOriginalFile.FileExists() )
  564. {
  565. wxFileName backupFileName( aOriginalFile );
  566. backupFileName.SetExt( "bck" );
  567. if( backupFileName.FileExists() )
  568. wxRemoveFile( backupFileName.GetFullPath() );
  569. if( !wxCopyFile( aOriginalFile.GetFullPath(), backupFileName.GetFullPath() ) )
  570. {
  571. DisplayError( this, wxString::Format( _( "Failed to save backup to \"%s\"" ),
  572. backupFileName.GetFullPath() ) );
  573. return false;
  574. }
  575. }
  576. return true;
  577. }
  578. void LIB_EDIT_FRAME::storeCurrentPart()
  579. {
  580. if( m_my_part && !GetCurLib().IsEmpty() && GetScreen()->IsModify() )
  581. m_libMgr->UpdatePart( m_my_part, GetCurLib() ); // UpdatePart() makes a copy
  582. }
  583. bool LIB_EDIT_FRAME::isCurrentPart( const LIB_ID& aLibId ) const
  584. {
  585. // This will return the root part of any alias
  586. LIB_PART* part = m_libMgr->GetBufferedPart( aLibId.GetLibItemName(), aLibId.GetLibNickname() );
  587. // Now we can compare the libId of the current part and the root part
  588. return ( part && m_my_part && part->GetLibId() == m_my_part->GetLibId() );
  589. }
  590. void LIB_EDIT_FRAME::emptyScreen()
  591. {
  592. m_treePane->GetLibTree()->Unselect();
  593. SetCurLib( wxEmptyString );
  594. SetCurPart( nullptr );
  595. SetScreen( m_dummyScreen );
  596. m_dummyScreen->ClearUndoRedoList();
  597. m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
  598. Refresh();
  599. }
  600. void LIB_EDIT_FRAME::CommonSettingsChanged( bool aEnvVarsChanged )
  601. {
  602. SCH_BASE_FRAME::CommonSettingsChanged( aEnvVarsChanged );
  603. RecreateToolbars();
  604. if( aEnvVarsChanged )
  605. SyncLibraries( true );
  606. Layout();
  607. SendSizeEvent();
  608. }
  609. void LIB_EDIT_FRAME::ShowChangedLanguage()
  610. {
  611. // call my base class
  612. SCH_BASE_FRAME::ShowChangedLanguage();
  613. // tooltips in toolbars
  614. RecreateToolbars();
  615. // status bar
  616. UpdateMsgPanel();
  617. }
  618. void LIB_EDIT_FRAME::SetScreen( BASE_SCREEN* aScreen )
  619. {
  620. SCH_BASE_FRAME::SetScreen( aScreen );
  621. }
  622. void LIB_EDIT_FRAME::RebuildView()
  623. {
  624. GetRenderSettings()->m_ShowUnit = m_unit;
  625. GetRenderSettings()->m_ShowConvert = m_convert;
  626. GetRenderSettings()->m_ShowDisabled = m_my_part && m_my_part->IsAlias();
  627. GetCanvas()->DisplayComponent( m_my_part );
  628. GetCanvas()->GetView()->HideWorksheet();
  629. GetCanvas()->GetView()->ClearHiddenFlags();
  630. GetCanvas()->Refresh();
  631. }
  632. void LIB_EDIT_FRAME::HardRedraw()
  633. {
  634. SyncLibraries( true );
  635. RebuildView();
  636. }
  637. const BOX2I LIB_EDIT_FRAME::GetDocumentExtents() const
  638. {
  639. if( !m_my_part )
  640. {
  641. return BOX2I( VECTOR2I( Mils2iu( -100 ), Mils2iu( -100 ) ),
  642. VECTOR2I( Mils2iu( 200 ), Mils2iu( 200 ) ) );
  643. }
  644. else
  645. {
  646. EDA_RECT boundingBox = m_my_part->Flatten()->GetUnitBoundingBox( m_unit, m_convert );
  647. return BOX2I( boundingBox.GetOrigin(), VECTOR2I( boundingBox.GetWidth(),
  648. boundingBox.GetHeight() ) );
  649. }
  650. }
  651. void LIB_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  652. {
  653. const std::string& payload = mail.GetPayload();
  654. switch( mail.Command() )
  655. {
  656. case MAIL_LIB_EDIT:
  657. if( !payload.empty() )
  658. {
  659. wxString libFileName( payload );
  660. wxString libNickname;
  661. wxString msg;
  662. SYMBOL_LIB_TABLE* libTable = Prj().SchSymbolLibTable();
  663. const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( libFileName );
  664. if( !libTableRow )
  665. {
  666. msg.Printf( _( "The current configuration does not include the symbol library\n"
  667. "\"%s\".\nUse Manage Symbol Libraries to edit the configuration." ),
  668. libFileName );
  669. DisplayErrorMessage( this, _( "Library not found in symbol library table." ), msg );
  670. break;
  671. }
  672. libNickname = libTableRow->GetNickName();
  673. if( !libTable->HasLibrary( libNickname, true ) )
  674. {
  675. msg.Printf( _( "The library with the nickname \"%s\" is not enabled\n"
  676. "in the current configuration. Use Manage Symbol Libraries to\n"
  677. "edit the configuration." ), libNickname );
  678. DisplayErrorMessage( this, _( "Symbol library not enabled." ), msg );
  679. break;
  680. }
  681. SetCurLib( libNickname );
  682. if( m_treePane )
  683. {
  684. LIB_ID id( libNickname, wxEmptyString );
  685. m_treePane->GetLibTree()->ExpandLibId( id );
  686. m_treePane->GetLibTree()->CenterLibId( id );
  687. }
  688. }
  689. break;
  690. default:
  691. ;
  692. }
  693. }
  694. void LIB_EDIT_FRAME::SwitchCanvas( EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType )
  695. {
  696. // switches currently used canvas ( Cairo / OpenGL):
  697. SCH_BASE_FRAME::SwitchCanvas( aCanvasType );
  698. // Set options specific to symbol editor (axies are always enabled):
  699. GetCanvas()->GetGAL()->SetAxesEnabled( true );
  700. }
  701. bool LIB_EDIT_FRAME::HasLibModifications() const
  702. {
  703. wxCHECK( m_libMgr, false );
  704. return m_libMgr->HasModifications();
  705. }
  706. bool LIB_EDIT_FRAME::IsContentModified()
  707. {
  708. wxCHECK( m_libMgr, false );
  709. // Test if the currently edited part is modified
  710. if( GetScreen() && GetScreen()->IsModify() && GetCurPart() )
  711. return true;
  712. // Test if any library has been modified
  713. for( const auto& libNickname : m_libMgr->GetLibraryNames() )
  714. {
  715. if( m_libMgr->IsLibraryModified( libNickname ) )
  716. return true;
  717. }
  718. return false;
  719. }