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 <eda_draw_frame.h>
26 #include <board.h>
27 #include <board_item.h>
28 #include <pcb_group.h>
29 #include <confirm.h>
30 #include <widgets/msgpanel.h>
31 #include <view/view.h>
32 
34  BOARD_ITEM( aParent, PCB_GROUP_T )
35 {
36 }
37 
38 
40 {
41  // Items can only be in one group at a time
42  if( aItem->GetParentGroup() )
43  aItem->GetParentGroup()->RemoveItem( aItem );
44 
45  m_items.insert( aItem );
46  aItem->SetParentGroup( this );
47  return true;
48 }
49 
50 
52 {
53  // Only clear the item's group field if it was inside this group
54  if( m_items.erase( aItem ) == 1 )
55  {
56  aItem->SetParentGroup( nullptr );
57  return true;
58  }
59 
60  return false;
61 }
62 
63 
65 {
66  for( BOARD_ITEM* item : m_items )
67  item->SetParentGroup( nullptr );
68 
69  m_items.clear();
70 }
71 
72 
73 PCB_GROUP* getTopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
74 {
75  PCB_GROUP* group = nullptr;
76 
77  if( isFootprintEditor )
78  {
79  group = aItem->GetParentGroup();
80  }
81  else
82  {
83  if( aItem->GetParent() && aItem->GetParent()->Type() == PCB_FOOTPRINT_T )
84  group = aItem->GetParent()->GetParentGroup();
85  else
86  group = aItem->GetParentGroup();
87  }
88 
89  while( group && group->GetParentGroup() && group->GetParentGroup() != aScope )
90  {
91  if( group->GetParent()->Type() == PCB_FOOTPRINT_T && isFootprintEditor )
92  break;
93 
94  group = group->GetParentGroup();
95  }
96 
97  return group;
98 }
99 
100 PCB_GROUP* PCB_GROUP::TopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
101 {
102  PCB_GROUP* candidate = getTopLevelGroup( aItem, aScope, isFootprintEditor );
103 
104  return candidate == aScope ? nullptr : candidate;
105 }
106 
107 
108 bool PCB_GROUP::WithinScope( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool isFootprintEditor )
109 {
110  PCB_GROUP* candidate = getTopLevelGroup( aItem, aScope, isFootprintEditor );
111 
112  return candidate == aScope;
113 }
114 
115 
116 wxPoint PCB_GROUP::GetPosition() const
117 {
118  return GetBoundingBox().Centre();
119 }
120 
121 
122 void PCB_GROUP::SetPosition( const wxPoint& aNewpos )
123 {
124  wxPoint delta = aNewpos - GetPosition();
125 
126  Move( delta );
127 }
128 
129 
130 void PCB_GROUP::SetLayerRecursive( PCB_LAYER_ID aLayer, int aDepth )
131 {
132  for( auto item : m_items )
133  {
134  if( ( item->Type() == PCB_GROUP_T ) && ( aDepth > 0 ) )
135  {
136  static_cast<PCB_GROUP*>( item )->SetLayerRecursive( aLayer, aDepth - 1 );
137  }
138  else
139  {
140  item->SetLayer( aLayer );
141  }
142  }
143 }
144 
145 
146 void PCB_GROUP::SetLocked( bool aLockState )
147 {
148  BOARD_ITEM::SetLocked( aLockState );
149 
151  [&]( BOARD_ITEM* child )
152  {
153  child->SetLocked( aLockState );
154  } );
155 }
156 
157 
159 {
160  // Use copy constructor to get the same uuid and other fields
161  PCB_GROUP* newGroup = new PCB_GROUP( *this );
162  return newGroup;
163 }
164 
165 
167 {
168  // Use copy constructor to get the same uuid and other fields
169  PCB_GROUP* newGroup = new PCB_GROUP( *this );
170  newGroup->m_items.clear();
171 
172  for( BOARD_ITEM* member : m_items )
173  {
174  if( member->Type() == PCB_GROUP_T )
175  newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
176  else
177  newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
178  }
179 
180  return newGroup;
181 }
182 
183 
185 {
186  PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( Duplicate() );
187  newGroup->m_items.clear();
188 
189  for( BOARD_ITEM* member : m_items )
190  {
191  if( member->Type() == PCB_GROUP_T )
192  newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepDuplicate() );
193  else
194  newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
195  }
196 
197  return newGroup;
198 }
199 
200 
202 {
203  assert( aImage->Type() == PCB_GROUP_T );
204 
205  std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
206 }
207 
208 
209 bool PCB_GROUP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
210 {
211  // Groups are selected by promoting a selection of one of their children
212  return false;
213 }
214 
215 
216 bool PCB_GROUP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
217 {
218  // Groups are selected by promoting a selection of one of their children
219  return false;
220 }
221 
222 
224 {
225  EDA_RECT area;
226 
227  for( BOARD_ITEM* item : m_items )
228  area.Merge( item->GetBoundingBox() );
229 
230  area.Inflate( Millimeter2iu( 0.25 ) ); // Give a min size to the area
231 
232  return area;
233 }
234 
235 
236 SEARCH_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData, const KICAD_T aScanTypes[] )
237 {
238  for( const KICAD_T* stype = aScanTypes; *stype != EOT; ++stype )
239  {
240  // If caller wants to inspect my type
241  if( *stype == Type() )
242  {
243  if( SEARCH_RESULT::QUIT == aInspector( this, aTestData ) )
244  return SEARCH_RESULT::QUIT;
245  }
246  }
247 
249 }
250 
251 
253 {
254  LSET aSet;
255 
256  for( BOARD_ITEM* item : m_items )
257  aSet |= item->GetLayerSet();
258 
259  return aSet;
260 }
261 
262 
263 bool PCB_GROUP::IsOnLayer( PCB_LAYER_ID aLayer ) const
264 {
265  // A group is on a layer if any item is on the layer
266  for( BOARD_ITEM* item : m_items )
267  {
268  if( item->IsOnLayer( aLayer ) )
269  return true;
270  }
271 
272  return false;
273 }
274 
275 
276 void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
277 {
278  aCount = 1;
279  aLayers[0] = LAYER_ANCHOR;
280 }
281 
282 
283 double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
284 {
285  if( aView->IsLayerVisible( LAYER_ANCHOR ) )
286  return 0.0;
287 
288  return std::numeric_limits<double>::max();
289 }
290 
291 
292 void PCB_GROUP::Move( const wxPoint& aMoveVector )
293 {
294  for( BOARD_ITEM* member : m_items )
295  member->Move( aMoveVector );
296 }
297 
298 
299 void PCB_GROUP::Rotate( const wxPoint& aRotCentre, double aAngle )
300 {
301  for( BOARD_ITEM* item : m_items )
302  item->Rotate( aRotCentre, aAngle );
303 }
304 
305 
306 void PCB_GROUP::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
307 {
308  for( BOARD_ITEM* item : m_items )
309  item->Flip( aCentre, aFlipLeftRight );
310 }
311 
312 
313 wxString PCB_GROUP::GetSelectMenuText( EDA_UNITS aUnits ) const
314 {
315  if( m_name.empty() )
316  {
317  return wxString::Format( _( "Unnamed Group, %zu members" ),
318  m_items.size() );
319  }
320 
321  return wxString::Format( _( "Group '%s', %zu members" ),
322  m_name,
323  m_items.size() );
324 }
325 
326 
328 {
329  return BITMAPS::module;
330 }
331 
332 
333 void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
334 {
335  aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
336  aList.emplace_back( _( "Members" ), wxString::Format( wxT( "%zu" ), m_items.size() ) );
337 
338  if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
339  aList.emplace_back( _( "Status" ), _( "Locked" ) );
340 }
341 
342 
343 void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
344 {
345  try
346  {
347  for( BOARD_ITEM* item : m_items )
348  aFunction( item );
349  }
350  catch( std::bad_function_call& )
351  {
352  wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
353  }
354 }
355 
356 
357 void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const
358 {
359  try
360  {
361  for( BOARD_ITEM* item : m_items )
362  {
363  aFunction( item );
364 
365  if( item->Type() == PCB_GROUP_T )
366  static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
367  }
368  }
369  catch( std::bad_function_call& )
370  {
371  wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
372  }
373 }
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:60
void RemoveAll()
Definition: pcb_group.cpp:64
void ViewGetLayers(int aLayers[], int &aCount) const override
Definition: pcb_group.cpp:276
bool AddItem(BOARD_ITEM *aItem)
Add item to group.
Definition: pcb_group.cpp:39
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return a pointer to an image to be used in menus.
Definition: pcb_group.cpp:313
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
Definition: pcb_group.cpp:327
void SetLocked(bool aLocked) override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_group.cpp:146
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:201
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:73
wxPoint GetPosition() const override
Definition: pcb_group.cpp:116
SEARCH_RESULT Visit(INSPECTOR aInspector, void *aTestData, const KICAD_T aScanTypes[]) override
Definition: pcb_group.cpp:236
anchor of items having an anchor point (texts, footprints)
Definition: layer_ids.h:208
PCB_GROUP * DeepClone() const
Definition: pcb_group.cpp:166
static PCB_GROUP * TopLevelGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:100
virtual void SetLocked(bool aLocked)
Modify the 'lock' status for of the item.
Definition: board_item.h:218
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:223
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:306
PCB_GROUP * DeepDuplicate() const
Swap data between aItem and aImage.
Definition: pcb_group.cpp:184
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:93
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:333
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Flip this object, i.e.
Definition: pcb_group.cpp:299
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:516
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition: pcb_group.cpp:209
void Move(const wxPoint &aMoveVector) override
Rotate this object.
Definition: pcb_group.cpp:292
wxString m_name
Definition: pcb_group.h:226
LSET GetLayerSet() const override
Set the layer this item is on.
Definition: pcb_group.cpp:252
#define _(s)
bool RemoveItem(BOARD_ITEM *aItem)
Remove item from group.
Definition: pcb_group.cpp:51
static bool WithinScope(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool isFootprintEditor)
Definition: pcb_group.cpp:108
void RunOnDescendants(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all descendants of the group.
Definition: pcb_group.cpp:357
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Move this object.
Definition: pcb_group.cpp:283
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 inside or on the boundary of this item.
Definition: pcb_group.cpp:263
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:130
void SetPosition(const wxPoint &aNewpos) override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_group.cpp:122
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:99
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all members of the group.
Definition: pcb_group.cpp:343
wxPoint Centre() const
Definition: eda_rect.h:64
constexpr int delta
#define PCB_EDIT_FRAME_NAME
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:136
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:33
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: pcb_group.cpp:158
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.cpp:144
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112
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