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-2017 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 <base_struct.h> // SEARCH_RESULT
36 #include <common.h> // GetNewTimeStamp()
37 #include <eda_rect.h>
38 
39 
40 class EDA_ITEM;
41 
42 
55 class COLLECTOR
56 {
57 protected:
58  std::vector<EDA_ITEM*> m_List; // Primary list of most likely items
59  std::vector<EDA_ITEM*> m_BackupList; // Secondary list with items removed by heuristics
60 
63  wxPoint m_RefPos; // Reference position used to generate the collection.
64  EDA_RECT m_RefBox; // Selection rectangle used to generate the collection.
65 
66 public:
67  int m_Threshold; // Hit-test threshold in internal units.
68 
69  wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
70  bool m_MenuCancelled; // Indicates selection disambiguation menu was cancelled
71 
72 public:
74  m_ScanTypes( 0 ),
75  // Inspect() is virtual so calling it from a class common inspector preserves polymorphism.
76  m_inspector( [=] ( EDA_ITEM* aItem, void* aTestData ) { return this->Inspect( aItem, aTestData ); } ),
77  m_Threshold( 0 ),
78  m_MenuCancelled( false )
79  {
80  }
81 
82  virtual ~COLLECTOR() {}
83 
84  virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData )
85  {
86  return SEARCH_RESULT::QUIT;
87  };
88 
89  using ITER = std::vector<EDA_ITEM*>::iterator;
90  using CITER = std::vector<EDA_ITEM*>::const_iterator;
91 
92  ITER begin() { return m_List.begin(); }
93  ITER end() { return m_List.end(); }
94  CITER begin() const { return m_List.cbegin(); }
95  CITER end() const { return m_List.cend(); }
96 
101  int GetCount() const
102  {
103  return (int) m_List.size();
104  }
105 
110  void Empty()
111  {
112  m_List.clear();
113  }
114 
120  void Append( EDA_ITEM* item )
121  {
122  m_List.push_back( item );
123  }
124 
130  void Remove( int aIndex )
131  {
132  m_List.erase( m_List.begin() + aIndex );
133  }
134 
140  void Remove( const EDA_ITEM* aItem )
141  {
142  for( size_t i = 0; i < m_List.size(); i++ )
143  {
144  if( m_List[i] == aItem )
145  {
146  m_List.erase( m_List.begin() + i);
147  return;
148  }
149  }
150  }
151 
157  {
158  return !m_BackupList.empty();
159  }
160 
164  void Combine()
165  {
166  std::copy( m_BackupList.begin(), m_BackupList.end(), std::back_inserter( m_List ) );
167  m_BackupList.clear();
168  }
169 
174  void Transfer( int aIndex )
175  {
176  m_BackupList.push_back( m_List[aIndex] );
177  m_List.erase( m_List.begin() + aIndex );
178  }
179 
184  void Transfer( EDA_ITEM* aItem )
185  {
186  for( size_t i = 0; i < m_List.size(); i++ )
187  {
188  if( m_List[i] == aItem )
189  {
190  m_List.erase( m_List.begin() + i );
191  m_BackupList.push_back( aItem );
192  return;
193  }
194  }
195  }
196 
203  virtual EDA_ITEM* operator[]( int aIndex ) const
204  {
205  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
206  return m_List[ aIndex ];
207 
208  return NULL;
209  }
210 
217  EDA_ITEM* const* BasePtr() const
218  {
219  return &m_List[0];
220  }
221 
229  bool HasItem( const EDA_ITEM* aItem ) const
230  {
231  for( size_t i = 0; i < m_List.size(); i++ )
232  {
233  if( m_List[i] == aItem )
234  return true;
235  }
236 
237  return false;
238  }
239 
247  void SetScanTypes( const KICAD_T* scanTypes )
248  {
249  m_ScanTypes = scanTypes;
250  }
251 
252  void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
253 
254  const EDA_RECT& GetBoundingBox() const { return m_RefBox; }
255 
262  int CountType( KICAD_T aType )
263  {
264  int cnt = 0;
265  for( size_t i = 0; i < m_List.size(); i++ )
266  {
267  if( m_List[i]->Type() == aType )
268  cnt++;
269  }
270  return cnt;
271  }
272 
296 };
297 
298 #endif // COLLECTOR_H
void Empty()
Function Empty sets the list to empty.
Definition: collector.h:110
int m_Threshold
Definition: collector.h:67
std::vector< EDA_ITEM * >::iterator ITER
Definition: collector.h:89
std::function< SEARCH_RESULT(EDA_ITEM *aItem, void *aTestData) > INSPECTOR_FUNC
Typedef INSPECTOR is used to inspect and possibly collect the (search) results of iterating over a li...
Definition: base_struct.h:80
std::vector< EDA_ITEM * > m_BackupList
Definition: collector.h:59
void SetScanTypes(const KICAD_T *scanTypes)
Function SetScanTypes records the list of KICAD_T types to consider for collection by the Inspect() f...
Definition: collector.h:247
CITER end() const
Definition: collector.h:95
ITER begin()
Definition: collector.h:92
ITER end()
Definition: collector.h:93
EDA_ITEM *const * BasePtr() const
Function BasePtr returns the address of the first element in the array.
Definition: collector.h:217
int CountType(KICAD_T aType)
Function CountType counts the number of items matching aType.
Definition: collector.h:262
void Remove(int aIndex)
Function Remove removes the item at aIndex (first position is 0);.
Definition: collector.h:130
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
CITER begin() const
Definition: collector.h:94
void Append(EDA_ITEM *item)
Function Append adds an item to the end of the list.
Definition: collector.h:120
bool m_MenuCancelled
Definition: collector.h:70
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:101
void Transfer(int aIndex)
Moves the item at aIndex (first position is 0) to the backup list.
Definition: collector.h:174
INSPECTOR_FUNC m_inspector
Definition: collector.h:62
#define NULL
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:252
const EDA_RECT & GetBoundingBox() const
Definition: collector.h:254
bool HasItem(const EDA_ITEM *aItem) const
Function HasItem tests if aItem has already been collected.
Definition: collector.h:229
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:90
wxString m_MenuTitle
Definition: collector.h:69
virtual ~COLLECTOR()
Definition: collector.h:82
virtual EDA_ITEM * operator[](int aIndex) const
Function operator[int] is used for read only access and returns the object at aIndex.
Definition: collector.h:203
bool HasAdditionalItems()
Test if the collector has heuristic backup items.
Definition: collector.h:156
void Remove(const EDA_ITEM *aItem)
Function Remove removes the item aItem (if exists in the collector).
Definition: collector.h:140
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
COLLECTOR is an abstract class that will find and hold all the objects according to an inspection don...
Definition: collector.h:55
The common library.
void Combine()
Re-combines the backup list into the main list of the collector.
Definition: collector.h:164
EDA_RECT m_RefBox
Definition: collector.h:64
wxPoint m_RefPos
Definition: collector.h:63
void Transfer(EDA_ITEM *aItem)
Moves the item aItem (if exists in the collector) to the backup list.
Definition: collector.h:184
COLLECTOR()
Definition: collector.h:73
SEARCH_RESULT
Definition: base_struct.h:51
std::vector< EDA_ITEM * > m_List
Definition: collector.h:58
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)
Definition: collector.h:84
const KICAD_T * m_ScanTypes
Definition: collector.h:61