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.

340 lines
8.3 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-2021 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. #include <bitmaps.h>
  25. #include <board.h>
  26. #include <board_item.h>
  27. #include <pcb_group.h>
  28. #include <confirm.h>
  29. #include <widgets/msgpanel.h>
  30. #include <view/view.h>
  31. PCB_GROUP::PCB_GROUP( BOARD_ITEM* aParent ) :
  32. BOARD_ITEM( aParent, PCB_GROUP_T )
  33. {
  34. }
  35. bool PCB_GROUP::AddItem( BOARD_ITEM* aItem )
  36. {
  37. // Items can only be in one group at a time
  38. if( aItem->GetParentGroup() )
  39. aItem->GetParentGroup()->RemoveItem( aItem );
  40. m_items.insert( aItem );
  41. aItem->SetParentGroup( this );
  42. return true;
  43. }
  44. bool PCB_GROUP::RemoveItem( BOARD_ITEM* aItem )
  45. {
  46. // Only clear the item's group field if it was inside this group
  47. if( m_items.erase( aItem ) == 1 )
  48. {
  49. aItem->SetParentGroup( nullptr );
  50. return true;
  51. }
  52. return false;
  53. }
  54. void PCB_GROUP::RemoveAll()
  55. {
  56. for( BOARD_ITEM* item : m_items )
  57. item->SetParentGroup( nullptr );
  58. m_items.clear();
  59. }
  60. PCB_GROUP* PCB_GROUP::TopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool aFootprintEditor )
  61. {
  62. PCB_GROUP* candidate = aItem->GetParentGroup();
  63. // Don't get the footprint's group if we are in the footprint editor
  64. if( !candidate && aItem->GetParent() && aItem->GetParent()->Type() == PCB_FOOTPRINT_T
  65. && !aFootprintEditor )
  66. candidate = aItem->GetParent()->GetParentGroup();
  67. while( candidate && candidate->GetParentGroup() && candidate->GetParentGroup() != aScope )
  68. {
  69. if( candidate->GetParent()->Type() == PCB_FOOTPRINT_T && aFootprintEditor )
  70. break;
  71. candidate = candidate->GetParentGroup();
  72. }
  73. return candidate == aScope ? nullptr : candidate;
  74. }
  75. bool PCB_GROUP::WithinScope( BOARD_ITEM* item, PCB_GROUP* scope )
  76. {
  77. for( PCB_GROUP* parent = item->GetParentGroup(); parent; parent = parent->GetParentGroup() )
  78. {
  79. if( parent == scope )
  80. return true;
  81. }
  82. if( item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
  83. item = item->GetParent();
  84. for( PCB_GROUP* parent = item->GetParentGroup(); parent; parent = parent->GetParentGroup() )
  85. {
  86. if( parent == scope )
  87. return true;
  88. }
  89. return false;
  90. }
  91. wxPoint PCB_GROUP::GetPosition() const
  92. {
  93. return GetBoundingBox().Centre();
  94. }
  95. void PCB_GROUP::SetPosition( const wxPoint& aNewpos )
  96. {
  97. wxPoint delta = aNewpos - GetPosition();
  98. Move( delta );
  99. }
  100. EDA_ITEM* PCB_GROUP::Clone() const
  101. {
  102. // Use copy constructor to get the same uuid and other fields
  103. PCB_GROUP* newGroup = new PCB_GROUP( *this );
  104. return newGroup;
  105. }
  106. PCB_GROUP* PCB_GROUP::DeepClone() const
  107. {
  108. // Use copy constructor to get the same uuid and other fields
  109. PCB_GROUP* newGroup = new PCB_GROUP( *this );
  110. newGroup->m_items.clear();
  111. for( BOARD_ITEM* member : m_items )
  112. {
  113. if( member->Type() == PCB_GROUP_T )
  114. newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
  115. else
  116. newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
  117. }
  118. return newGroup;
  119. }
  120. PCB_GROUP* PCB_GROUP::DeepDuplicate() const
  121. {
  122. PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( Duplicate() );
  123. newGroup->m_items.clear();
  124. for( BOARD_ITEM* member : m_items )
  125. {
  126. if( member->Type() == PCB_GROUP_T )
  127. newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
  128. else
  129. newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
  130. }
  131. return newGroup;
  132. }
  133. void PCB_GROUP::SwapData( BOARD_ITEM* aImage )
  134. {
  135. assert( aImage->Type() == PCB_GROUP_T );
  136. std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
  137. }
  138. bool PCB_GROUP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  139. {
  140. // Groups are selected by promoting a selection of one of their children
  141. return false;
  142. }
  143. bool PCB_GROUP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  144. {
  145. // Groups are selected by promoting a selection of one of their children
  146. return false;
  147. }
  148. const EDA_RECT PCB_GROUP::GetBoundingBox() const
  149. {
  150. EDA_RECT area;
  151. for( BOARD_ITEM* item : m_items )
  152. area.Merge( item->GetBoundingBox() );
  153. area.Inflate( Millimeter2iu( 0.25 ) ); // Give a min size to the area
  154. return area;
  155. }
  156. SEARCH_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData, const KICAD_T aScanTypes[] )
  157. {
  158. for( const KICAD_T* stype = aScanTypes; *stype != EOT; ++stype )
  159. {
  160. // If caller wants to inspect my type
  161. if( *stype == Type() )
  162. {
  163. if( SEARCH_RESULT::QUIT == aInspector( this, aTestData ) )
  164. return SEARCH_RESULT::QUIT;
  165. }
  166. }
  167. return SEARCH_RESULT::CONTINUE;
  168. }
  169. LSET PCB_GROUP::GetLayerSet() const
  170. {
  171. LSET aSet;
  172. for( BOARD_ITEM* item : m_items )
  173. aSet |= item->GetLayerSet();
  174. return aSet;
  175. }
  176. bool PCB_GROUP::IsOnLayer( PCB_LAYER_ID aLayer ) const
  177. {
  178. // A group is on a layer if any item is on the layer
  179. for( BOARD_ITEM* item : m_items )
  180. {
  181. if( item->IsOnLayer( aLayer ) )
  182. return true;
  183. }
  184. return false;
  185. }
  186. void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
  187. {
  188. aCount = 1;
  189. aLayers[0] = LAYER_ANCHOR;
  190. }
  191. double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  192. {
  193. if( aView->IsLayerVisible( LAYER_ANCHOR ) )
  194. return 0.0;
  195. return std::numeric_limits<double>::max();
  196. }
  197. void PCB_GROUP::Move( const wxPoint& aMoveVector )
  198. {
  199. for( BOARD_ITEM* member : m_items )
  200. member->Move( aMoveVector );
  201. }
  202. void PCB_GROUP::Rotate( const wxPoint& aRotCentre, double aAngle )
  203. {
  204. for( BOARD_ITEM* item : m_items )
  205. item->Rotate( aRotCentre, aAngle );
  206. }
  207. void PCB_GROUP::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
  208. {
  209. for( BOARD_ITEM* item : m_items )
  210. item->Flip( aCentre, aFlipLeftRight );
  211. }
  212. wxString PCB_GROUP::GetSelectMenuText( EDA_UNITS aUnits ) const
  213. {
  214. if( m_name.empty() )
  215. {
  216. return wxString::Format( _( "Unnamed Group, %zu members" ),
  217. m_items.size() );
  218. }
  219. return wxString::Format( _( "Group \"%s\", %zu members" ),
  220. m_name,
  221. m_items.size() );
  222. }
  223. BITMAP_DEF PCB_GROUP::GetMenuImage() const
  224. {
  225. return module_xpm;
  226. }
  227. void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  228. {
  229. aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
  230. aList.emplace_back( _( "Members" ), wxString::Format( "%zu", m_items.size() ) );
  231. }
  232. void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
  233. {
  234. try
  235. {
  236. for( BOARD_ITEM* item : m_items )
  237. aFunction( item );
  238. }
  239. catch( std::bad_function_call& )
  240. {
  241. wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
  242. }
  243. }
  244. void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const
  245. {
  246. try
  247. {
  248. for( BOARD_ITEM* item : m_items )
  249. {
  250. aFunction( item );
  251. if( item->Type() == PCB_GROUP_T )
  252. static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
  253. }
  254. }
  255. catch( std::bad_function_call& )
  256. {
  257. wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
  258. }
  259. }