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.

147 lines
4.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 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 <id.h>
  22. #include <preview_items/selection_area.h>
  23. #include <tool/actions.h>
  24. #include <tool/tool_manager.h>
  25. #include <tool/zoom_tool.h>
  26. #include <view/view.h>
  27. #include <view/view_controls.h>
  28. ZOOM_TOOL::ZOOM_TOOL() :
  29. TOOL_INTERACTIVE( "common.Control.zoomTool" )
  30. {
  31. m_frame = NULL;
  32. }
  33. ZOOM_TOOL::~ZOOM_TOOL() {}
  34. void ZOOM_TOOL::Reset( RESET_REASON aReason )
  35. {
  36. m_frame = getEditFrame<EDA_DRAW_FRAME>();
  37. }
  38. int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
  39. {
  40. // This method is called both when the zoom tool is activated (on) or deactivated (off)
  41. wxMenuBar* menu = m_frame->GetMenuBar();
  42. bool zoom_tool_is_on = m_frame->GetToolToggled( ID_ZOOM_SELECTION ) ||
  43. ( menu && menu->IsChecked( ID_ZOOM_SELECTION ) );
  44. if( !zoom_tool_is_on ) // This is a tool deselection: do nothing
  45. return 0;
  46. m_frame->SetToolID( ID_ZOOM_SELECTION, wxCURSOR_MAGNIFIER, _( "Zoom to selection" ) );
  47. while( auto evt = Wait() )
  48. {
  49. if( evt->IsCancel() || evt->IsActivate() )
  50. break;
  51. else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
  52. {
  53. if( selectRegion() )
  54. break;
  55. }
  56. else
  57. m_toolMgr->PassEvent();
  58. }
  59. // Exit zoom tool
  60. m_frame->SetNoToolSelected();
  61. return 0;
  62. }
  63. bool ZOOM_TOOL::selectRegion()
  64. {
  65. bool cancelled = false;
  66. auto view = getView();
  67. auto canvas = m_frame->GetGalCanvas();
  68. getViewControls()->SetAutoPan( true );
  69. KIGFX::PREVIEW::SELECTION_AREA area;
  70. view->Add( &area );
  71. while( auto evt = Wait() )
  72. {
  73. if( evt->IsCancel() || evt->IsActivate() )
  74. {
  75. cancelled = true;
  76. break;
  77. }
  78. if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
  79. {
  80. area.SetOrigin( evt->DragOrigin() );
  81. area.SetEnd( evt->Position() );
  82. view->SetVisible( &area, true );
  83. view->Update( &area, KIGFX::GEOMETRY );
  84. }
  85. if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
  86. {
  87. view->SetVisible( &area, false );
  88. auto selectionBox = area.ViewBBox();
  89. if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
  90. {
  91. break;
  92. }
  93. else
  94. {
  95. VECTOR2D sSize = view->ToWorld( canvas->GetClientSize(), false );
  96. VECTOR2D vSize = selectionBox.GetSize();
  97. double scale;
  98. double ratio = std::max( fabs( vSize.x / sSize.x ), fabs( vSize.y / sSize.y ) );
  99. if( evt->IsMouseUp( BUT_LEFT ) )
  100. scale = view->GetScale() / ratio;
  101. else
  102. scale = view->GetScale() * ratio;
  103. view->SetScale( scale );
  104. view->SetCenter( selectionBox.Centre() );
  105. break;
  106. }
  107. }
  108. }
  109. view->SetVisible( &area, false );
  110. view->Remove( &area );
  111. getViewControls()->SetAutoPan( false );
  112. return cancelled;
  113. }
  114. void ZOOM_TOOL::setTransitions()
  115. {
  116. Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
  117. }