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  timestamp_t m_TimeAtCollection; // The time at which the collection was made.
66 
67 public:
68  int m_Threshold; // Hit-test threshold in internal units.
69 
70  wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
71  bool m_MenuCancelled; // Indicates selection disambiguation menu was cancelled
72 
73 public:
75  m_ScanTypes( 0 ),
76  // Inspect() is virtual so calling it from a class common inspector preserves polymorphism.
77  m_inspector( [=] ( EDA_ITEM* aItem, void* aTestData ) { return this->Inspect( aItem, aTestData ); } ),
78  m_TimeAtCollection( 0 ),
79  m_Threshold( 0 ),
80  m_MenuCancelled( false )
81  {
82  }
83 
84  virtual ~COLLECTOR() {}
85 
86  virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData ) { return SEARCH_QUIT; };
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 
104  bool IsValidIndex( int aIndex )
105  {
106  return ( (unsigned) aIndex < m_List.size() );
107  }
108 
113  int GetCount() const
114  {
115  return (int) m_List.size();
116  }
117 
122  void Empty()
123  {
124  m_List.clear();
125  }
126 
132  void Append( EDA_ITEM* item )
133  {
134  m_List.push_back( item );
135  }
136 
142  void Remove( int aIndex )
143  {
144  m_List.erase( m_List.begin() + aIndex );
145  }
146 
152  void Remove( const EDA_ITEM* aItem )
153  {
154  for( size_t i = 0; i < m_List.size(); i++ )
155  {
156  if( m_List[i] == aItem )
157  {
158  m_List.erase( m_List.begin() + i);
159  return;
160  }
161  }
162  }
163 
170  virtual EDA_ITEM* operator[]( int aIndex ) const
171  {
172  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
173  return m_List[ aIndex ];
174 
175  return NULL;
176  }
177 
184  EDA_ITEM* const* BasePtr() const
185  {
186  return &m_List[0];
187  }
188 
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 
214  void SetScanTypes( const KICAD_T* scanTypes )
215  {
216  m_ScanTypes = scanTypes;
217  }
218 
219  void SetTimeNow()
220  {
222  }
223 
225  {
226  return m_TimeAtCollection;
227  }
228 
229  void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
230  const wxPoint& GetRefPos() const { return m_RefPos; }
231 
232  void SetBoundingBox( const EDA_RECT& aRefBox ) { m_RefBox = aRefBox; }
233  const EDA_RECT& GetBoundingBox() const { return m_RefBox; }
234 
244  bool IsSimilarPointAndTime( const wxPoint& aRefPos )
245  {
246  const int distMax = 2; // adjust these here
247  const timestamp_t timeMax = 3; // seconds
248 
249  int dx = abs( aRefPos.x - m_RefPos.x );
250  int dy = abs( aRefPos.y - m_RefPos.y );
251 
252  if( dx <= distMax && dy <= distMax &&
253  GetNewTimeStamp() - m_TimeAtCollection <= timeMax )
254  return true;
255  else
256  return false;
257  }
264  int CountType( KICAD_T aType )
265  {
266  int cnt = 0;
267  for( size_t i = 0; i < m_List.size(); i++ )
268  {
269  if( m_List[i]->Type() == aType )
270  cnt++;
271  }
272  return cnt;
273  }
274 
298 };
299 
300 #endif // COLLECTOR_H
void Empty()
Function Empty sets the list to empty.
Definition: collector.h:122
int m_Threshold
Definition: collector.h:68
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
const wxPoint & GetRefPos() const
Definition: collector.h:230
bool IsValidIndex(int aIndex)
Function IsValidIndex tests if aIndex is with the limits of the list of collected items.
Definition: collector.h:104
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:214
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:184
int CountType(KICAD_T aType)
Function CountType counts the number of items matching aType.
Definition: collector.h:264
#define abs(a)
Definition: auxiliary.h:84
void Remove(int aIndex)
Function Remove removes the item at aIndex (first position is 0);.
Definition: collector.h:142
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:132
bool m_MenuCancelled
Definition: collector.h:71
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:113
timestamp_t GetNewTimeStamp()
Definition: common.cpp:217
INSPECTOR_FUNC m_inspector
Definition: collector.h:61
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:229
const EDA_RECT & GetBoundingBox() const
Definition: collector.h:233
bool HasItem(const EDA_ITEM *aItem) const
Function HasItem tests if aItem has already been collected.
Definition: collector.h:196
bool IsSimilarPointAndTime(const wxPoint &aRefPos)
Function IsSimilarPointAndTime returns true if the given reference point is "similar" (defined here) ...
Definition: collector.h:244
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:89
wxString m_MenuTitle
Definition: collector.h:70
virtual ~COLLECTOR()
Definition: collector.h:84
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:170
void SetTimeNow()
Definition: collector.h:219
void Remove(const EDA_ITEM *aItem)
Function Remove removes the item aItem (if exists in the collector).
Definition: collector.h:152
size_t i
Definition: json11.cpp:597
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
Class COLLECTOR is an abstract class that will find and hold all the objects according to an inspecti...
Definition: collector.h:55
The common library.
void SetBoundingBox(const EDA_RECT &aRefBox)
Definition: collector.h:232
EDA_RECT m_RefBox
Definition: collector.h:63
wxPoint m_RefPos
Definition: collector.h:62
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:50
COLLECTOR()
Definition: collector.h:74
SEARCH_RESULT
Definition: base_struct.h:54
timestamp_t m_TimeAtCollection
Definition: collector.h:65
std::vector< EDA_ITEM * > m_List
Definition: collector.h:58
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)
Definition: collector.h:86
timestamp_t GetTime()
Definition: collector.h:224
const KICAD_T * m_ScanTypes
Definition: collector.h:60