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.

241 lines
7.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Joshua Redstone redstone at gmail.com
  5. * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file class_pcb_group.h
  26. * @brief Class to handle a set of BOARD_ITEMs.
  27. */
  28. #ifndef CLASS_PCB_GROUP_H_
  29. #define CLASS_PCB_GROUP_H_
  30. #include <board_commit.h>
  31. #include <board_item.h>
  32. #include <unordered_set>
  33. namespace KIGFX
  34. {
  35. class VIEW;
  36. }
  37. /**
  38. * A set of BOARD_ITEMs (i.e., without duplicates).
  39. *
  40. * The group parent is always board, not logical parent group. The group is transparent
  41. * container - e.g., its position is derived from the position of its members. A selection
  42. * containing a group implicitly contains its members. However other operations on sets of
  43. * items, like committing, updating the view, etc the set is explicit.
  44. */
  45. class PCB_GROUP : public BOARD_ITEM
  46. {
  47. public:
  48. PCB_GROUP( BOARD_ITEM* aParent );
  49. static inline bool ClassOf( const EDA_ITEM* aItem )
  50. {
  51. return aItem && PCB_GROUP_T == aItem->Type();
  52. }
  53. wxString GetClass() const override
  54. {
  55. return wxT( "PCB_GROUP" );
  56. }
  57. wxString GetName() const { return m_name; }
  58. void SetName( const wxString& aName ) { m_name = aName; }
  59. std::unordered_set<BOARD_ITEM*>& GetItems()
  60. {
  61. return m_items;
  62. }
  63. const std::unordered_set<BOARD_ITEM*>& GetItems() const
  64. {
  65. return m_items;
  66. }
  67. /**
  68. * Add item to group. Does not take ownership of item.
  69. *
  70. * @return true if item was added (false if item belongs to a different group).
  71. */
  72. bool AddItem( BOARD_ITEM* aItem );
  73. /**
  74. * Remove item from group.
  75. *
  76. * @return true if item was removed (false if item was not in the group).
  77. */
  78. bool RemoveItem( BOARD_ITEM* aItem );
  79. void RemoveAll();
  80. /*
  81. * Search for highest level group inside of aScope, containing item.
  82. *
  83. * @param aScope restricts the search to groups within the group scope.
  84. * @param isFootprintEditor true if we should stop promoting at the footprint level
  85. * @return group inside of aScope, containing item, if exists, otherwise, nullptr
  86. */
  87. static PCB_GROUP* TopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor );
  88. static bool WithinScope( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor );
  89. #if defined( DEBUG )
  90. void Show( int nestLevel, std::ostream& os ) const override
  91. {
  92. ShowDummy( os );
  93. }
  94. #endif
  95. ///< @copydoc EDA_ITEM::GetPosition
  96. VECTOR2I GetPosition() const override;
  97. ///< @copydoc EDA_ITEM::SetPosition
  98. void SetPosition( const VECTOR2I& aNewpos ) override;
  99. ///< @copydoc BOARD_ITEM::GetLayerSet
  100. LSET GetLayerSet() const override;
  101. ///< @copydoc BOARD_ITEM::SetLayer
  102. void SetLayer( PCB_LAYER_ID aLayer ) override
  103. {
  104. wxFAIL_MSG( wxT( "groups don't support layer SetLayer" ) );
  105. }
  106. /** Set layer for all items within the group.
  107. *
  108. * To avoid freezes with circular references, the maximum depth is 20 by default.
  109. */
  110. void SetLayerRecursive( PCB_LAYER_ID aLayer, int aDepth );
  111. void SetLocked( bool aLocked ) override;
  112. ///< @copydoc EDA_ITEM::Clone
  113. EDA_ITEM* Clone() const override;
  114. /*
  115. * Clone() this and all descendants
  116. */
  117. PCB_GROUP* DeepClone() const;
  118. /*
  119. * Duplicate() this and all descendants
  120. */
  121. PCB_GROUP* DeepDuplicate() const;
  122. ///< @copydoc BOARD_ITEM::IsOnLayer
  123. bool IsOnLayer( PCB_LAYER_ID aLayer, bool aIncludeCourtyards = false ) const override;
  124. ///< @copydoc EDA_ITEM::HitTest
  125. bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
  126. ///< @copydoc EDA_ITEM::HitTest
  127. bool HitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const override;
  128. ///< @copydoc EDA_ITEM::GetBoundingBox
  129. const BOX2I GetBoundingBox() const override;
  130. // @copydoc BOARD_ITEM::GetEffectiveShape
  131. std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER,
  132. FLASHING aFlash = FLASHING::DEFAULT ) const override;
  133. ///< @copydoc EDA_ITEM::Visit
  134. INSPECT_RESULT Visit( INSPECTOR aInspector, void* aTestData,
  135. const std::vector<KICAD_T>& aScanTypes ) override;
  136. ///< @copydoc VIEW_ITEM::ViewGetLayers
  137. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  138. ///< @copydoc VIEW_ITEM::ViewGetLOD
  139. double ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const override;
  140. ///< @copydoc BOARD_ITEM::Move
  141. void Move( const VECTOR2I& aMoveVector ) override;
  142. ///< @copydoc BOARD_ITEM::Rotate
  143. void Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle ) override;
  144. ///< @copydoc BOARD_ITEM::Flip
  145. void Flip( const VECTOR2I& aCentre, bool aFlipLeftRight ) override;
  146. ///< @copydoc EDA_ITEM::GetItemDescription
  147. wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const override;
  148. ///< @copydoc EDA_ITEM::GetMenuImage
  149. BITMAPS GetMenuImage() const override;
  150. /**
  151. * Add all the immediate children of this group to the board commit. This function does not
  152. * enter any subgroups of this group, or add the group itself.
  153. *
  154. * @param aCommit is the commit to add the children to.
  155. */
  156. void AddChildrenToCommit( BOARD_COMMIT& aCommit )
  157. {
  158. RunOnChildren( [&]( BOARD_ITEM* bItem )
  159. {
  160. aCommit.Add( bItem );
  161. } );
  162. }
  163. ///< @copydoc EDA_ITEM::GetMsgPanelInfo
  164. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  165. /**
  166. * Invoke a function on all members of the group.
  167. *
  168. * @note This function should not add or remove items to the group.
  169. *
  170. * @param aFunction is the function to be invoked.
  171. */
  172. void RunOnChildren( const std::function<void ( BOARD_ITEM* )>& aFunction ) const;
  173. /**
  174. * Invoke a function on all descendants of the group.
  175. *
  176. * @note This function should not add or remove items to the group or descendant groups.
  177. * @param aFunction is the function to be invoked.
  178. */
  179. void RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const;
  180. /**
  181. * Check if the proposed type can be added to a group
  182. * @param aType KICAD_T type to check
  183. * @return true if the type can belong to a group, false otherwise
  184. */
  185. static bool IsGroupableType( KICAD_T aType );
  186. protected:
  187. ///< @copydoc BOARD_ITEM::swapData
  188. void swapData( BOARD_ITEM* aImage ) override;
  189. private:
  190. std::unordered_set<BOARD_ITEM*> m_items; // Members of the group
  191. wxString m_name; // Optional group name
  192. };
  193. #endif // CLASS_PCB_GROUP_H_