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 (C) 2004-2022 KiCad Developers, see change_log.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 <eda_item.h> // SEARCH_RESULT
31
32
33class EDA_ITEM;
34
35
48{
49public:
51 m_Threshold( 0 ),
52 m_MenuCancelled( false ),
53 m_scanTypes( {} )
54 {
55 // Inspect() is virtual so calling it from a class common inspector preserves
56 // polymorphism.
57 m_inspector = [this]( EDA_ITEM* aItem, void* aTestData )
58 {
59 return this->Inspect( aItem, aTestData );
60 };
61 }
62
63 virtual ~COLLECTOR() {}
64
65 virtual INSPECT_RESULT Inspect( EDA_ITEM* aTestItem, void* aTestData )
66 {
67 return INSPECT_RESULT::QUIT;
68 };
69
70 using ITER = std::vector<EDA_ITEM*>::iterator;
71 using CITER = std::vector<EDA_ITEM*>::const_iterator;
72
73 ITER begin() { return m_list.begin(); }
74 ITER end() { return m_list.end(); }
75 CITER begin() const { return m_list.cbegin(); }
76 CITER end() const { return m_list.cend(); }
77
81 int GetCount() const
82 {
83 return (int) m_list.size();
84 }
85
89 void Empty()
90 {
91 m_list.clear();
92 }
93
99 void Append( EDA_ITEM* item )
100 {
101 m_list.push_back( item );
102 }
103
109 void Remove( int aIndex )
110 {
111 m_list.erase( m_list.begin() + aIndex );
112 }
113
119 void Remove( const EDA_ITEM* aItem )
120 {
121 alg::delete_if( m_list, [&aItem]( const EDA_ITEM* aCandidate )
122 {
123 return aCandidate == aItem;
124 } );
125 }
126
133 {
134 return !m_backupList.empty();
135 }
136
140 void Combine()
141 {
142 std::copy( m_backupList.begin(), m_backupList.end(), std::back_inserter( m_list ) );
143 m_backupList.clear();
144 }
145
151 void Transfer( int aIndex )
152 {
153 m_backupList.push_back( m_list[aIndex] );
154 m_list.erase( m_list.begin() + aIndex );
155 }
156
162 void Transfer( EDA_ITEM* aItem )
163 {
164 for( size_t i = 0; i < m_list.size(); i++ )
165 {
166 if( m_list[i] == aItem )
167 {
168 m_list.erase( m_list.begin() + i );
169 m_backupList.push_back( aItem );
170 return;
171 }
172 }
173 }
174
181 virtual EDA_ITEM* operator[]( int aIndex ) const
182 {
183 if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
184 return m_list[ aIndex ];
185
186 return nullptr;
187 }
188
195 bool HasItem( const EDA_ITEM* aItem ) const
196 {
197 for( size_t i = 0; i < m_list.size(); i++ )
198 {
199 if( m_list[i] == aItem )
200 return true;
201 }
202
203 return false;
204 }
205
211 void SetScanTypes( const std::vector<KICAD_T>& aTypes ) { m_scanTypes = aTypes; }
212
213 void SetRefPos( const VECTOR2I& aRefPos ) { m_refPos = aRefPos; }
214
221 int CountType( KICAD_T aType )
222 {
223 int cnt = 0;
224
225 for( size_t i = 0; i < m_list.size(); i++ )
226 {
227 if( m_list[i]->Type() == aType )
228 cnt++;
229 }
230
231 return cnt;
232 }
233
234 int m_Threshold; // Hit-test threshold in internal units.
235
236 wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
237 bool m_MenuCancelled; // Indicates selection disambiguation menu was canceled
238
239protected:
240 std::vector<EDA_ITEM*> m_list; // Primary list of most likely items
241 std::vector<EDA_ITEM*> m_backupList; // Secondary list with items removed by heuristics
242
243 std::vector<KICAD_T> m_scanTypes;
245
246 VECTOR2I m_refPos; // Reference pos used to generate the collection.
247};
248
249#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:48
std::vector< EDA_ITEM * > m_backupList
Definition: collector.h:241
void Transfer(EDA_ITEM *aItem)
Move aItem (if exists in the collector) to the backup list.
Definition: collector.h:162
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:71
void Transfer(int aIndex)
Move the item at aIndex (first position is 0) to the backup list.
Definition: collector.h:151
bool m_MenuCancelled
Definition: collector.h:237
virtual INSPECT_RESULT Inspect(EDA_ITEM *aTestItem, void *aTestData)
Definition: collector.h:65
virtual ~COLLECTOR()
Definition: collector.h:63
INSPECTOR_FUNC m_inspector
Definition: collector.h:244
void Empty()
Clear the list.
Definition: collector.h:89
ITER begin()
Definition: collector.h:73
VECTOR2I m_refPos
Definition: collector.h:246
COLLECTOR()
Definition: collector.h:50
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:81
std::vector< EDA_ITEM * >::iterator ITER
Definition: collector.h:70
wxString m_MenuTitle
Definition: collector.h:236
bool HasItem(const EDA_ITEM *aItem) const
Tests if aItem has already been collected.
Definition: collector.h:195
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:211
void SetRefPos(const VECTOR2I &aRefPos)
Definition: collector.h:213
int CountType(KICAD_T aType)
Count the number of items matching aType.
Definition: collector.h:221
std::vector< KICAD_T > m_scanTypes
Definition: collector.h:243
CITER end() const
Definition: collector.h:76
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition: collector.h:109
std::vector< EDA_ITEM * > m_list
Definition: collector.h:240
int m_Threshold
Definition: collector.h:234
virtual EDA_ITEM * operator[](int aIndex) const
Used for read only access and returns the object at aIndex.
Definition: collector.h:181
void Remove(const EDA_ITEM *aItem)
Remove the item aItem (if exists in the collector).
Definition: collector.h:119
ITER end()
Definition: collector.h:74
bool HasAdditionalItems()
Test if the collector has heuristic backup items.
Definition: collector.h:132
CITER begin() const
Definition: collector.h:75
void Combine()
Re-combine the backup list into the main list of the collector.
Definition: collector.h:140
void Append(EDA_ITEM *item)
Add an item to the end of the list.
Definition: collector.h:99
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
INSPECT_RESULT
Definition: eda_item.h:43
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:79
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:174
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78