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-2021 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 25 #ifndef COLLECTOR_H 26 #define COLLECTOR_H 27 28 #include <vector> 29 #include <core/kicad_algo.h> 30 #include <eda_item.h> // SEARCH_RESULT 31 #include <eda_rect.h> 32 33 34 class EDA_ITEM; 35 36 37 /** 38 * An abstract class that will find and hold all the objects according to 39 * an inspection done by the Inspect() function which must be implemented by 40 * any derived class. 41 * 42 * When Inspect() finds an object that it wants to collect, i.e. one that it "likes", then 43 * it only has to do an Append( testItem )on it to add it to its collection, but in all cases 44 * for the scan to continue, Inspect() must return SEARCH_CONTINUE. Later, after collection, 45 * the user can iterate through all the objects in the remembered collection using GetCount() 46 * and the [int] operator. 47 */ 48 class COLLECTOR 49 { 50 public: COLLECTOR()51 COLLECTOR() : 52 m_Threshold( 0 ), 53 m_MenuCancelled( false ), 54 m_scanTypes( nullptr ), 55 // Inspect() is virtual so calling it from a class common inspector preserves 56 // polymorphism. 57 m_inspector( [=]( EDA_ITEM* aItem, void* aTestData ) 58 { 59 return this->Inspect( aItem, aTestData ); 60 } ) 61 { 62 } 63 ~COLLECTOR()64 virtual ~COLLECTOR() {} 65 Inspect(EDA_ITEM * aItem,void * aTestData)66 virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData ) 67 { 68 return SEARCH_RESULT::QUIT; 69 }; 70 71 using ITER = std::vector<EDA_ITEM*>::iterator; 72 using CITER = std::vector<EDA_ITEM*>::const_iterator; 73 begin()74 ITER begin() { return m_list.begin(); } end()75 ITER end() { return m_list.end(); } begin()76 CITER begin() const { return m_list.cbegin(); } end()77 CITER end() const { return m_list.cend(); } 78 79 /** 80 * Return the number of objects in the list. 81 */ GetCount()82 int GetCount() const 83 { 84 return (int) m_list.size(); 85 } 86 87 /** 88 * Clear the list. 89 */ Empty()90 void Empty() 91 { 92 m_list.clear(); 93 } 94 95 /** 96 * Add an item to the end of the list. 97 * 98 * @param item An EDA_ITEM* to add. 99 */ Append(EDA_ITEM * item)100 void Append( EDA_ITEM* item ) 101 { 102 m_list.push_back( item ); 103 } 104 105 /** 106 * Remove the item at \a aIndex (first position is 0). 107 * 108 * @param aIndex The index into the list. 109 */ Remove(int aIndex)110 void Remove( int aIndex ) 111 { 112 m_list.erase( m_list.begin() + aIndex ); 113 } 114 115 /** 116 * Remove the item aItem (if exists in the collector). 117 * 118 * @param aItem the item to be removed. 119 */ Remove(const EDA_ITEM * aItem)120 void Remove( const EDA_ITEM* aItem ) 121 { 122 alg::delete_if( m_list, [&aItem]( const EDA_ITEM* aCandidate ) 123 { 124 return aCandidate == aItem; 125 } ); 126 } 127 128 /** 129 * Test if the collector has heuristic backup items. 130 * 131 * @return true if Combine() can run to bring secondary items into the list. 132 */ HasAdditionalItems()133 bool HasAdditionalItems() 134 { 135 return !m_backupList.empty(); 136 } 137 138 /** 139 * Re-combine the backup list into the main list of the collector. 140 */ Combine()141 void Combine() 142 { 143 std::copy( m_backupList.begin(), m_backupList.end(), std::back_inserter( m_list ) ); 144 m_backupList.clear(); 145 } 146 147 /** 148 * Move the item at \a aIndex (first position is 0) to the backup list. 149 * 150 * @param aIndex The index into the list. 151 */ Transfer(int aIndex)152 void Transfer( int aIndex ) 153 { 154 m_backupList.push_back( m_list[aIndex] ); 155 m_list.erase( m_list.begin() + aIndex ); 156 } 157 158 /** 159 * Move \a aItem (if exists in the collector) to the backup list. 160 * 161 * @param aItem the item to be moved. 162 */ Transfer(EDA_ITEM * aItem)163 void Transfer( EDA_ITEM* aItem ) 164 { 165 for( size_t i = 0; i < m_list.size(); i++ ) 166 { 167 if( m_list[i] == aItem ) 168 { 169 m_list.erase( m_list.begin() + i ); 170 m_backupList.push_back( aItem ); 171 return; 172 } 173 } 174 } 175 176 /** 177 * Used for read only access and returns the object at \a aIndex. 178 * 179 * @param aIndex The index into the list. 180 * @return the object at \a aIndex something derived from it or NULL. 181 */ 182 virtual EDA_ITEM* operator[]( int aIndex ) const 183 { 184 if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also 185 return m_list[ aIndex ]; 186 187 return nullptr; 188 } 189 190 /** 191 * Tests if \a aItem has already been collected. 192 * 193 * @param aItem The EDA_ITEM* to be tested. 194 * @return True if \a aItem is already collected. 195 */ HasItem(const EDA_ITEM * aItem)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 207 /** 208 * Record the list of #KICAD_T types to consider for collection by the Inspect() function. 209 * 210 * @param scanTypes An array of KICAD_T, terminated by EOT. No copy is 211 * is made of this array (so cannot come from caller's stack). 212 */ SetScanTypes(const KICAD_T * scanTypes)213 void SetScanTypes( const KICAD_T* scanTypes ) 214 { 215 m_scanTypes = scanTypes; 216 } 217 SetRefPos(const wxPoint & aRefPos)218 void SetRefPos( const wxPoint& aRefPos ) { m_refPos = aRefPos; } 219 GetBoundingBox()220 const EDA_RECT& GetBoundingBox() const { return m_refBox; } 221 222 /** 223 * Count the number of items matching \a aType. 224 * 225 * @param aType type we are interested in. 226 * @return number of occurrences. 227 */ CountType(KICAD_T aType)228 int CountType( KICAD_T aType ) 229 { 230 int cnt = 0; 231 232 for( size_t i = 0; i < m_list.size(); i++ ) 233 { 234 if( m_list[i]->Type() == aType ) 235 cnt++; 236 } 237 238 return cnt; 239 } 240 241 int m_Threshold; // Hit-test threshold in internal units. 242 243 wxString m_MenuTitle; // The title of selection disambiguation menu (if needed) 244 bool m_MenuCancelled; // Indicates selection disambiguation menu was canceled 245 246 protected: 247 std::vector<EDA_ITEM*> m_list; // Primary list of most likely items 248 std::vector<EDA_ITEM*> m_backupList; // Secondary list with items removed by heuristics 249 250 const KICAD_T* m_scanTypes; 251 INSPECTOR_FUNC m_inspector; 252 wxPoint m_refPos; // Reference position used to generate the collection. 253 EDA_RECT m_refBox; // Selection rectangle used to generate the collection.}; 254 }; 255 256 #endif // COLLECTOR_H 257