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-2020 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 
30 #ifndef COLLECTOR_H
31 #define COLLECTOR_H
32 
33 #include <vector>
34 
35 #include <eda_item.h> // SEARCH_RESULT
36 #include <eda_rect.h>
37 
38 
39 class EDA_ITEM;
40 
41 
53 class COLLECTOR
54 {
55 public:
57  m_Threshold( 0 ),
58  m_MenuCancelled( false ),
59  m_scanTypes( nullptr ),
60  // Inspect() is virtual so calling it from a class common inspector preserves
61  // polymorphism.
62  m_inspector( [=]( EDA_ITEM* aItem, void* aTestData )
63  {
64  return this->Inspect( aItem, aTestData );
65  } )
66  {
67  }
68 
69  virtual ~COLLECTOR() {}
70 
71  virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData )
72  {
73  return SEARCH_RESULT::QUIT;
74  };
75 
76  using ITER = std::vector<EDA_ITEM*>::iterator;
77  using CITER = std::vector<EDA_ITEM*>::const_iterator;
78 
79  ITER begin() { return m_list.begin(); }
80  ITER end() { return m_list.end(); }
81  CITER begin() const { return m_list.cbegin(); }
82  CITER end() const { return m_list.cend(); }
83 
87  int GetCount() const
88  {
89  return (int) m_list.size();
90  }
91 
95  void Empty()
96  {
97  m_list.clear();
98  }
99 
105  void Append( EDA_ITEM* item )
106  {
107  m_list.push_back( item );
108  }
109 
115  void Remove( int aIndex )
116  {
117  m_list.erase( m_list.begin() + aIndex );
118  }
119 
125  void Remove( const EDA_ITEM* aItem )
126  {
127  m_list.erase( std::remove_if( m_list.begin(), m_list.end(),
128  [&aItem]( const EDA_ITEM* aCandidate )
129  {
130  return aCandidate == aItem;
131  } ),
132  m_list.end() );
133  }
134 
141  {
142  return !m_backupList.empty();
143  }
144 
148  void Combine()
149  {
150  std::copy( m_backupList.begin(), m_backupList.end(), std::back_inserter( m_list ) );
151  m_backupList.clear();
152  }
153 
159  void Transfer( int aIndex )
160  {
161  m_backupList.push_back( m_list[aIndex] );
162  m_list.erase( m_list.begin() + aIndex );
163  }
164 
170  void Transfer( EDA_ITEM* aItem )
171  {
172  for( size_t i = 0; i < m_list.size(); i++ )
173  {
174  if( m_list[i] == aItem )
175  {
176  m_list.erase( m_list.begin() + i );
177  m_backupList.push_back( aItem );
178  return;
179  }
180  }
181  }
182 
189  virtual EDA_ITEM* operator[]( int aIndex ) const
190  {
191  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
192  return m_list[ aIndex ];
193 
194  return nullptr;
195  }
196 
203  bool HasItem( const EDA_ITEM* aItem ) const
204  {
205  for( size_t i = 0; i < m_list.size(); i++ )
206  {
207  if( m_list[i] == aItem )
208  return true;
209  }
210 
211  return false;
212  }
213 
220  void SetScanTypes( const KICAD_T* scanTypes )
221  {
222  m_scanTypes = scanTypes;
223  }
224 
225  void SetRefPos( const wxPoint& aRefPos ) { m_refPos = aRefPos; }
226 
227  const EDA_RECT& GetBoundingBox() const { return m_refBox; }
228 
235  int CountType( KICAD_T aType )
236  {
237  int cnt = 0;
238 
239  for( size_t i = 0; i < m_list.size(); i++ )
240  {
241  if( m_list[i]->Type() == aType )
242  cnt++;
243  }
244 
245  return cnt;
246  }
247 
248  int m_Threshold; // Hit-test threshold in internal units.
249 
250  wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
251  bool m_MenuCancelled; // Indicates selection disambiguation menu was canceled
252 
253 protected:
254  std::vector<EDA_ITEM*> m_list; // Primary list of most likely items
255  std::vector<EDA_ITEM*> m_backupList; // Secondary list with items removed by heuristics
256 
259  wxPoint m_refPos; // Reference position used to generate the collection.
260  EDA_RECT m_refBox; // Selection rectangle used to generate the collection.};
261 };
262 
263 #endif // COLLECTOR_H
void Empty()
Clear the list.
Definition: collector.h:95
int m_Threshold
Definition: collector.h:248
std::vector< EDA_ITEM * >::iterator ITER
Definition: collector.h:76
EDA_RECT m_refBox
Definition: collector.h:260
void SetScanTypes(const KICAD_T *scanTypes)
Record the list of KICAD_T types to consider for collection by the Inspect() function.
Definition: collector.h:220
CITER end() const
Definition: collector.h:82
ITER begin()
Definition: collector.h:79
std::vector< EDA_ITEM * > m_list
Definition: collector.h:254
ITER end()
Definition: collector.h:80
int CountType(KICAD_T aType)
Count the number of items matching aType.
Definition: collector.h:235
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition: collector.h:115
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
CITER begin() const
Definition: collector.h:81
void Append(EDA_ITEM *item)
Add an item to the end of the list.
Definition: collector.h:105
bool m_MenuCancelled
Definition: collector.h:251
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:87
void Transfer(int aIndex)
Move the item at aIndex (first position is 0) to the backup list.
Definition: collector.h:159
INSPECTOR_FUNC m_inspector
Definition: collector.h:258
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:70
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:225
wxPoint m_refPos
Definition: collector.h:259
const EDA_RECT & GetBoundingBox() const
Definition: collector.h:227
bool HasItem(const EDA_ITEM *aItem) const
Tests if aItem has already been collected.
Definition: collector.h:203
const KICAD_T * m_scanTypes
Definition: collector.h:257
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:77
wxString m_MenuTitle
Definition: collector.h:250
virtual ~COLLECTOR()
Definition: collector.h:69
virtual EDA_ITEM * operator[](int aIndex) const
Used for read only access and returns the object at aIndex.
Definition: collector.h:189
bool HasAdditionalItems()
Test if the collector has heuristic backup items.
Definition: collector.h:140
void Remove(const EDA_ITEM *aItem)
Remove the item aItem (if exists in the collector).
Definition: collector.h:125
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:150
An abstract class that will find and hold all the objects according to an inspection done by the Insp...
Definition: collector.h:53
void Combine()
Re-combine the backup list into the main list of the collector.
Definition: collector.h:148
std::vector< EDA_ITEM * > m_backupList
Definition: collector.h:255
void Transfer(EDA_ITEM *aItem)
Move aItem (if exists in the collector) to the backup list.
Definition: collector.h:170
COLLECTOR()
Definition: collector.h:56
SEARCH_RESULT
Definition: eda_item.h:41
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)
Definition: collector.h:71