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.

348 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
  5. * Copyright (C) 2011-2019 KiCad Developers, see AUTHORS.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 _SCH_COLLECTORS_H_
  25. #define _SCH_COLLECTORS_H_
  26. #include <collector.h>
  27. #include <sch_item_struct.h>
  28. #include <sch_sheet_path.h>
  29. #include <dialogs/dialog_schematic_find.h>
  30. /**
  31. * Class SCH_COLLECTOR
  32. */
  33. class SCH_COLLECTOR : public COLLECTOR
  34. {
  35. public:
  36. static const KICAD_T AllItems[];
  37. static const KICAD_T LibItems[];
  38. static const KICAD_T EditableItems[];
  39. static const KICAD_T RotatableItems[];
  40. static const KICAD_T AllItemsButPins[];
  41. static const KICAD_T ComponentsOnly[];
  42. static const KICAD_T SheetsOnly[];
  43. static const KICAD_T SheetsAndSheetLabels[];
  44. /**
  45. * Constructor SCH_COLLECTOR
  46. */
  47. SCH_COLLECTOR( const KICAD_T* aScanTypes = SCH_COLLECTOR::AllItems ) :
  48. m_MenuCancelled( false )
  49. {
  50. SetScanTypes( aScanTypes );
  51. }
  52. /**
  53. * Overload COLLECTOR::operator[](int) to return a SCH_ITEM instead of an EDA_ITEM.
  54. *
  55. * @param aIndex The index into the list.
  56. * @return SCH_ITEM* at \a aIndex or NULL.
  57. */
  58. SCH_ITEM* operator[]( int aIndex ) const override
  59. {
  60. if( (unsigned)aIndex < (unsigned)GetCount() )
  61. return (SCH_ITEM*) m_List[ aIndex ];
  62. return NULL;
  63. }
  64. SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData ) override;
  65. /**
  66. * Function Collect
  67. * scans a EDA_ITEM using this class's Inspector method, which does the collection.
  68. * @param aItem A EDA_ITEM to scan.
  69. * @param aFilterList A list of #KICAD_T types with a terminating #EOT, that determines
  70. * what is to be collected and the priority order of the resulting
  71. * collection.
  72. * @param aPos A wxPoint to use in hit-testing.
  73. * @param aUnit A symbol unit filter (for symbol editor)
  74. * @param aConvert A DeMorgan filter (for symbol editor)
  75. */
  76. void Collect( EDA_ITEM* aItem, const KICAD_T aFilterList[], const wxPoint& aPos,
  77. int aUnit = 0, int aConvert = 0 );
  78. /**
  79. * Function IsCorner
  80. * tests if the collected items forms as corner of two line segments.
  81. * @return True if the collected items form a corner of two line segments.
  82. */
  83. bool IsCorner() const;
  84. /**
  85. * Function IsDraggableJunction
  86. * tests to see if the collected items form a draggable junction.
  87. * <p>
  88. * Daggable junctions are defined as:
  89. * <ul>
  90. * <li> The intersection of three or more wire end points. </li>
  91. * <li> The intersection of one or more wire end point and one wire mid point. </li>
  92. * <li> The crossing of two or more wire mid points and a junction. </li>
  93. * </ul>
  94. * </p>
  95. * @return True if the collection is a draggable junction.
  96. */
  97. bool IsDraggableJunction() const;
  98. public:
  99. int m_Unit; // Fixed symbol unit filter (for symbol editor)
  100. int m_Convert; // Fixed DeMorgan filter (for symbol editor)
  101. wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
  102. bool m_MenuCancelled; // Indicates selection disambiguation menu was cancelled
  103. };
  104. /**
  105. * Class SCH_FIND_COLLECTOR_DATA
  106. * is used as a data container for the associated item found by the #SCH_FIND_COLLECTOR
  107. * object.
  108. */
  109. class SCH_FIND_COLLECTOR_DATA
  110. {
  111. /// The position in drawing units of the found item.
  112. wxPoint m_position;
  113. /// The human readable sheet path @see SCH_SHEET_PATH::PathHumanReadable() of the found item.
  114. wxString m_sheetPath;
  115. /// The parent object if the item found is a child object.
  116. SCH_ITEM* m_parent;
  117. public:
  118. SCH_FIND_COLLECTOR_DATA( const wxPoint& aPosition = wxDefaultPosition,
  119. const wxString& aSheetPath = wxEmptyString,
  120. SCH_ITEM* aParent = NULL )
  121. : m_position( aPosition )
  122. , m_sheetPath( aSheetPath )
  123. , m_parent( aParent )
  124. { }
  125. wxPoint GetPosition() const { return m_position; }
  126. wxString GetSheetPath() const { return m_sheetPath; }
  127. SCH_ITEM* GetParent() const { return m_parent; }
  128. };
  129. /**
  130. * Class SCH_FIND_COLLECTOR
  131. * is used to iterate over all of the items in a schematic or sheet and collect all
  132. * the items that match the given search criteria.
  133. */
  134. class SCH_FIND_COLLECTOR : public COLLECTOR
  135. {
  136. /// Data associated with each found item.
  137. std::vector< SCH_FIND_COLLECTOR_DATA > m_data;
  138. /// The criteria used to test for matching items.
  139. SCH_FIND_REPLACE_DATA m_findReplaceData;
  140. /// The path of the sheet *currently* being iterated over.
  141. SCH_SHEET_PATH* m_currentSheetPath;
  142. /// The paths of the all the sheets in the collector.
  143. SCH_SHEET_PATHS m_sheetPaths;
  144. /// The current found item list index.
  145. int m_foundIndex;
  146. /// A flag to indicate that the schemtic has been modified and a new search must be
  147. /// performed even if the search criteria hasn't changed.
  148. bool m_forceSearch;
  149. /// last known library change hash, used to detect library changes which
  150. /// should trigger cache obsolescence.
  151. int m_lib_hash;
  152. /**
  153. * Function dump
  154. * is a helper to dump the items in the find list for debugging purposes.
  155. */
  156. #if defined(DEBUG)
  157. void dump();
  158. #endif
  159. public:
  160. /**
  161. * Constructor SCH_FIND_COLLECTOR
  162. */
  163. SCH_FIND_COLLECTOR( const KICAD_T* aScanTypes = SCH_COLLECTOR::AllItems )
  164. {
  165. SetScanTypes( aScanTypes );
  166. m_foundIndex = 0;
  167. SetForceSearch( false );
  168. m_currentSheetPath = NULL;
  169. m_lib_hash = 0;
  170. }
  171. void Empty()
  172. {
  173. m_foundIndex = 0;
  174. COLLECTOR::Empty();
  175. m_data.clear();
  176. }
  177. SCH_ITEM* GetItem( int ndx ) const;
  178. SCH_ITEM* operator[]( int ndx ) const override;
  179. void SetForceSearch( bool doSearch = true ) { m_forceSearch = doSearch; }
  180. int GetLibHash() const { return m_lib_hash; }
  181. void SetLibHash( int aHash ) { m_lib_hash = aHash; }
  182. int GetFoundIndex() const { return m_foundIndex; }
  183. void SetFoundIndex( int aIndex )
  184. {
  185. m_foundIndex = ( (unsigned) aIndex < m_data.size() ) ? aIndex : 0;
  186. }
  187. /**
  188. * Function PassedEnd
  189. * tests if #m_foundIndex is beyond the end of the list give the current
  190. * find/replace criterial in #m_findReplaceData.
  191. *
  192. * @return True if #m_foundIndex has crossed the end of the found item list.
  193. */
  194. bool PassedEnd() const;
  195. /**
  196. * Function UpdateIndex
  197. * updates the list index according to the current find and replace criteria.
  198. */
  199. void UpdateIndex();
  200. /**
  201. * Function GetFindData
  202. * returns the data associated with the item found at \a aIndex.
  203. *
  204. * @param aIndex The list index of the data to return.
  205. * @return The associated found item data at \a aIndex if \a aIndex is within the
  206. * list limits. Otherwise an empty data item will be returned.
  207. */
  208. SCH_FIND_COLLECTOR_DATA GetFindData( int aIndex );
  209. /**
  210. * Function IsSearchRequired
  211. * checks the current collector state against \a aFindReplaceData to see if a new search
  212. * needs to be performed to update the collector.
  213. *
  214. * @param aFindReplaceData A #SCH_FIND_REPLACE_DATA object containing the search criteria
  215. * to test for changes against the current search criteria.
  216. * @return True if \a aFindReplaceData would require a new search to be performaed or
  217. * the force search flag is true. Otherwise, false is returned.
  218. */
  219. bool IsSearchRequired( const SCH_FIND_REPLACE_DATA& aFindReplaceData )
  220. {
  221. return m_findReplaceData.ChangesCompare( aFindReplaceData ) || m_forceSearch ||
  222. (m_findReplaceData.IsWrapping() != aFindReplaceData.IsWrapping());
  223. }
  224. /**
  225. * Function GetText()
  226. * @return A wxString object containing the description of the item found at the
  227. * current index or a wxEmptyString if the list is empty or the index is
  228. * invalid.
  229. */
  230. wxString GetText( EDA_UNITS_T aUnits );
  231. /**
  232. * Function GetItem
  233. * returns the item and associated data of the current index.
  234. *
  235. * @param aFindData A reference to a #SCH_FIND_COLLECTOR_DATA object to place the
  236. * associated data for the current item into if the current item
  237. * index is valid.
  238. * @return A pointer to the current #EDA_ITEM in the list if the list index is valid
  239. * Otherwise NULL is returned and the \a aFindData object is not updated.
  240. */
  241. EDA_ITEM* GetItem( SCH_FIND_COLLECTOR_DATA& aFindData );
  242. /**
  243. * Function ReplaceItem
  244. * performs a string replace of the item at the current index.
  245. *
  246. * @return True if the text replace occurred otherwise false.
  247. */
  248. bool ReplaceItem( SCH_SHEET_PATH* aSheetPath = NULL );
  249. SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData ) override;
  250. /**
  251. * Update the replace string without changing anything else.
  252. */
  253. void SetReplaceString( const wxString &aReplaceString );
  254. /**
  255. * Function Collect
  256. * scans \a aSheetPath using this class's Inspector method for items matching
  257. * \a aFindReplaceData.
  258. *
  259. * @param aFindReplaceData A #SCH_FIND_REPLACE_DATA object containing the search criteria.
  260. * @param aSheetPath A pointer to a #SCH_SHEET_PATH object to test for matches. A NULL
  261. * value searches the entire schematic hierarchy.
  262. */
  263. void Collect( SCH_FIND_REPLACE_DATA& aFindReplaceData, SCH_SHEET_PATH* aSheetPath = NULL );
  264. void IncrementIndex() { m_foundIndex += 1; }
  265. };
  266. /**
  267. * Class TYPE_COLLECTOR
  268. * merely gathers up all SCH_ITEMs of a given set of KICAD_T type(s). It does
  269. * no hit-testing.
  270. *
  271. * @see class COLLECTOR
  272. */
  273. class SCH_TYPE_COLLECTOR : public SCH_COLLECTOR
  274. {
  275. public:
  276. /**
  277. * Function Inspect
  278. * is the examining function within the INSPECTOR which is passed to the Iterate function.
  279. *
  280. * @param testItem An EDA_ITEM to examine.
  281. * @param testData is not used in this class.
  282. * @return SEARCH_RESULT - SEARCH_QUIT if the Iterator is to stop the scan,
  283. * else SCAN_CONTINUE;
  284. */
  285. SEARCH_RESULT Inspect( EDA_ITEM* testItem, void* testData ) override;
  286. /**
  287. * Function Collect
  288. * scans a DLIST using this class's Inspector method, which does the collection.
  289. * @param aItem The head of a DLIST to scan.
  290. * @param aScanList The KICAD_Ts to gather up.
  291. */
  292. void Collect( SCH_ITEM* aItem, const KICAD_T aScanList[] );
  293. };
  294. #endif // _SCH_COLLECTORS_H_