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.

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