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.

269 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 The 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 <core/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. int GetPinCount() override { return GetUniquePadCount(); }
  63. LIB_ID GetLIB_ID() const override
  64. {
  65. return LIB_ID( m_nickname, m_fpname );
  66. }
  67. wxString GetDesc() override
  68. {
  69. ensure_loaded();
  70. return m_doc;
  71. }
  72. wxString GetKeywords()
  73. {
  74. ensure_loaded();
  75. return m_keywords;
  76. }
  77. std::vector<SEARCH_TERM> GetSearchTerms() override;
  78. unsigned GetPadCount()
  79. {
  80. ensure_loaded();
  81. return m_pad_count;
  82. }
  83. unsigned GetUniquePadCount()
  84. {
  85. ensure_loaded();
  86. return m_unique_pad_count;
  87. }
  88. int GetOrderNum()
  89. {
  90. ensure_loaded();
  91. return m_num;
  92. }
  93. /**
  94. * Test if the #FOOTPRINT_INFO object was loaded from \a aLibrary.
  95. *
  96. * @param aLibrary is the nickname of the library to test.
  97. *
  98. * @return true if the #FOOTPRINT_INFO object was loaded from \a aLibrary. Otherwise
  99. * false.
  100. */
  101. bool InLibrary( const wxString& aLibrary ) const;
  102. /**
  103. * Less than comparison operator, intended for sorting FOOTPRINT_INFO objects
  104. */
  105. friend bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs );
  106. protected:
  107. void ensure_loaded()
  108. {
  109. if( !m_loaded )
  110. load();
  111. }
  112. /// lazily load stuff not filled in by constructor. This may throw IO_ERRORS.
  113. virtual void load() { };
  114. FOOTPRINT_LIST* m_owner; ///< provides access to FP_LIB_TABLE
  115. bool m_loaded;
  116. wxString m_nickname; ///< library as known in FP_LIB_TABLE
  117. wxString m_fpname; ///< Module name.
  118. int m_num; ///< Order number in the display list.
  119. unsigned m_pad_count; ///< Number of pads
  120. unsigned m_unique_pad_count; ///< Number of unique pads
  121. wxString m_doc; ///< Footprint description.
  122. wxString m_keywords; ///< Footprint keywords.
  123. };
  124. /**
  125. * Holds a list of #FOOTPRINT_INFO objects, along with a list of IO_ERRORs or
  126. * PARSE_ERRORs that were thrown acquiring the FOOTPRINT_INFOs.
  127. *
  128. * This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
  129. * To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
  130. */
  131. class APIEXPORT FOOTPRINT_LIST
  132. {
  133. public:
  134. FOOTPRINT_LIST() :
  135. 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 std::vector<std::unique_ptr<FOOTPRINT_INFO>>& GetList() const
  152. {
  153. return m_list;
  154. }
  155. /**
  156. * @return Clears the footprint info cache
  157. */
  158. virtual void Clear() = 0;
  159. /**
  160. * Get info for a footprint by id.
  161. */
  162. FOOTPRINT_INFO* GetFootprintInfo( const wxString& aFootprintName );
  163. /**
  164. * Get info for a footprint by libNickname/footprintName
  165. */
  166. FOOTPRINT_INFO* GetFootprintInfo( const wxString& aLibNickname,
  167. const wxString& aFootprintName );
  168. /**
  169. * Get info for a footprint by index.
  170. *
  171. * @param aIdx index of the given item.
  172. * @return the aIdx item in list.
  173. */
  174. FOOTPRINT_INFO& GetItem( unsigned aIdx ) const
  175. {
  176. return *m_list[aIdx];
  177. }
  178. unsigned GetErrorCount() const
  179. {
  180. return m_errors.size();
  181. }
  182. std::unique_ptr<IO_ERROR> PopError()
  183. {
  184. std::unique_ptr<IO_ERROR> error;
  185. m_errors.pop( error );
  186. return error;
  187. }
  188. /**
  189. * Read all the footprints provided by the combination of aTable and aNickname.
  190. *
  191. * @param aTable defines all the libraries.
  192. * @param aNickname is the library to read from, or if NULL means read all footprints
  193. * from all known libraries in aTable.
  194. * @param aProgressReporter is an optional progress reporter. ReadFootprintFiles() will
  195. * use 2 phases within the reporter.
  196. * @return true if it ran to completion, else false if it aborted after some number of
  197. * errors. If true, it does not mean there were no errors, check GetErrorCount()
  198. * for that, should be zero to indicate success.
  199. */
  200. virtual bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
  201. PROGRESS_REPORTER* aProgressReporter = nullptr ) = 0;
  202. void DisplayErrors( wxTopLevelWindow* aCaller = nullptr );
  203. FP_LIB_TABLE* GetTable() const
  204. {
  205. return m_lib_table;
  206. }
  207. /**
  208. * Factory function to return a #FOOTPRINT_LIST via Kiway.
  209. *
  210. * This is not guaranteed to succeed and will return null if the kiface is not available.
  211. *
  212. * @param aKiway active kiway instance.
  213. */
  214. static FOOTPRINT_LIST* GetInstance( KIWAY& aKiway );
  215. protected:
  216. FP_LIB_TABLE* m_lib_table; ///< no ownership
  217. std::vector<std::unique_ptr<FOOTPRINT_INFO>> m_list;
  218. SYNC_QUEUE<std::unique_ptr<IO_ERROR>> m_errors; ///< some can be PARSE_ERRORs also
  219. };
  220. #endif // FOOTPRINT_INFO_H_