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.

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