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.

245 lines
7.3 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 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-2007 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 "fctsys.h"
  28. #include "base_struct.h" // SEARCH_RESULT
  29. #include "common.h" // GetTimeStamp()
  30. class EDA_BaseStruct;
  31. /**
  32. * Class COLLECTOR
  33. * is an abstract class that will find and hold all the objects according to
  34. * an inspection done by the Inspect() function which must be implemented by
  35. * any derived class. When Inspect() finds an object that it wants to collect,
  36. * i.e. one that it "likes", then it only has to do an Append( testItem )
  37. * on it to add it to its collection, but in all cases for the scan to continue,
  38. * Inspect() must return SEARCH_CONTINUE.
  39. *
  40. * Later, after collection, the user can iterate through all the objects
  41. * in the remembered collection using GetCount() and the [int] operator.
  42. */
  43. class COLLECTOR : public INSPECTOR
  44. {
  45. protected:
  46. /// Which object types to scan
  47. const KICAD_T* m_ScanTypes;
  48. /// A place to hold collected objects without taking ownership of their memory.
  49. std::vector<EDA_BaseStruct*> m_List;
  50. /// A point to test against, andt that was used to make the collection.
  51. wxPoint m_RefPos;
  52. /// A bounding box to test against, and that was used to make the collection.
  53. EDA_Rect m_RefBox;
  54. /// The time at which the collection was made.
  55. int m_TimeAtCollection;
  56. public:
  57. COLLECTOR()
  58. {
  59. m_ScanTypes = 0;
  60. }
  61. virtual ~COLLECTOR()
  62. {
  63. }
  64. /**
  65. * Function GetCount
  66. * returns the number of objects in the list
  67. */
  68. int GetCount() const
  69. {
  70. return (int) m_List.size();
  71. }
  72. /**
  73. * Function Empty
  74. * sets the list to empty
  75. */
  76. void Empty()
  77. {
  78. m_List.clear();
  79. }
  80. /**
  81. * Function Append
  82. * adds an item to the end of the list.
  83. * @param item An EDA_BaseStruct* to add.
  84. */
  85. void Append( EDA_BaseStruct* item )
  86. {
  87. m_List.push_back( item );
  88. }
  89. /**
  90. * Function Remove
  91. * removes the item at item_position (first position is 0);
  92. * @param ndx The index into the list.
  93. */
  94. void Remove( int ndx )
  95. {
  96. m_List.erase( m_List.begin() + ndx );
  97. }
  98. /**
  99. * Function operator[int]
  100. * is used for read only access and returns the object at index ndx.
  101. * @param ndx The index into the list.
  102. * @return EDA_BaseStruct* - or something derived from it, or NULL.
  103. */
  104. EDA_BaseStruct* operator[]( int ndx ) const
  105. {
  106. if( (unsigned)ndx < (unsigned)GetCount() ) // (unsigned) excludes ndx<0 also
  107. return m_List[ ndx ];
  108. return NULL;
  109. }
  110. /**
  111. * Function BasePtr
  112. * returns the address of the first element in the array. Only call this
  113. * if there is at least one element in the vector m_List, otherwise a
  114. * C++ exception should get thrown.
  115. */
  116. EDA_BaseStruct* const* BasePtr() const
  117. {
  118. return &m_List[0];
  119. }
  120. /**
  121. * Function SetScanTypes
  122. * records the list of KICAD_T types to consider for collection by
  123. * the Inspect() function.
  124. * @param scanTypes An array of KICAD_T, terminated by EOT. No copy is
  125. * is made of this array (so cannot come from caller's stack).
  126. */
  127. void SetScanTypes( const KICAD_T* scanTypes )
  128. {
  129. m_ScanTypes = scanTypes;
  130. }
  131. void SetTimeNow()
  132. {
  133. m_TimeAtCollection = GetTimeStamp();
  134. }
  135. int GetTime()
  136. {
  137. return m_TimeAtCollection;
  138. }
  139. void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
  140. const wxPoint& GetRefPos() const { return m_RefPos; }
  141. void SetBoundingBox( const EDA_Rect& aRefBox ) { m_RefBox = aRefBox; }
  142. const EDA_Rect& GetBoundingBox() const { return m_RefBox; }
  143. /**
  144. * Function IsSimilarPointAndTime
  145. * returns true if the given reference point is "similar" (defined here)
  146. * to the internal reference point and the current time is within a few
  147. * seconds of the internal m_TimeAtCollection.
  148. *
  149. * @param aRefPos A wxPoint to compare to.
  150. * @return bool - true if the point and time are similar, else false.
  151. */
  152. bool IsSimilarPointAndTime( const wxPoint& aRefPos )
  153. {
  154. const int distMax = 2; // adjust these here
  155. const int timeMax = 3; // seconds, I think
  156. int dx = abs( aRefPos.x - m_RefPos.x );
  157. int dy = abs( aRefPos.y - m_RefPos.y );
  158. if( dx <= distMax && dy <= distMax
  159. && GetTimeStamp()-m_TimeAtCollection <= timeMax )
  160. return true;
  161. else
  162. return false;
  163. }
  164. /**
  165. * Function Inspect
  166. * is the examining function within the INSPECTOR which is passed to the
  167. * Iterate function. It is used primarily for searching, but not limited to
  168. * that. It can also collect or modify the scanned objects.
  169. *
  170. * @param testItem An EDA_BaseStruct to examine.
  171. * @param testData is arbitrary data needed by the inspector to determine
  172. * if the EDA_BaseStruct under test meets its match criteria.
  173. * @return SEARCH_RESULT - SEARCH_QUIT if the Iterator is to stop the scan,
  174. * else SCAN_CONTINUE;
  175. *
  176. * implement in derived class:
  177. SEARCH_RESULT virtual Inspect( EDA_BaseStruct* testItem,
  178. const void* testData ) = 0;
  179. */
  180. /**
  181. * Function Collect
  182. * scans an EDA_BaseStruct using this class's Inspector method, which does
  183. * the collection.
  184. * @param container An EDA_BaseStruct to scan, including those items it contains.
  185. * @param aRefPos A wxPoint to use in hit-testing.
  186. *
  187. * example implementation, in derived class:
  188. *
  189. virtual void Collect( EDA_BaseStruct* container, const wxPoint& aRefPos )
  190. {
  191. example implementation:
  192. SetRefPos( aRefPos ); // remember where the snapshot was taken from
  193. Empty(); // empty the collection
  194. // visit the board with the INSPECTOR (me).
  195. container->Visit( this, // INSPECTOR* inspector
  196. NULL, // const void* testData,
  197. m_ScanTypes);
  198. SetTimeNow(); // when it was taken
  199. }
  200. */
  201. };
  202. #endif // COLLECTOR_H