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.

199 lines
6.1 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-2020 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. * PCB_GROUP is 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( wxString aName ) { m_name = aName; }
  59. const std::unordered_set<BOARD_ITEM*>& GetItems() const
  60. {
  61. return m_items;
  62. }
  63. /**
  64. * Adds item to group. Does not take ownership of item.
  65. *
  66. * @return true if item was added (false if item belongs to a different group).
  67. */
  68. bool AddItem( BOARD_ITEM* aItem );
  69. /**
  70. * Removes item from group.
  71. *
  72. * @return true if item was removed (false if item was not in the group).
  73. */
  74. bool RemoveItem( BOARD_ITEM* aItem );
  75. void RemoveAll();
  76. /*
  77. * Searches for highest level group containing item.
  78. *
  79. * @param scope restricts the search to groups within the group scope.
  80. * @return group containing item, if it exists, otherwise, NULL
  81. */
  82. static PCB_GROUP* TopLevelGroup( BOARD_ITEM* item, PCB_GROUP* scope );
  83. static bool WithinScope( BOARD_ITEM* item, PCB_GROUP* scope );
  84. #if defined( DEBUG )
  85. void Show( int nestLevel, std::ostream& os ) const override
  86. {
  87. ShowDummy( os );
  88. }
  89. #endif
  90. ///> @copydoc EDA_ITEM::GetPosition
  91. wxPoint GetPosition() const override;
  92. ///> @copydoc EDA_ITEM::SetPosition
  93. void SetPosition( const wxPoint& aNewpos ) override;
  94. ///> @copydoc BOARD_ITEM::GetLayerSet
  95. LSET GetLayerSet() const override;
  96. ///> @copydoc BOARD_ITEM::SetLayer
  97. void SetLayer( PCB_LAYER_ID aLayer ) override
  98. {
  99. wxFAIL_MSG( "groups don't support layer SetLayer" );
  100. }
  101. ///> @copydoc EDA_ITEM::Clone
  102. EDA_ITEM* Clone() const override;
  103. /*
  104. * Clone() this and all descendants
  105. */
  106. PCB_GROUP* DeepClone() const;
  107. /*
  108. * Duplicate() this and all descendants
  109. */
  110. PCB_GROUP* DeepDuplicate() const;
  111. ///> @copydoc BOARD_ITEM::SwapData
  112. void SwapData( BOARD_ITEM* aImage ) override;
  113. ///> @copydoc BOARD_ITEM::IsOnLayer
  114. bool IsOnLayer( PCB_LAYER_ID aLayer ) const override;
  115. ///> @copydoc EDA_ITEM::HitTest
  116. bool HitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const override;
  117. ///> @copydoc EDA_ITEM::HitTest
  118. bool HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const override;
  119. ///> @copydoc EDA_ITEM::GetBoundingBox
  120. const EDA_RECT GetBoundingBox() const override;
  121. ///> @copydoc EDA_ITEM::Visit
  122. SEARCH_RESULT Visit( INSPECTOR aInspector, void* aTestData,
  123. const KICAD_T aScanTypes[] ) override;
  124. ///> @copydoc VIEW_ITEM::ViewGetLayers
  125. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  126. ///> @copydoc VIEW_ITEM::ViewGetLOD
  127. double ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const override;
  128. ///> @copydoc BOARD_ITEM::Move
  129. void Move( const wxPoint& aMoveVector ) override;
  130. ///> @copydoc BOARD_ITEM::Rotate
  131. void Rotate( const wxPoint& aRotCentre, double aAngle ) override;
  132. ///> @copydoc BOARD_ITEM::Flip
  133. void Flip( const wxPoint& aCentre, bool aFlipLeftRight ) override;
  134. ///> @copydoc EDA_ITEM::GetSelectMenuText
  135. wxString GetSelectMenuText( EDA_UNITS aUnits ) const override;
  136. ///> @copydoc EDA_ITEM::GetMenuImage
  137. BITMAP_DEF GetMenuImage() const override;
  138. ///> @copydoc EDA_ITEM::GetMsgPanelInfo
  139. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  140. /**
  141. * Invokes a function on all members of the group.
  142. *
  143. * @note This function should not add or remove items to the group.
  144. *
  145. * @param aFunction is the function to be invoked.
  146. */
  147. void RunOnChildren( const std::function<void ( BOARD_ITEM* )>& aFunction ) const;
  148. /**
  149. * Invokes a function on all descendants of the group.
  150. *
  151. * @note This function should not add or remove items to the group or descendant groups.
  152. * @param aFunction is the function to be invoked.
  153. */
  154. void RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const;
  155. private:
  156. std::unordered_set<BOARD_ITEM*> m_items; // Members of the group
  157. wxString m_name; // Optional group name
  158. };
  159. #endif // CLASS_PCB_GROUP_H_