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.

228 lines
6.5 KiB

12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  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. #ifndef __SELECTION_TOOL_H
  26. #define __SELECTION_TOOL_H
  27. #include <math/vector2d.h>
  28. #include <tool/tool_interactive.h>
  29. #include <tool/context_menu.h>
  30. #include <class_undoredo_container.h>
  31. class SELECTION_AREA;
  32. class BOARD_ITEM;
  33. class GENERAL_COLLECTOR;
  34. namespace KIGFX
  35. {
  36. class VIEW_GROUP;
  37. }
  38. /**
  39. * Class SELECTION_TOOL
  40. *
  41. * Our sample selection tool: currently supports:
  42. * - pick single objects (click LMB)
  43. * - add objects to existing selection (Shift+LMB)
  44. * - draw selection box (drag LMB)
  45. * - handles MODULEs properly (ie. selects either MODULE or its PADs, TEXTs, etc.)
  46. * - takes into account high-contrast & layer visibility settings
  47. * - invokes InteractiveEdit tool when user starts to drag selected items
  48. */
  49. class SELECTION_TOOL : public TOOL_INTERACTIVE
  50. {
  51. public:
  52. SELECTION_TOOL();
  53. ~SELECTION_TOOL();
  54. struct SELECTION
  55. {
  56. /// Set of selected items
  57. PICKED_ITEMS_LIST items;
  58. /// VIEW_GROUP that holds currently selected items
  59. KIGFX::VIEW_GROUP* group;
  60. /// Checks if there is anything selected
  61. bool Empty() const
  62. {
  63. return ( items.GetCount() == 0 );
  64. }
  65. /// Returns the number of selected parts
  66. int Size() const
  67. {
  68. return items.GetCount();
  69. }
  70. private:
  71. /// Clears both the VIEW_GROUP and set of selected items. Please note that it does not
  72. /// change properties of selected items (e.g. selection flag).
  73. void clear();
  74. friend class SELECTION_TOOL;
  75. };
  76. /// @copydoc TOOL_INTERACTIVE::Reset()
  77. void Reset( RESET_REASON aReason );
  78. /**
  79. * Function Main()
  80. *
  81. * The main loop.
  82. */
  83. int Main( TOOL_EVENT& aEvent );
  84. /**
  85. * Function GetSelection()
  86. *
  87. * Returns the set of currently selected items.
  88. */
  89. const SELECTION& GetSelection() const
  90. {
  91. return m_selection;
  92. }
  93. /**
  94. * Function ClearSelection()
  95. * Clears the current selection.
  96. */
  97. void ClearSelection();
  98. /**
  99. * Function AddMenuItem()
  100. *
  101. * Adds a menu entry to run a TOOL_ACTION on selected items.
  102. * @param aAction is a menu entry to be added.
  103. */
  104. void AddMenuItem( const TOOL_ACTION& aAction );
  105. private:
  106. /**
  107. * Function selectSingle()
  108. * Selects an item pointed by the parameter aWhere. If there is more than one item at that
  109. * place, there is a menu displayed that allows to choose the item.
  110. *
  111. * @param aWhere is the place where the item should be selected.
  112. */
  113. void selectSingle( const VECTOR2I& aWhere );
  114. /**
  115. * Function selectMultiple()
  116. * Handles drawing a selection box that allows to select many items at the same time.
  117. *
  118. * @return true if the function was cancelled (ie. CancelEvent was received).
  119. */
  120. bool selectMultiple();
  121. /**
  122. * Function disambiguationMenu()
  123. * Handles the menu that allows to select one of many items in case there is more than one
  124. * item at the selected point (@see selectSingle()).
  125. *
  126. * @param aItems contains list of items that are displayed to the user.
  127. */
  128. BOARD_ITEM* disambiguationMenu( GENERAL_COLLECTOR* aItems );
  129. /**
  130. * Function pickSmallestComponent()
  131. * Allows to find the smallest (in terms of bounding box area) item from the list.
  132. *
  133. * @param aCollector containes the list of items.
  134. */
  135. BOARD_ITEM* pickSmallestComponent( GENERAL_COLLECTOR* aCollector );
  136. /**
  137. * Function toggleSelection()
  138. * Changes selection status of a given item.
  139. *
  140. * @param aItem is the item to have selection status changed.
  141. */
  142. void toggleSelection( BOARD_ITEM* aItem );
  143. /**
  144. * Function selectable()
  145. * Checks conditions for an item to be selected.
  146. *
  147. * @return True if the item fulfills conditions to be selected.
  148. */
  149. bool selectable( const BOARD_ITEM* aItem ) const;
  150. /**
  151. * Function selectItem()
  152. * Takes necessary action mark an item as selected.
  153. *
  154. * @param aItem is an item to be selected.
  155. */
  156. void select( BOARD_ITEM* aItem );
  157. /**
  158. * Function deselectItem()
  159. * Takes necessary action mark an item as deselected.
  160. *
  161. * @param aItem is an item to be deselected.
  162. */
  163. void deselect( BOARD_ITEM* aItem );
  164. /**
  165. * Function deselectVisually()
  166. * Marks item as selected, but does not add it to the ITEMS_PICKED_LIST.
  167. * @param aItem is an item to be be marked.
  168. */
  169. void selectVisually( BOARD_ITEM* aItem ) const;
  170. /**
  171. * Function deselectVisually()
  172. * Marks item as selected, but does not add it to the ITEMS_PICKED_LIST.
  173. * @param aItem is an item to be be marked.
  174. */
  175. void deselectVisually( BOARD_ITEM* aItem ) const;
  176. /**
  177. * Function containsSelected()
  178. * Checks if the given point is placed within any of selected items' bounding box.
  179. *
  180. * @return True if the given point is contained in any of selected items' bouding box.
  181. */
  182. bool containsSelected( const VECTOR2I& aPoint ) const;
  183. /// Visual representation of selection box
  184. SELECTION_AREA* m_selArea;
  185. /// Current state of selection
  186. SELECTION m_selection;
  187. /// Flag saying if items should be added to the current selection or rather replace it
  188. bool m_additive;
  189. /// Flag saying if multiple selection mode is active
  190. bool m_multiple;
  191. /// Right click popup menu
  192. CONTEXT_MENU m_menu;
  193. };
  194. #endif