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 
145 void PCB_GROUP::SetLocked( bool aLockState )
146 {
147  BOARD_ITEM::SetLocked( aLockState );
148 
150  [&]( BOARD_ITEM* child )
151  {
152  child->SetLocked( aLockState );
153  } );
154 }
155 
156 
158 {
159  // Use copy constructor to get the same uuid and other fields
160  PCB_GROUP* newGroup = new PCB_GROUP( *this );
161  return newGroup;
162 }
163 
164 
166 {
167  // Use copy constructor to get the same uuid and other fields
168  PCB_GROUP* newGroup = new PCB_GROUP( *this );
169  newGroup->m_items.clear();
170 
171  for( BOARD_ITEM* member : m_items )
172  {
173  if( member->Type() == PCB_GROUP_T )
174  newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
175  else
176  newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
177  }
178 
179  return newGroup;
180 }
181 
182 
184 {
185  PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( Duplicate() );
186  newGroup->m_items.clear();
187 
188  for( BOARD_ITEM* member : m_items )
189  {
190  if( member->Type() == PCB_GROUP_T )
191  newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
192  else
193  newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
194  }
195 
196  return newGroup;
197 }
198 
199 
201 {
202  assert( aImage->Type() == PCB_GROUP_T );
203 
204  std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
205 }
206 
207 
208 bool PCB_GROUP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
209 {
210  // Groups are selected by promoting a selection of one of their children
211  return false;
212 }
213 
214 
215 bool PCB_GROUP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
216 {
217  // Groups are selected by promoting a selection of one of their children
218  return false;
219 }
220 
221 
223 {
224  EDA_RECT area;
225 
226  for( BOARD_ITEM* item : m_items )
227  area.Merge( item->GetBoundingBox() );
228 
229  area.Inflate( Millimeter2iu( 0.25 ) ); // Give a min size to the area
230 
231  return area;
232 }
233 
234 
235 SEARCH_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData, const KICAD_T aScanTypes[] )
236 {
237  for( const KICAD_T* stype = aScanTypes; *stype != EOT; ++stype )
238  {
239  // If caller wants to inspect my type
240  if( *stype == Type() )
241  {
242  if( SEARCH_RESULT::QUIT == aInspector( this, aTestData ) )
243  return SEARCH_RESULT::QUIT;
244  }
245  }
246 
248 }
249 
250 
252 {
253  LSET aSet;
254 
255  for( BOARD_ITEM* item : m_items )
256  aSet |= item->GetLayerSet();
257 
258  return aSet;
259 }
260 
261 
262 bool PCB_GROUP::IsOnLayer( PCB_LAYER_ID aLayer ) const
263 {
264  // A group is on a layer if any item is on the layer
265  for( BOARD_ITEM* item : m_items )
266  {
267  if( item->IsOnLayer( aLayer ) )
268  return true;
269  }
270 
271  return false;
272 }
273 
274 
275 void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
276 {
277  aCount = 1;
278  aLayers[0] = LAYER_ANCHOR;
279 }
280 
281 
282 double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
283 {
284  if( aView->IsLayerVisible( LAYER_ANCHOR ) )
285  return 0.0;
286 
287  return std::numeric_limits<double>::max();
288 }
289 
290 
291 void PCB_GROUP::Move( const wxPoint& aMoveVector )
292 {
293  for( BOARD_ITEM* member : m_items )
294  member->Move( aMoveVector );
295 }
296 
297 
298 void PCB_GROUP::Rotate( const wxPoint& aRotCentre, double aAngle )
299 {
300  for( BOARD_ITEM* item : m_items )
301  item->Rotate( aRotCentre, aAngle );
302 }
303 
304 
305 void PCB_GROUP::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
306 {
307  for( BOARD_ITEM* item : m_items )
308  item->Flip( aCentre, aFlipLeftRight );
309 }
310 
311 
312 wxString PCB_GROUP::GetSelectMenuText( EDA_UNITS aUnits ) const
313 {
314  if( m_name.empty() )
315  {
316  return wxString::Format( _( "Unnamed Group, %zu members" ),
317  m_items.size() );
318  }
319 
320  return wxString::Format( _( "Group '%s', %zu members" ),
321  m_name,
322  m_items.size() );
323 }
324 
325 
327 {
328  return BITMAPS::module;
329 }
330 
331 
332 void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
333 {
334  aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
335  aList.emplace_back( _( "Members" ), wxString::Format( "%zu", m_items.size() ) );
336 
337  if( IsLocked() )
338  aList.emplace_back( _( "Status" ), _( "Locked" ) );
339 }
340 
341 
342 void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
343 {
344  try
345  {
346  for( BOARD_ITEM* item : m_items )
347  aFunction( item );
348  }
349  catch( std::bad_function_call& )
350  {
351  wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
352  }
353 }
354 
355 
356 void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const
357 {
358  try
359  {
360  for( BOARD_ITEM* item : m_items )
361  {
362  aFunction( item );
363 
364  if( item->Type() == PCB_GROUP_T )
365  static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
366  }
367  }
368  catch( std::bad_function_call& )
369  {
370  wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
371  }
372 }
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:60
void RemoveAll()
Definition: pcb_group.cpp:63
void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: pcb_group.cpp:275
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:312
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_group.cpp:326
void SetLocked(bool aLocked) override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_group.cpp:145
void Merge(const EDA_RECT &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: eda_rect.cpp:432
void SwapData(BOARD_ITEM *aImage) override
Test to see if this object is on the given layer.
Definition: pcb_group.cpp:200
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:49
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
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:235
anchor of items having an anchor point (texts, footprints)
Definition: layer_ids.h:197
PCB_GROUP * DeepClone() const
Definition: pcb_group.cpp:165
static PCB_GROUP * TopLevelGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:99
virtual void SetLocked(bool aLocked)
Modify the 'lock' status for of the item.
Definition: board_item.h:221
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:222
The base class for create windows for drawing purpose.
virtual bool IsLocked() const
Definition: board_item.cpp:64
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:305
PCB_GROUP * DeepDuplicate() const
Swap data between aItem and aImage.
Definition: pcb_group.cpp:183
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:94
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:332
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Flip this object, i.e.
Definition: pcb_group.cpp:298
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:504
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:208
void Move(const wxPoint &aMoveVector) override
Rotate this object.
Definition: pcb_group.cpp:291
wxString m_name
Definition: pcb_group.h:226
LSET GetLayerSet() const override
Set the layer this item is on.
Definition: pcb_group.cpp:251
#define _(s)
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:356
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Move this object.
Definition: pcb_group.cpp:282
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:262
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
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
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:100
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all members of the group.
Definition: pcb_group.cpp:342
wxPoint Centre() const
Definition: eda_rect.h:55
constexpr int delta
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.h:169
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
SEARCH_RESULT
Definition: eda_item.h:41
Message panel definition file.
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:135
static constexpr int Millimeter2iu(double mm)
std::unordered_set< BOARD_ITEM * > m_items
Definition: pcb_group.h:225
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:157
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:405
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:59