KiCad PCB EDA Suite
Loading...
Searching...
No Matches
collector.h
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) 2007-2008 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright The 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
25#ifndef COLLECTOR_H
26#define COLLECTOR_H
27
28#include <vector>
29#include <core/kicad_algo.h>
30#include <algorithm>
31#include <eda_item.h> // SEARCH_RESULT
32
33
34class EDA_ITEM;
35
36
49{
50public:
52 m_Threshold( 0 ),
53 m_MenuCancelled( false ),
54 m_scanTypes( {} )
55 {
56 // Inspect() is virtual so calling it from a class common inspector preserves
57 // polymorphism.
59 [this]( EDA_ITEM* aItem, void* aTestData )
60 {
61 return this->Inspect( aItem, aTestData );
62 };
63 }
64
65 virtual ~COLLECTOR() {}
66
67 virtual INSPECT_RESULT Inspect( EDA_ITEM* aTestItem, void* aTestData )
68 {
69 return INSPECT_RESULT::QUIT;
70 };
71
72 using ITER = std::vector<EDA_ITEM*>::iterator;
73 using CITER = std::vector<EDA_ITEM*>::const_iterator;
74
75 ITER begin() { return m_list.begin(); }
76 ITER end() { return m_list.end(); }
77 CITER begin() const { return m_list.cbegin(); }
78 CITER end() const { return m_list.cend(); }
79
83 int GetCount() const
84 {
85 return (int) m_list.size();
86 }
87
91 void Empty()
92 {
93 m_list.clear();
94 }
95
101 void Append( EDA_ITEM* item )
102 {
103 m_list.push_back( item );
104 }
105
111 void Remove( int aIndex )
112 {
113 m_list.erase( m_list.begin() + aIndex );
114 }
115
121 void Remove( const EDA_ITEM* aItem )
122 {
123 std::erase_if( m_list, [&aItem]( const EDA_ITEM* aCandidate )
124 {
125 return aCandidate == aItem;
126 } );
127 }
128
135 {
136 return !m_backupList.empty();
137 }
138
142 void Combine()
143 {
144 std::copy( m_backupList.begin(), m_backupList.end(), std::back_inserter( m_list ) );
145 m_backupList.clear();
146 }
147
153 void Transfer( int aIndex )
154 {
155 m_backupList.push_back( m_list[aIndex] );
156 m_list.erase( m_list.begin() + aIndex );
157 }
158
164 void Transfer( EDA_ITEM* aItem )
165 {
166 for( size_t i = 0; i < m_list.size(); i++ )
167 {
168 if( m_list[i] == aItem )
169 {
170 m_list.erase( m_list.begin() + i );
171 m_backupList.push_back( aItem );
172 return;
173 }
174 }
175 }
176
183 virtual EDA_ITEM* operator[]( int aIndex ) const
184 {
185 if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
186 return m_list[ aIndex ];
187
188 return nullptr;
189 }
190
197 bool HasItem( const EDA_ITEM* aItem ) const
198 {
199 for( size_t i = 0; i < m_list.size(); i++ )
200 {
201 if( m_list[i] == aItem )
202 return true;
203 }
204
205 return false;
206 }
207
213 void SetScanTypes( const std::vector<KICAD_T>& aTypes ) { m_scanTypes = aTypes; }
214
215 void SetRefPos( const VECTOR2I& aRefPos ) { m_refPos = aRefPos; }
216
223 int CountType( KICAD_T aType )
224 {
225 int cnt = 0;
226
227 for( size_t i = 0; i < m_list.size(); i++ )
228 {
229 if( m_list[i]->Type() == aType )
230 cnt++;
231 }
232
233 return cnt;
234 }
235
236 int m_Threshold; // Hit-test threshold in internal units.
237
238 wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
239 bool m_MenuCancelled; // Indicates selection disambiguation menu was canceled
240
241protected:
242 std::vector<EDA_ITEM*> m_list; // Primary list of most likely items
243 std::vector<EDA_ITEM*> m_backupList; // Secondary list with items removed by heuristics
244
245 std::vector<KICAD_T> m_scanTypes;
247
248 VECTOR2I m_refPos; // Reference pos used to generate the collection.
249};
250
251#endif // COLLECTOR_H
An abstract class that will find and hold all the objects according to an inspection done by the Insp...
Definition: collector.h:49
std::vector< EDA_ITEM * > m_backupList
Definition: collector.h:243
void Transfer(EDA_ITEM *aItem)
Move aItem (if exists in the collector) to the backup list.
Definition: collector.h:164
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:73
void Transfer(int aIndex)
Move the item at aIndex (first position is 0) to the backup list.
Definition: collector.h:153
bool m_MenuCancelled
Definition: collector.h:239
virtual INSPECT_RESULT Inspect(EDA_ITEM *aTestItem, void *aTestData)
Definition: collector.h:67
virtual ~COLLECTOR()
Definition: collector.h:65
INSPECTOR_FUNC m_inspector
Definition: collector.h:246
void Empty()
Clear the list.
Definition: collector.h:91
ITER begin()
Definition: collector.h:75
VECTOR2I m_refPos
Definition: collector.h:248
COLLECTOR()
Definition: collector.h:51
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:83
std::vector< EDA_ITEM * >::iterator ITER
Definition: collector.h:72
wxString m_MenuTitle
Definition: collector.h:238
bool HasItem(const EDA_ITEM *aItem) const
Tests if aItem has already been collected.
Definition: collector.h:197
void SetScanTypes(const std::vector< KICAD_T > &aTypes)
Record the list of KICAD_T types to consider for collection by the Inspect() function.
Definition: collector.h:213
void SetRefPos(const VECTOR2I &aRefPos)
Definition: collector.h:215
int CountType(KICAD_T aType)
Count the number of items matching aType.
Definition: collector.h:223
std::vector< KICAD_T > m_scanTypes
Definition: collector.h:245
CITER end() const
Definition: collector.h:78
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition: collector.h:111
std::vector< EDA_ITEM * > m_list
Definition: collector.h:242
int m_Threshold
Definition: collector.h:236
virtual EDA_ITEM * operator[](int aIndex) const
Used for read only access and returns the object at aIndex.
Definition: collector.h:183
void Remove(const EDA_ITEM *aItem)
Remove the item aItem (if exists in the collector).
Definition: collector.h:121
ITER end()
Definition: collector.h:76
bool HasAdditionalItems()
Test if the collector has heuristic backup items.
Definition: collector.h:134
CITER begin() const
Definition: collector.h:77
void Combine()
Re-combine the backup list into the main list of the collector.
Definition: collector.h:142
void Append(EDA_ITEM *item)
Add an item to the end of the list.
Definition: collector.h:101
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:98
INSPECT_RESULT
Definition: eda_item.h:44
std::function< INSPECT_RESULT(EDA_ITEM *aItem, void *aTestData) > INSPECTOR_FUNC
Used to inspect and possibly collect the (search) results of iterating over a list or tree of KICAD_T...
Definition: eda_item.h:88
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78