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.

317 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-2021 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_SYMBOL;
  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. public:
  39. typedef SCH_IO_MGR::SCH_FILE_T LIB_T;
  40. SYMBOL_LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aType,
  41. const wxString& aOptions = wxEmptyString,
  42. const wxString& aDescr = wxEmptyString ) :
  43. LIB_TABLE_ROW( aNick, aURI, aOptions, aDescr )
  44. {
  45. SetType( aType );
  46. SetEnabled( true );
  47. }
  48. SYMBOL_LIB_TABLE_ROW() :
  49. type( SCH_IO_MGR::SCH_KICAD )
  50. {
  51. SetEnabled( true );
  52. }
  53. bool operator==( const SYMBOL_LIB_TABLE_ROW& aRow ) const;
  54. bool operator!=( const SYMBOL_LIB_TABLE_ROW& aRow ) const { return !( *this == aRow ); }
  55. /**
  56. * Return the type of symbol library table represented by this row.
  57. */
  58. const wxString GetType() const override { return SCH_IO_MGR::ShowType( type ); }
  59. /**
  60. * Change the schematic plugin type represented by this row.
  61. */
  62. void SetType( const wxString& aType ) override;
  63. /**
  64. * Attempt to reload the library.
  65. *
  66. * @return true if a reload was required.
  67. * @throw IO_ERROR if the reload was unsuccessful.
  68. */
  69. bool Refresh();
  70. bool SupportsSubLibraries() const { return plugin ? plugin->SupportsSubLibraries() : false; }
  71. void GetSubLibraryNames( std::vector<wxString>& aNames ) const;
  72. /**
  73. * @see SCH_PLUGIN::GetAvailableSymbolFields
  74. */
  75. void GetAvailableSymbolFields( std::vector<wxString>& aNames ) const
  76. {
  77. if( plugin )
  78. plugin->GetAvailableSymbolFields( aNames );
  79. }
  80. /**
  81. * @see SCH_PLUGIN::GetDefaultSymbolFields
  82. */
  83. void GetDefaultSymbolFields( std::vector<wxString>& aNames ) const
  84. {
  85. if( plugin )
  86. plugin->GetDefaultSymbolFields( aNames );
  87. }
  88. protected:
  89. SYMBOL_LIB_TABLE_ROW( const SYMBOL_LIB_TABLE_ROW& aRow ) :
  90. LIB_TABLE_ROW( aRow ),
  91. type( aRow.type )
  92. {
  93. SetEnabled( aRow.GetIsEnabled() );
  94. }
  95. private:
  96. friend class SYMBOL_LIB_TABLE;
  97. virtual LIB_TABLE_ROW* do_clone() const override
  98. {
  99. return new SYMBOL_LIB_TABLE_ROW( *this );
  100. }
  101. void setPlugin( SCH_PLUGIN* aPlugin )
  102. {
  103. plugin.set( aPlugin );
  104. }
  105. SCH_PLUGIN::SCH_PLUGIN_RELEASER plugin;
  106. LIB_T type;
  107. };
  108. class SYMBOL_LIB_TABLE : public LIB_TABLE
  109. {
  110. public:
  111. KICAD_T Type() override { return SYMBOL_LIB_TABLE_T; }
  112. static const char* PropPowerSymsOnly;
  113. static const char* PropNonPowerSymsOnly;
  114. virtual void Parse( LIB_TABLE_LEXER* aLexer ) override;
  115. virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const override;
  116. /**
  117. * Build a symbol library table by pre-pending this table fragment in front of
  118. * @a aFallBackTable. Loading of this table fragment is done by using Parse().
  119. *
  120. * @param aFallBackTable is another SYMBOL_LIB_TABLE which is searched only when
  121. * a row is not found in this table. No ownership is
  122. * taken of aFallBackTable.
  123. */
  124. SYMBOL_LIB_TABLE( SYMBOL_LIB_TABLE* aFallBackTable = nullptr );
  125. /**
  126. * Return an SYMBOL_LIB_TABLE_ROW if \a aNickName is found in this table or in any chained
  127. * fallBack table fragment.
  128. *
  129. * The #SCH_PLUGIN is loaded and attached to the "plugin" fieldf the #SYMBOL_LIB_TABLE_ROW if
  130. * not already loaded.
  131. *
  132. * @param aNickName is the name of the row to find.
  133. * @param aCheckIfEnabled is a flag to verify if the table entry is enabled or disabled.
  134. * @return the row found or NULL if \a aNickName was not found.
  135. */
  136. SYMBOL_LIB_TABLE_ROW* FindRow( const wxString& aNickName, bool aCheckIfEnabled = false );
  137. int GetModifyHash();
  138. //-----<PLUGIN API SUBSET, REBASED ON aNickname>---------------------------
  139. /**
  140. * Return a list of symbol alias names contained within 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 aAliasNames is a reference to an array for the alias names.
  144. * @param aPowerSymbolsOnly is a flag to enumerate only power symbols.
  145. * @throw IO_ERROR if the library cannot be found or loaded.
  146. */
  147. void EnumerateSymbolLib( const wxString& aNickname, wxArrayString& aAliasNames,
  148. bool aPowerSymbolsOnly = false );
  149. void LoadSymbolLib( std::vector<LIB_SYMBOL*>& aAliasList, const wxString& aNickname,
  150. bool aPowerSymbolsOnly = false );
  151. /**
  152. * Load a #LIB_SYMBOL having @a aName from the library given by @a aNickname.
  153. *
  154. * @param aNickname is a locator for the "library", it is a "name" in #LIB_TABLE_ROW
  155. * @param aName is the name of the #LIB_SYMBOL to load.
  156. * @param aFlatten set to true to flatten derived parts.
  157. * @return the symbol alias if found or NULL if not found.
  158. * @throw IO_ERROR if the library cannot be found or read. No exception
  159. * is thrown in the case where \a aNickname cannot be found.
  160. */
  161. LIB_SYMBOL* LoadSymbol( const wxString& aNickname, const wxString& aName );
  162. LIB_SYMBOL* LoadSymbol( const LIB_ID& aLibId )
  163. {
  164. return LoadSymbol( aLibId.GetLibNickname(), aLibId.GetLibItemName() );
  165. }
  166. /**
  167. * The set of return values from SaveSymbol() below.
  168. */
  169. enum SAVE_T
  170. {
  171. SAVE_OK,
  172. SAVE_SKIPPED,
  173. };
  174. /**
  175. * Write @a aSymbol to an existing library given by @a aNickname.
  176. *
  177. * If a #LIB_SYMBOL by the same name already exists or there are any conflicting alias
  178. * names, the new #LIB_SYMBOL will silently overwrite any existing aliases and/or part
  179. * because libraries cannot have duplicate alias names. It is the responsibility of
  180. * the caller to check the library for conflicts before saving.
  181. *
  182. * @param aNickname is a locator for the "library", it is a "name" in LIB_TABLE_ROW
  183. * @param aSymbol is what to store in the library. The library owns the symbol after this
  184. * call.
  185. * @param aOverwrite when true means overwrite any existing symbol by the same name,
  186. * else if false means skip the write and return SAVE_SKIPPED.
  187. * @return SAVE_T - SAVE_OK or SAVE_SKIPPED. If error saving, then IO_ERROR is thrown.
  188. * @throw IO_ERROR if there is a problem saving the symbol.
  189. */
  190. SAVE_T SaveSymbol( const wxString& aNickname, const LIB_SYMBOL* aSymbol,
  191. bool aOverwrite = true );
  192. /**
  193. * Deletes the @a aSymbolName from the library given by @a aNickname.
  194. *
  195. * @param aNickname is a locator for the "library", it is a "name" in LIB_TABLE_ROW.
  196. * @param aSymbolName is the name of a symbol to delete from the specified library.
  197. * @throw IO_ERROR if there is a problem finding the footprint or the library, or deleting it.
  198. */
  199. void DeleteSymbol( const wxString& aNickname, const wxString& aSymbolName );
  200. /**
  201. * Return true if the library given by @a aNickname is writable.
  202. *
  203. * It is possible that some symbols libraries are read only because of where they are
  204. * installed.
  205. *
  206. * @param aNickname is the library nickname in the symbol library table.
  207. * @throw IO_ERROR if no library at @a aNickname exists.
  208. */
  209. bool IsSymbolLibWritable( const wxString& aNickname );
  210. /**
  211. * Return true if the library given by @a aNickname was successfully loaded.
  212. *
  213. * @param aNickname is the library nickname in the symbol library table.
  214. * @throw IO_ERROR if no library at @a aNickname exists.
  215. */
  216. bool IsSymbolLibLoaded( const wxString& aNickname );
  217. void DeleteSymbolLib( const wxString& aNickname );
  218. void CreateSymbolLib( const wxString& aNickname );
  219. //-----</PLUGIN API SUBSET, REBASED ON aNickname>---------------------------
  220. /**
  221. * Load a #LIB_SYMBOL having @a aFootprintId with possibly an empty library nickname.
  222. *
  223. * @param aId the library nickname and name of the symbol to load.
  224. * @return the library symbol if found (the library owns it) or NULL if not found.
  225. * @throw IO_ERROR if the library cannot be found or read. No exception
  226. * is thrown in the case where aId cannot be found.
  227. * @throw PARSE_ERROR if @a aId is not parsed OK.
  228. */
  229. LIB_SYMBOL* LoadSymbolWithOptionalNickname( const LIB_ID& aId );
  230. /**
  231. * Load the global symbol library table into \a aTable.
  232. *
  233. * This probably should be move into the application object when KiCad is changed
  234. * to a single process application. This is the least painful solution for the
  235. * time being.
  236. *
  237. * @param aTable the #SYMBOL_LIB_TABLE object to load.
  238. * @return true if the global library table exists and is loaded properly.
  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. private:
  261. friend class SYMBOL_LIB_TABLE_GRID;
  262. friend class PANEL_SYM_LIB_TABLE;
  263. static int m_modifyHash; ///< helper for GetModifyHash()
  264. };
  265. #endif // _SYMBOL_LIB_TABLE_H_