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.

222 lines
7.0 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-2023 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 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 <lset.h>
  33. #include <unordered_set>
  34. namespace KIGFX
  35. {
  36. class VIEW;
  37. }
  38. /**
  39. * A set of BOARD_ITEMs (i.e., without duplicates).
  40. *
  41. * The group parent is always board, not logical parent group. The group is transparent
  42. * container - e.g., its position is derived from the position of its members. A selection
  43. * containing a group implicitly contains its members. However other operations on sets of
  44. * items, like committing, updating the view, etc the set is explicit.
  45. */
  46. class PCB_GROUP : public BOARD_ITEM
  47. {
  48. public:
  49. PCB_GROUP( BOARD_ITEM* aParent );
  50. static inline bool ClassOf( const EDA_ITEM* aItem )
  51. {
  52. return aItem && PCB_GROUP_T == aItem->Type();
  53. }
  54. wxString GetClass() const override
  55. {
  56. return wxT( "PCB_GROUP" );
  57. }
  58. wxString GetName() const { return m_name; }
  59. void SetName( const wxString& aName ) { m_name = aName; }
  60. std::unordered_set<BOARD_ITEM*>& GetItems()
  61. {
  62. return m_items;
  63. }
  64. const std::unordered_set<BOARD_ITEM*>& GetItems() const
  65. {
  66. return m_items;
  67. }
  68. /**
  69. * Add item to group. Does not take ownership of item.
  70. *
  71. * @return true if item was added (false if item belongs to a different group).
  72. */
  73. virtual bool AddItem( BOARD_ITEM* aItem );
  74. /**
  75. * Remove item from group.
  76. *
  77. * @return true if item was removed (false if item was not in the group).
  78. */
  79. virtual bool RemoveItem( BOARD_ITEM* aItem );
  80. void RemoveAll();
  81. /*
  82. * Search for highest level group inside of aScope, containing item.
  83. *
  84. * @param aScope restricts the search to groups within the group scope.
  85. * @param isFootprintEditor true if we should stop promoting at the footprint level
  86. * @return group inside of aScope, containing item, if exists, otherwise, nullptr
  87. */
  88. static PCB_GROUP* TopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor );
  89. static bool WithinScope( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor );
  90. double Similarity( const BOARD_ITEM& aOther ) const override;
  91. bool operator==( const PCB_GROUP& aOther ) const;
  92. bool operator==( const BOARD_ITEM& aBoardItem ) const override;
  93. #if defined( DEBUG )
  94. void Show( int nestLevel, std::ostream& os ) const override
  95. {
  96. ShowDummy( os );
  97. }
  98. #endif
  99. /// @copydoc EDA_ITEM::GetPosition
  100. VECTOR2I GetPosition() const override;
  101. /// @copydoc EDA_ITEM::SetPosition
  102. void SetPosition( const VECTOR2I& aNewpos ) override;
  103. /// @copydoc BOARD_ITEM::GetLayerSet
  104. LSET GetLayerSet() const override;
  105. /// @copydoc BOARD_ITEM::SetLayer
  106. void SetLayer( PCB_LAYER_ID aLayer ) override
  107. {
  108. // NOP
  109. }
  110. bool IsOnCopperLayer() const override
  111. {
  112. // A group might have members on a copper layer, but isn't itself on any layer.
  113. return false;
  114. }
  115. void SetLocked( bool aLocked ) override;
  116. /// @copydoc EDA_ITEM::Clone
  117. EDA_ITEM* Clone() const override;
  118. /*
  119. * Clone() this and all descendants
  120. */
  121. PCB_GROUP* DeepClone() const;
  122. /*
  123. * Duplicate() this and all descendants
  124. */
  125. PCB_GROUP* DeepDuplicate() const;
  126. /// @copydoc BOARD_ITEM::IsOnLayer
  127. bool IsOnLayer( PCB_LAYER_ID aLayer ) const override;
  128. /// @copydoc EDA_ITEM::HitTest
  129. bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override;
  130. /// @copydoc EDA_ITEM::HitTest
  131. bool HitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const override;
  132. /// @copydoc EDA_ITEM::GetBoundingBox
  133. const BOX2I GetBoundingBox() const override;
  134. // @copydoc BOARD_ITEM::GetEffectiveShape
  135. std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER,
  136. FLASHING aFlash = FLASHING::DEFAULT ) const override;
  137. /// @copydoc EDA_ITEM::Visit
  138. INSPECT_RESULT Visit( INSPECTOR aInspector, void* aTestData,
  139. const std::vector<KICAD_T>& aScanTypes ) override;
  140. /// @copydoc VIEW_ITEM::ViewGetLayers
  141. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  142. /// @copydoc VIEW_ITEM::ViewGetLOD
  143. double ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const override;
  144. /// @copydoc BOARD_ITEM::Move
  145. void Move( const VECTOR2I& aMoveVector ) override;
  146. /// @copydoc BOARD_ITEM::Rotate
  147. void Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle ) override;
  148. /// @copydoc BOARD_ITEM::Flip
  149. void Flip( const VECTOR2I& aCentre, bool aFlipLeftRight ) override;
  150. /// @copydoc EDA_ITEM::GetItemDescription
  151. wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
  152. /// @copydoc EDA_ITEM::GetMenuImage
  153. BITMAPS GetMenuImage() const override;
  154. /// @copydoc EDA_ITEM::GetMsgPanelInfo
  155. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  156. ///< @copydoc BOARD_ITEM::RunOnChildren
  157. void RunOnChildren( const std::function<void ( BOARD_ITEM* )>& aFunction ) const override;
  158. ///< @copydoc BOARD_ITEM::RunOnDescendants
  159. void RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction,
  160. int aDepth = 0 ) const override;
  161. /**
  162. * Check if the proposed type can be added to a group
  163. * @param aType KICAD_T type to check
  164. * @return true if the type can belong to a group, false otherwise
  165. */
  166. static bool IsGroupableType( KICAD_T aType );
  167. protected:
  168. PCB_GROUP( BOARD_ITEM* aParent, KICAD_T idtype, PCB_LAYER_ID aLayer = F_Cu );
  169. /// @copydoc BOARD_ITEM::swapData
  170. void swapData( BOARD_ITEM* aImage ) override;
  171. std::unordered_set<BOARD_ITEM*> m_items; // Members of the group
  172. wxString m_name; // Optional group name
  173. };
  174. #endif // CLASS_PCB_GROUP_H_