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.

218 lines
6.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2024 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /*
  24. * @file design_block_info.h
  25. */
  26. #ifndef DESIGN_BLOCK_INFO_H_
  27. #define DESIGN_BLOCK_INFO_H_
  28. #include <kicommon.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 DESIGN_BLOCK_LIB_TABLE;
  38. class DESIGN_BLOCK_LIST;
  39. class DESIGN_BLOCK_LIST_IMPL;
  40. class PROGRESS_REPORTER;
  41. class wxTopLevelWindow;
  42. class KIWAY;
  43. class wxTextFile;
  44. /*
  45. * Helper class to handle the list of design blocks available in libraries. It stores
  46. * design block names, doc and keywords.
  47. *
  48. * This is a virtual class; its implementation lives in common/design_block_info_impl.cpp.
  49. * To get instances of these classes, see DESIGN_BLOCK_LIST::GetInstance().
  50. */
  51. class KICOMMON_API DESIGN_BLOCK_INFO : public LIB_TREE_ITEM
  52. {
  53. public:
  54. virtual ~DESIGN_BLOCK_INFO() {}
  55. // These two accessors do not have to call ensure_loaded(), because constructor
  56. // fills in these fields:
  57. const wxString& GetDesignBlockName() const { return m_dbname; }
  58. wxString GetLibNickname() const override { return m_nickname; }
  59. wxString GetName() const override { return m_dbname; }
  60. LIB_ID GetLIB_ID() const override { return LIB_ID( m_nickname, m_dbname ); }
  61. wxString GetDesc() override
  62. {
  63. ensure_loaded();
  64. return m_doc;
  65. }
  66. void SetDesc( const wxString& aDesc ) { m_doc = aDesc; }
  67. wxString GetKeywords()
  68. {
  69. ensure_loaded();
  70. return m_keywords;
  71. }
  72. std::vector<SEARCH_TERM> GetSearchTerms() override;
  73. int GetOrderNum()
  74. {
  75. ensure_loaded();
  76. return m_num;
  77. }
  78. /**
  79. * Test if the #DESIGN_BLOCK_INFO object was loaded from \a aLibrary.
  80. *
  81. * @param aLibrary is the nickname of the library to test.
  82. *
  83. * @return true if the #DESIGN_BLOCK_INFO object was loaded from \a aLibrary. Otherwise
  84. * false.
  85. */
  86. bool InLibrary( const wxString& aLibrary ) const;
  87. /**
  88. * Less than comparison operator, intended for sorting DESIGN_BLOCK_INFO objects
  89. */
  90. friend bool operator<( const DESIGN_BLOCK_INFO& lhs, const DESIGN_BLOCK_INFO& rhs );
  91. protected:
  92. void ensure_loaded()
  93. {
  94. if( !m_loaded )
  95. load();
  96. }
  97. /// lazily load stuff not filled in by constructor. This may throw IO_ERRORS.
  98. virtual void load(){};
  99. DESIGN_BLOCK_LIST* m_owner; ///< provides access to DESIGN_BLOCK_LIB_TABLE
  100. bool m_loaded;
  101. wxString m_nickname; ///< library as known in DESIGN_BLOCK_LIB_TABLE
  102. wxString m_dbname; ///< Module name.
  103. int m_num; ///< Order number in the display list.
  104. wxString m_doc; ///< Design block description.
  105. wxString m_keywords; ///< Design block keywords.
  106. };
  107. /**
  108. * Holds a list of #DESIGN_BLOCK_INFO objects, along with a list of IO_ERRORs or
  109. * PARSE_ERRORs that were thrown acquiring the DESIGN_BLOCK_INFOs.
  110. *
  111. * This is a virtual class; its implementation lives in common/design_block_info_impl.cpp.
  112. * To get instances of these classes, see DESIGN_BLOCK_LIST::GetInstance().
  113. */
  114. class KICOMMON_API DESIGN_BLOCK_LIST
  115. {
  116. public:
  117. typedef std::vector<std::unique_ptr<DESIGN_BLOCK_INFO>> DBILIST;
  118. typedef SYNC_QUEUE<std::unique_ptr<IO_ERROR>> ERRLIST;
  119. DESIGN_BLOCK_LIST() : m_lib_table( nullptr ) {}
  120. virtual ~DESIGN_BLOCK_LIST() {}
  121. /**
  122. * @return the number of items stored in list
  123. */
  124. unsigned GetCount() const { return m_list.size(); }
  125. /// Was forced to add this by modview_frame.cpp
  126. const DBILIST& GetList() const { return m_list; }
  127. /**
  128. * @return Clears the design block info cache
  129. */
  130. void Clear() { m_list.clear(); }
  131. /**
  132. * Get info for a design block by id.
  133. */
  134. DESIGN_BLOCK_INFO* GetDesignBlockInfo( const wxString& aDesignBlockName );
  135. /**
  136. * Get info for a design block by libNickname/designBlockName
  137. */
  138. DESIGN_BLOCK_INFO* GetDesignBlockInfo( const wxString& aLibNickname,
  139. const wxString& aDesignBlockName );
  140. /**
  141. * Get info for a design block by index.
  142. *
  143. * @param aIdx index of the given item.
  144. * @return the aIdx item in list.
  145. */
  146. DESIGN_BLOCK_INFO& GetItem( unsigned aIdx ) const { return *m_list[aIdx]; }
  147. unsigned GetErrorCount() const { return m_errors.size(); }
  148. std::unique_ptr<IO_ERROR> PopError()
  149. {
  150. std::unique_ptr<IO_ERROR> error;
  151. m_errors.pop( error );
  152. return error;
  153. }
  154. /**
  155. * Read all the design blocks provided by the combination of aTable and aNickname.
  156. *
  157. * @param aTable defines all the libraries.
  158. * @param aNickname is the library to read from, or if NULL means read all design blocks
  159. * from all known libraries in aTable.
  160. * @param aProgressReporter is an optional progress reporter. ReadDesignBlockFiles() will
  161. * use 2 phases within the reporter.
  162. * @return true if it ran to completion, else false if it aborted after some number of
  163. * errors. If true, it does not mean there were no errors, check GetErrorCount()
  164. * for that, should be zero to indicate success.
  165. */
  166. virtual bool ReadDesignBlockFiles( DESIGN_BLOCK_LIB_TABLE* aTable,
  167. const wxString* aNickname = nullptr,
  168. PROGRESS_REPORTER* aProgressReporter = nullptr ) = 0;
  169. DESIGN_BLOCK_LIB_TABLE* GetTable() const { return m_lib_table; }
  170. protected:
  171. DESIGN_BLOCK_LIB_TABLE* m_lib_table = nullptr; ///< no ownership
  172. DBILIST m_list;
  173. ERRLIST m_errors; ///< some can be PARSE_ERRORs also
  174. };
  175. #endif // DESIGN_BLOCK_INFO_H_