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;
59 
62  wxPoint m_RefPos; // Reference position used to generate the collection.
63  EDA_RECT m_RefBox; // Selection rectangle used to generate the collection.
64 
65 public:
66  int m_Threshold; // Hit-test threshold in internal units.
67 
68  wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
69  bool m_MenuCancelled; // Indicates selection disambiguation menu was cancelled
70 
71 public:
73  m_ScanTypes( 0 ),
74  // Inspect() is virtual so calling it from a class common inspector preserves polymorphism.
75  m_inspector( [=] ( EDA_ITEM* aItem, void* aTestData ) { return this->Inspect( aItem, aTestData ); } ),
76  m_Threshold( 0 ),
77  m_MenuCancelled( false )
78  {
79  }
80 
81  virtual ~COLLECTOR() {}
82 
83  virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData )
84  {
85  return SEARCH_RESULT::QUIT;
86  };
87 
88  using ITER = std::vector<EDA_ITEM*>::iterator;
89  using CITER = std::vector<EDA_ITEM*>::const_iterator;
90 
91  ITER begin() { return m_List.begin(); }
92  ITER end() { return m_List.end(); }
93  CITER begin() const { return m_List.cbegin(); }
94  CITER end() const { return m_List.cend(); }
95 
100  int GetCount() const
101  {
102  return (int) m_List.size();
103  }
104 
109  void Empty()
110  {
111  m_List.clear();
112  }
113 
119  void Append( EDA_ITEM* item )
120  {
121  m_List.push_back( item );
122  }
123 
129  void Remove( int aIndex )
130  {
131  m_List.erase( m_List.begin() + aIndex );
132  }
133 
139  void Remove( const EDA_ITEM* aItem )
140  {
141  for( size_t i = 0; i < m_List.size(); i++ )
142  {
143  if( m_List[i] == aItem )
144  {
145  m_List.erase( m_List.begin() + i);
146  return;
147  }
148  }
149  }
150 
157  virtual EDA_ITEM* operator[]( int aIndex ) const
158  {
159  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
160  return m_List[ aIndex ];
161 
162  return NULL;
163  }
164 
171  EDA_ITEM* const* BasePtr() const
172  {
173  return &m_List[0];
174  }
175 
183  bool HasItem( const EDA_ITEM* aItem ) const
184  {
185  for( size_t i = 0; i < m_List.size(); i++ )
186  {
187  if( m_List[i] == aItem )
188  return true;
189  }
190 
191  return false;
192  }
193 
201  void SetScanTypes( const KICAD_T* scanTypes )
202  {
203  m_ScanTypes = scanTypes;
204  }
205 
206  void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
207 
208  const EDA_RECT& GetBoundingBox() const { return m_RefBox; }
209 
216  int CountType( KICAD_T aType )
217  {
218  int cnt = 0;
219  for( size_t i = 0; i < m_List.size(); i++ )
220  {
221  if( m_List[i]->Type() == aType )
222  cnt++;
223  }
224  return cnt;
225  }
226 
250 };
251 
252 #endif // COLLECTOR_H
void Empty()
Function Empty sets the list to empty.
Definition: collector.h:109
int m_Threshold
Definition: collector.h:66
std::vector< EDA_ITEM * >::iterator ITER
Definition: collector.h:88
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:83
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:201
CITER end() const
Definition: collector.h:94
ITER begin()
Definition: collector.h:91
ITER end()
Definition: collector.h:92
EDA_ITEM *const * BasePtr() const
Function BasePtr returns the address of the first element in the array.
Definition: collector.h:171
int CountType(KICAD_T aType)
Function CountType counts the number of items matching aType.
Definition: collector.h:216
void Remove(int aIndex)
Function Remove removes the item at aIndex (first position is 0);.
Definition: collector.h:129
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:93
void Append(EDA_ITEM *item)
Function Append adds an item to the end of the list.
Definition: collector.h:119
bool m_MenuCancelled
Definition: collector.h:69
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:100
INSPECTOR_FUNC m_inspector
Definition: collector.h:61
#define NULL
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:206
const EDA_RECT & GetBoundingBox() const
Definition: collector.h:208
bool HasItem(const EDA_ITEM *aItem) const
Function HasItem tests if aItem has already been collected.
Definition: collector.h:183
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:89
wxString m_MenuTitle
Definition: collector.h:68
virtual ~COLLECTOR()
Definition: collector.h:81
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:157
void Remove(const EDA_ITEM *aItem)
Function Remove removes the item aItem (if exists in the collector).
Definition: collector.h:139
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:166
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.
EDA_RECT m_RefBox
Definition: collector.h:63
wxPoint m_RefPos
Definition: collector.h:62
COLLECTOR()
Definition: collector.h:72
SEARCH_RESULT
Definition: base_struct.h:54
std::vector< EDA_ITEM * > m_List
Definition: collector.h:58
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)
Definition: collector.h:83
const KICAD_T * m_ScanTypes
Definition: collector.h:60