KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <eda_group.h>
21
22#include <set>
23#include <vector>
24
25
26bool EDA_GROUP::ContainsItem( const EDA_ITEM* aItem ) const
27{
28 if( !aItem )
29 return false;
30
31 std::set<const EDA_GROUP*> visitedGroups;
32 std::vector<const EDA_GROUP*> pendingGroups;
33
34 pendingGroups.push_back( this );
35
36 while( !pendingGroups.empty() )
37 {
38 const EDA_GROUP* group = pendingGroups.back();
39 pendingGroups.pop_back();
40
41 if( !visitedGroups.insert( group ).second )
42 continue;
43
44 for( EDA_ITEM* member : group->GetItems() )
45 {
46 if( member == aItem )
47 return true;
48
49 if( const EDA_GROUP* childGroup = dynamic_cast<const EDA_GROUP*>( member ) )
50 pendingGroups.push_back( childGroup );
51 }
52 }
53
54 return false;
55}
56
57
59{
60 wxCHECK_RET( aItem, wxT( "Nullptr added to group." ) );
61 wxCHECK_RET( aItem != AsEdaItem(), wxT( "Group added to itself." ) );
62
63 if( EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( aItem ) )
64 {
65 wxCHECK_RET( !group->ContainsItem( AsEdaItem() ), wxT( "Ancestor group added to group." ) );
66 }
67
68 // Items can only be in one group at a time
69 if( EDA_GROUP* parentGroup = aItem->GetParentGroup() )
70 parentGroup->RemoveItem( aItem );
71
72 m_items.insert( aItem );
73 aItem->SetParentGroup( this );
74}
75
76
78{
79 wxCHECK_RET( aItem, wxT( "Nullptr removed from group." ) );
80
81 if( m_items.erase( aItem ) == 1 )
82 aItem->SetParentGroup( nullptr );
83}
84
85
87{
88 for( EDA_ITEM* item : m_items )
89 item->SetParentGroup( nullptr );
90
91 m_items.clear();
92}
93
94
95std::vector<KIID> EDA_GROUP::GetGroupMemberIds() const
96{
97 std::vector<KIID> members;
98
99 for( EDA_ITEM* item : m_items )
100 members.push_back( item->m_Uuid );
101
102 return members;
103}
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:42
std::unordered_set< EDA_ITEM * > m_items
Definition eda_group.h:77
void RemoveAll()
Definition eda_group.cpp:86
void RemoveItem(EDA_ITEM *aItem)
Remove item from group.
Definition eda_group.cpp:77
void AddItem(EDA_ITEM *aItem)
Add item to group.
Definition eda_group.cpp:58
bool ContainsItem(const EDA_ITEM *aItem) const
Test if an item is a direct or nested member of this group.
Definition eda_group.cpp:26
std::vector< KIID > GetGroupMemberIds() const
Definition eda_group.cpp:95
virtual EDA_ITEM * AsEdaItem()=0
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:114
virtual void SetParentGroup(EDA_GROUP *aGroup)
Definition eda_item.h:113