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.

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