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.

421 lines
14 KiB

7 years ago
7 years ago
7 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-2022 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_mgr.h>
  35. #include <sch_screen.h>
  36. class LIB_SYMBOL;
  37. class SYMBOL_LIB;
  38. class PROGRESS_REPORTER;
  39. class SCH_PLUGIN;
  40. class SCH_BASE_FRAME;
  41. class SYMBOL_LIB_TABLE;
  42. class SYMBOL_LIB_TABLE_ROW;
  43. class LIB_LOGGER;
  44. /**
  45. * Class to handle modifications to the symbol libraries.
  46. */
  47. class SYMBOL_LIBRARY_MANAGER
  48. {
  49. public:
  50. SYMBOL_LIBRARY_MANAGER( SCH_BASE_FRAME& aFrame );
  51. virtual ~SYMBOL_LIBRARY_MANAGER();
  52. /**
  53. * Preloads all symbol libraries in the symbol library table using SYMBOL_ASYNC_LOADER.
  54. * Call before the first call to Sync() to get better performance.
  55. * @param aReporter is used to report progress of the load
  56. */
  57. void Preload( PROGRESS_REPORTER& aReporter );
  58. int GetHash() const;
  59. bool HasModifications() const;
  60. /**
  61. * Return a library hash value to determine if it has changed.
  62. *
  63. * For buffered libraries, it returns a number corresponding to the number of modifications.
  64. * For original libraries, hash is computed basing on the library URI. Returns -1 when the
  65. * requested library does not exist.
  66. */
  67. int GetLibraryHash( const wxString& aLibrary ) const;
  68. /**
  69. * Return the array of library names.
  70. */
  71. wxArrayString GetLibraryNames() const;
  72. /**
  73. * Find a single library within the (aggregate) library table.
  74. */
  75. SYMBOL_LIB_TABLE_ROW* GetLibrary( const wxString& aLibrary ) const;
  76. std::list<LIB_SYMBOL*> GetAliases( const wxString& aLibrary ) const;
  77. /**
  78. * Create an empty library and adds it to the library table. The library file is created.
  79. */
  80. bool CreateLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
  81. {
  82. return addLibrary( aFilePath, true, aTable );
  83. }
  84. /**
  85. * Add an existing library. The library is added to the library table as well.
  86. */
  87. bool AddLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
  88. {
  89. return addLibrary( aFilePath, false, aTable );
  90. }
  91. /**
  92. * Update the symbol buffer with a new version of the symbol.
  93. * The library buffer creates a copy of the symbol.
  94. * It is required to save the library to use the updated symbol in the schematic editor.
  95. */
  96. bool UpdateSymbol( LIB_SYMBOL* aSymbol, const wxString& aLibrary );
  97. /**
  98. * Update the symbol buffer with a new version of the symbol when the name has changed.
  99. * The old library buffer will be deleted and a new one created with the new name.
  100. */
  101. bool UpdateSymbolAfterRename( LIB_SYMBOL* aSymbol, const wxString& oldAlias,
  102. const wxString& aLibrary );
  103. /**
  104. * Update the library buffer with a new version of the library.
  105. */
  106. bool UpdateLibraryBuffer( const wxString& aLibrary );
  107. /**
  108. * Remove the symbol from the symbol buffer.
  109. * It is required to save the library to have the symbol removed in the schematic editor.
  110. */
  111. bool RemoveSymbol( const wxString& aName, const wxString& aLibrary );
  112. /**
  113. * Return either an alias of a working LIB_SYMBOL copy, or alias of the original symbol if there
  114. * is no working copy.
  115. */
  116. LIB_SYMBOL* GetAlias( const wxString& aAlias, const wxString& aLibrary ) const;
  117. /**
  118. * Return the symbol copy from the buffer. In case it does not exist yet, the copy is created.
  119. * #SYMBOL_LIBRARY_MANAGER retains the ownership.
  120. */
  121. LIB_SYMBOL* GetBufferedSymbol( const wxString& aAlias, const wxString& aLibrary );
  122. /**
  123. * Return the screen used to edit a specific symbol. #SYMBOL_LIBRARY_MANAGER retains the
  124. * ownership.
  125. */
  126. SCH_SCREEN* GetScreen( const wxString& aAlias, const wxString& aLibrary );
  127. /**
  128. * Return true if symbol with a specific alias exists in library (either original one or
  129. * buffered).
  130. */
  131. bool SymbolExists( const wxString& aAlias, const wxString& aLibrary ) const;
  132. /**
  133. * Return true if library exists. If \a aCheckEnabled is set, then the library must
  134. * also be enabled in the library table.
  135. */
  136. bool LibraryExists( const wxString& aLibrary, bool aCheckEnabled = false ) const;
  137. /**
  138. * Return true if the library was successfully loaded.
  139. */
  140. bool IsLibraryLoaded( const wxString& aLibrary ) const;
  141. /**
  142. * Return true if library has unsaved modifications.
  143. */
  144. bool IsLibraryModified( const wxString& aLibrary ) const;
  145. /**
  146. * Return true if symbol has unsaved modifications.
  147. */
  148. bool IsSymbolModified( const wxString& aAlias, const wxString& aLibrary ) const;
  149. void SetSymbolModified( const wxString& aAlias, const wxString& aLibrary );
  150. /**
  151. * Clear the modified flag for all symbols in a library.
  152. */
  153. bool ClearLibraryModified( const wxString& aLibrary ) const;
  154. /**
  155. * Clear the modified flag for a symbol.
  156. */
  157. bool ClearSymbolModified( const wxString& aAlias, const wxString& aLibrary ) const;
  158. /**
  159. * Return true if the library is stored in a read-only file.
  160. *
  161. * @return True on success, false otherwise.
  162. */
  163. bool IsLibraryReadOnly( const wxString& aLibrary ) const;
  164. /**
  165. * Save symbol changes to the library copy used by the schematic editor. Not it is not
  166. * necessarily saved to the file.
  167. *
  168. * @return True on success, false otherwise.
  169. */
  170. bool FlushSymbol( const wxString& aAlias, const wxString& aLibrary );
  171. /**
  172. * Save library to a file, including unsaved changes.
  173. *
  174. * @param aLibrary is the library name.
  175. * @param aFileName is the target file name.
  176. * @return True on success, false otherwise.
  177. */
  178. bool SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
  179. SCH_IO_MGR::SCH_FILE_T aFileType = SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY );
  180. /**
  181. * Revert unsaved changes for a symbolicular symbol.
  182. *
  183. * @return The LIB_ID of the reverted symbol (which may be different in the case
  184. * of a rename)
  185. */
  186. LIB_ID RevertSymbol( const wxString& aAlias, const wxString& aLibrary );
  187. /**
  188. * Revert unsaved changes for a symbolicular library.
  189. *
  190. * @return True on success, false otherwise.
  191. */
  192. bool RevertLibrary( const wxString& aLibrary );
  193. /**
  194. * Revert all pending changes.
  195. *
  196. * @return True if all changes successfully reverted.
  197. */
  198. bool RevertAll();
  199. /**
  200. * Return a library name that is not currently in use.
  201. * Used for generating names for new libraries.
  202. */
  203. wxString GetUniqueLibraryName() const;
  204. void GetRootSymbolNames( const wxString& aLibName, wxArrayString& aRootSymbolNames );
  205. /**
  206. * Check if symbol \a aSymbolName in library \a aLibraryName is a root symbol that
  207. * has derived symbols.
  208. *
  209. * @return true if \aSymbolName in \a aLibraryName has derived symbols.
  210. */
  211. bool HasDerivedSymbols( const wxString& aSymbolName, const wxString& aLibraryName );
  212. size_t GetLibraryCount() const;
  213. protected:
  214. virtual void OnDataChanged() const {}
  215. ///< Extract library name basing on the file name.
  216. static wxString getLibraryName( const wxString& aFilePath );
  217. ///< Helper function to add either existing or create new library
  218. bool addLibrary( const wxString& aFilePath, bool aCreate, SYMBOL_LIB_TABLE* aTable );
  219. ///< Return the current Symbol Library Table.
  220. SYMBOL_LIB_TABLE* symTable() const;
  221. ///< Class to store a working copy of a LIB_SYMBOL object and editor context.
  222. class SYMBOL_BUFFER
  223. {
  224. public:
  225. SYMBOL_BUFFER( LIB_SYMBOL* aSymbol = nullptr,
  226. std::unique_ptr<SCH_SCREEN> aScreen = nullptr );
  227. ~SYMBOL_BUFFER();
  228. LIB_SYMBOL* GetSymbol() const { return m_symbol; }
  229. void SetSymbol( LIB_SYMBOL* aSymbol );
  230. LIB_SYMBOL* GetOriginal() const { return m_original; }
  231. void SetOriginal( LIB_SYMBOL* aSymbol );
  232. bool IsModified() const;
  233. SCH_SCREEN* GetScreen() const { return m_screen.get(); }
  234. ///< Transfer the screen ownership
  235. std::unique_ptr<SCH_SCREEN> RemoveScreen()
  236. {
  237. return std::move( m_screen );
  238. }
  239. bool SetScreen( std::unique_ptr<SCH_SCREEN> aScreen )
  240. {
  241. bool ret = !!m_screen;
  242. m_screen = std::move( aScreen );
  243. return ret;
  244. }
  245. private:
  246. std::unique_ptr<SCH_SCREEN> m_screen;
  247. LIB_SYMBOL* m_symbol; // Working copy
  248. LIB_SYMBOL* m_original; // Initial state of the symbol
  249. };
  250. ///< Store a working copy of a library.
  251. class LIB_BUFFER
  252. {
  253. public:
  254. LIB_BUFFER( const wxString& aLibrary ) :
  255. m_libName( aLibrary ),
  256. m_hash( 1 )
  257. { }
  258. bool IsModified() const
  259. {
  260. if( !m_deleted.empty() )
  261. return true;
  262. for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : m_symbols )
  263. {
  264. if( symbolBuf->IsModified() )
  265. return true;
  266. }
  267. return false;
  268. }
  269. int GetHash() const { return m_hash; }
  270. ///< Return the working copy of a LIB_SYMBOL root object with specified alias.
  271. LIB_SYMBOL* GetSymbol( const wxString& aAlias ) const;
  272. ///< Create a new buffer to store a symbol. LIB_BUFFER takes ownership of aCopy.
  273. bool CreateBuffer( LIB_SYMBOL* aCopy, SCH_SCREEN* aScreen );
  274. ///< Update the buffered symbol with the contents of \a aCopy.
  275. bool UpdateBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf, LIB_SYMBOL* aCopy );
  276. bool DeleteBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf );
  277. void ClearDeletedBuffer()
  278. {
  279. m_deleted.clear();
  280. }
  281. ///< Save stored modifications to Symbol Lib Table. It may result in saving the symbol
  282. ///< to disk as well, depending on the row properties.
  283. bool SaveBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf, SYMBOL_LIB_TABLE* aLibTable );
  284. ///< Save stored modifications using a plugin. aBuffer decides whether the changes
  285. ///< should be cached or stored directly to the disk (for SCH_LEGACY_PLUGIN).
  286. bool SaveBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf, const wxString& aFileName,
  287. SCH_PLUGIN* aPlugin, bool aBuffer );
  288. ///< Return a symbol buffer with LIB_SYMBOL holding a symbolicular alias
  289. std::shared_ptr<SYMBOL_BUFFER> GetBuffer( const wxString& aAlias ) const;
  290. ///< Return all buffered symbols
  291. const std::deque< std::shared_ptr<SYMBOL_BUFFER> >& GetBuffers() const { return m_symbols; }
  292. /**
  293. * Check to see any symbols in the buffer are derived from a parent named \a aParentName.
  294. *
  295. * @param aParentName is the name of the parent to test.
  296. * @return true if any symbols are found derived from a symbol named \a aParent, otherwise
  297. * false.
  298. */
  299. bool HasDerivedSymbols( const wxString& aParentName ) const;
  300. /**
  301. * Fetch a list of root symbols names from the library buffer.
  302. *
  303. * @param aRootSymbolNames is a reference to a list to populate with root symbol names.
  304. */
  305. void GetRootSymbolNames( wxArrayString& aRootSymbolNames );
  306. /**
  307. * Fetch all of the symbols derived from a \a aSymbolName into \a aList.
  308. *
  309. * @param aSymbolName is the name of the symbol to search for derived symbols in this
  310. * buffer.
  311. * @param aList is the list of symbols names derived from \a aSymbolName.
  312. * @return a size_t count of the number of symbols derived from \a aSymbolName.
  313. */
  314. size_t GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList );
  315. private:
  316. /**
  317. * Remove all symbols derived from \a aParent from the library buffer.
  318. *
  319. * @param aParent is the #SYMBOL_BUFFER to check against.
  320. * @return the count of #SYMBOL_BUFFER objects removed from the library.
  321. */
  322. int removeChildSymbols( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf );
  323. std::deque< std::shared_ptr<SYMBOL_BUFFER> > m_symbols;
  324. ///< Buffer for deleted symbols until library is saved.
  325. std::deque< std::shared_ptr<SYMBOL_BUFFER> > m_deleted;
  326. const wxString m_libName; // Buffered library name
  327. int m_hash;
  328. };
  329. /**
  330. * Return a set of #LIB_SYMBOL objects belonging to the original library.
  331. */
  332. std::set<LIB_SYMBOL*> getOriginalSymbols( const wxString& aLibrary );
  333. /**
  334. * Return an existing library buffer or creates one to using Symbol Library Table to get
  335. * the original data.
  336. */
  337. LIB_BUFFER& getLibraryBuffer( const wxString& aLibrary );
  338. ///< The library buffers
  339. std::map<wxString, LIB_BUFFER> m_libs;
  340. SCH_BASE_FRAME& m_frame; ///< Parent frame
  341. LIB_LOGGER* m_logger;
  342. };
  343. #endif /* SYMBOL_LIBRARY_MANAGER_H */