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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <eda_group.h>
25
26#include <set>
27#include <vector>
28
29
30bool EDA_GROUP::ContainsItem( const EDA_ITEM* aItem ) const
31{
32 if( !aItem )
33 return false;
34
35 std::set<const EDA_GROUP*> visitedGroups;
36 std::vector<const EDA_GROUP*> pendingGroups;
37
38 pendingGroups.push_back( this );
39
40 while( !pendingGroups.empty() )
41 {
42 const EDA_GROUP* group = pendingGroups.back();
43 pendingGroups.pop_back();
44
45 if( !visitedGroups.insert( group ).second )
46 continue;
47
48 for( EDA_ITEM* member : group->GetItems() )
49 {
50 if( member == aItem )
51 return true;
52
53 if( const EDA_GROUP* childGroup = dynamic_cast<const EDA_GROUP*>( member ) )
54 pendingGroups.push_back( childGroup );
55 }
56 }
57
58 return false;
59}
60
61
63{
64 wxCHECK_RET( aItem, wxT( "Nullptr added to group." ) );
65 wxCHECK_RET( aItem != AsEdaItem(), wxT( "Group added to itself." ) );
66
67 if( EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( aItem ) )
68 {
69 wxCHECK_RET( !group->ContainsItem( AsEdaItem() ), wxT( "Ancestor group added to group." ) );
70 }
71
72 // Items can only be in one group at a time
73 if( EDA_GROUP* parentGroup = aItem->GetParentGroup() )
74 parentGroup->RemoveItem( aItem );
75
76 m_items.insert( aItem );
77 aItem->SetParentGroup( this );
78}
79
80
82{
83 wxCHECK_RET( aItem, wxT( "Nullptr removed from group." ) );
84
85 if( m_items.erase( aItem ) == 1 )
86 aItem->SetParentGroup( nullptr );
87}
88
89
91{
92 for( EDA_ITEM* item : m_items )
93 item->SetParentGroup( nullptr );
94
95 m_items.clear();
96}
97
98
99std::vector<KIID> EDA_GROUP::GetGroupMemberIds() const
100{
101 std::vector<KIID> members;
102
103 for( EDA_ITEM* item : m_items )
104 members.push_back( item->m_Uuid );
105
106 return members;
107}
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:46
std::unordered_set< EDA_ITEM * > m_items
Definition eda_group.h:81
void RemoveAll()
Definition eda_group.cpp:90
void RemoveItem(EDA_ITEM *aItem)
Remove item from group.
Definition eda_group.cpp:81
void AddItem(EDA_ITEM *aItem)
Add item to group.
Definition eda_group.cpp:62
bool ContainsItem(const EDA_ITEM *aItem) const
Test if an item is a direct or nested member of this group.
Definition eda_group.cpp:30
std::vector< KIID > GetGroupMemberIds() const
Definition eda_group.cpp:99
virtual EDA_ITEM * AsEdaItem()=0
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:100
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:118
virtual void SetParentGroup(EDA_GROUP *aGroup)
Definition eda_item.h:117