You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
7.0 KiB

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