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.

272 lines
7.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011 Jean-Pierre Charras, <jp.charras@wanadoo.fr>
  5. * Copyright (C) 1992-2023 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. /*
  25. * @file footprint_info.h
  26. */
  27. #ifndef FOOTPRINT_INFO_H_
  28. #define FOOTPRINT_INFO_H_
  29. #include <boost/ptr_container/ptr_vector.hpp>
  30. #include <import_export.h>
  31. #include <ki_exception.h>
  32. #include <sync_queue.h>
  33. #include <lib_tree_item.h>
  34. #include <atomic>
  35. #include <functional>
  36. #include <memory>
  37. class FP_LIB_TABLE;
  38. class FOOTPRINT_LIST;
  39. class FOOTPRINT_LIST_IMPL;
  40. class PROGRESS_REPORTER;
  41. class wxTopLevelWindow;
  42. class KIWAY;
  43. class wxTextFile;
  44. /*
  45. * Helper class to handle the list of footprints available in libraries. It stores
  46. * footprint names, doc and keywords.
  47. *
  48. * This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
  49. * To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
  50. */
  51. class APIEXPORT FOOTPRINT_INFO : public LIB_TREE_ITEM
  52. {
  53. public:
  54. virtual ~FOOTPRINT_INFO()
  55. {
  56. }
  57. // These two accessors do not have to call ensure_loaded(), because constructor
  58. // fills in these fields:
  59. const wxString& GetFootprintName() const { return m_fpname; }
  60. wxString GetLibNickname() const override { return m_nickname; }
  61. wxString GetName() const override { return m_fpname; }
  62. LIB_ID GetLibId() const override
  63. {
  64. return LIB_ID( m_nickname, m_fpname );
  65. }
  66. wxString GetDescription() override
  67. {
  68. ensure_loaded();
  69. return m_doc;
  70. }
  71. wxString GetKeywords()
  72. {
  73. ensure_loaded();
  74. return m_keywords;
  75. }
  76. std::vector<SEARCH_TERM> GetSearchTerms() override;
  77. unsigned GetPadCount()
  78. {
  79. ensure_loaded();
  80. return m_pad_count;
  81. }
  82. unsigned GetUniquePadCount()
  83. {
  84. ensure_loaded();
  85. return m_unique_pad_count;
  86. }
  87. int GetOrderNum()
  88. {
  89. ensure_loaded();
  90. return m_num;
  91. }
  92. /**
  93. * Test if the #FOOTPRINT_INFO object was loaded from \a aLibrary.
  94. *
  95. * @param aLibrary is the nickname of the library to test.
  96. *
  97. * @return true if the #FOOTPRINT_INFO object was loaded from \a aLibrary. Otherwise
  98. * false.
  99. */
  100. bool InLibrary( const wxString& aLibrary ) const;
  101. /**
  102. * Less than comparison operator, intended for sorting FOOTPRINT_INFO objects
  103. */
  104. friend bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs );
  105. protected:
  106. void ensure_loaded()
  107. {
  108. if( !m_loaded )
  109. load();
  110. }
  111. /// lazily load stuff not filled in by constructor. This may throw IO_ERRORS.
  112. virtual void load() { };
  113. FOOTPRINT_LIST* m_owner; ///< provides access to FP_LIB_TABLE
  114. bool m_loaded;
  115. wxString m_nickname; ///< library as known in FP_LIB_TABLE
  116. wxString m_fpname; ///< Module name.
  117. int m_num; ///< Order number in the display list.
  118. unsigned m_pad_count; ///< Number of pads
  119. unsigned m_unique_pad_count; ///< Number of unique pads
  120. wxString m_doc; ///< Footprint description.
  121. wxString m_keywords; ///< Footprint keywords.
  122. };
  123. /**
  124. * Holds a list of #FOOTPRINT_INFO objects, along with a list of IO_ERRORs or
  125. * PARSE_ERRORs that were thrown acquiring the FOOTPRINT_INFOs.
  126. *
  127. * This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
  128. * To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
  129. */
  130. class APIEXPORT FOOTPRINT_LIST
  131. {
  132. public:
  133. typedef std::vector<std::unique_ptr<FOOTPRINT_INFO>> FPILIST;
  134. typedef SYNC_QUEUE<std::unique_ptr<IO_ERROR>> ERRLIST;
  135. FOOTPRINT_LIST() : m_lib_table( nullptr )
  136. {
  137. }
  138. virtual ~FOOTPRINT_LIST()
  139. {
  140. }
  141. virtual void WriteCacheToFile( const wxString& aFilePath ) {};
  142. virtual void ReadCacheFromFile( const wxString& aFilePath ){};
  143. /**
  144. * @return the number of items stored in list
  145. */
  146. unsigned GetCount() const
  147. {
  148. return m_list.size();
  149. }
  150. /// Was forced to add this by modview_frame.cpp
  151. const FPILIST& GetList() const
  152. {
  153. return m_list;
  154. }
  155. /**
  156. * @return Clears the footprint info cache
  157. */
  158. void Clear()
  159. {
  160. m_list.clear();
  161. }
  162. /**
  163. * Get info for a footprint by id.
  164. */
  165. FOOTPRINT_INFO* GetFootprintInfo( const wxString& aFootprintName );
  166. /**
  167. * Get info for a footprint by libNickname/footprintName
  168. */
  169. FOOTPRINT_INFO* GetFootprintInfo( const wxString& aLibNickname,
  170. const wxString& aFootprintName );
  171. /**
  172. * Get info for a footprint by index.
  173. *
  174. * @param aIdx index of the given item.
  175. * @return the aIdx item in list.
  176. */
  177. FOOTPRINT_INFO& GetItem( unsigned aIdx ) const
  178. {
  179. return *m_list[aIdx];
  180. }
  181. unsigned GetErrorCount() const
  182. {
  183. return m_errors.size();
  184. }
  185. std::unique_ptr<IO_ERROR> PopError()
  186. {
  187. std::unique_ptr<IO_ERROR> error;
  188. m_errors.pop( error );
  189. return error;
  190. }
  191. /**
  192. * Read all the footprints provided by the combination of aTable and aNickname.
  193. *
  194. * @param aTable defines all the libraries.
  195. * @param aNickname is the library to read from, or if NULL means read all footprints
  196. * from all known libraries in aTable.
  197. * @param aProgressReporter is an optional progress reporter. ReadFootprintFiles() will
  198. * use 2 phases within the reporter.
  199. * @return true if it ran to completion, else false if it aborted after some number of
  200. * errors. If true, it does not mean there were no errors, check GetErrorCount()
  201. * for that, should be zero to indicate success.
  202. */
  203. virtual bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
  204. PROGRESS_REPORTER* aProgressReporter = nullptr ) = 0;
  205. void DisplayErrors( wxTopLevelWindow* aCaller = nullptr );
  206. FP_LIB_TABLE* GetTable() const
  207. {
  208. return m_lib_table;
  209. }
  210. /**
  211. * Factory function to return a #FOOTPRINT_LIST via Kiway.
  212. *
  213. * This is not guaranteed to succeed and will return null if the kiface is not available.
  214. *
  215. * @param aKiway active kiway instance.
  216. */
  217. static FOOTPRINT_LIST* GetInstance( KIWAY& aKiway );
  218. protected:
  219. FP_LIB_TABLE* m_lib_table; ///< no ownership
  220. FPILIST m_list;
  221. ERRLIST m_errors; ///< some can be PARSE_ERRORs also
  222. };
  223. #endif // FOOTPRINT_INFO_H_