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.

573 lines
17 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2016 CERN
  5. * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <bitmaps.h>
  26. #include <tool/actions.h>
  27. #include <tool/tool_manager.h>
  28. #include <eda_draw_frame.h>
  29. #include <class_draw_panel_gal.h>
  30. #include <view/view.h>
  31. #include <view/view_controls.h>
  32. #include <gal/graphics_abstraction_layer.h>
  33. #include <settings/app_settings.h>
  34. #include <base_screen.h>
  35. #include <tool/common_tools.h>
  36. #include <id.h>
  37. #include <project.h>
  38. #include <kiface_i.h>
  39. #include <dialog_configure_paths.h>
  40. #include <base_units.h>
  41. void COMMON_TOOLS::Reset( RESET_REASON aReason )
  42. {
  43. m_frame = getEditFrame<EDA_DRAW_FRAME>();
  44. m_grids.clear();
  45. for( const wxString& gridDef : m_toolMgr->GetSettings()->m_Window.grid.sizes )
  46. {
  47. int gridSize = (int) ValueFromString( EDA_UNITS::MILLIMETRES, gridDef, true );
  48. m_grids.emplace_back( gridSize, gridSize );
  49. }
  50. if( aReason == GAL_SWITCH )
  51. OnGridChanged();
  52. }
  53. int COMMON_TOOLS::SelectionTool( const TOOL_EVENT& aEvent )
  54. {
  55. // Since selection tools are run permanently underneath the toolStack, this is really
  56. // just a cancel of whatever other tools might be running.
  57. m_toolMgr->ProcessEvent( TOOL_EVENT( TC_COMMAND, TA_CANCEL_TOOL ) );
  58. return 0;
  59. }
  60. // Cursor control
  61. int COMMON_TOOLS::CursorControl( const TOOL_EVENT& aEvent )
  62. {
  63. long type = aEvent.Parameter<intptr_t>();
  64. bool fastMove = type & ACTIONS::CURSOR_FAST_MOVE;
  65. type &= ~ACTIONS::CURSOR_FAST_MOVE;
  66. bool mirroredX = getView()->IsMirroredX();
  67. VECTOR2D cursor = getViewControls()->GetRawCursorPosition( false );
  68. VECTOR2D gridSize = getView()->GetGAL()->GetGridSize();
  69. if( fastMove )
  70. gridSize = gridSize * 10;
  71. switch( type )
  72. {
  73. case ACTIONS::CURSOR_UP:
  74. cursor -= VECTOR2D( 0, gridSize.y );
  75. break;
  76. case ACTIONS::CURSOR_DOWN:
  77. cursor += VECTOR2D( 0, gridSize.y );
  78. break;
  79. case ACTIONS::CURSOR_LEFT:
  80. cursor -= VECTOR2D( mirroredX ? -gridSize.x : gridSize.x, 0 );
  81. break;
  82. case ACTIONS::CURSOR_RIGHT:
  83. cursor += VECTOR2D( mirroredX ? -gridSize.x : gridSize.x, 0 );
  84. break;
  85. case ACTIONS::CURSOR_CLICK: // fall through
  86. case ACTIONS::CURSOR_DBL_CLICK:
  87. case ACTIONS::CURSOR_RIGHT_CLICK:
  88. {
  89. TOOL_ACTIONS action = TA_MOUSE_CLICK;
  90. TOOL_MOUSE_BUTTONS button = BUT_LEFT;
  91. int modifiers = 0;
  92. modifiers |= wxGetKeyState( WXK_SHIFT ) ? MD_SHIFT : 0;
  93. modifiers |= wxGetKeyState( WXK_CONTROL ) ? MD_CTRL : 0;
  94. modifiers |= wxGetKeyState( WXK_ALT ) ? MD_ALT : 0;
  95. if( type == ACTIONS::CURSOR_DBL_CLICK )
  96. action = TA_MOUSE_DBLCLICK;
  97. if( type == ACTIONS::CURSOR_RIGHT_CLICK )
  98. button = BUT_RIGHT;
  99. TOOL_EVENT evt( TC_MOUSE, action, button | modifiers );
  100. evt.SetMousePosition( getViewControls()->GetCursorPosition() );
  101. m_toolMgr->ProcessEvent( evt );
  102. return 0;
  103. }
  104. default:
  105. wxFAIL_MSG( "CursorControl(): unexpected request" );
  106. }
  107. getViewControls()->SetCursorPosition( cursor, true, true, type );
  108. m_toolMgr->RunAction( ACTIONS::refreshPreview );
  109. return 0;
  110. }
  111. int COMMON_TOOLS::PanControl( const TOOL_EVENT& aEvent )
  112. {
  113. long type = aEvent.Parameter<intptr_t>();
  114. KIGFX::VIEW* view = getView();
  115. VECTOR2D center = view->GetCenter();
  116. VECTOR2D gridSize = getView()->GetGAL()->GetGridSize() * 10;
  117. bool mirroredX = view->IsMirroredX();
  118. switch( type )
  119. {
  120. case ACTIONS::CURSOR_UP:
  121. center -= VECTOR2D( 0, gridSize.y );
  122. break;
  123. case ACTIONS::CURSOR_DOWN:
  124. center += VECTOR2D( 0, gridSize.y );
  125. break;
  126. case ACTIONS::CURSOR_LEFT:
  127. center -= VECTOR2D( mirroredX ? -gridSize.x : gridSize.x, 0 );
  128. break;
  129. case ACTIONS::CURSOR_RIGHT:
  130. center += VECTOR2D( mirroredX ? -gridSize.x : gridSize.x, 0 );
  131. break;
  132. default:
  133. wxFAIL;
  134. break;
  135. }
  136. view->SetCenter( center );
  137. return 0;
  138. }
  139. int COMMON_TOOLS::ZoomRedraw( const TOOL_EVENT& aEvent )
  140. {
  141. m_frame->HardRedraw();
  142. return 0;
  143. }
  144. int COMMON_TOOLS::ZoomInOut( const TOOL_EVENT& aEvent )
  145. {
  146. bool direction = aEvent.IsAction( &ACTIONS::zoomIn );
  147. return doZoomInOut( direction, true );
  148. }
  149. int COMMON_TOOLS::ZoomInOutCenter( const TOOL_EVENT& aEvent )
  150. {
  151. bool direction = aEvent.IsAction( &ACTIONS::zoomInCenter );
  152. return doZoomInOut( direction, false );
  153. }
  154. int COMMON_TOOLS::doZoomInOut( bool aDirection, bool aCenterOnCursor )
  155. {
  156. double zoom = m_frame->GetCanvas()->GetLegacyZoom();
  157. // Step must be AT LEAST 1.3
  158. if( aDirection )
  159. zoom /= 1.3;
  160. else
  161. zoom *= 1.3;
  162. // Now look for the next closest menu step
  163. std::vector<double>& zoomList = m_frame->GetScreen()->m_ZoomList;
  164. int idx;
  165. if( aDirection )
  166. {
  167. for( idx = zoomList.size() - 1; idx >= 0; --idx )
  168. {
  169. if( zoomList[idx] <= zoom )
  170. break;
  171. }
  172. if( idx < 0 )
  173. idx = 0; // if we ran off the end then peg to the end
  174. }
  175. else
  176. {
  177. for( idx = 0; idx < (int)zoomList.size(); ++idx )
  178. {
  179. if( zoomList[idx] >= zoom )
  180. break;
  181. }
  182. if( idx >= (int)zoomList.size() )
  183. idx = zoomList.size() - 1; // if we ran off the end then peg to the end
  184. }
  185. return doZoomToPreset( idx + 1, aCenterOnCursor );
  186. }
  187. int COMMON_TOOLS::ZoomCenter( const TOOL_EVENT& aEvent )
  188. {
  189. KIGFX::VIEW_CONTROLS* ctls = getViewControls();
  190. ctls->CenterOnCursor();
  191. return 0;
  192. }
  193. int COMMON_TOOLS::ZoomFitScreen( const TOOL_EVENT& aEvent )
  194. {
  195. KIGFX::VIEW* view = getView();
  196. EDA_DRAW_PANEL_GAL* canvas = m_frame->GetCanvas();
  197. EDA_DRAW_FRAME* frame = getEditFrame<EDA_DRAW_FRAME>();
  198. BOX2I bBox = frame->GetDocumentExtents();
  199. BOX2I defaultBox = canvas->GetDefaultViewBBox();
  200. VECTOR2D scrollbarSize = VECTOR2D( canvas->GetSize() - canvas->GetClientSize() );
  201. view->SetScale( 1.0 ); // the best scale will be fixed later, from this initial value
  202. // but this call ensure all view parameters are up to date
  203. // especially at init time
  204. VECTOR2D screenSize = view->ToWorld( canvas->GetClientSize(), false );
  205. if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
  206. bBox = defaultBox;
  207. VECTOR2D vsize = bBox.GetSize();
  208. double scale = view->GetScale() / std::max( fabs( vsize.x / screenSize.x ),
  209. fabs( vsize.y / screenSize.y ) );
  210. // Reserve a 10% margin around component bounding box.
  211. double margin_scale_factor = 1.1;
  212. // Leave a bigger margin for library editors & viewers
  213. if( frame->IsType( FRAME_FOOTPRINT_VIEWER ) || frame->IsType( FRAME_FOOTPRINT_VIEWER_MODAL )
  214. || frame->IsType( FRAME_SCH_VIEWER ) || frame->IsType( FRAME_SCH_VIEWER_MODAL ) )
  215. {
  216. margin_scale_factor = 1.4;
  217. }
  218. else if( frame->IsType( FRAME_SCH_LIB_EDITOR ) || frame->IsType( FRAME_FOOTPRINT_EDITOR ) )
  219. {
  220. margin_scale_factor = 2;
  221. }
  222. view->SetScale( scale / margin_scale_factor );
  223. view->SetCenter( bBox.Centre() );
  224. // Take scrollbars into account
  225. VECTOR2D worldScrollbarSize = view->ToWorld( scrollbarSize, false );
  226. view->SetCenter( view->GetCenter() + worldScrollbarSize / 2.0 );
  227. return 0;
  228. }
  229. int COMMON_TOOLS::CenterContents( const TOOL_EVENT& aEvent )
  230. {
  231. EDA_DRAW_PANEL_GAL* canvas = m_frame->GetCanvas();
  232. BOX2I bBox = getModel<EDA_ITEM>()->ViewBBox();
  233. if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
  234. bBox = canvas->GetDefaultViewBBox();
  235. getView()->SetCenter( bBox.Centre() );
  236. // Take scrollbars into account
  237. VECTOR2D scrollbarSize = VECTOR2D( canvas->GetSize() - canvas->GetClientSize() );
  238. VECTOR2D worldScrollbarSize = getView()->ToWorld( scrollbarSize, false );
  239. getView()->SetCenter( getView()->GetCenter() + worldScrollbarSize / 2.0 );
  240. return 0;
  241. }
  242. int COMMON_TOOLS::ZoomPreset( const TOOL_EVENT& aEvent )
  243. {
  244. unsigned int idx = aEvent.Parameter<intptr_t>();
  245. return doZoomToPreset( idx, false );
  246. }
  247. // Note: idx == 0 is Auto; idx == 1 is first entry in zoomList
  248. int COMMON_TOOLS::doZoomToPreset( int idx, bool aCenterOnCursor )
  249. {
  250. std::vector<double>& zoomList = m_frame->GetScreen()->m_ZoomList;
  251. KIGFX::VIEW* view = m_frame->GetCanvas()->GetView();
  252. if( idx == 0 ) // Zoom Auto
  253. {
  254. TOOL_EVENT dummy;
  255. return ZoomFitScreen( dummy );
  256. }
  257. else
  258. {
  259. idx--;
  260. }
  261. double scale = m_frame->GetZoomLevelCoeff() / zoomList[idx];
  262. if( aCenterOnCursor )
  263. {
  264. view->SetScale( scale, getViewControls()->GetCursorPosition() );
  265. if( getViewControls()->IsCursorWarpingEnabled() )
  266. getViewControls()->CenterOnCursor();
  267. }
  268. else
  269. {
  270. view->SetScale( scale );
  271. }
  272. return 0;
  273. }
  274. // Grid control
  275. int COMMON_TOOLS::GridNext( const TOOL_EVENT& aEvent )
  276. {
  277. int& currentGrid = m_toolMgr->GetSettings()->m_Window.grid.last_size_idx;
  278. if( currentGrid + 1 < int( m_grids.size() ) )
  279. currentGrid++;
  280. return OnGridChanged();
  281. }
  282. int COMMON_TOOLS::GridPrev( const TOOL_EVENT& aEvent )
  283. {
  284. int& currentGrid = m_toolMgr->GetSettings()->m_Window.grid.last_size_idx;
  285. if( currentGrid > 0 )
  286. currentGrid--;
  287. return OnGridChanged();
  288. }
  289. int COMMON_TOOLS::GridPreset( const TOOL_EVENT& aEvent )
  290. {
  291. return GridPreset( aEvent.Parameter<intptr_t>() );
  292. }
  293. int COMMON_TOOLS::GridPreset( int idx )
  294. {
  295. int& currentGrid = m_toolMgr->GetSettings()->m_Window.grid.last_size_idx;
  296. currentGrid = std::max( 0, std::min( idx, (int) m_grids.size() - 1 ) );
  297. return OnGridChanged();
  298. }
  299. int COMMON_TOOLS::OnGridChanged()
  300. {
  301. int& currentGrid = m_toolMgr->GetSettings()->m_Window.grid.last_size_idx;
  302. // Update the combobox (if any)
  303. wxUpdateUIEvent dummy;
  304. m_frame->OnUpdateSelectGrid( dummy );
  305. // Update GAL canvas from screen
  306. getView()->GetGAL()->SetGridSize( m_grids[ currentGrid ] );
  307. getView()->GetGAL()->SetGridVisibility( m_toolMgr->GetSettings()->m_Window.grid.show );
  308. getView()->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
  309. // Put cursor on new grid
  310. VECTOR2D gridCursor = getViewControls()->GetCursorPosition( true );
  311. getViewControls()->SetCrossHairCursorPosition( gridCursor, false );
  312. return 0;
  313. }
  314. int COMMON_TOOLS::ToggleGrid( const TOOL_EVENT& aEvent )
  315. {
  316. m_frame->SetGridVisibility( !m_frame->IsGridVisible() );
  317. return 0;
  318. }
  319. int COMMON_TOOLS::GridProperties( const TOOL_EVENT& aEvent )
  320. {
  321. wxCommandEvent cmd( wxEVT_COMMAND_MENU_SELECTED );
  322. cmd.SetId( ID_GRID_SETTINGS );
  323. m_frame->ProcessEvent( cmd );
  324. return 0;
  325. }
  326. int COMMON_TOOLS::MetricUnits( const TOOL_EVENT& aEvent )
  327. {
  328. m_frame->ChangeUserUnits( EDA_UNITS::MILLIMETRES );
  329. return 0;
  330. }
  331. int COMMON_TOOLS::ImperialUnits( const TOOL_EVENT& aEvent )
  332. {
  333. m_frame->ChangeUserUnits( EDA_UNITS::INCHES );
  334. return 0;
  335. }
  336. int COMMON_TOOLS::ToggleUnits( const TOOL_EVENT& aEvent )
  337. {
  338. m_frame->ChangeUserUnits( m_frame->GetUserUnits() == EDA_UNITS::INCHES ?
  339. EDA_UNITS::MILLIMETRES :
  340. EDA_UNITS::INCHES );
  341. return 0;
  342. }
  343. int COMMON_TOOLS::TogglePolarCoords( const TOOL_EVENT& aEvent )
  344. {
  345. m_frame->SetStatusText( wxEmptyString );
  346. m_frame->SetShowPolarCoords( !m_frame->GetShowPolarCoords() );
  347. m_frame->UpdateStatusBar();
  348. return 0;
  349. }
  350. int COMMON_TOOLS::ResetLocalCoords( const TOOL_EVENT& aEvent )
  351. {
  352. auto vcSettings = m_toolMgr->GetCurrentToolVC();
  353. // Use either the active tool forced cursor position or the general settings
  354. if( vcSettings.m_forceCursorPosition )
  355. m_frame->GetScreen()->m_LocalOrigin = vcSettings.m_forcedPosition;
  356. else
  357. m_frame->GetScreen()->m_LocalOrigin = getViewControls()->GetCursorPosition();
  358. m_frame->UpdateStatusBar();
  359. return 0;
  360. }
  361. int COMMON_TOOLS::ToggleCursor( const TOOL_EVENT& aEvent )
  362. {
  363. auto& galOpts = m_frame->GetGalDisplayOptions();
  364. galOpts.m_forceDisplayCursor = !galOpts.m_forceDisplayCursor;
  365. galOpts.NotifyChanged();
  366. return 0;
  367. }
  368. int COMMON_TOOLS::ToggleCursorStyle( const TOOL_EVENT& aEvent )
  369. {
  370. KIGFX::GAL_DISPLAY_OPTIONS& galOpts = m_frame->GetGalDisplayOptions();
  371. galOpts.m_fullscreenCursor = !galOpts.m_fullscreenCursor;
  372. galOpts.NotifyChanged();
  373. return 0;
  374. }
  375. int COMMON_TOOLS::SwitchCanvas( const TOOL_EVENT& aEvent )
  376. {
  377. if( aEvent.IsAction( &ACTIONS::acceleratedGraphics ) )
  378. m_frame->SwitchCanvas( EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL );
  379. else if( aEvent.IsAction( &ACTIONS::standardGraphics ) )
  380. m_frame->SwitchCanvas( EDA_DRAW_PANEL_GAL::GAL_TYPE_CAIRO );
  381. else
  382. wxFAIL_MSG( "Unknown canvas type" );
  383. return 0;
  384. }
  385. void COMMON_TOOLS::setTransitions()
  386. {
  387. Go( &COMMON_TOOLS::SelectionTool, ACTIONS::selectionTool.MakeEvent() );
  388. // Cursor control
  389. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorUp.MakeEvent() );
  390. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorDown.MakeEvent() );
  391. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorLeft.MakeEvent() );
  392. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorRight.MakeEvent() );
  393. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorUpFast.MakeEvent() );
  394. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorDownFast.MakeEvent() );
  395. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorLeftFast.MakeEvent() );
  396. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorRightFast.MakeEvent() );
  397. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorClick.MakeEvent() );
  398. Go( &COMMON_TOOLS::CursorControl, ACTIONS::cursorDblClick.MakeEvent() );
  399. Go( &COMMON_TOOLS::CursorControl, ACTIONS::showContextMenu.MakeEvent() );
  400. // Pan control
  401. Go( &COMMON_TOOLS::PanControl, ACTIONS::panUp.MakeEvent() );
  402. Go( &COMMON_TOOLS::PanControl, ACTIONS::panDown.MakeEvent() );
  403. Go( &COMMON_TOOLS::PanControl, ACTIONS::panLeft.MakeEvent() );
  404. Go( &COMMON_TOOLS::PanControl, ACTIONS::panRight.MakeEvent() );
  405. // Zoom control
  406. Go( &COMMON_TOOLS::ZoomRedraw, ACTIONS::zoomRedraw.MakeEvent() );
  407. Go( &COMMON_TOOLS::ZoomInOut, ACTIONS::zoomIn.MakeEvent() );
  408. Go( &COMMON_TOOLS::ZoomInOut, ACTIONS::zoomOut.MakeEvent() );
  409. Go( &COMMON_TOOLS::ZoomInOutCenter, ACTIONS::zoomInCenter.MakeEvent() );
  410. Go( &COMMON_TOOLS::ZoomInOutCenter, ACTIONS::zoomOutCenter.MakeEvent() );
  411. Go( &COMMON_TOOLS::ZoomCenter, ACTIONS::zoomCenter.MakeEvent() );
  412. Go( &COMMON_TOOLS::ZoomFitScreen, ACTIONS::zoomFitScreen.MakeEvent() );
  413. Go( &COMMON_TOOLS::ZoomPreset, ACTIONS::zoomPreset.MakeEvent() );
  414. Go( &COMMON_TOOLS::CenterContents, ACTIONS::centerContents.MakeEvent() );
  415. // Grid control
  416. Go( &COMMON_TOOLS::GridNext, ACTIONS::gridNext.MakeEvent() );
  417. Go( &COMMON_TOOLS::GridPrev, ACTIONS::gridPrev.MakeEvent() );
  418. Go( &COMMON_TOOLS::GridPreset, ACTIONS::gridPreset.MakeEvent() );
  419. Go( &COMMON_TOOLS::ToggleGrid, ACTIONS::toggleGrid.MakeEvent() );
  420. Go( &COMMON_TOOLS::GridProperties, ACTIONS::gridProperties.MakeEvent() );
  421. // Units and coordinates
  422. Go( &COMMON_TOOLS::ImperialUnits, ACTIONS::imperialUnits.MakeEvent() );
  423. Go( &COMMON_TOOLS::MetricUnits, ACTIONS::metricUnits.MakeEvent() );
  424. Go( &COMMON_TOOLS::ToggleUnits, ACTIONS::toggleUnits.MakeEvent() );
  425. Go( &COMMON_TOOLS::TogglePolarCoords, ACTIONS::togglePolarCoords.MakeEvent() );
  426. Go( &COMMON_TOOLS::ResetLocalCoords, ACTIONS::resetLocalCoords.MakeEvent() );
  427. // Misc
  428. Go( &COMMON_TOOLS::ToggleCursor, ACTIONS::toggleCursor.MakeEvent() );
  429. Go( &COMMON_TOOLS::ToggleCursorStyle, ACTIONS::toggleCursorStyle.MakeEvent() );
  430. Go( &COMMON_TOOLS::SwitchCanvas, ACTIONS::acceleratedGraphics.MakeEvent() );
  431. Go( &COMMON_TOOLS::SwitchCanvas, ACTIONS::standardGraphics.MakeEvent() );
  432. }