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.

173 lines
4.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <class_draw_panel_gal.h>
  20. #include <eda_draw_frame.h>
  21. #include <preview_items/selection_area.h>
  22. #include <tool/actions.h>
  23. #include <tool/tool_manager.h>
  24. #include <tool/zoom_tool.h>
  25. #include <view/view.h>
  26. #include <math/vector2wx.h>
  27. ZOOM_TOOL::ZOOM_TOOL() :
  28. TOOL_INTERACTIVE( "common.Control.zoomTool" )
  29. {
  30. m_frame = nullptr;
  31. }
  32. ZOOM_TOOL::~ZOOM_TOOL() {}
  33. bool ZOOM_TOOL::Init()
  34. {
  35. auto& ctxMenu = m_menu.GetMenu();
  36. // cancel current tool goes in main context menu at the top if present
  37. ctxMenu.AddItem( ACTIONS::cancelInteractive, SELECTION_CONDITIONS::ShowAlways, 1 );
  38. ctxMenu.AddSeparator( 1 );
  39. // Finally, add the standard zoom/grid items
  40. getEditFrame<EDA_DRAW_FRAME>()->AddStandardSubMenus( m_menu );
  41. return true;
  42. }
  43. void ZOOM_TOOL::Reset( RESET_REASON aReason )
  44. {
  45. m_frame = getEditFrame<EDA_DRAW_FRAME>();
  46. }
  47. int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
  48. {
  49. m_frame->PushTool( aEvent );
  50. auto setCursor =
  51. [&]()
  52. {
  53. m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ZOOM_IN );
  54. };
  55. // Set initial cursor
  56. setCursor();
  57. while( TOOL_EVENT* evt = Wait() )
  58. {
  59. setCursor();
  60. if( evt->IsCancelInteractive() || evt->IsActivate() )
  61. {
  62. break;
  63. }
  64. else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
  65. {
  66. if( selectRegion() )
  67. break;
  68. }
  69. else if( evt->IsClick( BUT_RIGHT ) )
  70. {
  71. SELECTION dummy;
  72. m_menu.ShowContextMenu( dummy );
  73. }
  74. else
  75. {
  76. evt->SetPassEvent();
  77. }
  78. }
  79. // Exit zoom tool
  80. m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
  81. m_frame->PopTool( aEvent );
  82. return 0;
  83. }
  84. bool ZOOM_TOOL::selectRegion()
  85. {
  86. bool cancelled = false;
  87. KIGFX::VIEW* view = getView();
  88. EDA_DRAW_PANEL_GAL* canvas = m_frame->GetCanvas();
  89. getViewControls()->SetAutoPan( true );
  90. KIGFX::PREVIEW::SELECTION_AREA area;
  91. view->Add( &area );
  92. while( TOOL_EVENT* evt = Wait() )
  93. {
  94. if( evt->IsCancelInteractive() || evt->IsActivate() )
  95. {
  96. cancelled = true;
  97. break;
  98. }
  99. if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
  100. {
  101. area.SetOrigin( evt->DragOrigin() );
  102. area.SetEnd( evt->Position() );
  103. view->SetVisible( &area, true );
  104. view->Update( &area, KIGFX::GEOMETRY );
  105. }
  106. if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
  107. {
  108. view->SetVisible( &area, false );
  109. auto selectionBox = area.ViewBBox();
  110. if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
  111. {
  112. break;
  113. }
  114. else
  115. {
  116. VECTOR2D sSize = view->ToWorld( ToVECTOR2I( canvas->GetClientSize() ), false );
  117. VECTOR2D vSize = selectionBox.GetSize();
  118. double scale;
  119. double ratio = std::max( fabs( vSize.x / sSize.x ), fabs( vSize.y / sSize.y ) );
  120. if( evt->IsMouseUp( BUT_LEFT ) )
  121. scale = view->GetScale() / ratio;
  122. else
  123. scale = view->GetScale() * ratio;
  124. view->SetScale( scale );
  125. view->SetCenter( selectionBox.Centre() );
  126. break;
  127. }
  128. }
  129. }
  130. view->SetVisible( &area, false );
  131. view->Remove( &area );
  132. getViewControls()->SetAutoPan( false );
  133. return cancelled;
  134. }
  135. void ZOOM_TOOL::setTransitions()
  136. {
  137. Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
  138. }