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.

295 lines
11 KiB

8 months 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. * Copyright (C) 2019 CERN
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <advanced_config.h>
  27. #include <api/api_plugin_manager.h>
  28. #include <sch_draw_panel.h>
  29. #include <sch_edit_frame.h>
  30. #include <kiface_base.h>
  31. #include <bitmaps.h>
  32. #include <eeschema_id.h>
  33. #include <pgm_base.h>
  34. #include <python_scripting.h>
  35. #include <tool/tool_manager.h>
  36. #include <tool/action_toolbar.h>
  37. #include <tools/sch_actions.h>
  38. #include <tools/sch_selection_tool.h>
  39. #include <widgets/hierarchy_pane.h>
  40. #include <widgets/wx_aui_utils.h>
  41. #include <widgets/sch_design_block_pane.h>
  42. #include <widgets/sch_properties_panel.h>
  43. #include <widgets/sch_search_pane.h>
  44. #include <toolbars_sch_editor.h>
  45. ACTION_TOOLBAR_CONTROL SCH_ACTION_TOOLBAR_CONTROLS::currentVariant(
  46. "control.currentVariant",
  47. _( "Current Variant" ),
  48. _( "Control to select the current schematic varaint" ) );
  49. std::optional<TOOLBAR_CONFIGURATION> SCH_EDIT_TOOLBAR_SETTINGS::DefaultToolbarConfig( TOOLBAR_LOC aToolbar )
  50. {
  51. TOOLBAR_CONFIGURATION config;
  52. // clang-format off
  53. switch( aToolbar )
  54. {
  55. case TOOLBAR_LOC::TOP_AUX:
  56. return std::nullopt;
  57. case TOOLBAR_LOC::LEFT:
  58. config.AppendAction( ACTIONS::toggleGrid )
  59. .AppendAction( ACTIONS::toggleGridOverrides )
  60. .AppendGroup( TOOLBAR_GROUP_CONFIG( _( "Units" ) )
  61. .AddAction( ACTIONS::inchesUnits )
  62. .AddAction( ACTIONS::milsUnits )
  63. .AddAction( ACTIONS::millimetersUnits ) )
  64. .AppendGroup( TOOLBAR_GROUP_CONFIG( _( "Crosshair modes" ) )
  65. .AddAction( ACTIONS::cursorSmallCrosshairs )
  66. .AddAction( ACTIONS::cursorFullCrosshairs )
  67. .AddAction( ACTIONS::cursor45Crosshairs ) );
  68. config.AppendSeparator()
  69. .AppendAction( SCH_ACTIONS::toggleHiddenPins );
  70. config.AppendSeparator()
  71. .AppendGroup( TOOLBAR_GROUP_CONFIG( _( "Line modes" ) )
  72. .AddAction( SCH_ACTIONS::lineModeFree )
  73. .AddAction( SCH_ACTIONS::lineMode90 )
  74. .AddAction( SCH_ACTIONS::lineMode45 ) );
  75. config.AppendSeparator()
  76. .AppendAction( SCH_ACTIONS::toggleAnnotateAuto );
  77. config.AppendSeparator()
  78. .AppendAction( SCH_ACTIONS::showHierarchy )
  79. .AppendAction( ACTIONS::showProperties );
  80. if( ADVANCED_CFG::GetCfg().m_DrawBoundingBoxes )
  81. config.AppendAction( ACTIONS::toggleBoundingBoxes );
  82. /* TODO (ISM): Handle context menus
  83. EE_SELECTION_TOOL* selTool = m_toolManager->GetTool<EE_SELECTION_TOOL>();
  84. std::unique_ptr<ACTION_MENU> gridMenu = std::make_unique<ACTION_MENU>( false, selTool );
  85. gridMenu->Add( ACTIONS::gridProperties );
  86. m_tbLeft->AddToolContextMenu( ACTIONS::toggleGrid, std::move( gridMenu ) );
  87. */
  88. break;
  89. case TOOLBAR_LOC::RIGHT:
  90. config.AppendGroup( TOOLBAR_GROUP_CONFIG( _( "Selection modes" ) )
  91. .AddAction( ACTIONS::selectSetRect )
  92. .AddAction( ACTIONS::selectSetLasso ) )
  93. .AppendAction( SCH_ACTIONS::highlightNetTool );
  94. config.AppendSeparator()
  95. .AppendAction( SCH_ACTIONS::placeSymbol )
  96. .AppendAction( SCH_ACTIONS::placePower )
  97. .AppendAction( SCH_ACTIONS::drawWire )
  98. .AppendAction( SCH_ACTIONS::drawBus )
  99. .AppendAction( SCH_ACTIONS::placeBusWireEntry )
  100. .AppendAction( SCH_ACTIONS::placeNoConnect )
  101. .AppendAction( SCH_ACTIONS::placeJunction )
  102. .AppendAction( SCH_ACTIONS::placeLabel )
  103. .AppendAction( SCH_ACTIONS::placeClassLabel )
  104. .AppendAction( SCH_ACTIONS::drawRuleArea )
  105. .AppendAction( SCH_ACTIONS::placeGlobalLabel )
  106. .AppendAction( SCH_ACTIONS::placeHierLabel )
  107. .AppendAction( SCH_ACTIONS::drawSheet )
  108. .AppendAction( SCH_ACTIONS::placeSheetPin )
  109. .AppendAction( SCH_ACTIONS::syncAllSheetsPins );
  110. config.AppendSeparator()
  111. .AppendAction( SCH_ACTIONS::placeSchematicText )
  112. .AppendAction( SCH_ACTIONS::drawTextBox )
  113. .AppendAction( SCH_ACTIONS::drawTable )
  114. .AppendAction( SCH_ACTIONS::drawRectangle )
  115. .AppendAction( SCH_ACTIONS::drawCircle )
  116. .AppendAction( SCH_ACTIONS::drawArc )
  117. .AppendAction( SCH_ACTIONS::drawBezier )
  118. .AppendAction( SCH_ACTIONS::drawLines )
  119. .AppendAction( SCH_ACTIONS::placeImage )
  120. .AppendAction( ACTIONS::deleteTool );
  121. break;
  122. case TOOLBAR_LOC::TOP_MAIN:
  123. if( Kiface().IsSingle() ) // not when under a project mgr
  124. {
  125. config.AppendAction( ACTIONS::doNew );
  126. config.AppendAction( ACTIONS::open );
  127. }
  128. config.AppendAction( ACTIONS::save );
  129. config.AppendSeparator()
  130. .AppendAction( SCH_ACTIONS::schematicSetup );
  131. config.AppendSeparator()
  132. .AppendAction( ACTIONS::pageSettings )
  133. .AppendAction( ACTIONS::print )
  134. .AppendAction( ACTIONS::plot );
  135. config.AppendSeparator()
  136. .AppendAction( ACTIONS::paste );
  137. config.AppendSeparator()
  138. .AppendAction( ACTIONS::undo )
  139. .AppendAction( ACTIONS::redo );
  140. config.AppendSeparator()
  141. .AppendAction( ACTIONS::find )
  142. .AppendAction( ACTIONS::findAndReplace );
  143. config.AppendSeparator()
  144. .AppendAction( ACTIONS::zoomRedraw )
  145. .AppendAction( ACTIONS::zoomInCenter )
  146. .AppendAction( ACTIONS::zoomOutCenter )
  147. .AppendAction( ACTIONS::zoomFitScreen )
  148. .AppendAction( ACTIONS::zoomFitObjects )
  149. .AppendAction( ACTIONS::zoomTool );
  150. config.AppendSeparator()
  151. .AppendAction( SCH_ACTIONS::navigateBack )
  152. .AppendAction( SCH_ACTIONS::navigateUp )
  153. .AppendAction( SCH_ACTIONS::navigateForward );
  154. config.AppendSeparator()
  155. .AppendAction( SCH_ACTIONS::rotateCCW )
  156. .AppendAction( SCH_ACTIONS::rotateCW )
  157. .AppendAction( SCH_ACTIONS::mirrorV )
  158. .AppendAction( SCH_ACTIONS::mirrorH )
  159. .AppendAction( ACTIONS::group )
  160. .AppendAction( ACTIONS::ungroup );
  161. config.AppendSeparator()
  162. .AppendAction( ACTIONS::showSymbolEditor )
  163. .AppendAction( ACTIONS::showSymbolBrowser )
  164. .AppendAction( ACTIONS::showFootprintEditor );
  165. config.AppendSeparator()
  166. .AppendAction( SCH_ACTIONS::annotate )
  167. .AppendAction( SCH_ACTIONS::runERC )
  168. .AppendAction( SCH_ACTIONS::showSimulator )
  169. .AppendAction( SCH_ACTIONS::assignFootprints )
  170. .AppendAction( SCH_ACTIONS::editSymbolFields )
  171. .AppendAction( SCH_ACTIONS::generateBOM );
  172. config.AppendSeparator()
  173. .AppendAction( SCH_ACTIONS::showPcbNew );
  174. if( ADVANCED_CFG::GetCfg().m_EnableVariantsUI )
  175. config.AppendControl( SCH_ACTION_TOOLBAR_CONTROLS::currentVariant );
  176. // Insert all the IPC plugins here on the toolbar
  177. // TODO (ISM): Move this to individual actions for each script
  178. config.AppendControl( ACTION_TOOLBAR_CONTROLS::ipcScripting );
  179. break;
  180. }
  181. // clang-format on
  182. return config;
  183. }
  184. void SCH_EDIT_FRAME::configureToolbars()
  185. {
  186. SCH_BASE_FRAME::configureToolbars();
  187. if( ADVANCED_CFG::GetCfg().m_EnableVariantsUI )
  188. {
  189. // Variant selection drop down control on main tool bar.
  190. auto variantSelectionCtrlFactory =
  191. [this]( ACTION_TOOLBAR* aToolbar )
  192. {
  193. std::optional<wxString> currentVariantName = Schematic().GetCurrentVariant();
  194. wxString tmp = currentVariantName ? *currentVariantName : GetDefaultVariantName();
  195. m_currentVariantCtrl = new wxChoice( aToolbar, ID_TOOLBAR_SCH_SELECT_VARAIANT, wxDefaultPosition,
  196. wxDefaultSize, Schematic().GetVariantNamesForUI(), 0,
  197. wxDefaultValidator, tmp );
  198. m_currentVariantCtrl->SetToolTip( _( "Select the current variant to display and edit." ) );
  199. aToolbar->Add( m_currentVariantCtrl );
  200. UpdateVariantSelectionCtrl( Schematic().GetVariantNamesForUI() );
  201. };
  202. RegisterCustomToolbarControlFactory( SCH_ACTION_TOOLBAR_CONTROLS::currentVariant, variantSelectionCtrlFactory );
  203. }
  204. // IPC/Scripting plugin control
  205. // TODO (ISM): Clean this up to make IPC actions just normal tool actions to get rid of this entire
  206. // control
  207. auto pluginControlFactory =
  208. [this]( ACTION_TOOLBAR* aToolbar )
  209. {
  210. // Add scripting console and API plugins
  211. bool scriptingAvailable = SCRIPTING::IsWxAvailable();
  212. #ifdef KICAD_IPC_API
  213. bool haveApiPlugins = Pgm().GetCommonSettings()->m_Api.enable_server &&
  214. !Pgm().GetPluginManager().GetActionsForScope( PluginActionScope() ).empty();
  215. #else
  216. bool haveApiPlugins = false;
  217. #endif
  218. if( scriptingAvailable || haveApiPlugins )
  219. {
  220. aToolbar->AddScaledSeparator( aToolbar->GetParent() );
  221. if( haveApiPlugins )
  222. AddApiPluginTools( aToolbar );
  223. }
  224. };
  225. RegisterCustomToolbarControlFactory( ACTION_TOOLBAR_CONTROLS::ipcScripting, pluginControlFactory );
  226. }
  227. void SCH_EDIT_FRAME::UpdateVariantSelectionCtrl( const wxArrayString& aVariantNames )
  228. {
  229. if( !m_currentVariantCtrl )
  230. return;
  231. // Fall back to the default if nothing is currently selected.
  232. wxString currentSelection = GetDefaultVariantName();
  233. int selectionIndex = m_currentVariantCtrl->GetSelection();
  234. if( selectionIndex != wxNOT_FOUND )
  235. currentSelection = m_currentVariantCtrl->GetString( selectionIndex );
  236. m_currentVariantCtrl->Set( aVariantNames );
  237. selectionIndex = m_currentVariantCtrl->FindString( currentSelection );
  238. if( ( selectionIndex == wxNOT_FOUND ) && ( m_currentVariantCtrl->GetCount() != 0 ) )
  239. selectionIndex = 0;
  240. m_currentVariantCtrl->SetSelection( selectionIndex );
  241. }