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.

191 lines
5.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 CERN
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Tomasz Wlostowski <tomasz.wlostowski@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. #ifndef PCB_TOOL_BASE_H
  26. #define PCB_TOOL_BASE_H
  27. #include <string>
  28. #include <tool/tool_interactive.h>
  29. #include <pcb_edit_frame.h>
  30. #include <board.h>
  31. #include <view/view_group.h>
  32. #include <pcb_view.h>
  33. #include <pcb_draw_panel_gal.h>
  34. #include <pcbnew_settings.h>
  35. #include <functional>
  36. #include <tool/tool_menu.h>
  37. /**
  38. * PCB_TOOL_BASE
  39. *
  40. * A tool operating on a BOARD object
  41. **/
  42. class PCB_TOOL_BASE;
  43. class PCB_EDIT_FRAME;
  44. class PCB_DISPLAY_OPTIONS;
  45. class PCB_SELECTION;
  46. struct INTERACTIVE_PLACER_BASE
  47. {
  48. virtual ~INTERACTIVE_PLACER_BASE()
  49. {
  50. }
  51. virtual std::unique_ptr<BOARD_ITEM> CreateItem() = 0;
  52. virtual void SnapItem( BOARD_ITEM *aItem );
  53. virtual bool PlaceItem( BOARD_ITEM *aItem, BOARD_COMMIT& aCommit );
  54. PCB_BASE_EDIT_FRAME* m_frame;
  55. BOARD* m_board;
  56. int m_modifiers;
  57. };
  58. class PCB_TOOL_BASE : public TOOL_INTERACTIVE
  59. {
  60. public:
  61. /**
  62. * Constructor
  63. *
  64. * Creates a tool with given id & name. The name must be unique. */
  65. PCB_TOOL_BASE( TOOL_ID aId, const std::string& aName ) :
  66. TOOL_INTERACTIVE ( aId, aName ),
  67. m_isFootprintEditor( false ),
  68. m_isBoardEditor( false )
  69. {};
  70. /**
  71. * Constructor
  72. *
  73. * Creates a tool with given name. The name must be unique. */
  74. PCB_TOOL_BASE( const std::string& aName ) :
  75. TOOL_INTERACTIVE ( aName ),
  76. m_isFootprintEditor( false ),
  77. m_isBoardEditor( false )
  78. {};
  79. virtual ~PCB_TOOL_BASE() {};
  80. virtual bool Init() override;
  81. virtual void Reset( RESET_REASON aReason ) override;
  82. /**
  83. * Function SetIsFootprintEditor()
  84. *
  85. * Toggles edit footprint mode. When enabled, one may select parts of footprints individually
  86. * (graphics, pads, etc.), so they can be modified.
  87. * @param aEnabled decides if the mode should be enabled.
  88. */
  89. void SetIsFootprintEditor( bool aEnabled ) { m_isFootprintEditor = aEnabled; }
  90. bool IsFootprintEditor() const { return m_isFootprintEditor; }
  91. void SetIsBoardEditor( bool aEnabled ) { m_isBoardEditor = aEnabled; }
  92. bool IsBoardEditor() const { return m_isBoardEditor; }
  93. /**
  94. * Should the tool use its 45° mode option?
  95. * @return True if set to use 45°
  96. */
  97. virtual bool Is45Limited() const;
  98. protected:
  99. /**
  100. * Options for placing items interactively.
  101. */
  102. enum INTERACTIVE_PLACEMENT_OPTIONS {
  103. /// Handle the rotate action in the loop by calling the item's rotate method
  104. IPO_ROTATE = 0x01,
  105. /// Handle flip action in the loop by calling the item's flip method
  106. IPO_FLIP = 0x02,
  107. /// Create an item immediately on placement starting, otherwise show the pencil cursor
  108. /// until the item is created
  109. IPO_SINGLE_CLICK = 0x04,
  110. /// Allow repeat placement of the item
  111. IPO_REPEAT = 0x08
  112. };
  113. /**
  114. * Helper function for performing a common interactive idiom:
  115. * wait for a left click, place an item there (perhaps with a
  116. * dialog or other user interaction), then have it move with
  117. * the mouse and respond to rotate/flip, etc
  118. *
  119. * More complex interactive processes are not supported here, you
  120. * should implement a customised event loop for those.
  121. *
  122. * @param aItemCreator the callable that will attempt to create the item
  123. * @param aCommitMessage the message used on a successful commit
  124. */
  125. void doInteractiveItemPlacement( const TOOL_EVENT& aTool, INTERACTIVE_PLACER_BASE* aPlacer,
  126. const wxString& aCommitMessage,
  127. int aOptions = IPO_ROTATE | IPO_FLIP | IPO_REPEAT );
  128. virtual void setTransitions() override;
  129. KIGFX::PCB_VIEW* view() const
  130. {
  131. return static_cast<KIGFX::PCB_VIEW*>( getView() );
  132. }
  133. KIGFX::VIEW_CONTROLS* controls() const
  134. {
  135. return getViewControls();
  136. }
  137. template<class T = PCB_BASE_EDIT_FRAME>
  138. T* frame() const
  139. {
  140. return getEditFrame<T>();
  141. }
  142. BOARD* board() const { return getModel<BOARD>(); }
  143. FOOTPRINT* footprint() const
  144. {
  145. return board()->GetFirstFootprint();
  146. }
  147. PCBNEW_SETTINGS::DISPLAY_OPTIONS& displayOptions() const;
  148. PCB_DRAW_PANEL_GAL* canvas() const;
  149. const PCB_SELECTION& selection() const;
  150. PCB_SELECTION& selection();
  151. protected:
  152. bool m_isFootprintEditor;
  153. bool m_isBoardEditor;
  154. };
  155. #endif