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, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef COLLECTOR_H
22#define COLLECTOR_H
23
24#include <vector>
25#include <core/kicad_algo.h>
26#include <algorithm>
27#include <eda_item.h> // SEARCH_RESULT
28
29
30class EDA_ITEM;
31
32
45{
46public:
48 m_Threshold( 0 ),
49 m_MenuCancelled( false ),
50 m_scanTypes( {} )
51 {
52 // Inspect() is virtual so calling it from a class common inspector preserves
53 // polymorphism.
55 [this]( EDA_ITEM* aItem, void* aTestData )
56 {
57 return this->Inspect( aItem, aTestData );
58 };
59 }
60
61 virtual ~COLLECTOR() {}
62
63 virtual INSPECT_RESULT Inspect( EDA_ITEM* aTestItem, void* aTestData )
64 {
66 };
67
68 using ITER = std::vector<EDA_ITEM*>::iterator;
69 using CITER = std::vector<EDA_ITEM*>::const_iterator;
70
71 ITER begin() { return m_list.begin(); }
72 ITER end() { return m_list.end(); }
73 CITER begin() const { return m_list.cbegin(); }
74 CITER end() const { return m_list.cend(); }
75
79 int GetCount() const
80 {
81 return (int) m_list.size();
82 }
83
87 void Empty()
88 {
89 m_list.clear();
90 }
91
97 void Append( EDA_ITEM* item )
98 {
99 m_list.push_back( item );
100 }
101
107 void Remove( int aIndex )
108 {
109 m_list.erase( m_list.begin() + aIndex );
110 }
111
117 void Remove( const EDA_ITEM* aItem )
118 {
119 std::erase_if( m_list, [&aItem]( const EDA_ITEM* aCandidate )
120 {
121 return aCandidate == aItem;
122 } );
123 }
124
131 {
132 return !m_backupList.empty();
133 }
134
138 void Combine()
139 {
140 std::copy( m_backupList.begin(), m_backupList.end(), std::back_inserter( m_list ) );
141 m_backupList.clear();
142 }
143
149 void Transfer( int aIndex )
150 {
151 m_backupList.push_back( m_list[aIndex] );
152 m_list.erase( m_list.begin() + aIndex );
153 }
154
160 void Transfer( EDA_ITEM* aItem )
161 {
162 for( size_t i = 0; i < m_list.size(); i++ )
163 {
164 if( m_list[i] == aItem )
165 {
166 m_list.erase( m_list.begin() + i );
167 m_backupList.push_back( aItem );
168 return;
169 }
170 }
171 }
172
179 virtual EDA_ITEM* operator[]( int aIndex ) const
180 {
181 if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
182 return m_list[ aIndex ];
183
184 return nullptr;
185 }
186
193 bool HasItem( const EDA_ITEM* aItem ) const
194 {
195 for( size_t i = 0; i < m_list.size(); i++ )
196 {
197 if( m_list[i] == aItem )
198 return true;
199 }
200
201 return false;
202 }
203
209 void SetScanTypes( const std::vector<KICAD_T>& aTypes ) { m_scanTypes = aTypes; }
210
211 void SetRefPos( const VECTOR2I& aRefPos ) { m_refPos = aRefPos; }
212
219 int CountType( KICAD_T aType )
220 {
221 int cnt = 0;
222
223 for( size_t i = 0; i < m_list.size(); i++ )
224 {
225 if( m_list[i]->Type() == aType )
226 cnt++;
227 }
228
229 return cnt;
230 }
231
232 int m_Threshold; // Hit-test threshold in internal units.
233
234 wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
235 bool m_MenuCancelled; // Indicates selection disambiguation menu was canceled
236
237protected:
238 std::vector<EDA_ITEM*> m_list; // Primary list of most likely items
239 std::vector<EDA_ITEM*> m_backupList; // Secondary list with items removed by heuristics
240
241 std::vector<KICAD_T> m_scanTypes;
243
244 VECTOR2I m_refPos; // Reference pos used to generate the collection.
245};
246
247#endif // COLLECTOR_H
std::vector< EDA_ITEM * > m_backupList
Definition collector.h:239
void Transfer(EDA_ITEM *aItem)
Move aItem (if exists in the collector) to the backup list.
Definition collector.h:160
std::vector< EDA_ITEM * >::const_iterator CITER
Definition collector.h:69
void Transfer(int aIndex)
Move the item at aIndex (first position is 0) to the backup list.
Definition collector.h:149
bool m_MenuCancelled
Definition collector.h:235
virtual INSPECT_RESULT Inspect(EDA_ITEM *aTestItem, void *aTestData)
Definition collector.h:63
virtual ~COLLECTOR()
Definition collector.h:61
INSPECTOR_FUNC m_inspector
Definition collector.h:242
void Empty()
Clear the list.
Definition collector.h:87
ITER begin()
Definition collector.h:71
VECTOR2I m_refPos
Definition collector.h:244
int GetCount() const
Return the number of objects in the list.
Definition collector.h:79
std::vector< EDA_ITEM * >::iterator ITER
Definition collector.h:68
wxString m_MenuTitle
Definition collector.h:234
bool HasItem(const EDA_ITEM *aItem) const
Tests if aItem has already been collected.
Definition collector.h:193
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:209
void SetRefPos(const VECTOR2I &aRefPos)
Definition collector.h:211
int CountType(KICAD_T aType)
Count the number of items matching aType.
Definition collector.h:219
std::vector< KICAD_T > m_scanTypes
Definition collector.h:241
CITER end() const
Definition collector.h:74
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:107
std::vector< EDA_ITEM * > m_list
Definition collector.h:238
int m_Threshold
Definition collector.h:232
virtual EDA_ITEM * operator[](int aIndex) const
Used for read only access and returns the object at aIndex.
Definition collector.h:179
void Remove(const EDA_ITEM *aItem)
Remove the item aItem (if exists in the collector).
Definition collector.h:117
ITER end()
Definition collector.h:72
bool HasAdditionalItems()
Test if the collector has heuristic backup items.
Definition collector.h:130
CITER begin() const
Definition collector.h:73
void Combine()
Re-combine the backup list into the main list of the collector.
Definition collector.h:138
void Append(EDA_ITEM *item)
Add an item to the end of the list.
Definition collector.h:97
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
INSPECT_RESULT
Definition eda_item.h:42
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:86
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683