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.

402 lines
13 KiB

6 years ago
  1. /*
  2. * This program source code file is symbol of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 CERN
  5. * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Maciej Suminski <maciej.suminski@cern.ch>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 3
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A SYMBOLICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * https://www.gnu.org/licenses/gpl-3.0.html
  22. * or you may search the http://www.gnu.org website for the version 3 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #ifndef SYMBOL_LIBRARY_MANAGER_H
  27. #define SYMBOL_LIBRARY_MANAGER_H
  28. #include <map>
  29. #include <list>
  30. #include <deque>
  31. #include <set>
  32. #include <memory>
  33. #include <wx/arrstr.h>
  34. #include <sch_io/sch_io_mgr.h>
  35. #include <sch_screen.h>
  36. class LIB_SYMBOL;
  37. class SYMBOL_LIB;
  38. class PROGRESS_REPORTER;
  39. class SCH_IO;
  40. class SCH_BASE_FRAME;
  41. class SYMBOL_LIB_TABLE;
  42. class SYMBOL_LIB_TABLE_ROW;
  43. class LIB_LOGGER;
  44. enum class SYMBOL_NAME_FILTER
  45. {
  46. ALL,
  47. ROOT_ONLY,
  48. DERIVED_ONLY
  49. };
  50. class SYMBOL_BUFFER
  51. {
  52. public:
  53. SYMBOL_BUFFER( LIB_SYMBOL* aSymbol = nullptr, std::unique_ptr<SCH_SCREEN> aScreen = nullptr );
  54. ~SYMBOL_BUFFER();
  55. LIB_SYMBOL* GetSymbol() const { return m_symbol; }
  56. void SetSymbol( LIB_SYMBOL* aSymbol );
  57. LIB_SYMBOL* GetOriginal() const { return m_original; }
  58. void SetOriginal( LIB_SYMBOL* aSymbol );
  59. bool IsModified() const;
  60. SCH_SCREEN* GetScreen() const { return m_screen.get(); }
  61. private:
  62. std::unique_ptr<SCH_SCREEN> m_screen;
  63. LIB_SYMBOL* m_symbol; // Working copy
  64. LIB_SYMBOL* m_original; // Initial state of the symbol
  65. };
  66. ///< Store a working copy of a library.
  67. class LIB_BUFFER
  68. {
  69. public:
  70. LIB_BUFFER( const wxString& aLibrary ) :
  71. m_libName( aLibrary ),
  72. m_hash( 1 )
  73. {}
  74. bool IsModified() const
  75. {
  76. if( !m_deleted.empty() )
  77. return true;
  78. for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : m_symbols )
  79. {
  80. if( symbolBuf->IsModified() )
  81. return true;
  82. }
  83. return false;
  84. }
  85. int GetHash() const { return m_hash; }
  86. ///< Return the working copy of a LIB_SYMBOL root object with specified alias.
  87. LIB_SYMBOL* GetSymbol( const wxString& aAlias ) const;
  88. ///< Create a new buffer to store a symbol. LIB_BUFFER takes ownership of aCopy.
  89. bool CreateBuffer( LIB_SYMBOL* aCopy, SCH_SCREEN* aScreen );
  90. ///< Update the buffered symbol with the contents of \a aCopy.
  91. bool UpdateBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf, LIB_SYMBOL* aCopy );
  92. bool DeleteBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf );
  93. void ClearDeletedBuffer() { m_deleted.clear(); }
  94. ///< Save stored modifications using a plugin. aBuffer decides whether the changes
  95. ///< should be cached or stored directly to the disk (for SCH_IO_KICAD_LEGACY).
  96. bool SaveBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf, const wxString& aFileName,
  97. SCH_IO* aPlugin, bool aBuffer );
  98. ///< Return a symbol buffer with LIB_SYMBOL holding a symbolicular alias
  99. std::shared_ptr<SYMBOL_BUFFER> GetBuffer( const wxString& aAlias ) const;
  100. ///< Return all buffered symbols
  101. const std::deque<std::shared_ptr<SYMBOL_BUFFER>>& GetBuffers() const { return m_symbols; }
  102. /**
  103. * Check to see any symbols in the buffer are derived from a parent named \a aParentName.
  104. *
  105. * @param aParentName is the name of the parent to test.
  106. * @return true if any symbols are found derived from a symbol named \a aParent, otherwise
  107. * false.
  108. */
  109. bool HasDerivedSymbols( const wxString& aParentName ) const;
  110. /**
  111. * Fetch a list of root symbols names from the library buffer.
  112. *
  113. * @param aRootSymbolNames is a reference to a list to populate with root symbol names.
  114. * @param aFilter is the symbol derivation type.
  115. */
  116. void GetSymbolNames( wxArrayString& aSymbolNames,
  117. SYMBOL_NAME_FILTER aFilter = SYMBOL_NAME_FILTER::ALL );
  118. /**
  119. * Fetch all of the symbols derived from a \a aSymbolName into \a aList.
  120. *
  121. * @param aSymbolName is the name of the symbol to search for derived symbols in this
  122. * buffer.
  123. * @param aList is the list of symbols names derived from \a aSymbolName.
  124. * @return a size_t count of the number of symbols derived from \a aSymbolName.
  125. */
  126. size_t GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList );
  127. private:
  128. /**
  129. * Remove all symbols derived from \a aParent from the library buffer.
  130. *
  131. * @param aParent is the #SYMBOL_BUFFER to check against.
  132. * @return the count of #SYMBOL_BUFFER objects removed from the library.
  133. */
  134. int removeChildSymbols( std::shared_ptr<SYMBOL_BUFFER>& aSymbolBuf );
  135. private:
  136. std::deque<std::shared_ptr<SYMBOL_BUFFER>> m_symbols;
  137. std::deque<std::shared_ptr<SYMBOL_BUFFER>> m_deleted; ///< Buffer for deleted symbols until
  138. ///< library is saved.
  139. const wxString m_libName; ///< Buffered library name
  140. int m_hash;
  141. };
  142. /**
  143. * Class to handle modifications to the symbol libraries.
  144. */
  145. class SYMBOL_LIBRARY_MANAGER
  146. {
  147. public:
  148. SYMBOL_LIBRARY_MANAGER( SCH_BASE_FRAME& aFrame );
  149. virtual ~SYMBOL_LIBRARY_MANAGER();
  150. /**
  151. * Preloads all symbol libraries in the symbol library table using SYMBOL_ASYNC_LOADER.
  152. * Call before the first call to Sync() to get better performance.
  153. * @param aReporter is used to report progress of the load
  154. */
  155. void Preload( PROGRESS_REPORTER& aReporter );
  156. int GetHash() const;
  157. bool HasModifications() const;
  158. /**
  159. * Return a library hash value to determine if it has changed.
  160. *
  161. * For buffered libraries, it returns a number corresponding to the number of modifications.
  162. * For original libraries, hash is computed basing on the library URI. Returns -1 when the
  163. * requested library does not exist.
  164. */
  165. int GetLibraryHash( const wxString& aLibrary ) const;
  166. /**
  167. * Return the array of library names.
  168. */
  169. wxArrayString GetLibraryNames() const;
  170. /**
  171. * Find a single library within the (aggregate) library table.
  172. */
  173. SYMBOL_LIB_TABLE_ROW* GetLibrary( const wxString& aLibrary ) const;
  174. std::list<LIB_SYMBOL*> GetAliases( const wxString& aLibrary ) const;
  175. /**
  176. * Create an empty library and adds it to the library table. The library file is created.
  177. */
  178. bool CreateLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
  179. {
  180. return addLibrary( aFilePath, true, aTable );
  181. }
  182. /**
  183. * Add an existing library. The library is added to the library table as well.
  184. */
  185. bool AddLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
  186. {
  187. return addLibrary( aFilePath, false, aTable );
  188. }
  189. /**
  190. * Update the symbol buffer with a new version of the symbol.
  191. * The library buffer creates a copy of the symbol.
  192. * It is required to save the library to use the updated symbol in the schematic editor.
  193. */
  194. bool UpdateSymbol( LIB_SYMBOL* aSymbol, const wxString& aLibrary );
  195. /**
  196. * Update the symbol buffer with a new version of the symbol when the name has changed.
  197. * The old library buffer will be deleted and a new one created with the new name.
  198. */
  199. bool UpdateSymbolAfterRename( LIB_SYMBOL* aSymbol, const wxString& oldAlias,
  200. const wxString& aLibrary );
  201. /**
  202. * Update the library buffer with a new version of the library.
  203. */
  204. bool UpdateLibraryBuffer( const wxString& aLibrary );
  205. /**
  206. * Remove the symbol from the symbol buffer.
  207. * It is required to save the library to have the symbol removed in the schematic editor.
  208. */
  209. bool RemoveSymbol( const wxString& aName, const wxString& aLibrary );
  210. /**
  211. * Return either an alias of a working LIB_SYMBOL copy, or alias of the original symbol if there
  212. * is no working copy.
  213. */
  214. LIB_SYMBOL* GetAlias( const wxString& aAlias, const wxString& aLibrary ) const;
  215. /**
  216. * Return the symbol copy from the buffer. In case it does not exist yet, the copy is created.
  217. * #SYMBOL_LIBRARY_MANAGER retains the ownership.
  218. */
  219. LIB_SYMBOL* GetBufferedSymbol( const wxString& aAlias, const wxString& aLibrary );
  220. /**
  221. * Return the screen used to edit a specific symbol. #SYMBOL_LIBRARY_MANAGER retains the
  222. * ownership.
  223. */
  224. SCH_SCREEN* GetScreen( const wxString& aAlias, const wxString& aLibrary );
  225. /**
  226. * Return true if symbol with a specific alias exists in library (either original one or
  227. * buffered).
  228. */
  229. bool SymbolExists( const wxString& aAlias, const wxString& aLibrary ) const;
  230. /**
  231. * Return true if library exists. If \a aCheckEnabled is set, then the library must
  232. * also be enabled in the library table.
  233. */
  234. bool LibraryExists( const wxString& aLibrary, bool aCheckEnabled = false ) const;
  235. /**
  236. * Return true if the library was successfully loaded.
  237. */
  238. bool IsLibraryLoaded( const wxString& aLibrary ) const;
  239. /**
  240. * Return true if library has unsaved modifications.
  241. */
  242. bool IsLibraryModified( const wxString& aLibrary ) const;
  243. /**
  244. * Return true if symbol has unsaved modifications.
  245. */
  246. bool IsSymbolModified( const wxString& aAlias, const wxString& aLibrary ) const;
  247. void SetSymbolModified( const wxString& aAlias, const wxString& aLibrary );
  248. /**
  249. * Clear the modified flag for all symbols in a library.
  250. */
  251. bool ClearLibraryModified( const wxString& aLibrary ) const;
  252. /**
  253. * Clear the modified flag for a symbol.
  254. */
  255. bool ClearSymbolModified( const wxString& aAlias, const wxString& aLibrary ) const;
  256. /**
  257. * Return true if the library is stored in a read-only file.
  258. *
  259. * @return True on success, false otherwise.
  260. */
  261. bool IsLibraryReadOnly( const wxString& aLibrary ) const;
  262. /**
  263. * Save library to a file, including unsaved changes.
  264. *
  265. * @param aLibrary is the library name.
  266. * @param aFileName is the target file name.
  267. * @return True on success, false otherwise.
  268. */
  269. bool SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
  270. SCH_IO_MGR::SCH_FILE_T aFileType = SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY );
  271. /**
  272. * Revert unsaved changes for a symbolicular symbol.
  273. *
  274. * @return The LIB_ID of the reverted symbol (which may be different in the case
  275. * of a rename)
  276. */
  277. LIB_ID RevertSymbol( const wxString& aAlias, const wxString& aLibrary );
  278. /**
  279. * Revert unsaved changes for a symbolicular library.
  280. *
  281. * @return True on success, false otherwise.
  282. */
  283. bool RevertLibrary( const wxString& aLibrary );
  284. /**
  285. * Revert all pending changes.
  286. *
  287. * @return True if all changes successfully reverted.
  288. */
  289. bool RevertAll();
  290. /**
  291. * Return a library name that is not currently in use.
  292. * Used for generating names for new libraries.
  293. */
  294. wxString GetUniqueLibraryName() const;
  295. void GetSymbolNames( const wxString& aLibName, wxArrayString& aSymbolNames,
  296. SYMBOL_NAME_FILTER aFilter = SYMBOL_NAME_FILTER::ALL );
  297. /**
  298. * Check if symbol \a aSymbolName in library \a aLibraryName is a root symbol that
  299. * has derived symbols.
  300. *
  301. * @return true if \aSymbolName in \a aLibraryName has derived symbols.
  302. */
  303. bool HasDerivedSymbols( const wxString& aSymbolName, const wxString& aLibraryName );
  304. size_t GetLibraryCount() const;
  305. protected:
  306. virtual void OnDataChanged() const {}
  307. ///< Extract library name basing on the file name.
  308. static wxString getLibraryName( const wxString& aFilePath );
  309. ///< Helper function to add either existing or create new library
  310. bool addLibrary( const wxString& aFilePath, bool aCreate, SYMBOL_LIB_TABLE* aTable );
  311. ///< Return the current Symbol Library Table.
  312. SYMBOL_LIB_TABLE* symTable() const;
  313. ///< Class to store a working copy of a LIB_SYMBOL object and editor context.
  314. /**
  315. * Return a set of #LIB_SYMBOL objects belonging to the original library.
  316. */
  317. std::set<LIB_SYMBOL*> getOriginalSymbols( const wxString& aLibrary );
  318. /**
  319. * Return an existing library buffer or creates one to using Symbol Library Table to get
  320. * the original data.
  321. */
  322. LIB_BUFFER& getLibraryBuffer( const wxString& aLibrary );
  323. protected:
  324. std::map<wxString, LIB_BUFFER> m_libs; ///< The library buffers
  325. SCH_BASE_FRAME& m_frame; ///< Parent frame
  326. LIB_LOGGER* m_logger;
  327. };
  328. #endif /* SYMBOL_LIBRARY_MANAGER_H */