KiCad PCB EDA Suite
pcb_group.cpp
Go to the documentation of this file.
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 
33  BOARD_ITEM( aParent, PCB_GROUP_T )
34 {
35 }
36 
37 
39 {
40  // Items can only be in one group at a time
41  if( aItem->GetParentGroup() )
42  aItem->GetParentGroup()->RemoveItem( aItem );
43 
44  m_items.insert( aItem );
45  aItem->SetParentGroup( this );
46  return true;
47 }
48 
49 
51 {
52  // Only clear the item's group field if it was inside this group
53  if( m_items.erase( aItem ) == 1 )
54  {
55  aItem->SetParentGroup( nullptr );
56  return true;
57  }
58 
59  return false;
60 }
61 
62 
64 {
65  for( BOARD_ITEM* item : m_items )
66  item->SetParentGroup( nullptr );
67 
68  m_items.clear();
69 }
70 
71 
72 PCB_GROUP* getTopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
73 {
74  PCB_GROUP* group = nullptr;
75 
76  if( isFootprintEditor )
77  {
78  group = aItem->GetParentGroup();
79  }
80  else
81  {
82  if( aItem->GetParent() && aItem->GetParent()->Type() == PCB_FOOTPRINT_T )
83  group = aItem->GetParent()->GetParentGroup();
84  else
85  group = aItem->GetParentGroup();
86  }
87 
88  while( group && group->GetParentGroup() && group->GetParentGroup() != aScope )
89  {
90  if( group->GetParent()->Type() == PCB_FOOTPRINT_T && isFootprintEditor )
91  break;
92 
93  group = group->GetParentGroup();
94  }
95 
96  return group;
97 }
98 
99 PCB_GROUP* PCB_GROUP::TopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
100 {
101  PCB_GROUP* candidate = getTopLevelGroup( aItem, aScope, isFootprintEditor );
102 
103  return candidate == aScope ? nullptr : candidate;
104 }
105 
106 
107 bool PCB_GROUP::WithinScope( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
108 {
109  PCB_GROUP* candidate = getTopLevelGroup( aItem, aScope, isFootprintEditor );
110 
111  return candidate == aScope;
112 }
113 
114 
115 wxPoint PCB_GROUP::GetPosition() const
116 {
117  return GetBoundingBox().Centre();
118 }
119 
120 
121 void PCB_GROUP::SetPosition( const wxPoint& aNewpos )
122 {
123  wxPoint delta = aNewpos - GetPosition();
124 
125  Move( delta );
126 }
127 
128 
129 void PCB_GROUP::SetLayerRecursive( PCB_LAYER_ID aLayer, int aDepth )
130 {
131  for( auto item : m_items )
132  {
133  if( ( item->Type() == PCB_GROUP_T ) && ( aDepth > 0 ) )
134  {
135  static_cast<PCB_GROUP*>( item )->SetLayerRecursive( aLayer, aDepth - 1 );
136  }
137  else
138  {
139  item->SetLayer( aLayer );
140  }
141  }
142 }
143 
144 
146 {
147  // Use copy constructor to get the same uuid and other fields
148  PCB_GROUP* newGroup = new PCB_GROUP( *this );
149  return newGroup;
150 }
151 
152 
154 {
155  // Use copy constructor to get the same uuid and other fields
156  PCB_GROUP* newGroup = new PCB_GROUP( *this );
157  newGroup->m_items.clear();
158 
159  for( BOARD_ITEM* member : m_items )
160  {
161  if( member->Type() == PCB_GROUP_T )
162  newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
163  else
164  newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
165  }
166 
167  return newGroup;
168 }
169 
170 
172 {
173  PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( Duplicate() );
174  newGroup->m_items.clear();
175 
176  for( BOARD_ITEM* member : m_items )
177  {
178  if( member->Type() == PCB_GROUP_T )
179  newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
180  else
181  newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
182  }
183 
184  return newGroup;
185 }
186 
187 
189 {
190  assert( aImage->Type() == PCB_GROUP_T );
191 
192  std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
193 }
194 
195 
196 bool PCB_GROUP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
197 {
198  // Groups are selected by promoting a selection of one of their children
199  return false;
200 }
201 
202 
203 bool PCB_GROUP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
204 {
205  // Groups are selected by promoting a selection of one of their children
206  return false;
207 }
208 
209 
211 {
212  EDA_RECT area;
213 
214  for( BOARD_ITEM* item : m_items )
215  area.Merge( item->GetBoundingBox() );
216 
217  area.Inflate( Millimeter2iu( 0.25 ) ); // Give a min size to the area
218 
219  return area;
220 }
221 
222 
223 SEARCH_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData, const KICAD_T aScanTypes[] )
224 {
225  for( const KICAD_T* stype = aScanTypes; *stype != EOT; ++stype )
226  {
227  // If caller wants to inspect my type
228  if( *stype == Type() )
229  {
230  if( SEARCH_RESULT::QUIT == aInspector( this, aTestData ) )
231  return SEARCH_RESULT::QUIT;
232  }
233  }
234 
236 }
237 
238 
240 {
241  LSET aSet;
242 
243  for( BOARD_ITEM* item : m_items )
244  aSet |= item->GetLayerSet();
245 
246  return aSet;
247 }
248 
249 
250 bool PCB_GROUP::IsOnLayer( PCB_LAYER_ID aLayer ) const
251 {
252  // A group is on a layer if any item is on the layer
253  for( BOARD_ITEM* item : m_items )
254  {
255  if( item->IsOnLayer( aLayer ) )
256  return true;
257  }
258 
259  return false;
260 }
261 
262 
263 void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
264 {
265  aCount = 1;
266  aLayers[0] = LAYER_ANCHOR;
267 }
268 
269 
270 double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
271 {
272  if( aView->IsLayerVisible( LAYER_ANCHOR ) )
273  return 0.0;
274 
275  return std::numeric_limits<double>::max();
276 }
277 
278 
279 void PCB_GROUP::Move( const wxPoint& aMoveVector )
280 {
281  for( BOARD_ITEM* member : m_items )
282  member->Move( aMoveVector );
283 }
284 
285 
286 void PCB_GROUP::Rotate( const wxPoint& aRotCentre, double aAngle )
287 {
288  for( BOARD_ITEM* item : m_items )
289  item->Rotate( aRotCentre, aAngle );
290 }
291 
292 
293 void PCB_GROUP::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
294 {
295  for( BOARD_ITEM* item : m_items )
296  item->Flip( aCentre, aFlipLeftRight );
297 }
298 
299 
300 wxString PCB_GROUP::GetSelectMenuText( EDA_UNITS aUnits ) const
301 {
302  if( m_name.empty() )
303  {
304  return wxString::Format( _( "Unnamed Group, %zu members" ),
305  m_items.size() );
306  }
307 
308  return wxString::Format( _( "Group \"%s\", %zu members" ),
309  m_name,
310  m_items.size() );
311 }
312 
313 
315 {
316  return BITMAPS::module;
317 }
318 
319 
320 void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
321 {
322  aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
323  aList.emplace_back( _( "Members" ), wxString::Format( "%zu", m_items.size() ) );
324 }
325 
326 
327 void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
328 {
329  try
330  {
331  for( BOARD_ITEM* item : m_items )
332  aFunction( item );
333  }
334  catch( std::bad_function_call& )
335  {
336  wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
337  }
338 }
339 
340 
341 void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const
342 {
343  try
344  {
345  for( BOARD_ITEM* item : m_items )
346  {
347  aFunction( item );
348 
349  if( item->Type() == PCB_GROUP_T )
350  static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
351  }
352  }
353  catch( std::bad_function_call& )
354  {
355  wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
356  }
357 }
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:93
void RemoveAll()
Definition: pcb_group.cpp:63
void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: pcb_group.cpp:263
bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:38
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return a pointer to an image to be used in menus.
Definition: pcb_group.cpp:300
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_group.cpp:314
void Merge(const EDA_RECT &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: eda_rect.cpp:431
void SwapData(BOARD_ITEM *aImage) override
Test to see if this object is on the given layer.
Definition: pcb_group.cpp:188
This file is part of the common library.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:108
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:50
anchor of items having an anchor point (texts, footprints)
PCB_GROUP * getTopLevelGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:72
wxPoint GetPosition() const override
Definition: pcb_group.cpp:115
SEARCH_RESULT Visit(INSPECTOR aInspector, void *aTestData, const KICAD_T aScanTypes[]) override
Definition: pcb_group.cpp:223
PCB_GROUP * DeepClone() const
Definition: pcb_group.cpp:153
static PCB_GROUP * TopLevelGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:99
const EDA_RECT GetBoundingBox() const override
May be re-implemented for each derived class in order to handle all the types given by its member dat...
Definition: pcb_group.cpp:210
The base class for create windows for drawing purpose.
void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Return the text to display to be used in the selection clarification context menu when multiple items...
Definition: pcb_group.cpp:293
PCB_GROUP * DeepDuplicate() const
Swap data between aItem and aImage.
Definition: pcb_group.cpp:171
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:95
search types array terminator (End Of Types)
Definition: typeinfo.h:81
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: pcb_group.cpp:320
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Flip this object, i.e.
Definition: pcb_group.cpp:286
PCB_LAYER_ID
A quick note on layer IDs:
LSET is a set of PCB_LAYER_IDs.
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is contained within or on the bounding box of an item.
Definition: pcb_group.cpp:196
void Move(const wxPoint &aMoveVector) override
Rotate this object.
Definition: pcb_group.cpp:279
wxString m_name
Definition: pcb_group.h:224
LSET GetLayerSet() const override
Set the layer this item is on.
Definition: pcb_group.cpp:239
bool RemoveItem(BOARD_ITEM *aItem)
Remove item from group.
Definition: pcb_group.cpp:50
static bool WithinScope(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:107
void RunOnDescendants(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all descendants of the group.
Definition: pcb_group.cpp:341
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Move this object.
Definition: pcb_group.cpp:270
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
EDA_UNITS
Definition: eda_units.h:38
bool IsOnLayer(PCB_LAYER_ID aLayer) const override
Test if aPosition is contained within or on the bounding box of an item.
Definition: pcb_group.cpp:250
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
void SetLayerRecursive(PCB_LAYER_ID aLayer, int aDepth)
Set layer for all items within the group.
Definition: pcb_group.cpp:129
void SetPosition(const wxPoint &aNewpos) override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_group.cpp:121
#define _(s)
Definition: 3d_actions.cpp:33
Handle the component boundary box.
Definition: eda_rect.h:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:150
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all members of the group.
Definition: pcb_group.cpp:327
wxPoint Centre() const
Definition: eda_rect.h:60
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.h:202
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
SEARCH_RESULT
Definition: eda_item.h:41
Message panel definition file.
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:168
static constexpr int Millimeter2iu(double mm)
std::unordered_set< BOARD_ITEM * > m_items
Definition: pcb_group.h:223
PCB_GROUP(BOARD_ITEM *aParent)
Definition: pcb_group.cpp:32
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_group.cpp:145
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:363
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:163
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:404
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:92