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.

188 lines
5.0 KiB

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) 2019 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <tool/tool_manager.h>
  24. #include <tool/selection_conditions.h>
  25. #include <tools/pl_picker_tool.h>
  26. #include <tools/pl_actions.h>
  27. #include <view/view_controls.h>
  28. #include <pl_editor_frame.h>
  29. PL_PICKER_TOOL::PL_PICKER_TOOL() :
  30. TOOL_INTERACTIVE( "plEditor.InteractivePicker" ),
  31. m_frame( nullptr )
  32. {
  33. resetPicker();
  34. }
  35. bool PL_PICKER_TOOL::Init()
  36. {
  37. m_frame = getEditFrame<PL_EDITOR_FRAME>();
  38. auto& ctxMenu = m_menu.GetMenu();
  39. // cancel current tool goes in main context menu at the top if present
  40. ctxMenu.AddItem( ACTIONS::cancelInteractive, SELECTION_CONDITIONS::ShowAlways, 1 );
  41. ctxMenu.AddSeparator( 1 );
  42. // Finally, add the standard zoom/grid items
  43. m_frame->AddStandardSubMenus( m_menu );
  44. return true;
  45. }
  46. void PL_PICKER_TOOL::Reset( RESET_REASON aReason )
  47. {
  48. }
  49. int PL_PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
  50. {
  51. KIGFX::VIEW_CONTROLS* controls = getViewControls();
  52. int finalize_state = WAIT_CANCEL;
  53. setControls();
  54. while( TOOL_EVENT* evt = Wait() )
  55. {
  56. m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_BULLSEYE );
  57. VECTOR2I cursorPos = controls->GetCursorPosition( !evt->Modifier( MD_ALT ) );
  58. if( evt->IsClick( BUT_LEFT ) )
  59. {
  60. bool getNext = false;
  61. m_picked = cursorPos;
  62. if( m_clickHandler )
  63. {
  64. try
  65. {
  66. getNext = (*m_clickHandler)( *m_picked );
  67. }
  68. catch( std::exception& e )
  69. {
  70. std::cerr << "PL_PICKER_TOOL click handler error: " << e.what() << std::endl;
  71. finalize_state = EXCEPTION_CANCEL;
  72. break;
  73. }
  74. }
  75. if( !getNext )
  76. {
  77. finalize_state = CLICK_CANCEL;
  78. break;
  79. }
  80. else
  81. setControls();
  82. }
  83. else if( evt->IsMotion() )
  84. {
  85. if( m_motionHandler )
  86. {
  87. try
  88. {
  89. (*m_motionHandler)( cursorPos );
  90. }
  91. catch( std::exception& e )
  92. {
  93. std::cerr << "PL_PICKER_TOOL motion handler error: " << e.what() << std::endl;
  94. }
  95. }
  96. }
  97. else if( evt->IsCancelInteractive() || evt->IsActivate() )
  98. {
  99. if( m_cancelHandler )
  100. {
  101. try
  102. {
  103. (*m_cancelHandler)();
  104. }
  105. catch( std::exception& e )
  106. {
  107. std::cerr << "PL_PICKER_TOOL cancel handler error: " << e.what() << std::endl;
  108. }
  109. }
  110. // Activating a new tool may have alternate finalization from canceling the current tool
  111. if( evt->IsActivate() )
  112. finalize_state = END_ACTIVATE;
  113. else
  114. finalize_state = EVT_CANCEL;
  115. break;
  116. }
  117. else if( evt->IsClick( BUT_RIGHT ) )
  118. {
  119. m_menu.ShowContextMenu();
  120. }
  121. else
  122. evt->SetPassEvent();
  123. }
  124. if( m_finalizeHandler )
  125. {
  126. try
  127. {
  128. (*m_finalizeHandler)( finalize_state );
  129. }
  130. catch( std::exception& e )
  131. {
  132. std::cerr << "PL_PICKER_TOOL finalize handler error: " << e.what() << std::endl;
  133. }
  134. }
  135. resetPicker();
  136. controls->ForceCursorPosition( false );
  137. return 0;
  138. }
  139. void PL_PICKER_TOOL::setTransitions()
  140. {
  141. Go( &PL_PICKER_TOOL::Main, PL_ACTIONS::pickerTool.MakeEvent() );
  142. }
  143. void PL_PICKER_TOOL::resetPicker()
  144. {
  145. m_picked = NULLOPT;
  146. m_motionHandler = NULLOPT;
  147. m_clickHandler = NULLOPT;
  148. m_cancelHandler = NULLOPT;
  149. m_finalizeHandler = NULLOPT;
  150. }
  151. void PL_PICKER_TOOL::setControls()
  152. {
  153. KIGFX::VIEW_CONTROLS* controls = getViewControls();
  154. controls->CaptureCursor( false );
  155. controls->SetAutoPan( false );
  156. }