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* PCB_GROUP::TopLevelGroup( BOARD_ITEM* aItem, PCB_GROUP* aScope, bool aFootprintEditor )
73 {
74  PCB_GROUP* candidate = aItem->GetParentGroup();
75 
76  // Don't get the footprint's group if we are in the footprint editor
77  if( !candidate && aItem->GetParent() && aItem->GetParent()->Type() == PCB_FOOTPRINT_T
78  && !aFootprintEditor )
79  candidate = aItem->GetParent()->GetParentGroup();
80 
81  while( candidate && candidate->GetParentGroup() && candidate->GetParentGroup() != aScope )
82  {
83  if( candidate->GetParent()->Type() == PCB_FOOTPRINT_T && aFootprintEditor )
84  break;
85 
86  candidate = candidate->GetParentGroup();
87  }
88 
89  return candidate == aScope ? nullptr : candidate;
90 }
91 
92 
94 {
95  for( PCB_GROUP* parent = item->GetParentGroup(); parent; parent = parent->GetParentGroup() )
96  {
97  if( parent == scope )
98  return true;
99  }
100 
101  if( item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
102  item = item->GetParent();
103 
104  for( PCB_GROUP* parent = item->GetParentGroup(); parent; parent = parent->GetParentGroup() )
105  {
106  if( parent == scope )
107  return true;
108  }
109 
110  return false;
111 }
112 
113 
114 wxPoint PCB_GROUP::GetPosition() const
115 {
116  return GetBoundingBox().Centre();
117 }
118 
119 
120 void PCB_GROUP::SetPosition( const wxPoint& aNewpos )
121 {
122  wxPoint delta = aNewpos - GetPosition();
123 
124  Move( delta );
125 }
126 
127 
129 {
130  // Use copy constructor to get the same uuid and other fields
131  PCB_GROUP* newGroup = new PCB_GROUP( *this );
132  return newGroup;
133 }
134 
135 
137 {
138  // Use copy constructor to get the same uuid and other fields
139  PCB_GROUP* newGroup = new PCB_GROUP( *this );
140  newGroup->m_items.clear();
141 
142  for( BOARD_ITEM* member : m_items )
143  {
144  if( member->Type() == PCB_GROUP_T )
145  newGroup->AddItem( static_cast<PCB_GROUP*>( member )->DeepClone() );
146  else
147  newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Clone() ) );
148  }
149 
150  return newGroup;
151 }
152 
153 
155 {
156  PCB_GROUP* newGroup = static_cast<PCB_GROUP*>( Duplicate() );
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 )->DeepDuplicate() );
163  else
164  newGroup->AddItem( static_cast<BOARD_ITEM*>( member->Duplicate() ) );
165  }
166 
167  return newGroup;
168 }
169 
170 
172 {
173  assert( aImage->Type() == PCB_GROUP_T );
174 
175  std::swap( *( (PCB_GROUP*) this ), *( (PCB_GROUP*) aImage ) );
176 }
177 
178 
179 bool PCB_GROUP::HitTest( const wxPoint& aPosition, int aAccuracy ) const
180 {
181  // Groups are selected by promoting a selection of one of their children
182  return false;
183 }
184 
185 
186 bool PCB_GROUP::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
187 {
188  // Groups are selected by promoting a selection of one of their children
189  return false;
190 }
191 
192 
194 {
195  EDA_RECT area;
196 
197  for( BOARD_ITEM* item : m_items )
198  area.Merge( item->GetBoundingBox() );
199 
200  area.Inflate( Millimeter2iu( 0.25 ) ); // Give a min size to the area
201 
202  return area;
203 }
204 
205 
206 SEARCH_RESULT PCB_GROUP::Visit( INSPECTOR aInspector, void* aTestData, const KICAD_T aScanTypes[] )
207 {
208  for( const KICAD_T* stype = aScanTypes; *stype != EOT; ++stype )
209  {
210  // If caller wants to inspect my type
211  if( *stype == Type() )
212  {
213  if( SEARCH_RESULT::QUIT == aInspector( this, aTestData ) )
214  return SEARCH_RESULT::QUIT;
215  }
216  }
217 
219 }
220 
221 
223 {
224  LSET aSet;
225 
226  for( BOARD_ITEM* item : m_items )
227  aSet |= item->GetLayerSet();
228 
229  return aSet;
230 }
231 
232 
233 bool PCB_GROUP::IsOnLayer( PCB_LAYER_ID aLayer ) const
234 {
235  // A group is on a layer if any item is on the layer
236  for( BOARD_ITEM* item : m_items )
237  {
238  if( item->IsOnLayer( aLayer ) )
239  return true;
240  }
241 
242  return false;
243 }
244 
245 
246 void PCB_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
247 {
248  aCount = 1;
249  aLayers[0] = LAYER_ANCHOR;
250 }
251 
252 
253 double PCB_GROUP::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
254 {
255  if( aView->IsLayerVisible( LAYER_ANCHOR ) )
256  return 0.0;
257 
258  return std::numeric_limits<double>::max();
259 }
260 
261 
262 void PCB_GROUP::Move( const wxPoint& aMoveVector )
263 {
264  for( BOARD_ITEM* member : m_items )
265  member->Move( aMoveVector );
266 }
267 
268 
269 void PCB_GROUP::Rotate( const wxPoint& aRotCentre, double aAngle )
270 {
271  for( BOARD_ITEM* item : m_items )
272  item->Rotate( aRotCentre, aAngle );
273 }
274 
275 
276 void PCB_GROUP::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
277 {
278  for( BOARD_ITEM* item : m_items )
279  item->Flip( aCentre, aFlipLeftRight );
280 }
281 
282 
283 wxString PCB_GROUP::GetSelectMenuText( EDA_UNITS aUnits ) const
284 {
285  if( m_name.empty() )
286  {
287  return wxString::Format( _( "Unnamed Group, %zu members" ),
288  m_items.size() );
289  }
290 
291  return wxString::Format( _( "Group \"%s\", %zu members" ),
292  m_name,
293  m_items.size() );
294 }
295 
296 
298 {
299  return module_xpm;
300 }
301 
302 
303 void PCB_GROUP::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
304 {
305  aList.emplace_back( _( "Group" ), m_name.empty() ? _( "<unnamed>" ) : m_name );
306  aList.emplace_back( _( "Members" ), wxString::Format( "%zu", m_items.size() ) );
307 }
308 
309 
310 void PCB_GROUP::RunOnChildren( const std::function<void( BOARD_ITEM* )>& aFunction ) const
311 {
312  try
313  {
314  for( BOARD_ITEM* item : m_items )
315  aFunction( item );
316  }
317  catch( std::bad_function_call& )
318  {
319  wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnChildren" ) );
320  }
321 }
322 
323 
324 void PCB_GROUP::RunOnDescendants( const std::function<void( BOARD_ITEM* )>& aFunction ) const
325 {
326  try
327  {
328  for( BOARD_ITEM* item : m_items )
329  {
330  aFunction( item );
331 
332  if( item->Type() == PCB_GROUP_T )
333  static_cast<PCB_GROUP*>( item )->RunOnDescendants( aFunction );
334  }
335  }
336  catch( std::bad_function_call& )
337  {
338  wxFAIL_MSG( wxT( "Error calling function in PCB_GROUP::RunOnDescendants" ) );
339  }
340 }
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:246
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:283
const BITMAP_OPAQUE module_xpm[1]
Definition: module.cpp:29
void Merge(const EDA_RECT &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: eda_rect.cpp:431
PNG memory record (file in memory).
Definition: bitmap_def.h:29
void SwapData(BOARD_ITEM *aImage) override
Test to see if this object is on the given layer.
Definition: pcb_group.cpp:171
static bool WithinScope(BOARD_ITEM *item, PCB_GROUP *scope)
Definition: pcb_group.cpp:93
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)
wxPoint GetPosition() const override
Definition: pcb_group.cpp:114
SEARCH_RESULT Visit(INSPECTOR aInspector, void *aTestData, const KICAD_T aScanTypes[]) override
Definition: pcb_group.cpp:206
PCB_GROUP * DeepClone() const
Definition: pcb_group.cpp:136
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:193
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:276
PCB_GROUP * DeepDuplicate() const
Swap data between aItem and aImage.
Definition: pcb_group.cpp:154
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:303
void Rotate(const wxPoint &aRotCentre, double aAngle) override
Flip this object, i.e.
Definition: pcb_group.cpp:269
PCB_LAYER_ID
A quick note on layer IDs:
LSET is a set of PCB_LAYER_IDs.
static PCB_GROUP * TopLevelGroup(BOARD_ITEM *aItem, PCB_GROUP *aScope, bool aFootprintEditor)
Definition: pcb_group.cpp:72
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:179
void Move(const wxPoint &aMoveVector) override
Rotate this object.
Definition: pcb_group.cpp:262
wxString m_name
Definition: pcb_group.h:202
LSET GetLayerSet() const override
Set the layer this item is on.
Definition: pcb_group.cpp:222
bool RemoveItem(BOARD_ITEM *aItem)
Remove item from group.
Definition: pcb_group.cpp:50
void RunOnDescendants(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all descendants of the group.
Definition: pcb_group.cpp:324
BITMAP_DEF GetMenuImage() const override
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition: pcb_group.cpp:297
double ViewGetLOD(int aLayer, KIGFX::VIEW *aView) const override
Move this object.
Definition: pcb_group.cpp:253
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:233
void SetPosition(const wxPoint &aNewpos) override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_group.cpp:120
#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:149
void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction) const
Invoke a function on all members of the group.
Definition: pcb_group.cpp:310
wxPoint Centre() const
Definition: eda_rect.h:60
virtual BOARD_ITEM * Duplicate() const
Create a copy of a 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:40
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:201
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:128
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:162
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