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.

290 lines
11 KiB

9 months ago
9 months ago
9 months ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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. #ifndef DESIGN_BLOCK_LIB_TABLE_H_
  24. #define DESIGN_BLOCK_LIB_TABLE_H_
  25. #include <kicommon.h>
  26. #include <lib_table_base.h>
  27. #include <design_block_io.h>
  28. #include <design_block_info_impl.h>
  29. class DESIGN_BLOCK;
  30. class DESIGN_BLOCK_LIB_TABLE_GRID;
  31. /**
  32. * Hold a record identifying a library accessed by the appropriate design block library #PLUGIN
  33. * object in the #DESIGN_BLOCK_LIB_TABLE.
  34. */
  35. class KICOMMON_API DESIGN_BLOCK_LIB_TABLE_ROW : public LIB_TABLE_ROW
  36. {
  37. public:
  38. DESIGN_BLOCK_LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aType,
  39. const wxString& aOptions, const wxString& aDescr = wxEmptyString ) :
  40. LIB_TABLE_ROW( aNick, aURI, aOptions, aDescr )
  41. {
  42. SetType( aType );
  43. }
  44. DESIGN_BLOCK_LIB_TABLE_ROW() :
  45. LIB_TABLE_ROW(),
  46. type( DESIGN_BLOCK_IO_MGR::KICAD_SEXP )
  47. {
  48. }
  49. bool operator==( const DESIGN_BLOCK_LIB_TABLE_ROW& aRow ) const;
  50. bool operator!=( const DESIGN_BLOCK_LIB_TABLE_ROW& aRow ) const
  51. {
  52. return !( *this == aRow );
  53. }
  54. /**
  55. * Return the type of design block library table represented by this row.
  56. */
  57. const wxString GetType() const override { return DESIGN_BLOCK_IO_MGR::ShowType( type ); }
  58. /**
  59. * Change the type represented by this row.
  60. */
  61. void SetType( const wxString& aType ) override;
  62. DESIGN_BLOCK_IO_MGR::DESIGN_BLOCK_FILE_T GetFileType() { return type; }
  63. protected:
  64. DESIGN_BLOCK_LIB_TABLE_ROW( const DESIGN_BLOCK_LIB_TABLE_ROW& aRow ) :
  65. LIB_TABLE_ROW( aRow ),
  66. type( aRow.type )
  67. {
  68. }
  69. private:
  70. virtual LIB_TABLE_ROW* do_clone() const override
  71. {
  72. return new DESIGN_BLOCK_LIB_TABLE_ROW( *this );
  73. }
  74. void setPlugin( DESIGN_BLOCK_IO* aPlugin ) { plugin.reset( aPlugin ); }
  75. friend class DESIGN_BLOCK_LIB_TABLE;
  76. private:
  77. IO_RELEASER<DESIGN_BLOCK_IO> plugin;
  78. DESIGN_BLOCK_IO_MGR::DESIGN_BLOCK_FILE_T type;
  79. };
  80. class KICOMMON_API DESIGN_BLOCK_LIB_TABLE : public LIB_TABLE
  81. {
  82. public:
  83. PROJECT::ELEM ProjectElementType() override { return PROJECT::ELEM::DESIGN_BLOCK_LIB_TABLE; }
  84. virtual void Parse( LIB_TABLE_LEXER* aLexer ) override;
  85. virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const override;
  86. /**
  87. * Build a design block library table by pre-pending this table fragment in front of
  88. * @a aFallBackTable. Loading of this table fragment is done by using Parse().
  89. *
  90. * @param aFallBackTable is another DESIGN_BLOCK_LIB_TABLE which is searched only when a row
  91. * is not found in this table. No ownership is taken of
  92. * \a aFallBackTable.
  93. */
  94. DESIGN_BLOCK_LIB_TABLE( DESIGN_BLOCK_LIB_TABLE* aFallBackTable = nullptr );
  95. bool operator==( const DESIGN_BLOCK_LIB_TABLE& aFpTable ) const;
  96. bool operator!=( const DESIGN_BLOCK_LIB_TABLE& r ) const { return !( *this == r ); }
  97. /**
  98. * Return an #DESIGN_BLOCK_LIB_TABLE_ROW if \a aNickName is found in this table or in any
  99. * chained fall back table fragment.
  100. *
  101. * If \a aCheckIfEnabled is true, the library will be ignored even if it is disabled.
  102. * Otherwise, the row found will be returned even if entry is disabled.
  103. *
  104. * The #PLUGIN is loaded and attached to the "plugin" field of the #DESIGN_BLOCK_LIB_TABLE_ROW
  105. * if not already loaded.
  106. *
  107. * @param aNickName is the name of library nickname to find.
  108. * @param aCheckIfEnabled is the flag to check if the library found is enabled.
  109. * @return the library \a NickName if found.
  110. * @throw IO_ERROR if \a aNickName cannot be found.
  111. */
  112. const DESIGN_BLOCK_LIB_TABLE_ROW* FindRow( const wxString& aNickName,
  113. bool aCheckIfEnabled = false );
  114. /**
  115. * Return a list of design block names contained within the library given by @a aNickname.
  116. *
  117. * @param aDesignBlockNames is the list to fill with the design block names found in
  118. * \a aNickname.
  119. * @param aNickname is a locator for the "library", it is a "name" in LIB_TABLE_ROW.
  120. * @param aBestEfforts if true, don't throw on errors.
  121. *
  122. * @throw IO_ERROR if the library cannot be found, or design block cannot be loaded.
  123. */
  124. void DesignBlockEnumerate( wxArrayString& aDesignBlockNames, const wxString& aNickname,
  125. bool aBestEfforts );
  126. /**
  127. * Generate a hashed timestamp representing the last-mod-times of the library indicated
  128. * by \a aNickname, or all libraries if \a aNickname is NULL.
  129. */
  130. long long GenerateTimestamp( const wxString* aNickname );
  131. /**
  132. * Load a design block having @a aDesignBlockName from the library given by @a aNickname.
  133. *
  134. * @param aNickname is a locator for the "library", it is a "name" in #LIB_TABLE_ROW.
  135. * @param aDesignBlockName is the name of the design block to load.
  136. * @param aKeepUUID = true to keep initial items UUID, false to set new UUID
  137. * normally true if loaded in the design block editor, false
  138. * if loaded in the board editor. Used only in kicad_plugin
  139. * @return the design block if found caller owns it, else NULL if not found.
  140. *
  141. * @throw IO_ERROR if the library cannot be found or read. No exception
  142. * is thrown in the case where aDesignBlockName cannot be found.
  143. */
  144. DESIGN_BLOCK* DesignBlockLoad( const wxString& aNickname, const wxString& aDesignBlockName,
  145. bool aKeepUUID = false );
  146. /**
  147. * Indicates whether or not the given design block already exists in the given library.
  148. */
  149. bool DesignBlockExists( const wxString& aNickname, const wxString& aDesignBlockName );
  150. /**
  151. * A version of #DesignBlockLoad() for use after #DesignBlockEnumerate() for more efficient
  152. * cache management.
  153. *
  154. * The return value is const to allow it to return a reference to a cached item.
  155. */
  156. const DESIGN_BLOCK* GetEnumeratedDesignBlock( const wxString& aNickname,
  157. const wxString& aDesignBlockName );
  158. /**
  159. * The set of return values from DesignBlockSave() below.
  160. */
  161. enum SAVE_T
  162. {
  163. SAVE_OK,
  164. SAVE_SKIPPED,
  165. };
  166. /**
  167. * Write @a aDesignBlock to an existing library given by @a aNickname.
  168. *
  169. * If a design block by the same name already exists, it is replaced.
  170. *
  171. * @param aNickname is a locator for the "library", it is a "name" in #LIB_TABLE_ROW.
  172. * @param aDesignBlock is what to store in the library. The caller continues to own the
  173. * design block after this call.
  174. * @param aOverwrite when true means overwrite any existing design block by the same name,
  175. * else if false means skip the write and return SAVE_SKIPPED.
  176. * @return #SAVE_OK or #SAVE_SKIPPED. If error saving, then #IO_ERROR is thrown.
  177. *
  178. * @throw IO_ERROR if there is a problem saving.
  179. */
  180. SAVE_T DesignBlockSave( const wxString& aNickname, const DESIGN_BLOCK* aDesignBlock,
  181. bool aOverwrite = true );
  182. /**
  183. * Delete the @a aDesignBlockName from the library given by @a aNickname.
  184. *
  185. * @param aNickname is a locator for the "library", it is a "name" in #LIB_TABLE_ROW.
  186. * @param aDesignBlockName is the name of a design block to delete from the specified library.
  187. *
  188. * @throw IO_ERROR if there is a problem finding the design block or the library, or deleting
  189. * it.
  190. */
  191. void DesignBlockDelete( const wxString& aNickname, const wxString& aDesignBlockName );
  192. /**
  193. * Return true if the library given by @a aNickname is writable.
  194. *
  195. * Often system libraries are read only because of where they are installed.
  196. *
  197. * @throw IO_ERROR if no library at aLibraryPath exists.
  198. */
  199. bool IsDesignBlockLibWritable( const wxString& aNickname );
  200. void DesignBlockLibDelete( const wxString& aNickname );
  201. void DesignBlockLibCreate( const wxString& aNickname );
  202. /**
  203. * Load a design block having @a aDesignBlockId with possibly an empty nickname.
  204. *
  205. * @param aDesignBlockId the [nickname] and name of the design block to load.
  206. * @param aKeepUUID = true to keep initial items UUID, false to set new UUID
  207. * normally true if loaded in the design block editor, false
  208. * if loaded in the board editor
  209. * used only in kicad_plugin
  210. * @return the #DESIGN_BLOCK if found caller owns it, else NULL if not found.
  211. *
  212. * @throw IO_ERROR if the library cannot be found or read. No exception is
  213. * thrown in the case where \a aDesignBlockName cannot be found.
  214. * @throw PARSE_ERROR if @a aDesignBlockId is not parsed OK.
  215. */
  216. DESIGN_BLOCK* DesignBlockLoadWithOptionalNickname( const LIB_ID& aDesignBlockId,
  217. bool aKeepUUID = false );
  218. /**
  219. * Load the global design block library table into \a aTable.
  220. *
  221. * This probably should be move into the application object when KiCad is changed
  222. * to a single process application. This is the least painful solution for the
  223. * time being.
  224. *
  225. * @param aTable the #DESIGN_BLOCK_LIB_TABLE object to load.
  226. * @return true if the global library table exists and is loaded properly.
  227. * @throw IO_ERROR if an error occurs attempting to load the design block library
  228. * table.
  229. */
  230. static bool LoadGlobalTable( DESIGN_BLOCK_LIB_TABLE& aTable );
  231. static DESIGN_BLOCK_LIB_TABLE& GetGlobalLibTable();
  232. static DESIGN_BLOCK_LIST_IMPL& GetGlobalList();
  233. /**
  234. * @return the platform specific global design block library path and file name.
  235. */
  236. static wxString GetGlobalTableFileName();
  237. /**
  238. * Return the name of the environment variable used to hold the directory of
  239. * locally installed "KiCad sponsored" system design block libraries.
  240. *
  241. * These can be either legacy or pretty format. The only thing special about this
  242. * particular environment variable is that it is set automatically by KiCad on
  243. * program start up, <b>if</b> it is not set already in the environment.
  244. */
  245. static const wxString GlobalPathEnvVariableName();
  246. private:
  247. friend class DESIGN_BLOCK_LIB_TABLE_GRID;
  248. };
  249. #endif // DESIGN_BLOCK_LIB_TABLE_H_