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 
54 class COLLECTOR
55 {
56 protected:
57  std::vector<EDA_ITEM*> m_list; // Primary list of most likely items
58  std::vector<EDA_ITEM*> m_backupList; // Secondary list with items removed by heuristics
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
75  // polymorphism.
76  m_inspector( [=]( EDA_ITEM* aItem, void* aTestData )
77  {
78  return this->Inspect( aItem, aTestData );
79  } ),
80  m_Threshold( 0 ),
81  m_MenuCancelled( false )
82  {
83  }
84 
85  virtual ~COLLECTOR() {}
86 
87  virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData )
88  {
89  return SEARCH_RESULT::QUIT;
90  };
91 
92  using ITER = std::vector<EDA_ITEM*>::iterator;
93  using CITER = std::vector<EDA_ITEM*>::const_iterator;
94 
95  ITER begin() { return m_list.begin(); }
96  ITER end() { return m_list.end(); }
97  CITER begin() const { return m_list.cbegin(); }
98  CITER end() const { return m_list.cend(); }
99 
104  int GetCount() const
105  {
106  return (int) m_list.size();
107  }
108 
113  void Empty()
114  {
115  m_list.clear();
116  }
117 
123  void Append( EDA_ITEM* item )
124  {
125  m_list.push_back( item );
126  }
127 
133  void Remove( int aIndex )
134  {
135  m_list.erase( m_list.begin() + aIndex );
136  }
137 
143  void Remove( const EDA_ITEM* aItem )
144  {
145  m_list.erase( std::remove_if( m_list.begin(), m_list.end(),
146  [&aItem]( const EDA_ITEM* aCandidate )
147  {
148  return aCandidate == aItem;
149  } ),
150  m_list.end() );
151  }
152 
158  {
159  return !m_backupList.empty();
160  }
161 
165  void Combine()
166  {
167  std::copy( m_backupList.begin(), m_backupList.end(), std::back_inserter( m_list ) );
168  m_backupList.clear();
169  }
170 
175  void Transfer( int aIndex )
176  {
177  m_backupList.push_back( m_list[aIndex] );
178  m_list.erase( m_list.begin() + aIndex );
179  }
180 
185  void Transfer( EDA_ITEM* aItem )
186  {
187  for( size_t i = 0; i < m_list.size(); i++ )
188  {
189  if( m_list[i] == aItem )
190  {
191  m_list.erase( m_list.begin() + i );
192  m_backupList.push_back( aItem );
193  return;
194  }
195  }
196  }
197 
204  virtual EDA_ITEM* operator[]( int aIndex ) const
205  {
206  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
207  return m_list[ aIndex ];
208 
209  return NULL;
210  }
211 
219  bool HasItem( const EDA_ITEM* aItem ) const
220  {
221  for( size_t i = 0; i < m_list.size(); i++ )
222  {
223  if( m_list[i] == aItem )
224  return true;
225  }
226 
227  return false;
228  }
229 
237  void SetScanTypes( const KICAD_T* scanTypes )
238  {
239  m_scanTypes = scanTypes;
240  }
241 
242  void SetRefPos( const wxPoint& aRefPos ) { m_refPos = aRefPos; }
243 
244  const EDA_RECT& GetBoundingBox() const { return m_refBox; }
245 
252  int CountType( KICAD_T aType )
253  {
254  int cnt = 0;
255  for( size_t i = 0; i < m_list.size(); i++ )
256  {
257  if( m_list[i]->Type() == aType )
258  cnt++;
259  }
260  return cnt;
261  }
262 };
263 
264 #endif // COLLECTOR_H
void Empty()
Function Empty sets the list to empty.
Definition: collector.h:113
int m_Threshold
Definition: collector.h:66
std::vector< EDA_ITEM * >::iterator ITER
Definition: collector.h:92
EDA_RECT m_refBox
Definition: collector.h:63
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:237
CITER end() const
Definition: collector.h:98
ITER begin()
Definition: collector.h:95
std::vector< EDA_ITEM * > m_list
Definition: collector.h:57
ITER end()
Definition: collector.h:96
int CountType(KICAD_T aType)
Function CountType counts the number of items matching aType.
Definition: collector.h:252
void Remove(int aIndex)
Function Remove removes the item at aIndex (first position is 0);.
Definition: collector.h:133
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:97
void Append(EDA_ITEM *item)
Function Append adds an item to the end of the list.
Definition: collector.h:123
bool m_MenuCancelled
Definition: collector.h:69
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:104
void Transfer(int aIndex)
Moves the item at aIndex (first position is 0) to the backup list.
Definition: collector.h:175
INSPECTOR_FUNC m_inspector
Definition: collector.h:61
#define NULL
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: eda_item.h:69
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:242
wxPoint m_refPos
Definition: collector.h:62
const EDA_RECT & GetBoundingBox() const
Definition: collector.h:244
bool HasItem(const EDA_ITEM *aItem) const
Function HasItem tests if aItem has already been collected.
Definition: collector.h:219
const KICAD_T * m_scanTypes
Definition: collector.h:60
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:93
wxString m_MenuTitle
Definition: collector.h:68
virtual ~COLLECTOR()
Definition: collector.h:85
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:204
bool HasAdditionalItems()
Test if the collector has heuristic backup items.
Definition: collector.h:157
void Remove(const EDA_ITEM *aItem)
Function Remove removes the item aItem (if exists in the collector).
Definition: collector.h:143
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: eda_item.h:148
COLLECTOR is an abstract class that will find and hold all the objects according to an inspection don...
Definition: collector.h:54
void Combine()
Re-combines the backup list into the main list of the collector.
Definition: collector.h:165
std::vector< EDA_ITEM * > m_backupList
Definition: collector.h:58
void Transfer(EDA_ITEM *aItem)
Moves the item aItem (if exists in the collector) to the backup list.
Definition: collector.h:185
COLLECTOR()
Definition: collector.h:72
SEARCH_RESULT
Definition: eda_item.h:40
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)
Definition: collector.h:87