KiCad PCB EDA Suite
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 <dick@softplc.com>
5  * Copyright (C) 2004-2021 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 #include <eda_rect.h>
32 
33 
34 class EDA_ITEM;
35 
36 
48 class COLLECTOR
49 {
50 public:
52  m_Threshold( 0 ),
53  m_MenuCancelled( false ),
54  m_scanTypes( nullptr ),
55  // Inspect() is virtual so calling it from a class common inspector preserves
56  // polymorphism.
57  m_inspector( [=]( EDA_ITEM* aItem, void* aTestData )
58  {
59  return this->Inspect( aItem, aTestData );
60  } )
61  {
62  }
63 
64  virtual ~COLLECTOR() {}
65 
66  virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData )
67  {
68  return SEARCH_RESULT::QUIT;
69  };
70 
71  using ITER = std::vector<EDA_ITEM*>::iterator;
72  using CITER = std::vector<EDA_ITEM*>::const_iterator;
73 
74  ITER begin() { return m_list.begin(); }
75  ITER end() { return m_list.end(); }
76  CITER begin() const { return m_list.cbegin(); }
77  CITER end() const { return m_list.cend(); }
78 
82  int GetCount() const
83  {
84  return (int) m_list.size();
85  }
86 
90  void Empty()
91  {
92  m_list.clear();
93  }
94 
100  void Append( EDA_ITEM* item )
101  {
102  m_list.push_back( item );
103  }
104 
110  void Remove( int aIndex )
111  {
112  m_list.erase( m_list.begin() + aIndex );
113  }
114 
120  void Remove( const EDA_ITEM* aItem )
121  {
122  alg::delete_if( m_list, [&aItem]( const EDA_ITEM* aCandidate )
123  {
124  return aCandidate == aItem;
125  } );
126  }
127 
134  {
135  return !m_backupList.empty();
136  }
137 
141  void Combine()
142  {
143  std::copy( m_backupList.begin(), m_backupList.end(), std::back_inserter( m_list ) );
144  m_backupList.clear();
145  }
146 
152  void Transfer( int aIndex )
153  {
154  m_backupList.push_back( m_list[aIndex] );
155  m_list.erase( m_list.begin() + aIndex );
156  }
157 
163  void Transfer( EDA_ITEM* aItem )
164  {
165  for( size_t i = 0; i < m_list.size(); i++ )
166  {
167  if( m_list[i] == aItem )
168  {
169  m_list.erase( m_list.begin() + i );
170  m_backupList.push_back( aItem );
171  return;
172  }
173  }
174  }
175 
182  virtual EDA_ITEM* operator[]( int aIndex ) const
183  {
184  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
185  return m_list[ aIndex ];
186 
187  return nullptr;
188  }
189 
196  bool HasItem( const EDA_ITEM* aItem ) const
197  {
198  for( size_t i = 0; i < m_list.size(); i++ )
199  {
200  if( m_list[i] == aItem )
201  return true;
202  }
203 
204  return false;
205  }
206 
213  void SetScanTypes( const KICAD_T* scanTypes )
214  {
215  m_scanTypes = scanTypes;
216  }
217 
218  void SetRefPos( const wxPoint& aRefPos ) { m_refPos = aRefPos; }
219 
220  const EDA_RECT& GetBoundingBox() const { return m_refBox; }
221 
228  int CountType( KICAD_T aType )
229  {
230  int cnt = 0;
231 
232  for( size_t i = 0; i < m_list.size(); i++ )
233  {
234  if( m_list[i]->Type() == aType )
235  cnt++;
236  }
237 
238  return cnt;
239  }
240 
241  int m_Threshold; // Hit-test threshold in internal units.
242 
243  wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
244  bool m_MenuCancelled; // Indicates selection disambiguation menu was canceled
245 
246 protected:
247  std::vector<EDA_ITEM*> m_list; // Primary list of most likely items
248  std::vector<EDA_ITEM*> m_backupList; // Secondary list with items removed by heuristics
249 
252  wxPoint m_refPos; // Reference position used to generate the collection.
253  EDA_RECT m_refBox; // Selection rectangle used to generate the collection.};
254 };
255 
256 #endif // COLLECTOR_H
void Empty()
Clear the list.
Definition: collector.h:90
int m_Threshold
Definition: collector.h:241
std::vector< EDA_ITEM * >::iterator ITER
Definition: collector.h:71
EDA_RECT m_refBox
Definition: collector.h:253
void SetScanTypes(const KICAD_T *scanTypes)
Record the list of KICAD_T types to consider for collection by the Inspect() function.
Definition: collector.h:213
CITER end() const
Definition: collector.h:77
ITER begin()
Definition: collector.h:74
std::vector< EDA_ITEM * > m_list
Definition: collector.h:247
ITER end()
Definition: collector.h:75
int CountType(KICAD_T aType)
Count the number of items matching aType.
Definition: collector.h:228
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition: collector.h:110
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
CITER begin() const
Definition: collector.h:76
void Append(EDA_ITEM *item)
Add an item to the end of the list.
Definition: collector.h:100
bool m_MenuCancelled
Definition: collector.h:244
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:82
void Transfer(int aIndex)
Move the item at aIndex (first position is 0) to the backup list.
Definition: collector.h:152
INSPECTOR_FUNC m_inspector
Definition: collector.h:251
std::function< SEARCH_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:69
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:218
wxPoint m_refPos
Definition: collector.h:252
const EDA_RECT & GetBoundingBox() const
Definition: collector.h:220
bool HasItem(const EDA_ITEM *aItem) const
Tests if aItem has already been collected.
Definition: collector.h:196
const KICAD_T * m_scanTypes
Definition: collector.h:250
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:72
wxString m_MenuTitle
Definition: collector.h:243
virtual ~COLLECTOR()
Definition: collector.h:64
virtual EDA_ITEM * operator[](int aIndex) const
Used for read only access and returns the object at aIndex.
Definition: collector.h:182
bool HasAdditionalItems()
Test if the collector has heuristic backup items.
Definition: collector.h:133
void Remove(const EDA_ITEM *aItem)
Remove the item aItem (if exists in the collector).
Definition: collector.h:120
Handle the component boundary box.
Definition: eda_rect.h:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
An abstract class that will find and hold all the objects according to an inspection done by the Insp...
Definition: collector.h:48
void Combine()
Re-combine the backup list into the main list of the collector.
Definition: collector.h:141
std::vector< EDA_ITEM * > m_backupList
Definition: collector.h:248
void Transfer(EDA_ITEM *aItem)
Move aItem (if exists in the collector) to the backup list.
Definition: collector.h:163
COLLECTOR()
Definition: collector.h:51
SEARCH_RESULT
Definition: eda_item.h:41
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)
Definition: collector.h:66
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:173