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.

2799 lines
93 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
5 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 CERN
  5. * Copyright (C) 2019-2024 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <advanced_config.h>
  25. #include <core/typeinfo.h>
  26. #include <core/kicad_algo.h>
  27. #include <gal/graphics_abstraction_layer.h>
  28. #include <geometry/shape_compound.h>
  29. #include <ee_actions.h>
  30. #include <ee_collectors.h>
  31. #include <ee_selection_tool.h>
  32. #include <eeschema_id.h>
  33. #include <symbol_edit_frame.h>
  34. #include <symbol_viewer_frame.h>
  35. #include <math/util.h>
  36. #include <geometry/shape_rect.h>
  37. #include <sch_painter.h>
  38. #include <preview_items/selection_area.h>
  39. #include <sch_base_frame.h>
  40. #include <sch_commit.h>
  41. #include <sch_symbol.h>
  42. #include <sch_field.h>
  43. #include <sch_edit_frame.h>
  44. #include <sch_item.h>
  45. #include <sch_line.h>
  46. #include <sch_bus_entry.h>
  47. #include <sch_marker.h>
  48. #include <sch_no_connect.h>
  49. #include <sch_render_settings.h>
  50. #include <sch_sheet.h>
  51. #include <sch_sheet_pin.h>
  52. #include <sch_table.h>
  53. #include <sch_tablecell.h>
  54. #include <schematic.h>
  55. #include <tool/tool_event.h>
  56. #include <tool/tool_manager.h>
  57. #include <tools/ee_grid_helper.h>
  58. #include <tools/ee_point_editor.h>
  59. #include <tools/sch_line_wire_bus_tool.h>
  60. #include <tools/sch_editor_control.h>
  61. #include <trigo.h>
  62. #include <view/view.h>
  63. #include <view/view_controls.h>
  64. #include <wx/log.h>
  65. #include "symb_transforms_utils.h"
  66. SELECTION_CONDITION EE_CONDITIONS::SingleSymbol = []( const SELECTION& aSel )
  67. {
  68. if( aSel.GetSize() == 1 )
  69. {
  70. SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( aSel.Front() );
  71. if( symbol )
  72. return !symbol->GetLibSymbolRef() || !symbol->GetLibSymbolRef()->IsPower();
  73. }
  74. return false;
  75. };
  76. SELECTION_CONDITION EE_CONDITIONS::SingleSymbolOrPower = []( const SELECTION& aSel )
  77. {
  78. return aSel.GetSize() == 1 && aSel.Front()->Type() == SCH_SYMBOL_T;
  79. };
  80. SELECTION_CONDITION EE_CONDITIONS::SingleDeMorganSymbol = []( const SELECTION& aSel )
  81. {
  82. if( aSel.GetSize() == 1 )
  83. {
  84. SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( aSel.Front() );
  85. if( symbol )
  86. return symbol->GetLibSymbolRef() && symbol->GetLibSymbolRef()->HasAlternateBodyStyle();
  87. }
  88. return false;
  89. };
  90. SELECTION_CONDITION EE_CONDITIONS::SingleMultiUnitSymbol = []( const SELECTION& aSel )
  91. {
  92. if( aSel.GetSize() == 1 )
  93. {
  94. SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( aSel.Front() );
  95. if( symbol )
  96. return symbol->GetLibSymbolRef() && symbol->GetLibSymbolRef()->GetUnitCount() >= 2;
  97. }
  98. return false;
  99. };
  100. SELECTION_CONDITION EE_CONDITIONS::SingleMultiFunctionPin = []( const SELECTION& aSel )
  101. {
  102. if( aSel.GetSize() == 1 )
  103. {
  104. SCH_PIN* pin = dynamic_cast<SCH_PIN*>( aSel.Front() );
  105. if( pin && pin->GetLibPin() )
  106. return !pin->GetLibPin()->GetAlternates().empty();
  107. }
  108. return false;
  109. };
  110. SELECTION_CONDITION EE_CONDITIONS::SingleNonExcludedMarker = []( const SELECTION& aSel )
  111. {
  112. if( aSel.CountType( SCH_MARKER_T ) != 1 )
  113. return false;
  114. return !static_cast<SCH_MARKER*>( aSel.Front() )->IsExcluded();
  115. };
  116. SELECTION_CONDITION EE_CONDITIONS::MultipleSymbolsOrPower = []( const SELECTION& aSel )
  117. {
  118. return aSel.GetSize() > 1 && aSel.OnlyContains( { SCH_SYMBOL_T } );
  119. };
  120. SELECTION_CONDITION EE_CONDITIONS::AllPins = []( const SELECTION& aSel )
  121. {
  122. return aSel.GetSize() >= 1 && aSel.OnlyContains( { SCH_PIN_T } );
  123. };
  124. SELECTION_CONDITION EE_CONDITIONS::AllPinsOrSheetPins = []( const SELECTION& aSel )
  125. {
  126. return aSel.GetSize() >= 1 && aSel.OnlyContains( { SCH_PIN_T, SCH_SHEET_PIN_T } );
  127. };
  128. #define HITTEST_THRESHOLD_PIXELS 5
  129. EE_SELECTION_TOOL::EE_SELECTION_TOOL() :
  130. SELECTION_TOOL( "eeschema.InteractiveSelection" ),
  131. m_frame( nullptr ),
  132. m_nonModifiedCursor( KICURSOR::ARROW ),
  133. m_isSymbolEditor( false ),
  134. m_isSymbolViewer( false ),
  135. m_unit( 0 ),
  136. m_bodyStyle( 0 )
  137. {
  138. m_filter.SetDefaults();
  139. m_selection.Clear();
  140. }
  141. EE_SELECTION_TOOL::~EE_SELECTION_TOOL()
  142. {
  143. getView()->Remove( &m_selection );
  144. }
  145. using E_C = EE_CONDITIONS;
  146. static std::vector<KICAD_T> connectedTypes =
  147. {
  148. SCH_SYMBOL_LOCATE_POWER_T,
  149. SCH_PIN_T,
  150. SCH_ITEM_LOCATE_WIRE_T,
  151. SCH_ITEM_LOCATE_BUS_T,
  152. SCH_BUS_WIRE_ENTRY_T,
  153. SCH_BUS_BUS_ENTRY_T,
  154. SCH_LABEL_T,
  155. SCH_HIER_LABEL_T,
  156. SCH_GLOBAL_LABEL_T,
  157. SCH_SHEET_PIN_T,
  158. SCH_DIRECTIVE_LABEL_T,
  159. SCH_JUNCTION_T
  160. };
  161. bool EE_SELECTION_TOOL::Init()
  162. {
  163. m_frame = getEditFrame<SCH_BASE_FRAME>();
  164. SYMBOL_VIEWER_FRAME* symbolViewerFrame = dynamic_cast<SYMBOL_VIEWER_FRAME*>( m_frame );
  165. SYMBOL_EDIT_FRAME* symbolEditorFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
  166. if( symbolEditorFrame )
  167. {
  168. m_isSymbolEditor = true;
  169. m_unit = symbolEditorFrame->GetUnit();
  170. m_bodyStyle = symbolEditorFrame->GetBodyStyle();
  171. }
  172. else
  173. {
  174. m_isSymbolViewer = symbolViewerFrame != nullptr;
  175. }
  176. auto linesSelection = E_C::MoreThan( 0 ) &&
  177. E_C::OnlyTypes( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T,
  178. SCH_ITEM_LOCATE_GRAPHIC_LINE_T } );
  179. auto wireOrBusSelection = E_C::Count( 1 ) &&
  180. E_C::OnlyTypes( { SCH_ITEM_LOCATE_WIRE_T,
  181. SCH_ITEM_LOCATE_BUS_T } );
  182. auto connectedSelection = E_C::Count( 1 ) && E_C::OnlyTypes( connectedTypes );
  183. auto sheetSelection = E_C::Count( 1 ) && E_C::OnlyTypes( { SCH_SHEET_T } );
  184. auto crossProbingSelection = E_C::MoreThan( 0 ) && E_C::HasTypes( { SCH_SYMBOL_T, SCH_PIN_T, SCH_SHEET_T } );
  185. auto tableCellSelection = E_C::MoreThan( 0 ) && E_C::OnlyTypes( { SCH_TABLECELL_T } );
  186. auto schEditSheetPageNumberCondition =
  187. [&] ( const SELECTION& aSel )
  188. {
  189. if( m_isSymbolEditor || m_isSymbolViewer )
  190. return false;
  191. return E_C::LessThan( 2 )( aSel ) && E_C::OnlyTypes( { SCH_SHEET_T } )( aSel );
  192. };
  193. auto schEditCondition =
  194. [this] ( const SELECTION& aSel )
  195. {
  196. return !m_isSymbolEditor && !m_isSymbolViewer;
  197. };
  198. auto belowRootSheetCondition =
  199. [&]( const SELECTION& aSel )
  200. {
  201. SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
  202. return editFrame
  203. && editFrame->GetCurrentSheet().Last() != &editFrame->Schematic().Root();
  204. };
  205. auto haveHighlight =
  206. [&]( const SELECTION& sel )
  207. {
  208. SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
  209. return editFrame && !editFrame->GetHighlightedConnection().IsEmpty();
  210. };
  211. auto haveSymbol =
  212. [&]( const SELECTION& sel )
  213. {
  214. return m_isSymbolEditor &&
  215. static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->GetCurSymbol();
  216. };
  217. auto symbolDisplayNameIsEditable =
  218. [&]( const SELECTION& sel )
  219. {
  220. if ( !m_isSymbolEditor )
  221. return false;
  222. SYMBOL_EDIT_FRAME* symbEditorFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
  223. return symbEditorFrame
  224. && symbEditorFrame->GetCurSymbol()
  225. && symbEditorFrame->GetCurSymbol()->IsMulti()
  226. && symbEditorFrame->IsSymbolEditable()
  227. && !symbEditorFrame->IsSymbolAlias();
  228. };
  229. auto& menu = m_menu.GetMenu();
  230. menu.AddItem( EE_ACTIONS::clearHighlight, haveHighlight && EE_CONDITIONS::Idle, 1 );
  231. menu.AddSeparator( haveHighlight && EE_CONDITIONS::Idle, 1 );
  232. menu.AddItem( ACTIONS::selectColumns, tableCellSelection && EE_CONDITIONS::Idle, 2 );
  233. menu.AddItem( ACTIONS::selectRows, tableCellSelection && EE_CONDITIONS::Idle, 2 );
  234. menu.AddItem( ACTIONS::selectTable, tableCellSelection && EE_CONDITIONS::Idle, 2 );
  235. menu.AddSeparator( 100 );
  236. menu.AddItem( EE_ACTIONS::drawWire, schEditCondition && EE_CONDITIONS::Empty, 100 );
  237. menu.AddItem( EE_ACTIONS::drawBus, schEditCondition && EE_CONDITIONS::Empty, 100 );
  238. menu.AddSeparator( 100 );
  239. menu.AddItem( ACTIONS::finishInteractive,
  240. SCH_LINE_WIRE_BUS_TOOL::IsDrawingLineWireOrBus, 100 );
  241. menu.AddItem( EE_ACTIONS::enterSheet, sheetSelection && EE_CONDITIONS::Idle, 150 );
  242. menu.AddItem( EE_ACTIONS::selectOnPCB,
  243. crossProbingSelection && EE_CONDITIONS::Idle, 150 );
  244. menu.AddItem( EE_ACTIONS::leaveSheet, belowRootSheetCondition, 150 );
  245. menu.AddSeparator( 200 );
  246. menu.AddItem( EE_ACTIONS::placeJunction, wireOrBusSelection && EE_CONDITIONS::Idle, 250 );
  247. menu.AddItem( EE_ACTIONS::placeLabel, wireOrBusSelection && EE_CONDITIONS::Idle, 250 );
  248. menu.AddItem( EE_ACTIONS::placeClassLabel, wireOrBusSelection && EE_CONDITIONS::Idle, 250 );
  249. menu.AddItem( EE_ACTIONS::placeGlobalLabel, wireOrBusSelection && EE_CONDITIONS::Idle, 250 );
  250. menu.AddItem( EE_ACTIONS::placeHierLabel, wireOrBusSelection && EE_CONDITIONS::Idle, 250 );
  251. menu.AddItem( EE_ACTIONS::breakWire, linesSelection && EE_CONDITIONS::Idle, 250 );
  252. menu.AddItem( EE_ACTIONS::slice, linesSelection && EE_CONDITIONS::Idle, 250 );
  253. menu.AddItem( EE_ACTIONS::placeSheetPin, sheetSelection && EE_CONDITIONS::Idle, 250 );
  254. menu.AddItem( EE_ACTIONS::syncSheetPins, sheetSelection && EE_CONDITIONS::Idle, 250 );
  255. menu.AddItem( EE_ACTIONS::assignNetclass, connectedSelection && EE_CONDITIONS::Idle, 250 );
  256. menu.AddItem( EE_ACTIONS::editPageNumber, schEditSheetPageNumberCondition, 250 );
  257. menu.AddSeparator( 400 );
  258. menu.AddItem( EE_ACTIONS::symbolProperties, haveSymbol && EE_CONDITIONS::Empty, 400 );
  259. menu.AddItem( EE_ACTIONS::pinTable, haveSymbol && EE_CONDITIONS::Empty, 400 );
  260. menu.AddItem( EE_ACTIONS::setUnitDisplayName,
  261. haveSymbol && symbolDisplayNameIsEditable && EE_CONDITIONS::Empty, 400 );
  262. menu.AddSeparator( 1000 );
  263. m_frame->AddStandardSubMenus( m_menu );
  264. m_disambiguateTimer.SetOwner( this );
  265. Connect( wxEVT_TIMER, wxTimerEventHandler( EE_SELECTION_TOOL::onDisambiguationExpire ),
  266. nullptr, this );
  267. return true;
  268. }
  269. void EE_SELECTION_TOOL::Reset( RESET_REASON aReason )
  270. {
  271. m_frame = getEditFrame<SCH_BASE_FRAME>();
  272. if( aReason != TOOL_BASE::REDRAW )
  273. {
  274. // Remove pointers to the selected items from containers without changing their
  275. // properties (as they are already deleted while a new sheet is loaded)
  276. m_selection.Clear();
  277. }
  278. if( aReason == TOOL_BASE::MODEL_RELOAD || aReason == TOOL_BASE::SUPERMODEL_RELOAD )
  279. {
  280. getView()->GetPainter()->GetSettings()->SetHighlight( false );
  281. SYMBOL_EDIT_FRAME* symbolEditFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
  282. SYMBOL_VIEWER_FRAME* symbolViewerFrame = dynamic_cast<SYMBOL_VIEWER_FRAME*>( m_frame );
  283. if( symbolEditFrame )
  284. {
  285. m_isSymbolEditor = true;
  286. m_unit = symbolEditFrame->GetUnit();
  287. m_bodyStyle = symbolEditFrame->GetBodyStyle();
  288. }
  289. else
  290. {
  291. m_isSymbolViewer = symbolViewerFrame != nullptr;
  292. }
  293. }
  294. // Reinsert the VIEW_GROUP, in case it was removed from the VIEW
  295. getView()->Remove( &m_selection );
  296. getView()->Add( &m_selection );
  297. }
  298. int EE_SELECTION_TOOL::Main( const TOOL_EVENT& aEvent )
  299. {
  300. m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
  301. KIID lastRolloverItem = niluuid;
  302. EE_GRID_HELPER grid( m_toolMgr );
  303. auto pinOrientation =
  304. []( EDA_ITEM* aItem )
  305. {
  306. SCH_PIN* pin = dynamic_cast<SCH_PIN*>( aItem );
  307. if( pin )
  308. {
  309. const SCH_SYMBOL* parent = dynamic_cast<const SCH_SYMBOL*>( pin->GetParentSymbol() );
  310. if( !parent )
  311. return pin->GetOrientation();
  312. else
  313. {
  314. SCH_PIN dummy( *pin );
  315. RotateAndMirrorPin( dummy, parent->GetOrientation() );
  316. return dummy.GetOrientation();
  317. }
  318. }
  319. SCH_SHEET_PIN* sheetPin = dynamic_cast<SCH_SHEET_PIN*>( aItem );
  320. if( sheetPin )
  321. {
  322. switch( sheetPin->GetSide() )
  323. {
  324. default:
  325. case SHEET_SIDE::LEFT: return PIN_ORIENTATION::PIN_RIGHT;
  326. case SHEET_SIDE::RIGHT: return PIN_ORIENTATION::PIN_LEFT;
  327. case SHEET_SIDE::TOP: return PIN_ORIENTATION::PIN_DOWN;
  328. case SHEET_SIDE::BOTTOM: return PIN_ORIENTATION::PIN_UP;
  329. }
  330. }
  331. return PIN_ORIENTATION::PIN_LEFT;
  332. };
  333. // Main loop: keep receiving events
  334. while( TOOL_EVENT* evt = Wait() )
  335. {
  336. bool selCancelled = false;
  337. bool displayWireCursor = false;
  338. bool displayBusCursor = false;
  339. bool displayLineCursor = false;
  340. KIID rolloverItem = lastRolloverItem;
  341. // on left click, a selection is made, depending on modifiers ALT, SHIFT, CTRL:
  342. setModifiersState( evt->Modifier( MD_SHIFT ), evt->Modifier( MD_CTRL ),
  343. evt->Modifier( MD_ALT ) );
  344. MOUSE_DRAG_ACTION drag_action = m_frame->GetDragAction();
  345. if( evt->IsMouseDown( BUT_LEFT ) )
  346. {
  347. if( !m_frame->ToolStackIsEmpty() )
  348. {
  349. // Avoid triggering when running under other tools
  350. }
  351. else if( m_toolMgr->GetTool<EE_POINT_EDITOR>()
  352. && m_toolMgr->GetTool<EE_POINT_EDITOR>()->HasPoint() )
  353. {
  354. // Distinguish point editor from selection modification by checking modifiers
  355. if( hasModifier() )
  356. {
  357. m_originalCursor = m_toolMgr->GetMousePosition();
  358. m_disambiguateTimer.StartOnce( ADVANCED_CFG::GetCfg().m_DisambiguationMenuDelay );
  359. }
  360. }
  361. else
  362. {
  363. m_originalCursor = m_toolMgr->GetMousePosition();
  364. m_disambiguateTimer.StartOnce( ADVANCED_CFG::GetCfg().m_DisambiguationMenuDelay );
  365. }
  366. }
  367. // Single click? Select single object
  368. else if( evt->IsClick( BUT_LEFT ) )
  369. {
  370. // If the timer has stopped, then we have already run the disambiguate routine
  371. // and we don't want to register an extra click here
  372. if( !m_disambiguateTimer.IsRunning() )
  373. {
  374. evt->SetPassEvent();
  375. continue;
  376. }
  377. m_disambiguateTimer.Stop();
  378. if( SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
  379. schframe->FocusOnItem( nullptr );
  380. // Collect items at the clicked location (doesn't select them yet)
  381. EE_COLLECTOR collector;
  382. CollectHits( collector, evt->Position() );
  383. narrowSelection( collector, evt->Position(), false );
  384. if( collector.GetCount() == 1 && !m_isSymbolEditor && !hasModifier() )
  385. {
  386. OPT_TOOL_EVENT autostart = autostartEvent( evt, grid, collector[0] );
  387. if( autostart )
  388. {
  389. DRAW_SEGMENT_EVENT_PARAMS* params = new DRAW_SEGMENT_EVENT_PARAMS();
  390. params->layer = autostart->Parameter<const DRAW_SEGMENT_EVENT_PARAMS*>()->layer;
  391. params->quitOnDraw = true;
  392. params->sourceSegment = dynamic_cast<SCH_LINE*>( collector[0] );
  393. autostart->SetParameter<const DRAW_SEGMENT_EVENT_PARAMS*>( params );
  394. m_toolMgr->ProcessEvent( *autostart );
  395. selCancelled = true;
  396. }
  397. else if( collector[0]->IsHypertext() )
  398. {
  399. collector[ 0 ]->DoHypertextAction( m_frame );
  400. selCancelled = true;
  401. }
  402. else if( collector[0]->IsBrightened() )
  403. {
  404. if( SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
  405. {
  406. NET_NAVIGATOR_ITEM_DATA itemData( schframe->GetCurrentSheet(),
  407. collector[0] );
  408. schframe->SelectNetNavigatorItem( &itemData );
  409. }
  410. }
  411. }
  412. if( !selCancelled )
  413. {
  414. selectPoint( collector, evt->Position(), nullptr, nullptr, m_additive,
  415. m_subtractive, m_exclusive_or );
  416. m_selection.SetIsHover( false );
  417. }
  418. }
  419. else if( evt->IsClick( BUT_RIGHT ) )
  420. {
  421. m_disambiguateTimer.Stop();
  422. // right click? if there is any object - show the context menu
  423. if( m_selection.Empty() )
  424. {
  425. ClearSelection();
  426. SelectPoint( evt->Position(), { SCH_LOCATE_ANY_T }, nullptr, &selCancelled );
  427. m_selection.SetIsHover( true );
  428. }
  429. // If the cursor has moved off the bounding box of the selection by more than
  430. // a grid square, check to see if there is another item available for selection
  431. // under the cursor. If there is, the user likely meant to get the context menu
  432. // for that item. If there is no new item, then keep the original selection and
  433. // show the context menu for it.
  434. else if( !m_selection.GetBoundingBox().Inflate( grid.GetGrid().x, grid.GetGrid().y )
  435. .Contains( evt->Position() ) )
  436. {
  437. EE_COLLECTOR collector;
  438. if( CollectHits( collector, evt->Position(), { SCH_LOCATE_ANY_T } ) )
  439. {
  440. ClearSelection();
  441. SelectPoint( evt->Position(), { SCH_LOCATE_ANY_T }, nullptr, &selCancelled );
  442. m_selection.SetIsHover( true );
  443. }
  444. }
  445. if( !selCancelled )
  446. m_menu.ShowContextMenu( m_selection );
  447. }
  448. else if( evt->IsDblClick( BUT_LEFT ) )
  449. {
  450. m_disambiguateTimer.Stop();
  451. // double click? Display the properties window
  452. if( SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
  453. schframe->FocusOnItem( nullptr );
  454. if( m_selection.Empty() )
  455. SelectPoint( evt->Position() );
  456. EDA_ITEM* item = m_selection.Front();
  457. if( item && item->Type() == SCH_SHEET_T )
  458. m_toolMgr->PostAction( EE_ACTIONS::enterSheet );
  459. else
  460. m_toolMgr->PostAction( EE_ACTIONS::properties );
  461. }
  462. else if( evt->IsDblClick( BUT_MIDDLE ) )
  463. {
  464. m_disambiguateTimer.Stop();
  465. // Middle double click? Do zoom to fit or zoom to objects
  466. if( evt->Modifier( MD_CTRL ) ) // Is CTRL key down?
  467. m_toolMgr->RunAction( ACTIONS::zoomFitObjects );
  468. else
  469. m_toolMgr->RunAction( ACTIONS::zoomFitScreen );
  470. }
  471. else if( evt->IsDrag( BUT_LEFT ) )
  472. {
  473. m_disambiguateTimer.Stop();
  474. // Is another tool already moving a new object? Don't allow a drag start
  475. if( !m_selection.Empty() && m_selection[0]->HasFlag( IS_NEW | IS_MOVING ) )
  476. {
  477. evt->SetPassEvent();
  478. continue;
  479. }
  480. // drag with LMB? Select multiple objects (or at least draw a selection box) or
  481. // drag them
  482. if( SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
  483. schframe->FocusOnItem( nullptr );
  484. EE_COLLECTOR collector;
  485. if( CollectHits( collector, evt->DragOrigin(), { SCH_TABLECELL_T } ) )
  486. {
  487. selectTableCells( static_cast<SCH_TABLE*>( collector[0]->GetParent() ) );
  488. }
  489. else if( hasModifier() || drag_action == MOUSE_DRAG_ACTION::SELECT )
  490. {
  491. selectMultiple();
  492. }
  493. else if( m_selection.Empty() && drag_action != MOUSE_DRAG_ACTION::DRAG_ANY )
  494. {
  495. selectMultiple();
  496. }
  497. else
  498. {
  499. if( m_isSymbolEditor )
  500. {
  501. if( static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->IsSymbolAlias() )
  502. {
  503. m_selection = RequestSelection( { SCH_FIELD_T } );
  504. }
  505. else
  506. {
  507. m_selection = RequestSelection( { SCH_SHAPE_T,
  508. SCH_TEXT_T,
  509. SCH_TEXTBOX_T,
  510. SCH_PIN_T,
  511. SCH_FIELD_T } );
  512. }
  513. }
  514. else
  515. {
  516. m_selection = RequestSelection( EE_COLLECTOR::MovableItems );
  517. }
  518. // Check if dragging has started within any of selected items bounding box
  519. if( selectionContains( evt->DragOrigin() ) )
  520. {
  521. // drag_is_move option exists only in schematic editor, not in symbol editor
  522. // (m_frame->eeconfig() returns nullptr in Symbol Editor)
  523. if( m_isSymbolEditor || m_frame->eeconfig()->m_Input.drag_is_move )
  524. m_toolMgr->RunAction( EE_ACTIONS::move );
  525. else
  526. m_toolMgr->RunAction( EE_ACTIONS::drag );
  527. }
  528. else
  529. {
  530. // No -> drag a selection box
  531. selectMultiple();
  532. }
  533. }
  534. }
  535. else if( evt->IsMouseDown( BUT_AUX1 ) )
  536. {
  537. m_toolMgr->RunAction( EE_ACTIONS::navigateBack );
  538. }
  539. else if( evt->IsMouseDown( BUT_AUX2 ) )
  540. {
  541. m_toolMgr->RunAction( EE_ACTIONS::navigateForward );
  542. }
  543. else if( evt->Category() == TC_COMMAND && evt->Action() == TA_CHOICE_MENU_CHOICE )
  544. {
  545. m_disambiguateTimer.Stop();
  546. // context sub-menu selection? Handle unit selection or bus unfolding
  547. if( *evt->GetCommandId() >= ID_POPUP_SCH_SELECT_UNIT
  548. && *evt->GetCommandId() <= ID_POPUP_SCH_SELECT_UNIT_END )
  549. {
  550. SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( m_selection.Front() );
  551. int unit = *evt->GetCommandId() - ID_POPUP_SCH_SELECT_UNIT;
  552. if( symbol )
  553. static_cast<SCH_EDIT_FRAME*>( m_frame )->SelectUnit( symbol, unit );
  554. }
  555. else if( *evt->GetCommandId() >= ID_POPUP_SCH_ALT_PIN_FUNCTION
  556. && *evt->GetCommandId() <= ID_POPUP_SCH_ALT_PIN_FUNCTION_END )
  557. {
  558. SCH_PIN* pin = dynamic_cast<SCH_PIN*>( m_selection.Front() );
  559. wxString alt = *evt->Parameter<wxString*>();
  560. if( pin )
  561. static_cast<SCH_EDIT_FRAME*>( m_frame )->SetAltPinFunction( pin, alt );
  562. }
  563. else if( *evt->GetCommandId() >= ID_POPUP_SCH_PIN_TRICKS_START
  564. && *evt->GetCommandId() <= ID_POPUP_SCH_PIN_TRICKS_END )
  565. {
  566. if( !m_selection.OnlyContains( { SCH_PIN_T, SCH_SHEET_PIN_T } )
  567. || m_selection.Empty() )
  568. {
  569. return 0;
  570. }
  571. // Keep track of new items so we make them the new selection at the end
  572. EDA_ITEMS newItems;
  573. SCH_COMMIT commit( static_cast<SCH_EDIT_FRAME*>( m_frame ) );
  574. if( *evt->GetCommandId() == ID_POPUP_SCH_PIN_TRICKS_NO_CONNECT )
  575. {
  576. for( EDA_ITEM* item : m_selection )
  577. {
  578. SCH_NO_CONNECT* nc = new SCH_NO_CONNECT( item->GetPosition() );
  579. commit.Add( nc, m_frame->GetScreen() );
  580. newItems.push_back( nc );
  581. }
  582. commit.Push( wxS( "No Connect Pins" ) );
  583. ClearSelection();
  584. }
  585. else if( *evt->GetCommandId() == ID_POPUP_SCH_PIN_TRICKS_WIRE )
  586. {
  587. VECTOR2I wireGrid = grid.GetGridSize( GRID_HELPER_GRIDS::GRID_WIRES );
  588. for( EDA_ITEM* item : m_selection )
  589. {
  590. SCH_LINE* wire = new SCH_LINE( item->GetPosition(), LAYER_WIRE );
  591. // Add some length to the wire as nothing in our code base handles
  592. // 0 length wires very well, least of all the ortho drag algorithm
  593. VECTOR2I stub;
  594. switch( pinOrientation( item ) )
  595. {
  596. default:
  597. case PIN_ORIENTATION::PIN_RIGHT:
  598. stub = VECTOR2I( -1 * wireGrid.x, 0 );
  599. break;
  600. case PIN_ORIENTATION::PIN_LEFT:
  601. stub = VECTOR2I( 1 * wireGrid.x, 0 );
  602. break;
  603. case PIN_ORIENTATION::PIN_UP:
  604. stub = VECTOR2I( 0, 1 * wireGrid.y );
  605. break;
  606. case PIN_ORIENTATION::PIN_DOWN:
  607. stub = VECTOR2I( 0, -1 * wireGrid.y );
  608. break;
  609. }
  610. wire->SetEndPoint( item->GetPosition() + stub );
  611. m_frame->AddToScreen( wire, m_frame->GetScreen() );
  612. commit.Added( wire, m_frame->GetScreen() );
  613. newItems.push_back( wire );
  614. }
  615. ClearSelection();
  616. AddItemsToSel( &newItems );
  617. // Select only the ends so we can immediately start dragging them
  618. for( EDA_ITEM* item : newItems )
  619. static_cast<SCH_LINE*>( item )->SetFlags( ENDPOINT );
  620. // Put the mouse on the nearest point of the first wire
  621. SCH_LINE* first = static_cast<SCH_LINE*>( newItems[0] );
  622. getViewControls()->SetCrossHairCursorPosition( first->GetEndPoint(), false );
  623. getViewControls()->WarpMouseCursor( getViewControls()->GetCursorPosition(),
  624. true );
  625. // Start the drag tool, canceling will remove the wires
  626. if( m_toolMgr->RunSynchronousAction( EE_ACTIONS::drag, &commit, false ) )
  627. commit.Push( wxS( "Wire Pins" ) );
  628. else
  629. commit.Revert();
  630. }
  631. else
  632. {
  633. // For every pin in the selection, add a label according to menu item
  634. // selected by the user
  635. for( EDA_ITEM* item : m_selection )
  636. {
  637. SCH_PIN* pin = dynamic_cast<SCH_PIN*>( item );
  638. SCH_SHEET_PIN* sheetPin = dynamic_cast<SCH_SHEET_PIN*>( item );
  639. SCH_EDIT_FRAME* sf = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
  640. SCH_LABEL_BASE* label = nullptr;
  641. wxString labelText;
  642. if( pin )
  643. labelText = pin->GetShownName();
  644. else if( sheetPin && sf )
  645. labelText = sheetPin->GetShownText( &sf->GetCurrentSheet(), false );
  646. switch( *evt->GetCommandId() )
  647. {
  648. case ID_POPUP_SCH_PIN_TRICKS_NET_LABEL:
  649. label = new SCH_LABEL( item->GetPosition(), labelText );
  650. break;
  651. case ID_POPUP_SCH_PIN_TRICKS_HIER_LABEL:
  652. label = new SCH_HIERLABEL( item->GetPosition(), labelText );
  653. break;
  654. case ID_POPUP_SCH_PIN_TRICKS_GLOBAL_LABEL:
  655. label = new SCH_GLOBALLABEL( item->GetPosition(), labelText );
  656. break;
  657. default: continue;
  658. }
  659. switch( pinOrientation( item ) )
  660. {
  661. default:
  662. case PIN_ORIENTATION::PIN_RIGHT:
  663. label->SetSpinStyle( SPIN_STYLE::SPIN::LEFT );
  664. break;
  665. case PIN_ORIENTATION::PIN_LEFT:
  666. label->SetSpinStyle( SPIN_STYLE::SPIN::RIGHT );
  667. break;
  668. case PIN_ORIENTATION::PIN_UP:
  669. label->SetSpinStyle( SPIN_STYLE::SPIN::BOTTOM );
  670. break;
  671. case PIN_ORIENTATION::PIN_DOWN:
  672. label->SetSpinStyle( SPIN_STYLE::SPIN::UP );
  673. break;
  674. }
  675. ELECTRICAL_PINTYPE pinType = ELECTRICAL_PINTYPE::PT_UNSPECIFIED;
  676. if( pin )
  677. {
  678. pinType = pin->GetType();
  679. }
  680. else if( sheetPin )
  681. {
  682. switch( sheetPin->GetLabelShape() )
  683. {
  684. case LABEL_INPUT: pinType = ELECTRICAL_PINTYPE::PT_INPUT; break;
  685. case LABEL_OUTPUT: pinType = ELECTRICAL_PINTYPE::PT_OUTPUT; break;
  686. case LABEL_BIDI: pinType = ELECTRICAL_PINTYPE::PT_BIDI; break;
  687. case LABEL_TRISTATE: pinType = ELECTRICAL_PINTYPE::PT_TRISTATE; break;
  688. case LABEL_PASSIVE: pinType = ELECTRICAL_PINTYPE::PT_PASSIVE; break;
  689. }
  690. }
  691. switch( pinType )
  692. {
  693. case ELECTRICAL_PINTYPE::PT_BIDI:
  694. label->SetShape( LABEL_FLAG_SHAPE::L_BIDI );
  695. break;
  696. case ELECTRICAL_PINTYPE::PT_INPUT:
  697. label->SetShape( LABEL_FLAG_SHAPE::L_INPUT );
  698. break;
  699. case ELECTRICAL_PINTYPE::PT_OUTPUT:
  700. label->SetShape( LABEL_FLAG_SHAPE::L_OUTPUT );
  701. break;
  702. case ELECTRICAL_PINTYPE::PT_TRISTATE:
  703. label->SetShape( LABEL_FLAG_SHAPE::L_TRISTATE );
  704. break;
  705. case ELECTRICAL_PINTYPE::PT_UNSPECIFIED:
  706. label->SetShape( LABEL_FLAG_SHAPE::L_UNSPECIFIED );
  707. break;
  708. default:
  709. label->SetShape( LABEL_FLAG_SHAPE::L_INPUT );
  710. }
  711. commit.Add( label, m_frame->GetScreen() );
  712. newItems.push_back( label );
  713. }
  714. commit.Push( wxS( "Label Pins" ) );
  715. // Many users will want to drag these items to wire off of the pins, so
  716. // pre-select them.
  717. ClearSelection();
  718. AddItemsToSel( &newItems );
  719. }
  720. }
  721. else if( *evt->GetCommandId() >= ID_POPUP_SCH_UNFOLD_BUS
  722. && *evt->GetCommandId() <= ID_POPUP_SCH_UNFOLD_BUS_END )
  723. {
  724. wxString* net = new wxString( *evt->Parameter<wxString*>() );
  725. m_toolMgr->RunAction<wxString*>( EE_ACTIONS::unfoldBus, net );
  726. }
  727. }
  728. else if( evt->IsCancelInteractive() )
  729. {
  730. m_disambiguateTimer.Stop();
  731. // We didn't set these, but we have reports that they leak out of some other tools,
  732. // so we clear them here.
  733. getViewControls()->SetAutoPan( false );
  734. getViewControls()->CaptureCursor( false );
  735. if( SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
  736. schframe->FocusOnItem( nullptr );
  737. if( !GetSelection().Empty() )
  738. {
  739. ClearSelection();
  740. }
  741. else if( evt->FirstResponder() == this && evt->GetCommandId() == (int) WXK_ESCAPE )
  742. {
  743. SCH_EDITOR_CONTROL* editor = m_toolMgr->GetTool<SCH_EDITOR_CONTROL>();
  744. if( editor && m_frame->eeconfig()->m_Input.esc_clears_net_highlight )
  745. editor->ClearHighlight( *evt );
  746. }
  747. }
  748. else if( evt->Action() == TA_UNDO_REDO_PRE )
  749. {
  750. if( SCH_EDIT_FRAME* schframe = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
  751. schframe->FocusOnItem( nullptr );
  752. }
  753. else if( evt->IsMotion() && !m_isSymbolEditor && evt->FirstResponder() == this )
  754. {
  755. // Update cursor and rollover item
  756. rolloverItem = niluuid;
  757. EE_COLLECTOR collector;
  758. getViewControls()->ForceCursorPosition( false );
  759. if( CollectHits( collector, evt->Position() ) )
  760. {
  761. narrowSelection( collector, evt->Position(), false );
  762. if( collector.GetCount() == 1 && !hasModifier() )
  763. {
  764. OPT_TOOL_EVENT autostartEvt = autostartEvent( evt, grid, collector[0] );
  765. if( autostartEvt )
  766. {
  767. if( autostartEvt->Matches( EE_ACTIONS::drawBus.MakeEvent() ) )
  768. displayBusCursor = true;
  769. else if( autostartEvt->Matches( EE_ACTIONS::drawWire.MakeEvent() ) )
  770. displayWireCursor = true;
  771. else if( autostartEvt->Matches( EE_ACTIONS::drawLines.MakeEvent() ) )
  772. displayLineCursor = true;
  773. }
  774. else if( collector[0]->IsHypertext() && !collector[0]->IsSelected() )
  775. {
  776. rolloverItem = collector[0]->m_Uuid;
  777. }
  778. }
  779. }
  780. }
  781. else
  782. {
  783. evt->SetPassEvent();
  784. }
  785. if( rolloverItem != lastRolloverItem )
  786. {
  787. if( EDA_ITEM* item = m_frame->GetItem( lastRolloverItem ) )
  788. {
  789. item->ClearFlags( IS_ROLLOVER );
  790. lastRolloverItem = niluuid;
  791. if( item->Type() == SCH_FIELD_T )
  792. m_frame->GetCanvas()->GetView()->Update( item->GetParent() );
  793. else
  794. m_frame->GetCanvas()->GetView()->Update( item );
  795. }
  796. }
  797. if( rolloverItem != niluuid )
  798. {
  799. EDA_ITEM* item = m_frame->GetItem( rolloverItem );
  800. if( item && !( item->GetFlags() & IS_ROLLOVER ) )
  801. {
  802. item->SetFlags( IS_ROLLOVER );
  803. lastRolloverItem = rolloverItem;
  804. if( item->Type() == SCH_FIELD_T )
  805. m_frame->GetCanvas()->GetView()->Update( item->GetParent() );
  806. else
  807. m_frame->GetCanvas()->GetView()->Update( item );
  808. }
  809. }
  810. if( m_frame->ToolStackIsEmpty() )
  811. {
  812. if( displayWireCursor )
  813. {
  814. m_nonModifiedCursor = KICURSOR::LINE_WIRE_ADD;
  815. }
  816. else if( displayBusCursor )
  817. {
  818. m_nonModifiedCursor = KICURSOR::LINE_BUS;
  819. }
  820. else if( displayLineCursor )
  821. {
  822. m_nonModifiedCursor = KICURSOR::LINE_GRAPHIC;
  823. }
  824. else if( rolloverItem != niluuid )
  825. {
  826. m_nonModifiedCursor = KICURSOR::HAND;
  827. }
  828. else if( !m_selection.Empty()
  829. && drag_action == MOUSE_DRAG_ACTION::DRAG_SELECTED
  830. && evt->HasPosition()
  831. && selectionContains( evt->Position() ) ) //move/drag option prediction
  832. {
  833. m_nonModifiedCursor = KICURSOR::MOVING;
  834. }
  835. else
  836. {
  837. m_nonModifiedCursor = KICURSOR::ARROW;
  838. }
  839. }
  840. }
  841. m_disambiguateTimer.Stop();
  842. // Shutting down; clear the selection
  843. m_selection.Clear();
  844. return 0;
  845. }
  846. OPT_TOOL_EVENT EE_SELECTION_TOOL::autostartEvent( TOOL_EVENT* aEvent, EE_GRID_HELPER& aGrid,
  847. SCH_ITEM* aItem )
  848. {
  849. VECTOR2I pos = aGrid.BestSnapAnchor( aEvent->Position(), aGrid.GetItemGrid( aItem ) );
  850. if( m_frame->eeconfig()->m_Drawing.auto_start_wires
  851. && !m_toolMgr->GetTool<EE_POINT_EDITOR>()->HasPoint()
  852. && aItem->IsPointClickableAnchor( pos ) )
  853. {
  854. OPT_TOOL_EVENT newEvt = EE_ACTIONS::drawWire.MakeEvent();
  855. if( aItem->Type() == SCH_BUS_BUS_ENTRY_T )
  856. {
  857. newEvt = EE_ACTIONS::drawBus.MakeEvent();
  858. }
  859. else if( aItem->Type() == SCH_BUS_WIRE_ENTRY_T )
  860. {
  861. SCH_BUS_WIRE_ENTRY* busEntry = static_cast<SCH_BUS_WIRE_ENTRY*>( aItem );
  862. if( !busEntry->m_connected_bus_item )
  863. newEvt = EE_ACTIONS::drawBus.MakeEvent();
  864. }
  865. else if( aItem->Type() == SCH_LINE_T )
  866. {
  867. SCH_LINE* line = static_cast<SCH_LINE*>( aItem );
  868. if( line->IsBus() )
  869. newEvt = EE_ACTIONS::drawBus.MakeEvent();
  870. else if( line->IsGraphicLine() )
  871. newEvt = EE_ACTIONS::drawLines.MakeEvent();
  872. }
  873. else if( aItem->Type() == SCH_LABEL_T || aItem->Type() == SCH_HIER_LABEL_T
  874. || aItem->Type() == SCH_SHEET_PIN_T )
  875. {
  876. SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( aItem );
  877. SCH_CONNECTION possibleConnection( label->Schematic()->ConnectionGraph() );
  878. possibleConnection.ConfigureFromLabel( label->GetText() );
  879. if( possibleConnection.IsBus() )
  880. newEvt = EE_ACTIONS::drawBus.MakeEvent();
  881. }
  882. else if( aItem->Type() == SCH_SYMBOL_T )
  883. {
  884. const SCH_SYMBOL* symbol = static_cast<const SCH_SYMBOL*>( aItem );
  885. wxCHECK( symbol, OPT_TOOL_EVENT() );
  886. const SCH_PIN* pin = symbol->GetPin( pos );
  887. wxCHECK( pin, OPT_TOOL_EVENT() );
  888. if( !pin->IsVisible()
  889. && !( m_frame->eeconfig()->m_Appearance.show_hidden_pins
  890. || m_frame->GetRenderSettings()->m_ShowHiddenPins ) )
  891. {
  892. return OPT_TOOL_EVENT();
  893. }
  894. }
  895. newEvt->SetMousePosition( pos );
  896. newEvt->SetHasPosition( true );
  897. newEvt->SetForceImmediate( true );
  898. getViewControls()->ForceCursorPosition( true, pos );
  899. return newEvt;
  900. }
  901. return OPT_TOOL_EVENT();
  902. }
  903. int EE_SELECTION_TOOL::disambiguateCursor( const TOOL_EVENT& aEvent )
  904. {
  905. wxMouseState keyboardState = wxGetMouseState();
  906. setModifiersState( keyboardState.ShiftDown(), keyboardState.ControlDown(),
  907. keyboardState.AltDown() );
  908. m_skip_heuristics = true;
  909. SelectPoint( m_originalCursor, { SCH_LOCATE_ANY_T }, nullptr, &m_canceledMenu, false,
  910. m_additive, m_subtractive, m_exclusive_or );
  911. m_skip_heuristics = false;
  912. return 0;
  913. }
  914. void EE_SELECTION_TOOL::OnIdle( wxIdleEvent& aEvent )
  915. {
  916. if( m_frame->ToolStackIsEmpty() && !m_multiple )
  917. {
  918. wxMouseState keyboardState = wxGetMouseState();
  919. setModifiersState( keyboardState.ShiftDown(), keyboardState.ControlDown(),
  920. keyboardState.AltDown() );
  921. if( m_additive )
  922. m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ADD );
  923. else if( m_subtractive )
  924. m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::SUBTRACT );
  925. else if( m_exclusive_or )
  926. m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::XOR );
  927. else
  928. m_frame->GetCanvas()->SetCurrentCursor( m_nonModifiedCursor );
  929. }
  930. }
  931. EE_SELECTION& EE_SELECTION_TOOL::GetSelection()
  932. {
  933. return m_selection;
  934. }
  935. bool EE_SELECTION_TOOL::CollectHits( EE_COLLECTOR& aCollector, const VECTOR2I& aWhere,
  936. const std::vector<KICAD_T>& aScanTypes )
  937. {
  938. int pixelThreshold = KiROUND( getView()->ToWorld( HITTEST_THRESHOLD_PIXELS ) );
  939. int gridThreshold = KiROUND( getView()->GetGAL()->GetGridSize().EuclideanNorm() / 2 );
  940. aCollector.m_Threshold = std::max( pixelThreshold, gridThreshold );
  941. aCollector.m_ShowPinElectricalTypes = m_frame->GetRenderSettings()->m_ShowPinsElectricalType;
  942. if( m_isSymbolEditor )
  943. {
  944. LIB_SYMBOL* symbol = static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->GetCurSymbol();
  945. if( !symbol )
  946. return false;
  947. aCollector.Collect( symbol->GetDrawItems(), aScanTypes, aWhere, m_unit, m_bodyStyle );
  948. }
  949. else
  950. {
  951. aCollector.Collect( m_frame->GetScreen(), aScanTypes, aWhere, m_unit, m_bodyStyle );
  952. // If pins are disabled in the filter, they will be removed later. Let's add the parent
  953. // so that people can use pins to select symbols in this case.
  954. if( !m_filter.pins )
  955. {
  956. int originalCount = aCollector.GetCount();
  957. for( int ii = 0; ii < originalCount; ++ii )
  958. {
  959. if( aCollector[ii]->Type() == SCH_PIN_T )
  960. {
  961. SCH_PIN* pin = static_cast<SCH_PIN*>( aCollector[ii] );
  962. if( !aCollector.HasItem( pin->GetParentSymbol() ) )
  963. aCollector.Append( pin->GetParentSymbol() );
  964. }
  965. }
  966. }
  967. }
  968. return aCollector.GetCount() > 0;
  969. }
  970. void EE_SELECTION_TOOL::narrowSelection( EE_COLLECTOR& collector, const VECTOR2I& aWhere,
  971. bool aCheckLocked, bool aSelectedOnly )
  972. {
  973. SYMBOL_EDIT_FRAME* symbolEditorFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
  974. for( int i = collector.GetCount() - 1; i >= 0; --i )
  975. {
  976. if( symbolEditorFrame )
  977. {
  978. // Do not select invisible items if they are not displayed
  979. EDA_ITEM* item = collector[i];
  980. if( item->Type() == SCH_FIELD_T )
  981. {
  982. if( !static_cast<SCH_FIELD*>( item )->IsVisible()
  983. && !symbolEditorFrame->GetShowInvisibleFields() )
  984. {
  985. collector.Remove( i );
  986. continue;
  987. }
  988. }
  989. else if( item->Type() == SCH_PIN_T )
  990. {
  991. if( !static_cast<SCH_PIN*>( item )->IsVisible()
  992. && !symbolEditorFrame->GetShowInvisiblePins() )
  993. {
  994. collector.Remove( i );
  995. continue;
  996. }
  997. }
  998. }
  999. if( !Selectable( collector[i], &aWhere ) )
  1000. {
  1001. collector.Remove( i );
  1002. continue;
  1003. }
  1004. if( aCheckLocked && collector[i]->IsLocked() )
  1005. {
  1006. collector.Remove( i );
  1007. continue;
  1008. }
  1009. if( !itemPassesFilter( collector[i] ) )
  1010. {
  1011. collector.Remove( i );
  1012. continue;
  1013. }
  1014. if( aSelectedOnly && !collector[i]->IsSelected() )
  1015. {
  1016. collector.Remove( i );
  1017. continue;
  1018. }
  1019. }
  1020. // Apply some ugly heuristics to avoid disambiguation menus whenever possible
  1021. if( collector.GetCount() > 1 && !m_skip_heuristics )
  1022. GuessSelectionCandidates( collector, aWhere );
  1023. }
  1024. bool EE_SELECTION_TOOL::selectPoint( EE_COLLECTOR& aCollector, const VECTOR2I& aWhere,
  1025. EDA_ITEM** aItem, bool* aSelectionCancelledFlag, bool aAdd,
  1026. bool aSubtract, bool aExclusiveOr )
  1027. {
  1028. m_selection.ClearReferencePoint();
  1029. // If still more than one item we're going to have to ask the user.
  1030. if( aCollector.GetCount() > 1 )
  1031. {
  1032. // Try to call selectionMenu via RunAction() to avoid event-loop contention
  1033. // But it we cannot handle the event, then we don't have an active tool loop, so
  1034. // handle it directly.
  1035. if( !m_toolMgr->RunAction<COLLECTOR*>( EE_ACTIONS::selectionMenu, &aCollector ) )
  1036. {
  1037. if( !doSelectionMenu( &aCollector ) )
  1038. aCollector.m_MenuCancelled = true;
  1039. }
  1040. if( aCollector.m_MenuCancelled )
  1041. {
  1042. if( aSelectionCancelledFlag )
  1043. *aSelectionCancelledFlag = true;
  1044. return false;
  1045. }
  1046. }
  1047. if( !aAdd && !aSubtract && !aExclusiveOr )
  1048. ClearSelection();
  1049. int addedCount = 0;
  1050. bool anySubtracted = false;
  1051. if( aCollector.GetCount() > 0 )
  1052. {
  1053. for( int i = 0; i < aCollector.GetCount(); ++i )
  1054. {
  1055. EDA_ITEM_FLAGS flags = 0;
  1056. bool isLine = aCollector[i]->Type() == SCH_LINE_T;
  1057. // Handle line ends specially
  1058. if( isLine )
  1059. {
  1060. SCH_LINE* line = (SCH_LINE*) aCollector[i];
  1061. if( line->GetStartPoint().Distance( aWhere ) <= aCollector.m_Threshold )
  1062. flags = STARTPOINT;
  1063. else if( line->GetEndPoint().Distance( aWhere ) <= aCollector.m_Threshold )
  1064. flags = ENDPOINT;
  1065. else
  1066. flags = STARTPOINT | ENDPOINT;
  1067. }
  1068. if( aSubtract
  1069. || ( aExclusiveOr && aCollector[i]->IsSelected()
  1070. && ( !isLine || ( isLine && aCollector[i]->HasFlag( flags ) ) ) ) )
  1071. {
  1072. aCollector[i]->ClearFlags( flags );
  1073. // Need to update end shadows after ctrl-click unselecting one of two selected
  1074. // endpoints.
  1075. if( isLine )
  1076. getView()->Update( aCollector[i] );
  1077. if( !aCollector[i]->HasFlag( STARTPOINT ) && !aCollector[i]->HasFlag( ENDPOINT ) )
  1078. {
  1079. unselect( aCollector[i] );
  1080. anySubtracted = true;
  1081. }
  1082. }
  1083. else
  1084. {
  1085. aCollector[i]->SetFlags( flags );
  1086. select( aCollector[i] );
  1087. addedCount++;
  1088. }
  1089. }
  1090. }
  1091. if( addedCount == 1 )
  1092. {
  1093. m_toolMgr->ProcessEvent( EVENTS::PointSelectedEvent );
  1094. if( aItem && aCollector.GetCount() == 1 )
  1095. *aItem = aCollector[0];
  1096. return true;
  1097. }
  1098. else if( addedCount > 1 )
  1099. {
  1100. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1101. return true;
  1102. }
  1103. else if( anySubtracted )
  1104. {
  1105. m_toolMgr->ProcessEvent( EVENTS::UnselectedEvent );
  1106. return true;
  1107. }
  1108. return false;
  1109. }
  1110. bool EE_SELECTION_TOOL::SelectPoint( const VECTOR2I& aWhere,
  1111. const std::vector<KICAD_T>& aScanTypes,
  1112. EDA_ITEM** aItem, bool* aSelectionCancelledFlag,
  1113. bool aCheckLocked, bool aAdd, bool aSubtract,
  1114. bool aExclusiveOr )
  1115. {
  1116. EE_COLLECTOR collector;
  1117. if( !CollectHits( collector, aWhere, aScanTypes ) )
  1118. return false;
  1119. narrowSelection( collector, aWhere, aCheckLocked, aSubtract );
  1120. return selectPoint( collector, aWhere, aItem, aSelectionCancelledFlag, aAdd, aSubtract,
  1121. aExclusiveOr );
  1122. }
  1123. int EE_SELECTION_TOOL::SelectAll( const TOOL_EVENT& aEvent )
  1124. {
  1125. m_multiple = true; // Multiple selection mode is active
  1126. KIGFX::VIEW* view = getView();
  1127. // hold all visible items
  1128. std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> selectedItems;
  1129. std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> sheetPins;
  1130. // Filter the view items based on the selection box
  1131. BOX2I selectionBox;
  1132. selectionBox.SetMaximum();
  1133. view->Query( selectionBox, selectedItems ); // Get the list of selected items
  1134. // Sheet pins aren't in the view; add them by hand
  1135. for( KIGFX::VIEW::LAYER_ITEM_PAIR& pair : selectedItems )
  1136. {
  1137. SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( pair.first );
  1138. if( sheet )
  1139. {
  1140. int layer = pair.second;
  1141. for( SCH_SHEET_PIN* pin : sheet->GetPins() )
  1142. sheetPins.emplace_back( KIGFX::VIEW::LAYER_ITEM_PAIR( pin, layer ) );
  1143. }
  1144. }
  1145. selectedItems.insert( selectedItems.end(), sheetPins.begin(), sheetPins.end() );
  1146. for( const std::pair<KIGFX::VIEW_ITEM*, int>& item_pair : selectedItems )
  1147. {
  1148. if( EDA_ITEM* item = dynamic_cast<EDA_ITEM*>( item_pair.first ) )
  1149. {
  1150. if( Selectable( item ) && itemPassesFilter( item ) )
  1151. {
  1152. if( item->Type() == SCH_LINE_T )
  1153. item->SetFlags( STARTPOINT | ENDPOINT );
  1154. select( item );
  1155. }
  1156. }
  1157. }
  1158. m_multiple = false;
  1159. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1160. return 0;
  1161. }
  1162. int EE_SELECTION_TOOL::UnselectAll( const TOOL_EVENT& aEvent )
  1163. {
  1164. m_multiple = true; // Multiple selection mode is active
  1165. KIGFX::VIEW* view = getView();
  1166. // hold all visible items
  1167. std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> selectedItems;
  1168. // Filter the view items based on the selection box
  1169. BOX2I selectionBox;
  1170. selectionBox.SetMaximum();
  1171. view->Query( selectionBox, selectedItems ); // Get the list of selected items
  1172. for( KIGFX::VIEW::LAYER_ITEM_PAIR& pair : selectedItems )
  1173. {
  1174. SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( pair.first );
  1175. if( sheet )
  1176. {
  1177. for( SCH_SHEET_PIN* pin : sheet->GetPins() )
  1178. {
  1179. EDA_ITEM* item = dynamic_cast<EDA_ITEM*>( pin );
  1180. if( item && Selectable( item ) )
  1181. unselect( item );
  1182. }
  1183. }
  1184. if( EDA_ITEM* item = dynamic_cast<EDA_ITEM*>( pair.first ) )
  1185. {
  1186. if( Selectable( item ) )
  1187. unselect( item );
  1188. }
  1189. }
  1190. m_multiple = false;
  1191. m_toolMgr->ProcessEvent( EVENTS::UnselectedEvent );
  1192. return 0;
  1193. }
  1194. void EE_SELECTION_TOOL::GuessSelectionCandidates( EE_COLLECTOR& collector, const VECTOR2I& aPos )
  1195. {
  1196. // Prefer exact hits to sloppy ones
  1197. std::set<EDA_ITEM*> exactHits;
  1198. for( int i = collector.GetCount() - 1; i >= 0; --i )
  1199. {
  1200. EDA_ITEM* item = collector[ i ];
  1201. SCH_LINE* line = dynamic_cast<SCH_LINE*>( item );
  1202. SCH_SHAPE* shape = dynamic_cast<SCH_SHAPE*>( item );
  1203. SCH_TABLE* table = dynamic_cast<SCH_TABLE*>( item );
  1204. // Lines are hard to hit. Give them a bit more slop to still be considered "exact".
  1205. if( line || ( shape && shape->GetShape() == SHAPE_T::POLY )
  1206. || ( shape && shape->GetShape() == SHAPE_T::ARC ) )
  1207. {
  1208. int pixelThreshold = KiROUND( getView()->ToWorld( 6 ) );
  1209. if( item->HitTest( aPos, pixelThreshold ) )
  1210. exactHits.insert( item );
  1211. }
  1212. else if( table )
  1213. {
  1214. // Consider table cells exact, but not the table itself
  1215. }
  1216. else
  1217. {
  1218. if( m_frame->GetRenderSettings()->m_ShowPinsElectricalType )
  1219. item->SetFlags( SHOW_ELEC_TYPE );
  1220. if( item->HitTest( aPos, 0 ) )
  1221. exactHits.insert( item );
  1222. item->ClearFlags( SHOW_ELEC_TYPE );
  1223. }
  1224. }
  1225. if( exactHits.size() > 0 && exactHits.size() < (unsigned) collector.GetCount() )
  1226. {
  1227. for( int i = collector.GetCount() - 1; i >= 0; --i )
  1228. {
  1229. EDA_ITEM* item = collector[ i ];
  1230. if( !exactHits.contains( item ) )
  1231. collector.Transfer( item );
  1232. }
  1233. }
  1234. // Find the closest item. (Note that at this point all hits are either exact or non-exact.)
  1235. SEG poss( aPos, aPos );
  1236. EDA_ITEM* closest = nullptr;
  1237. int closestDist = INT_MAX / 4;
  1238. for( EDA_ITEM* item : collector )
  1239. {
  1240. BOX2I bbox = item->GetBoundingBox();
  1241. int dist = INT_MAX / 4;
  1242. // A dominating item is one that would unfairly win distance tests
  1243. // and mask out other items. For example, a filled rectangle "wins"
  1244. // with a zero distance over anything inside it.
  1245. bool dominating = false;
  1246. if( exactHits.contains( item ) )
  1247. {
  1248. if( item->Type() == SCH_PIN_T || item->Type() == SCH_JUNCTION_T )
  1249. {
  1250. closest = item;
  1251. break;
  1252. }
  1253. SCH_LINE* line = dynamic_cast<SCH_LINE*>( item );
  1254. SCH_FIELD* field = dynamic_cast<SCH_FIELD*>( item );
  1255. EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item );
  1256. EDA_SHAPE* shape = dynamic_cast<EDA_SHAPE*>( item );
  1257. SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
  1258. if( line )
  1259. {
  1260. dist = line->GetSeg().Distance( aPos );
  1261. }
  1262. else if( field )
  1263. {
  1264. BOX2I box = field->GetBoundingBox();
  1265. EDA_ANGLE orient = field->GetTextAngle();
  1266. if( field->GetParent() && field->GetParent()->Type() == SCH_SYMBOL_T )
  1267. {
  1268. if( static_cast<SCH_SYMBOL*>( field->GetParent() )->GetTransform().y1 )
  1269. {
  1270. if( orient.IsHorizontal() )
  1271. orient = ANGLE_VERTICAL;
  1272. else
  1273. orient = ANGLE_HORIZONTAL;
  1274. }
  1275. }
  1276. field->GetEffectiveTextShape( false, box, orient )
  1277. ->Collide( poss, INT_MAX / 4, &dist );
  1278. }
  1279. else if( text )
  1280. {
  1281. text->GetEffectiveTextShape( false )->Collide( poss, INT_MAX / 4, &dist );
  1282. }
  1283. else if( shape )
  1284. {
  1285. std::vector<SHAPE*> shapes = shape->MakeEffectiveShapes();
  1286. for( SHAPE* s : shapes )
  1287. {
  1288. int shapeDist = dist;
  1289. s->Collide( poss, INT_MAX / 4, &shapeDist );
  1290. if( shapeDist < dist )
  1291. dist = shapeDist;
  1292. delete s;
  1293. }
  1294. // Filled shapes win hit tests anywhere inside them
  1295. dominating = shape->IsFilled();
  1296. }
  1297. else if( symbol )
  1298. {
  1299. bbox = symbol->GetBodyBoundingBox();
  1300. SHAPE_RECT rect( bbox.GetPosition(), bbox.GetWidth(), bbox.GetHeight() );
  1301. if( bbox.Contains( aPos ) )
  1302. dist = bbox.GetCenter().Distance( aPos );
  1303. else
  1304. rect.Collide( poss, closestDist, &dist );
  1305. }
  1306. else
  1307. {
  1308. dist = bbox.GetCenter().Distance( aPos );
  1309. }
  1310. }
  1311. else
  1312. {
  1313. SHAPE_RECT rect( bbox.GetPosition(), bbox.GetWidth(), bbox.GetHeight() );
  1314. rect.Collide( poss, collector.m_Threshold, &dist );
  1315. }
  1316. // Don't promote dominating items to be the closest item
  1317. // (they'll always win) - they'll still be available for selection, but they
  1318. // won't boot out worthy competitors.
  1319. if ( !dominating )
  1320. {
  1321. if( dist == closestDist )
  1322. {
  1323. if( item->GetParent() == closest )
  1324. closest = item;
  1325. }
  1326. else if( dist < closestDist )
  1327. {
  1328. closestDist = dist;
  1329. closest = item;
  1330. }
  1331. }
  1332. }
  1333. // Construct a tight box (1/2 height and width) around the center of the closest item.
  1334. // All items which exist at least partly outside this box have sufficient other areas
  1335. // for selection and can be dropped.
  1336. if( closest ) // Don't try and get a tight bbox if nothing is near the mouse pointer
  1337. {
  1338. BOX2I tightBox = closest->GetBoundingBox();
  1339. tightBox.Inflate( -tightBox.GetWidth() / 4, -tightBox.GetHeight() / 4 );
  1340. for( int i = collector.GetCount() - 1; i >= 0; --i )
  1341. {
  1342. EDA_ITEM* item = collector[i];
  1343. if( item == closest )
  1344. continue;
  1345. if( !item->HitTest( tightBox, true ) )
  1346. collector.Transfer( item );
  1347. }
  1348. }
  1349. }
  1350. EE_SELECTION& EE_SELECTION_TOOL::RequestSelection( const std::vector<KICAD_T>& aScanTypes,
  1351. bool aPromoteCellSelections )
  1352. {
  1353. bool anyUnselected = false;
  1354. bool anySelected = false;
  1355. if( m_selection.Empty() )
  1356. {
  1357. VECTOR2D cursorPos = getViewControls()->GetCursorPosition( true );
  1358. ClearSelection();
  1359. SelectPoint( cursorPos, aScanTypes );
  1360. m_selection.SetIsHover( true );
  1361. m_selection.ClearReferencePoint();
  1362. }
  1363. else // Trim an existing selection by aFilterList
  1364. {
  1365. bool isMoving = false;
  1366. for( int i = (int) m_selection.GetSize() - 1; i >= 0; --i )
  1367. {
  1368. EDA_ITEM* item = (EDA_ITEM*) m_selection.GetItem( i );
  1369. isMoving |= static_cast<SCH_ITEM*>( item )->IsMoving();
  1370. if( !item->IsType( aScanTypes ) )
  1371. {
  1372. unselect( item );
  1373. anyUnselected = true;
  1374. }
  1375. }
  1376. if( !isMoving )
  1377. updateReferencePoint();
  1378. }
  1379. if( aPromoteCellSelections )
  1380. {
  1381. std::set<EDA_ITEM*> parents;
  1382. for( int i = (int) m_selection.GetSize() - 1; i >= 0; --i )
  1383. {
  1384. EDA_ITEM* item = (EDA_ITEM*) m_selection.GetItem( i );
  1385. if( item->Type() == SCH_TABLECELL_T )
  1386. {
  1387. parents.insert( item->GetParent() );
  1388. unselect( item );
  1389. anyUnselected = true;
  1390. }
  1391. }
  1392. for( EDA_ITEM* parent : parents )
  1393. {
  1394. if( !parent->IsSelected() )
  1395. {
  1396. select( parent );
  1397. anySelected = true;
  1398. }
  1399. }
  1400. }
  1401. if( anyUnselected )
  1402. m_toolMgr->ProcessEvent( EVENTS::UnselectedEvent );
  1403. if( anySelected )
  1404. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1405. return m_selection;
  1406. }
  1407. bool EE_SELECTION_TOOL::itemPassesFilter( EDA_ITEM* aItem )
  1408. {
  1409. if( !aItem )
  1410. return false;
  1411. // Locking is not yet exposed uniformly in the schematic
  1412. #if 0
  1413. if( SCH_ITEM* schItem = dynamic_cast<SCH_ITEM*>( aItem ) )
  1414. {
  1415. if( schItem->IsLocked() && !m_filter.lockedItems )
  1416. return false;
  1417. }
  1418. #endif
  1419. switch( aItem->Type() )
  1420. {
  1421. case SCH_SYMBOL_T:
  1422. case SCH_SHEET_T:
  1423. if( !m_filter.symbols )
  1424. return false;
  1425. break;
  1426. case SCH_PIN_T:
  1427. case SCH_SHEET_PIN_T:
  1428. if( !m_filter.pins )
  1429. return false;
  1430. break;
  1431. case SCH_LINE_T:
  1432. {
  1433. switch( static_cast<SCH_LINE*>( aItem )->GetLayer() )
  1434. {
  1435. case LAYER_WIRE:
  1436. case LAYER_BUS:
  1437. if( !m_filter.wires )
  1438. return false;
  1439. break;
  1440. default:
  1441. if( !m_filter.graphics )
  1442. return false;
  1443. }
  1444. break;
  1445. }
  1446. case SCH_SHAPE_T:
  1447. if( !m_filter.graphics )
  1448. return false;
  1449. break;
  1450. case SCH_TEXT_T:
  1451. case SCH_TEXTBOX_T:
  1452. case SCH_TABLE_T:
  1453. case SCH_TABLECELL_T:
  1454. case SCH_FIELD_T:
  1455. if( !m_filter.text )
  1456. return false;
  1457. break;
  1458. case SCH_LABEL_T:
  1459. case SCH_GLOBAL_LABEL_T:
  1460. case SCH_HIER_LABEL_T:
  1461. if( !m_filter.labels )
  1462. return false;
  1463. break;
  1464. case SCH_BITMAP_T:
  1465. if( !m_filter.images )
  1466. return false;
  1467. break;
  1468. default:
  1469. if( !m_filter.otherItems )
  1470. return false;
  1471. break;
  1472. }
  1473. return true;
  1474. }
  1475. void EE_SELECTION_TOOL::updateReferencePoint()
  1476. {
  1477. VECTOR2I refP( 0, 0 );
  1478. if( m_selection.Size() > 0 )
  1479. refP = static_cast<SCH_ITEM*>( m_selection.GetTopLeftItem() )->GetPosition();
  1480. m_selection.SetReferencePoint( refP );
  1481. }
  1482. // Some navigation actions are allowed in selectMultiple
  1483. const TOOL_ACTION* allowedActions[] = { &ACTIONS::panUp, &ACTIONS::panDown,
  1484. &ACTIONS::panLeft, &ACTIONS::panRight,
  1485. &ACTIONS::cursorUp, &ACTIONS::cursorDown,
  1486. &ACTIONS::cursorLeft, &ACTIONS::cursorRight,
  1487. &ACTIONS::cursorUpFast, &ACTIONS::cursorDownFast,
  1488. &ACTIONS::cursorLeftFast, &ACTIONS::cursorRightFast,
  1489. &ACTIONS::zoomIn, &ACTIONS::zoomOut,
  1490. &ACTIONS::zoomInCenter, &ACTIONS::zoomOutCenter,
  1491. &ACTIONS::zoomCenter, &ACTIONS::zoomFitScreen,
  1492. &ACTIONS::zoomFitObjects, nullptr };
  1493. bool EE_SELECTION_TOOL::selectMultiple()
  1494. {
  1495. bool cancelled = false; // Was the tool canceled while it was running?
  1496. m_multiple = true; // Multiple selection mode is active
  1497. KIGFX::VIEW* view = getView();
  1498. KIGFX::PREVIEW::SELECTION_AREA area;
  1499. view->Add( &area );
  1500. while( TOOL_EVENT* evt = Wait() )
  1501. {
  1502. int width = area.GetEnd().x - area.GetOrigin().x;
  1503. int height = area.GetEnd().y - area.GetOrigin().y;
  1504. /* Selection mode depends on direction of drag-selection:
  1505. * Left > Right : Select objects that are fully enclosed by selection
  1506. * Right > Left : Select objects that are crossed by selection
  1507. */
  1508. bool isGreedy = width < 0;
  1509. if( view->IsMirroredX() )
  1510. isGreedy = !isGreedy;
  1511. m_frame->GetCanvas()->SetCurrentCursor( isGreedy ? KICURSOR::SELECT_LASSO
  1512. : KICURSOR::SELECT_WINDOW );
  1513. if( evt->IsCancelInteractive() || evt->IsActivate() )
  1514. {
  1515. cancelled = true;
  1516. break;
  1517. }
  1518. if( evt->IsDrag( BUT_LEFT ) )
  1519. {
  1520. if( !m_drag_additive && !m_drag_subtractive )
  1521. ClearSelection();
  1522. // Start drawing a selection box
  1523. area.SetOrigin( evt->DragOrigin() );
  1524. area.SetEnd( evt->Position() );
  1525. area.SetAdditive( m_drag_additive );
  1526. area.SetSubtractive( m_drag_subtractive );
  1527. area.SetExclusiveOr( false );
  1528. view->SetVisible( &area, true );
  1529. view->Update( &area );
  1530. getViewControls()->SetAutoPan( true );
  1531. }
  1532. if( evt->IsMouseUp( BUT_LEFT ) )
  1533. {
  1534. getViewControls()->SetAutoPan( false );
  1535. // End drawing the selection box
  1536. view->SetVisible( &area, false );
  1537. // Fetch items from the RTree that are in our area of interest
  1538. std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> nearbyViewItems;
  1539. view->Query( area.ViewBBox(), nearbyViewItems );
  1540. // Build lists of nearby items and their children
  1541. std::unordered_set<EDA_ITEM*> nearbyItems;
  1542. std::vector<EDA_ITEM*> nearbyChildren;
  1543. std::vector<EDA_ITEM*> flaggedItems;
  1544. for( KIGFX::VIEW::LAYER_ITEM_PAIR& pair : nearbyViewItems )
  1545. {
  1546. if( EDA_ITEM* item = dynamic_cast<EDA_ITEM*>( pair.first ) )
  1547. {
  1548. if( nearbyItems.insert( item ).second )
  1549. {
  1550. item->ClearFlags( CANDIDATE );
  1551. if( SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( item ) )
  1552. {
  1553. sch_item->RunOnChildren(
  1554. [&]( SCH_ITEM* aChild )
  1555. {
  1556. // Filter pins by unit
  1557. if( SCH_PIN* pin = dynamic_cast<SCH_PIN*>( aChild ) )
  1558. {
  1559. int unit = pin->GetLibPin()->GetUnit();
  1560. if( unit && unit != pin->GetParentSymbol()->GetUnit() )
  1561. return;
  1562. }
  1563. nearbyChildren.push_back( aChild );
  1564. } );
  1565. }
  1566. }
  1567. }
  1568. }
  1569. BOX2I selectionRect( area.GetOrigin(), VECTOR2I( width, height ) );
  1570. selectionRect.Normalize();
  1571. bool anyAdded = false;
  1572. bool anySubtracted = false;
  1573. auto selectItem =
  1574. [&]( EDA_ITEM* aItem, EDA_ITEM_FLAGS flags )
  1575. {
  1576. if( m_subtractive || ( m_exclusive_or && aItem->IsSelected() ) )
  1577. {
  1578. if ( m_exclusive_or )
  1579. aItem->XorFlags( flags );
  1580. else
  1581. aItem->ClearFlags( flags );
  1582. if( !aItem->HasFlag( STARTPOINT ) && !aItem->HasFlag( ENDPOINT ) )
  1583. {
  1584. unselect( aItem );
  1585. anySubtracted = true;
  1586. }
  1587. // We changed one line endpoint on a selected line,
  1588. // update the view at least.
  1589. if( flags && !anySubtracted )
  1590. getView()->Update( aItem );
  1591. }
  1592. else
  1593. {
  1594. aItem->SetFlags( flags );
  1595. select( aItem );
  1596. anyAdded = true;
  1597. }
  1598. };
  1599. for( EDA_ITEM* item : nearbyItems )
  1600. {
  1601. bool selected = false;
  1602. EDA_ITEM_FLAGS flags = 0;
  1603. if( m_frame->GetRenderSettings()->m_ShowPinsElectricalType )
  1604. item->SetFlags( SHOW_ELEC_TYPE );
  1605. if( Selectable( item ) && itemPassesFilter( item ) )
  1606. {
  1607. if( item->Type() == SCH_LINE_T )
  1608. {
  1609. SCH_LINE* line = static_cast<SCH_LINE*>( item );
  1610. if( ( isGreedy && line->HitTest( selectionRect, false ) )
  1611. || ( selectionRect.Contains( line->GetEndPoint() )
  1612. && selectionRect.Contains( line->GetStartPoint() ) ) )
  1613. {
  1614. selected = true;
  1615. flags |= STARTPOINT | ENDPOINT;
  1616. }
  1617. else if( !isGreedy )
  1618. {
  1619. if( selectionRect.Contains( line->GetStartPoint() )
  1620. && line->IsStartDangling() )
  1621. {
  1622. selected = true;
  1623. flags |= STARTPOINT;
  1624. }
  1625. if( selectionRect.Contains( line->GetEndPoint() )
  1626. && line->IsEndDangling() )
  1627. {
  1628. selected = true;
  1629. flags |= ENDPOINT;
  1630. }
  1631. }
  1632. }
  1633. else
  1634. {
  1635. selected = item->HitTest( selectionRect, !isGreedy );
  1636. }
  1637. }
  1638. if( selected )
  1639. {
  1640. item->SetFlags( CANDIDATE );
  1641. flaggedItems.push_back( item );
  1642. selectItem( item, flags );
  1643. }
  1644. item->ClearFlags( SHOW_ELEC_TYPE );
  1645. }
  1646. for( EDA_ITEM* item : nearbyChildren )
  1647. {
  1648. if( m_frame->GetRenderSettings()->m_ShowPinsElectricalType )
  1649. item->SetFlags( SHOW_ELEC_TYPE );
  1650. if( Selectable( item )
  1651. && itemPassesFilter( item )
  1652. && !item->GetParent()->HasFlag( CANDIDATE )
  1653. && item->HitTest( selectionRect, !isGreedy ) )
  1654. {
  1655. selectItem( item, 0 );
  1656. }
  1657. item->ClearFlags( SHOW_ELEC_TYPE );
  1658. }
  1659. for( EDA_ITEM* item : flaggedItems )
  1660. item->ClearFlags( CANDIDATE );
  1661. m_selection.SetIsHover( false );
  1662. // Inform other potentially interested tools
  1663. if( anyAdded )
  1664. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1665. if( anySubtracted )
  1666. m_toolMgr->ProcessEvent( EVENTS::UnselectedEvent );
  1667. break; // Stop waiting for events
  1668. }
  1669. // Allow some actions for navigation
  1670. for( int i = 0; allowedActions[i]; ++i )
  1671. {
  1672. if( evt->IsAction( allowedActions[i] ) )
  1673. {
  1674. evt->SetPassEvent();
  1675. break;
  1676. }
  1677. }
  1678. }
  1679. getViewControls()->SetAutoPan( false );
  1680. // Stop drawing the selection box
  1681. view->Remove( &area );
  1682. m_multiple = false; // Multiple selection mode is inactive
  1683. if( !cancelled )
  1684. m_selection.ClearReferencePoint();
  1685. return cancelled;
  1686. }
  1687. bool EE_SELECTION_TOOL::selectTableCells( SCH_TABLE* aTable )
  1688. {
  1689. bool cancelled = false; // Was the tool canceled while it was running?
  1690. m_multiple = true; // Multiple selection mode is active
  1691. for( SCH_TABLECELL* cell : aTable->GetCells() )
  1692. {
  1693. if( cell->IsSelected() )
  1694. cell->SetFlags( CANDIDATE );
  1695. else
  1696. cell->ClearFlags( CANDIDATE );
  1697. }
  1698. auto wasSelected =
  1699. []( EDA_ITEM* aItem )
  1700. {
  1701. return ( aItem->GetFlags() & CANDIDATE ) > 0;
  1702. };
  1703. while( TOOL_EVENT* evt = Wait() )
  1704. {
  1705. if( evt->IsCancelInteractive() || evt->IsActivate() )
  1706. {
  1707. cancelled = true;
  1708. break;
  1709. }
  1710. else if( evt->IsDrag( BUT_LEFT ) )
  1711. {
  1712. getViewControls()->SetAutoPan( true );
  1713. BOX2I selectionRect( evt->DragOrigin(), evt->Position() - evt->DragOrigin() );
  1714. selectionRect.Normalize();
  1715. for( SCH_TABLECELL* cell : aTable->GetCells() )
  1716. {
  1717. bool doSelect = false;
  1718. if( cell->HitTest( selectionRect, false ) )
  1719. {
  1720. if( m_subtractive )
  1721. doSelect = false;
  1722. else if( m_exclusive_or )
  1723. doSelect = !wasSelected( cell );
  1724. else
  1725. doSelect = true;
  1726. }
  1727. else if( wasSelected( cell ) )
  1728. {
  1729. doSelect = m_additive || m_subtractive || m_exclusive_or;
  1730. }
  1731. if( doSelect && !cell->IsSelected() )
  1732. select( cell );
  1733. else if( !doSelect && cell->IsSelected() )
  1734. unselect( cell );
  1735. }
  1736. }
  1737. else if( evt->IsMouseUp( BUT_LEFT ) )
  1738. {
  1739. m_selection.SetIsHover( false );
  1740. bool anyAdded = false;
  1741. bool anySubtracted = false;
  1742. for( SCH_TABLECELL* cell : aTable->GetCells() )
  1743. {
  1744. if( cell->IsSelected() && !wasSelected( cell ) )
  1745. anyAdded = true;
  1746. else if( wasSelected( cell ) && !cell->IsSelected() )
  1747. anySubtracted = true;
  1748. }
  1749. // Inform other potentially interested tools
  1750. if( anyAdded )
  1751. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1752. if( anySubtracted )
  1753. m_toolMgr->ProcessEvent( EVENTS::UnselectedEvent );
  1754. break; // Stop waiting for events
  1755. }
  1756. else
  1757. {
  1758. // Allow some actions for navigation
  1759. for( int i = 0; allowedActions[i]; ++i )
  1760. {
  1761. if( evt->IsAction( allowedActions[i] ) )
  1762. {
  1763. evt->SetPassEvent();
  1764. break;
  1765. }
  1766. }
  1767. }
  1768. }
  1769. getViewControls()->SetAutoPan( false );
  1770. m_multiple = false; // Multiple selection mode is inactive
  1771. if( !cancelled )
  1772. m_selection.ClearReferencePoint();
  1773. return cancelled;
  1774. }
  1775. EDA_ITEM* EE_SELECTION_TOOL::GetNode( const VECTOR2I& aPosition )
  1776. {
  1777. EE_COLLECTOR collector;
  1778. //TODO(snh): Reimplement after exposing KNN interface
  1779. int pixelThreshold = KiROUND( getView()->ToWorld( HITTEST_THRESHOLD_PIXELS ) );
  1780. int gridThreshold = KiROUND( getView()->GetGAL()->GetGridSize().EuclideanNorm() );
  1781. int thresholdMax = std::max( pixelThreshold, gridThreshold );
  1782. for( int threshold : { 0, thresholdMax/4, thresholdMax/2, thresholdMax } )
  1783. {
  1784. collector.m_Threshold = threshold;
  1785. collector.Collect( m_frame->GetScreen(), connectedTypes, aPosition );
  1786. if( collector.GetCount() > 0 )
  1787. break;
  1788. }
  1789. return collector.GetCount() ? collector[ 0 ] : nullptr;
  1790. }
  1791. int EE_SELECTION_TOOL::SelectNode( const TOOL_EVENT& aEvent )
  1792. {
  1793. VECTOR2I cursorPos = getViewControls()->GetCursorPosition( false );
  1794. SelectPoint( cursorPos, connectedTypes );
  1795. return 0;
  1796. }
  1797. int EE_SELECTION_TOOL::SelectConnection( const TOOL_EVENT& aEvent )
  1798. {
  1799. RequestSelection( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T,
  1800. SCH_ITEM_LOCATE_GRAPHIC_LINE_T } );
  1801. if( m_selection.Empty() )
  1802. return 0;
  1803. unsigned done = false;
  1804. m_frame->GetScreen()->ClearDrawingState();
  1805. for( EDA_ITEM* selItem : m_selection.GetItems() )
  1806. {
  1807. if( selItem->Type() != SCH_LINE_T )
  1808. continue;
  1809. SCH_LINE* line = static_cast<SCH_LINE*>( selItem );
  1810. std::set<SCH_ITEM*> conns = m_frame->GetScreen()->MarkConnections( line, false );
  1811. for( SCH_ITEM* item : conns )
  1812. {
  1813. if( item->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T,
  1814. SCH_ITEM_LOCATE_GRAPHIC_LINE_T } )
  1815. && !item->IsSelected() )
  1816. {
  1817. done = true;
  1818. }
  1819. select( item );
  1820. }
  1821. if( !done )
  1822. {
  1823. conns = m_frame->GetScreen()->MarkConnections( line, true );
  1824. for( SCH_ITEM* item : conns )
  1825. select( item );
  1826. }
  1827. }
  1828. if( m_selection.GetSize() > 1 )
  1829. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1830. return 0;
  1831. }
  1832. int EE_SELECTION_TOOL::SelectColumns( const TOOL_EVENT& aEvent )
  1833. {
  1834. std::set<std::pair<SCH_TABLE*, int>> columns;
  1835. bool added = false;
  1836. for( EDA_ITEM* item : m_selection )
  1837. {
  1838. if( SCH_TABLECELL* cell = dynamic_cast<SCH_TABLECELL*>( item ) )
  1839. {
  1840. SCH_TABLE* table = static_cast<SCH_TABLE*>( cell->GetParent() );
  1841. columns.insert( std::make_pair( table, cell->GetColumn() ) );
  1842. }
  1843. }
  1844. for( auto& [ table, col ] : columns )
  1845. {
  1846. for( int row = 0; row < table->GetRowCount(); ++row )
  1847. {
  1848. SCH_TABLECELL* cell = table->GetCell( row, col );
  1849. if( !cell->IsSelected() )
  1850. {
  1851. select( table->GetCell( row, col ) );
  1852. added = true;
  1853. }
  1854. }
  1855. }
  1856. if( added )
  1857. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1858. return 0;
  1859. }
  1860. int EE_SELECTION_TOOL::SelectRows( const TOOL_EVENT& aEvent )
  1861. {
  1862. std::set<std::pair<SCH_TABLE*, int>> rows;
  1863. bool added = false;
  1864. for( EDA_ITEM* item : m_selection )
  1865. {
  1866. if( SCH_TABLECELL* cell = dynamic_cast<SCH_TABLECELL*>( item ) )
  1867. {
  1868. SCH_TABLE* table = static_cast<SCH_TABLE*>( cell->GetParent() );
  1869. rows.insert( std::make_pair( table, cell->GetRow() ) );
  1870. }
  1871. }
  1872. for( auto& [ table, row ] : rows )
  1873. {
  1874. for( int col = 0; col < table->GetColCount(); ++col )
  1875. {
  1876. SCH_TABLECELL* cell = table->GetCell( row, col );
  1877. if( !cell->IsSelected() )
  1878. {
  1879. select( table->GetCell( row, col ) );
  1880. added = true;
  1881. }
  1882. }
  1883. }
  1884. if( added )
  1885. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1886. return 0;
  1887. }
  1888. int EE_SELECTION_TOOL::SelectTable( const TOOL_EVENT& aEvent )
  1889. {
  1890. std::set<SCH_TABLE*> tables;
  1891. bool added = false;
  1892. for( EDA_ITEM* item : m_selection )
  1893. {
  1894. if( SCH_TABLECELL* cell = dynamic_cast<SCH_TABLECELL*>( item ) )
  1895. tables.insert( static_cast<SCH_TABLE*>( cell->GetParent() ) );
  1896. }
  1897. ClearSelection();
  1898. for( SCH_TABLE* table : tables )
  1899. {
  1900. if( !table->IsSelected() )
  1901. {
  1902. select( table );
  1903. added = true;
  1904. }
  1905. }
  1906. if( added )
  1907. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  1908. return 0;
  1909. }
  1910. int EE_SELECTION_TOOL::ClearSelection( const TOOL_EVENT& aEvent )
  1911. {
  1912. ClearSelection();
  1913. return 0;
  1914. }
  1915. void EE_SELECTION_TOOL::ZoomFitCrossProbeBBox( const BOX2I& aBBox )
  1916. {
  1917. if( aBBox.GetWidth() == 0 )
  1918. return;
  1919. BOX2I bbox = aBBox;
  1920. bbox.Normalize();
  1921. VECTOR2I bbSize = bbox.Inflate( KiROUND( bbox.GetWidth() * 0.2f ) ).GetSize();
  1922. VECTOR2D screenSize = getView()->GetViewport().GetSize();
  1923. // This code tries to come up with a zoom factor that doesn't simply zoom in to the cross
  1924. // probed symbol, but instead shows a reasonable amount of the circuit around it to provide
  1925. // context. This reduces the need to manually change the zoom because it's too close.
  1926. // Using the default text height as a constant to compare against, use the height of the
  1927. // bounding box of visible items for a footprint to figure out if this is a big symbol (like
  1928. // a processor) or a small symbol (like a resistor). This ratio is not useful by itself as a
  1929. // scaling factor. It must be "bent" to provide good scaling at varying symbol sizes. Bigger
  1930. // symbols need less scaling than small ones.
  1931. double currTextHeight = schIUScale.MilsToIU( DEFAULT_TEXT_SIZE );
  1932. double compRatio = bbSize.y / currTextHeight; // Ratio of symbol to text height
  1933. double compRatioBent = 1.0;
  1934. // LUT to scale zoom ratio to provide reasonable schematic context. Must work with symbols
  1935. // of varying sizes (e.g. 0402 package and 200 pin BGA).
  1936. // Each entry represents a compRatio (symbol height / default text height) and an amount to
  1937. // scale by.
  1938. std::vector<std::pair<double, double>> lut{ { 1.25, 16 },
  1939. { 2.5, 12 },
  1940. { 5, 8 },
  1941. { 6, 6 },
  1942. { 10, 4 },
  1943. { 20, 2 },
  1944. { 40, 1.5 },
  1945. { 100, 1 } };
  1946. std::vector<std::pair<double, double>>::iterator it;
  1947. // Large symbol default is last LUT entry (1:1).
  1948. compRatioBent = lut.back().second;
  1949. // Use LUT to do linear interpolation of "compRatio" within "first", then use that result to
  1950. // linearly interpolate "second" which gives the scaling factor needed.
  1951. if( compRatio >= lut.front().first )
  1952. {
  1953. for( it = lut.begin(); it < lut.end() - 1; ++it )
  1954. {
  1955. if( it->first <= compRatio && next( it )->first >= compRatio )
  1956. {
  1957. double diffx = compRatio - it->first;
  1958. double diffn = next( it )->first - it->first;
  1959. compRatioBent = it->second + ( next( it )->second - it->second ) * diffx / diffn;
  1960. break; // We have our interpolated value
  1961. }
  1962. }
  1963. }
  1964. else
  1965. {
  1966. compRatioBent = lut.front().second; // Small symbol default is first entry
  1967. }
  1968. // This is similar to the original KiCad code that scaled the zoom to make sure symbols were
  1969. // visible on screen. It's simply a ratio of screen size to symbol size, and its job is to
  1970. // zoom in to make the component fullscreen. Earlier in the code the symbol BBox is given a
  1971. // 20% margin to add some breathing room. We compare the height of this enlarged symbol bbox
  1972. // to the default text height. If a symbol will end up with the sides clipped, we adjust
  1973. // later to make sure it fits on screen.
  1974. screenSize.x = std::max( 10.0, screenSize.x );
  1975. screenSize.y = std::max( 10.0, screenSize.y );
  1976. double ratio = std::max( -1.0, fabs( bbSize.y / screenSize.y ) );
  1977. // Original KiCad code for how much to scale the zoom
  1978. double kicadRatio = std::max( fabs( bbSize.x / screenSize.x ),
  1979. fabs( bbSize.y / screenSize.y ) );
  1980. // If the width of the part we're probing is bigger than what the screen width will be after
  1981. // the zoom, then punt and use the KiCad zoom algorithm since it guarantees the part's width
  1982. // will be encompassed within the screen.
  1983. if( bbSize.x > screenSize.x * ratio * compRatioBent )
  1984. {
  1985. // Use standard KiCad zoom for parts too wide to fit on screen/
  1986. ratio = kicadRatio;
  1987. compRatioBent = 1.0; // Reset so we don't modify the "KiCad" ratio
  1988. wxLogTrace( "CROSS_PROBE_SCALE",
  1989. "Part TOO WIDE for screen. Using normal KiCad zoom ratio: %1.5f", ratio );
  1990. }
  1991. // Now that "compRatioBent" holds our final scaling factor we apply it to the original
  1992. // fullscreen zoom ratio to arrive at the final ratio itself.
  1993. ratio *= compRatioBent;
  1994. bool alwaysZoom = false; // DEBUG - allows us to minimize zooming or not
  1995. // Try not to zoom on every cross-probe; it gets very noisy
  1996. if( ( ratio < 0.5 || ratio > 1.0 ) || alwaysZoom )
  1997. getView()->SetScale( getView()->GetScale() / ratio );
  1998. }
  1999. void EE_SELECTION_TOOL::SyncSelection( const std::optional<SCH_SHEET_PATH>& targetSheetPath,
  2000. SCH_ITEM* focusItem, const std::vector<SCH_ITEM*>& items )
  2001. {
  2002. SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
  2003. if( !editFrame )
  2004. return;
  2005. if( targetSheetPath && targetSheetPath != editFrame->Schematic().CurrentSheet() )
  2006. {
  2007. editFrame->Schematic().SetCurrentSheet( *targetSheetPath );
  2008. editFrame->DisplayCurrentSheet();
  2009. }
  2010. ClearSelection( items.size() > 0 ? true /*quiet mode*/ : false );
  2011. // Perform individual selection of each item before processing the event.
  2012. for( SCH_ITEM* item : items )
  2013. {
  2014. SCH_ITEM* parent = dynamic_cast<SCH_ITEM*>( item->GetParent() );
  2015. // Make sure we only select items on the current screen
  2016. if( m_frame->GetScreen()->CheckIfOnDrawList( item )
  2017. || ( parent && m_frame->GetScreen()->CheckIfOnDrawList( parent ) ) )
  2018. {
  2019. select( item );
  2020. }
  2021. }
  2022. BOX2I bbox = m_selection.GetBoundingBox();
  2023. if( bbox.GetWidth() != 0 && bbox.GetHeight() != 0 )
  2024. {
  2025. if( m_frame->eeconfig()->m_CrossProbing.center_on_items )
  2026. {
  2027. if( m_frame->eeconfig()->m_CrossProbing.zoom_to_fit )
  2028. ZoomFitCrossProbeBBox( bbox );
  2029. editFrame->FocusOnItem( focusItem );
  2030. if( !focusItem )
  2031. editFrame->FocusOnLocation( bbox.Centre() );
  2032. }
  2033. }
  2034. if( m_selection.Size() > 0 )
  2035. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  2036. }
  2037. void EE_SELECTION_TOOL::RebuildSelection()
  2038. {
  2039. m_selection.Clear();
  2040. if( m_isSymbolEditor )
  2041. {
  2042. LIB_SYMBOL* start = static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->GetCurSymbol();
  2043. for( SCH_ITEM& item : start->GetDrawItems() )
  2044. {
  2045. if( item.IsSelected() )
  2046. select( &item );
  2047. }
  2048. }
  2049. else
  2050. {
  2051. for( SCH_ITEM* item : m_frame->GetScreen()->Items() )
  2052. {
  2053. // If the field and symbol are selected, only use the symbol
  2054. if( item->IsSelected() )
  2055. {
  2056. select( item );
  2057. }
  2058. else
  2059. {
  2060. item->RunOnChildren(
  2061. [&]( SCH_ITEM* aChild )
  2062. {
  2063. if( aChild->IsSelected() )
  2064. select( aChild );
  2065. } );
  2066. }
  2067. }
  2068. }
  2069. updateReferencePoint();
  2070. // Inform other potentially interested tools
  2071. m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
  2072. }
  2073. bool EE_SELECTION_TOOL::Selectable( const EDA_ITEM* aItem, const VECTOR2I* aPos,
  2074. bool checkVisibilityOnly ) const
  2075. {
  2076. // NOTE: in the future this is where Eeschema layer/itemtype visibility will be handled
  2077. SYMBOL_EDIT_FRAME* symEditFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
  2078. // Do not allow selection of anything except fields when the current symbol in the symbol
  2079. // editor is a derived symbol.
  2080. if( symEditFrame && symEditFrame->IsSymbolAlias() && aItem->Type() != SCH_FIELD_T )
  2081. return false;
  2082. switch( aItem->Type() )
  2083. {
  2084. case SCH_PIN_T:
  2085. {
  2086. const SCH_PIN* pin = static_cast<const SCH_PIN*>( aItem );
  2087. if( symEditFrame )
  2088. {
  2089. if( pin->GetUnit() && pin->GetUnit() != symEditFrame->GetUnit() )
  2090. return false;
  2091. if( pin->GetBodyStyle() && pin->GetBodyStyle() != symEditFrame->GetBodyStyle() )
  2092. return false;
  2093. }
  2094. if( !pin->IsVisible() && !m_frame->GetShowAllPins() )
  2095. return false;
  2096. if( !m_filter.pins )
  2097. {
  2098. // Pin anchors have to be allowed for auto-starting wires.
  2099. if( aPos )
  2100. {
  2101. EE_GRID_HELPER grid( m_toolMgr );
  2102. GRID_HELPER_GRIDS pinGrid = grid.GetItemGrid( pin );
  2103. if( pin->IsPointClickableAnchor( grid.BestSnapAnchor( *aPos, pinGrid ) ) )
  2104. return true;
  2105. }
  2106. return false;
  2107. }
  2108. break;
  2109. }
  2110. case SCH_DIRECTIVE_LABEL_T:
  2111. if( !m_frame->eeconfig()->m_Appearance.show_directive_labels )
  2112. return false;
  2113. break;
  2114. case LIB_SYMBOL_T: // In symbol_editor we do not want to select the symbol itself.
  2115. return false;
  2116. case SCH_FIELD_T: // SCH_FIELD objects are not unit/body-style-specific.
  2117. {
  2118. const SCH_FIELD* field = static_cast<const SCH_FIELD*>( aItem );
  2119. if( !field->IsVisible() && !( symEditFrame && symEditFrame->GetShowInvisibleFields() ) )
  2120. return false;
  2121. break;
  2122. }
  2123. case SCH_SHAPE_T:
  2124. case SCH_TEXT_T:
  2125. case SCH_TEXTBOX_T:
  2126. if( symEditFrame )
  2127. {
  2128. const SCH_ITEM* sch_item = static_cast<const SCH_ITEM*>( aItem );
  2129. if( sch_item->GetUnit() && sch_item->GetUnit() != symEditFrame->GetUnit() )
  2130. return false;
  2131. if( sch_item->GetBodyStyle() && sch_item->GetBodyStyle() != symEditFrame->GetBodyStyle() )
  2132. return false;
  2133. }
  2134. break;
  2135. case SCH_MARKER_T: // Always selectable
  2136. return true;
  2137. case SCH_TABLECELL_T:
  2138. {
  2139. const SCH_TABLECELL* cell = static_cast<const SCH_TABLECELL*>( aItem );
  2140. if( cell->GetColSpan() == 0 || cell->GetRowSpan() == 0 )
  2141. return false;
  2142. break;
  2143. }
  2144. default: // Suppress warnings
  2145. break;
  2146. }
  2147. return true;
  2148. }
  2149. void EE_SELECTION_TOOL::ClearSelection( bool aQuietMode )
  2150. {
  2151. if( m_selection.Empty() )
  2152. return;
  2153. while( m_selection.GetSize() )
  2154. unhighlight( m_selection.Front(), SELECTED, &m_selection );
  2155. getView()->Update( &m_selection );
  2156. m_selection.SetIsHover( false );
  2157. m_selection.ClearReferencePoint();
  2158. // Inform other potentially interested tools
  2159. if( !aQuietMode )
  2160. m_toolMgr->ProcessEvent( EVENTS::ClearedEvent );
  2161. }
  2162. void EE_SELECTION_TOOL::select( EDA_ITEM* aItem )
  2163. {
  2164. highlight( aItem, SELECTED, &m_selection );
  2165. }
  2166. void EE_SELECTION_TOOL::unselect( EDA_ITEM* aItem )
  2167. {
  2168. unhighlight( aItem, SELECTED, &m_selection );
  2169. }
  2170. void EE_SELECTION_TOOL::highlight( EDA_ITEM* aItem, int aMode, SELECTION* aGroup )
  2171. {
  2172. if( aMode == SELECTED )
  2173. aItem->SetSelected();
  2174. else if( aMode == BRIGHTENED )
  2175. aItem->SetBrightened();
  2176. if( aGroup )
  2177. aGroup->Add( aItem );
  2178. // Highlight pins and fields. (All the other symbol children are currently only
  2179. // represented in the LIB_SYMBOL and will inherit the settings of the parent symbol.)
  2180. if( SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( aItem ) )
  2181. {
  2182. sch_item->RunOnChildren(
  2183. [&]( SCH_ITEM* aChild )
  2184. {
  2185. if( aMode == SELECTED )
  2186. {
  2187. aChild->SetSelected();
  2188. getView()->Hide( aChild, true );
  2189. }
  2190. else if( aMode == BRIGHTENED )
  2191. {
  2192. aChild->SetBrightened();
  2193. }
  2194. } );
  2195. }
  2196. if( aGroup && aMode != BRIGHTENED )
  2197. getView()->Hide( aItem, true );
  2198. if( aItem->GetParent() && aItem->GetParent()->Type() != SCHEMATIC_T )
  2199. getView()->Update( aItem->GetParent(), KIGFX::REPAINT );
  2200. getView()->Update( aItem, KIGFX::REPAINT );
  2201. }
  2202. void EE_SELECTION_TOOL::unhighlight( EDA_ITEM* aItem, int aMode, SELECTION* aGroup )
  2203. {
  2204. if( aMode == SELECTED )
  2205. {
  2206. aItem->ClearSelected();
  2207. // Lines need endpoints cleared here
  2208. if( aItem->Type() == SCH_LINE_T )
  2209. aItem->ClearFlags( STARTPOINT | ENDPOINT );
  2210. if( aMode != BRIGHTENED )
  2211. getView()->Hide( aItem, false );
  2212. }
  2213. else if( aMode == BRIGHTENED )
  2214. {
  2215. aItem->ClearBrightened();
  2216. }
  2217. if( aGroup )
  2218. aGroup->Remove( aItem );
  2219. // Unhighlight pins and fields. (All the other symbol children are currently only
  2220. // represented in the LIB_SYMBOL.)
  2221. if( SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( aItem ) )
  2222. {
  2223. sch_item->RunOnChildren(
  2224. [&]( SCH_ITEM* aChild )
  2225. {
  2226. if( aMode == SELECTED )
  2227. {
  2228. aChild->ClearSelected();
  2229. getView()->Hide( aChild, false );
  2230. }
  2231. else if( aMode == BRIGHTENED )
  2232. {
  2233. aChild->ClearBrightened();
  2234. }
  2235. if( aGroup )
  2236. aGroup->Remove( aChild );
  2237. } );
  2238. }
  2239. if( aItem->GetParent() && aItem->GetParent()->Type() != SCHEMATIC_T )
  2240. getView()->Update( aItem->GetParent(), KIGFX::REPAINT );
  2241. getView()->Update( aItem, KIGFX::REPAINT );
  2242. }
  2243. bool EE_SELECTION_TOOL::selectionContains( const VECTOR2I& aPoint ) const
  2244. {
  2245. const unsigned GRIP_MARGIN = 20;
  2246. int margin = KiROUND( getView()->ToWorld( GRIP_MARGIN ) );
  2247. // Check if the point is located within any of the currently selected items bounding boxes
  2248. for( EDA_ITEM* item : m_selection )
  2249. {
  2250. BOX2I itemBox = item->ViewBBox();
  2251. itemBox.Inflate( margin ); // Give some margin for gripping an item
  2252. if( itemBox.Contains( aPoint ) )
  2253. return true;
  2254. }
  2255. return false;
  2256. }
  2257. void EE_SELECTION_TOOL::setTransitions()
  2258. {
  2259. Go( &EE_SELECTION_TOOL::UpdateMenu, ACTIONS::updateMenu.MakeEvent() );
  2260. Go( &EE_SELECTION_TOOL::Main, EE_ACTIONS::selectionActivate.MakeEvent() );
  2261. Go( &EE_SELECTION_TOOL::SelectNode, EE_ACTIONS::selectNode.MakeEvent() );
  2262. Go( &EE_SELECTION_TOOL::SelectConnection, EE_ACTIONS::selectConnection.MakeEvent() );
  2263. Go( &EE_SELECTION_TOOL::SelectColumns, ACTIONS::selectColumns.MakeEvent() );
  2264. Go( &EE_SELECTION_TOOL::SelectRows, ACTIONS::selectRows.MakeEvent() );
  2265. Go( &EE_SELECTION_TOOL::SelectTable, ACTIONS::selectTable.MakeEvent() );
  2266. Go( &EE_SELECTION_TOOL::ClearSelection, EE_ACTIONS::clearSelection.MakeEvent() );
  2267. Go( &EE_SELECTION_TOOL::AddItemToSel, EE_ACTIONS::addItemToSel.MakeEvent() );
  2268. Go( &EE_SELECTION_TOOL::AddItemsToSel, EE_ACTIONS::addItemsToSel.MakeEvent() );
  2269. Go( &EE_SELECTION_TOOL::RemoveItemFromSel, EE_ACTIONS::removeItemFromSel.MakeEvent() );
  2270. Go( &EE_SELECTION_TOOL::RemoveItemsFromSel, EE_ACTIONS::removeItemsFromSel.MakeEvent() );
  2271. Go( &EE_SELECTION_TOOL::SelectionMenu, EE_ACTIONS::selectionMenu.MakeEvent() );
  2272. Go( &EE_SELECTION_TOOL::SelectAll, EE_ACTIONS::selectAll.MakeEvent() );
  2273. Go( &EE_SELECTION_TOOL::UnselectAll, EE_ACTIONS::unselectAll.MakeEvent() );
  2274. Go( &EE_SELECTION_TOOL::disambiguateCursor, EVENTS::DisambiguatePoint );
  2275. }