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.

310 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Wayne Stambaugh <stambaughw@gmail.com>
  5. * Copyright (C) 2016-2020 KiCad Developers, see change_log.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 _SYMBOL_LIB_TABLE_H_
  25. #define _SYMBOL_LIB_TABLE_H_
  26. #include <lib_table_base.h>
  27. #include <sch_io_mgr.h>
  28. #include <lib_id.h>
  29. #include <class_libentry.h>
  30. //class LIB_PART;
  31. class SYMBOL_LIB_TABLE_GRID;
  32. class DIALOG_SYMBOL_LIB_TABLE;
  33. /**
  34. * Hold a record identifying a symbol library accessed by the appropriate symbol library
  35. * #SCH_PLUGIN object in the #SYMBOL_LIB_TABLE.
  36. */
  37. class SYMBOL_LIB_TABLE_ROW : public LIB_TABLE_ROW
  38. {
  39. friend class SYMBOL_LIB_TABLE;
  40. public:
  41. typedef SCH_IO_MGR::SCH_FILE_T LIB_T;
  42. SYMBOL_LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aType,
  43. const wxString& aOptions = wxEmptyString,
  44. const wxString& aDescr = wxEmptyString ) :
  45. LIB_TABLE_ROW( aNick, aURI, aOptions, aDescr )
  46. {
  47. SetType( aType );
  48. SetEnabled( true );
  49. }
  50. SYMBOL_LIB_TABLE_ROW() :
  51. type( SCH_IO_MGR::SCH_KICAD )
  52. {
  53. SetEnabled( true );
  54. }
  55. bool operator==( const SYMBOL_LIB_TABLE_ROW& aRow ) const;
  56. bool operator!=( const SYMBOL_LIB_TABLE_ROW& aRow ) const { return !( *this == aRow ); }
  57. /**
  58. * Return the type of symbol library table represented by this row.
  59. */
  60. const wxString GetType() const override { return SCH_IO_MGR::ShowType( type ); }
  61. /**
  62. * Change the schematic plugin type represented by this row.
  63. */
  64. void SetType( const wxString& aType ) override;
  65. /**
  66. * Attempt to reload the library.
  67. * @return true if a reload was required.
  68. * @throws IO_ERROR if the reload was unsuccessful.
  69. */
  70. bool Refresh();
  71. protected:
  72. SYMBOL_LIB_TABLE_ROW( const SYMBOL_LIB_TABLE_ROW& aRow ) :
  73. LIB_TABLE_ROW( aRow ),
  74. type( aRow.type )
  75. {
  76. SetEnabled( aRow.GetIsEnabled() );
  77. }
  78. private:
  79. virtual LIB_TABLE_ROW* do_clone() const override
  80. {
  81. return new SYMBOL_LIB_TABLE_ROW( *this );
  82. }
  83. void setPlugin( SCH_PLUGIN* aPlugin )
  84. {
  85. plugin.set( aPlugin );
  86. }
  87. SCH_PLUGIN::SCH_PLUGIN_RELEASER plugin;
  88. LIB_T type;
  89. };
  90. class SYMBOL_LIB_TABLE : public LIB_TABLE
  91. {
  92. friend class SYMBOL_LIB_TABLE_GRID;
  93. friend class PANEL_SYM_LIB_TABLE;
  94. static int m_modifyHash; ///< helper for GetModifyHash()
  95. public:
  96. KICAD_T Type() override { return SYMBOL_LIB_TABLE_T; }
  97. static const char* PropPowerSymsOnly;
  98. static const char* PropNonPowerSymsOnly;
  99. virtual void Parse( LIB_TABLE_LEXER* aLexer ) override;
  100. virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const override;
  101. /**
  102. * Build a symbol library table by pre-pending this table fragment in front of
  103. * @a aFallBackTable. Loading of this table fragment is done by using Parse().
  104. *
  105. * @param aFallBackTable is another SYMBOL_LIB_TABLE which is searched only when
  106. * a row is not found in this table. No ownership is
  107. * taken of aFallBackTable.
  108. */
  109. SYMBOL_LIB_TABLE( SYMBOL_LIB_TABLE* aFallBackTable = NULL );
  110. /**
  111. * Return an SYMBOL_LIB_TABLE_ROW if \a aNickName is found in this table or in any chained
  112. * fallBack table fragment.
  113. *
  114. * The #SCH_PLUGIN is loaded and attached to the "plugin" fieldf the #SYMBOL_LIB_TABLE_ROW if
  115. * not already loaded.
  116. *
  117. * @param aNickName is the name of the row to find.
  118. *
  119. * @return the row found or NULL if \a aNickName was not found.
  120. *
  121. * @throw IO_ERROR if \a aNickName cannot be found.
  122. */
  123. SYMBOL_LIB_TABLE_ROW* FindRow( const wxString& aNickName );
  124. int GetModifyHash();
  125. //-----<PLUGIN API SUBSET, REBASED ON aNickname>---------------------------
  126. /**
  127. * Return a list of symbol alias names contained within the library given by @a aNickname.
  128. *
  129. * @param aNickname is a locator for the "library", it is a "name" in LIB_TABLE_ROW.
  130. * @param aAliasNames is a reference to an array for the alias names.
  131. * @param aPowerSymbolsOnly is a flag to enumerate only power symbols.
  132. *
  133. * @throw IO_ERROR if the library cannot be found or loaded.
  134. */
  135. void EnumerateSymbolLib( const wxString& aNickname, wxArrayString& aAliasNames,
  136. bool aPowerSymbolsOnly = false );
  137. void LoadSymbolLib( std::vector<LIB_PART*>& aAliasList, const wxString& aNickname,
  138. bool aPowerSymbolsOnly = false );
  139. /**
  140. * Load a #LIB_PART having @a aName from the library given by @a aNickname.
  141. *
  142. * @param aNickname is a locator for the "library", it is a "name" in #LIB_TABLE_ROW
  143. * @param aName is the name of the #LIB_PART to load.
  144. * @param aFlatten set to true to flatten derived parts.
  145. *
  146. * @return the symbol alias if found or NULL if not found.
  147. *
  148. * @throw IO_ERROR if the library cannot be found or read. No exception
  149. * is thrown in the case where \a aNickname cannot be found.
  150. */
  151. LIB_PART* LoadSymbol( const wxString& aNickname, const wxString& aName );
  152. LIB_PART* LoadSymbol( const LIB_ID& aLibId )
  153. {
  154. return LoadSymbol( aLibId.GetLibNickname(), aLibId.GetLibItemName() );
  155. }
  156. /**
  157. * The set of return values from SaveSymbol() below.
  158. */
  159. enum SAVE_T
  160. {
  161. SAVE_OK,
  162. SAVE_SKIPPED,
  163. };
  164. /**
  165. * Write @a aSymbol to an existing library given by @a aNickname.
  166. *
  167. * If a #LIB_PART by the same name already exists or there are any conflicting alias
  168. * names, the new #LIB_PART will silently overwrite any existing aliases and/or part
  169. * becaue libraries cannot have duplicate alias names. It is the responsibility of
  170. * the caller to check the library for conflicts before saving.
  171. *
  172. * @param aNickname is a locator for the "library", it is a "name" in LIB_TABLE_ROW
  173. * @param aSymbol is what to store in the library. The library owns the symbol after this
  174. * call.
  175. * @param aOverwrite when true means overwrite any existing symbol by the same name,
  176. * else if false means skip the write and return SAVE_SKIPPED.
  177. *
  178. * @return SAVE_T - SAVE_OK or SAVE_SKIPPED. If error saving, then IO_ERROR is thrown.
  179. *
  180. * @throw IO_ERROR if there is a problem saving the symbol.
  181. */
  182. SAVE_T SaveSymbol( const wxString& aNickname, const LIB_PART* aSymbol,
  183. bool aOverwrite = true );
  184. /**
  185. * Deletes the @a aSymbolName from the library given by @a aNickname.
  186. *
  187. * @param aNickname is a locator for the "library", it is a "name" in LIB_TABLE_ROW.
  188. *
  189. * @param aSymbolName is the name of a symbol to delete from the specified library.
  190. *
  191. * @throw IO_ERROR if there is a problem finding the footprint or the library, or deleting it.
  192. */
  193. void DeleteSymbol( const wxString& aNickname, const wxString& aSymbolName );
  194. /**
  195. * Return true if the library given by @a aNickname is writable.
  196. *
  197. * It is possible that some symbols libraries are read only because of where they are
  198. * installed.
  199. *
  200. * @param aNickname is the library nickname in the symbol library table.
  201. *
  202. * @throw IO_ERROR if no library at @a aNickname exists.
  203. */
  204. bool IsSymbolLibWritable( const wxString& aNickname );
  205. /**
  206. * Return true if the library given by @a aNickname was successfully loaded.
  207. *
  208. * @param aNickname is the library nickname in the symbol library table.
  209. *
  210. * @throw IO_ERROR if no library at @a aNickname exists.
  211. */
  212. bool IsSymbolLibLoaded( const wxString& aNickname );
  213. void DeleteSymbolLib( const wxString& aNickname );
  214. void CreateSymbolLib( const wxString& aNickname );
  215. //-----</PLUGIN API SUBSET, REBASED ON aNickname>---------------------------
  216. /**
  217. * Load a #LIB_PART having @a aFootprintId with possibly an empty library nickname.
  218. *
  219. * @param aId the library nickname and name of the symbol to load.
  220. *
  221. * @return the library symbol if found (the library owns it) or NULL if not found.
  222. *
  223. * @throw IO_ERROR if the library cannot be found or read. No exception
  224. * is thrown in the case where aId cannot be found.
  225. * @throw PARSE_ERROR if @a aId is not parsed OK.
  226. */
  227. LIB_PART* LoadSymbolWithOptionalNickname( const LIB_ID& aId );
  228. /**
  229. * Load the global symbol library table into \a aTable.
  230. *
  231. * This probably should be move into the application object when KiCad is changed
  232. * to a single process application. This is the least painful solution for the
  233. * time being.
  234. *
  235. * @param aTable the #SYMBOL_LIB_TABLE object to load.
  236. *
  237. * @return true if the global library table exists and is loaded properly.
  238. *
  239. * @throw IO_ERROR if an error occurs attempting to load the symbol library table.
  240. */
  241. static bool LoadGlobalTable( SYMBOL_LIB_TABLE& aTable );
  242. /**
  243. *
  244. * Fetch the global symbol library table file name.
  245. *
  246. * @return the platform specific global symbol library path and file name.
  247. */
  248. static wxString GetGlobalTableFileName();
  249. /**
  250. * Return the name of the environment variable used to hold the directory of locally
  251. * installed "KiCad sponsored" system symbol libraries.
  252. *
  253. * These can be either legacy or sweet format. The only thing special about this
  254. * particular environment variable is that it is set automatically by KiCad on
  255. * program start up, <b>if</b> it is not set already in the environment.
  256. */
  257. static const wxString GlobalPathEnvVariableName();
  258. static SYMBOL_LIB_TABLE& GetGlobalLibTable();
  259. static const wxString& GetSymbolLibTableFileName();
  260. };
  261. #endif // _SYMBOL_LIB_TABLE_H_