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.

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