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.

384 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Chris Pavlina <pavlina.chris@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 _LIB_CACHE_RESCUE_H_
  25. #define _LIB_CACHE_RESCUE_H_
  26. /* This code handles the case where an old schematic was made before
  27. * various changes were made, either to KiCad or to the libraries, and
  28. * the project needs to be recovered. The function of note is a member
  29. * of SCH_EDIT_FRAME, defined thus:
  30. *
  31. * bool SCH_EDIT_FRAME::RescueProject( bool aSilentIfNone );
  32. *
  33. * When this is called, a list of problematic symbols is compiled. If
  34. * this list is empty, then the function displays a notification and returns
  35. * (if aSilentIfNone is true, the notification is silenced).
  36. */
  37. #include <wx/string.h>
  38. #include <lib_symbol.h>
  39. #include <sch_io/kicad_legacy/sch_io_kicad_legacy.h>
  40. #include <class_draw_panel_gal.h>
  41. class LIB_SYMBOL;
  42. class SCH_SYMBOL;
  43. class RESCUER;
  44. class SCH_EDIT_FRAME;
  45. class SCH_IO_KICAD_LEGACY;
  46. class SCH_SHEET_PATH;
  47. class SCHEMATIC;
  48. enum RESCUE_TYPE
  49. {
  50. RESCUE_CONFLICT,
  51. RESCUE_CASE,
  52. };
  53. class RESCUE_CANDIDATE
  54. {
  55. public:
  56. virtual ~RESCUE_CANDIDATE() {}
  57. /**
  58. * Get the name that was originally requested in the schematic
  59. */
  60. virtual wxString GetRequestedName() const { return m_requested_name; }
  61. /**
  62. * Get the name we're proposing changing it to
  63. */
  64. virtual wxString GetNewName() const { return m_new_name; }
  65. /**
  66. * Get the part that can be loaded from the project cache, if possible, or
  67. * else NULL.
  68. */
  69. virtual LIB_SYMBOL* GetCacheCandidate() const { return nullptr; }
  70. /**
  71. * Get the part the would be loaded from the libraries, if possible, or else
  72. * NULL.
  73. */
  74. virtual LIB_SYMBOL* GetLibCandidate() const { return m_lib_candidate; }
  75. int GetUnit() const { return m_unit; }
  76. int GetConvert() const { return m_convert; }
  77. /**
  78. * Get a description of the action proposed, for displaying in the UI.
  79. */
  80. virtual wxString GetActionDescription() const = 0;
  81. /**
  82. * Perform the actual rescue action. If successful, this must log the rescue using
  83. * RESCUER::LogRescue to allow it to be reversed.
  84. * @return True on success.
  85. */
  86. virtual bool PerformAction( RESCUER* aRescuer ) = 0;
  87. protected:
  88. wxString m_requested_name;
  89. wxString m_new_name;
  90. LIB_SYMBOL* m_lib_candidate;
  91. int m_unit;
  92. int m_convert;
  93. };
  94. class RESCUE_CASE_CANDIDATE : public RESCUE_CANDIDATE
  95. {
  96. public:
  97. /**
  98. * Grab all possible RESCUE_CASE_CANDIDATE objects into a vector.
  99. *
  100. * @param aRescuer is the working RESCUER instance.
  101. * @param aCandidates is the vector the will hold the candidates.
  102. */
  103. static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
  104. /**
  105. * Create a RESCUE_CANDIDATE.
  106. *
  107. * @param aRequestedName us the name the schematic asks for.
  108. * @param aNewName is the name we want to change it to.
  109. * @param aLibCandidate is the part that will give us.
  110. * @param aUnit is the unit of the rescued symbol.
  111. * @param aConvert is the body style of the rescued symbol.
  112. */
  113. RESCUE_CASE_CANDIDATE( const wxString& aRequestedName, const wxString& aNewName,
  114. LIB_SYMBOL* aLibCandidate, int aUnit = 0, int aConvert = 0 );
  115. RESCUE_CASE_CANDIDATE() { m_lib_candidate = nullptr; }
  116. virtual wxString GetActionDescription() const override;
  117. virtual bool PerformAction( RESCUER* aRescuer ) override;
  118. };
  119. class RESCUE_CACHE_CANDIDATE: public RESCUE_CANDIDATE
  120. {
  121. LIB_SYMBOL* m_cache_candidate;
  122. public:
  123. /**
  124. * Grab all possible #RESCUE_CACHE_CANDIDATE objects into a vector.
  125. *
  126. * @param aRescuer is the working RESCUER instance.
  127. * @param aCandidates is the vector the will hold the candidates.
  128. */
  129. static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
  130. /**
  131. * Create a RESCUE_CACHE_CANDIDATE.
  132. *
  133. * @param aRequestedName is the name the schematic asks for.
  134. * @param aNewName is the name we want to change it to.
  135. * @param aCacheCandidate is the part from the cache.
  136. * @param aLibCandidate is the part that would be loaded from the library.
  137. * @param aUnit is the unit of the rescued symbol.
  138. * @param aConvert is the body style of the rescued symbol.
  139. */
  140. RESCUE_CACHE_CANDIDATE( const wxString& aRequestedName, const wxString& aNewName,
  141. LIB_SYMBOL* aCacheCandidate, LIB_SYMBOL* aLibCandidate,
  142. int aUnit = 0, int aConvert = 0 );
  143. RESCUE_CACHE_CANDIDATE();
  144. virtual LIB_SYMBOL* GetCacheCandidate() const override { return m_cache_candidate; }
  145. virtual wxString GetActionDescription() const override;
  146. virtual bool PerformAction( RESCUER* aRescuer ) override;
  147. };
  148. class RESCUE_SYMBOL_LIB_TABLE_CANDIDATE : public RESCUE_CANDIDATE
  149. {
  150. public:
  151. /**
  152. * Grab all possible RESCUE_SYMBOL_LIB_TABLE_CANDIDATE objects into a vector.
  153. *
  154. * @param aRescuer is the working #RESCUER instance.
  155. * @param aCandidates is the vector the will hold the candidates.
  156. */
  157. static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
  158. /**
  159. * Create RESCUE_CANDIDATE.
  160. *
  161. * @param aRequestedName is the name the schematic asks for.
  162. * @param aNewName is the name we want to change it to.
  163. * @param aCacheCandidate is the part from the cache.
  164. * @param aLibCandidate is the part that would be loaded from the library.
  165. * @param aUnit is the unit of the rescued symbol.
  166. * @param aConvert is the body style of the rescued symbol.
  167. */
  168. RESCUE_SYMBOL_LIB_TABLE_CANDIDATE( const LIB_ID& aRequestedId, const LIB_ID& aNewId,
  169. LIB_SYMBOL* aCacheCandidate, LIB_SYMBOL* aLibCandidate,
  170. int aUnit = 0, int aConvert = 0 );
  171. RESCUE_SYMBOL_LIB_TABLE_CANDIDATE();
  172. virtual LIB_SYMBOL* GetCacheCandidate() const override { return m_cache_candidate; }
  173. virtual wxString GetActionDescription() const override;
  174. virtual bool PerformAction( RESCUER* aRescuer ) override;
  175. private:
  176. LIB_ID m_requested_id;
  177. LIB_ID m_new_id;
  178. LIB_SYMBOL* m_cache_candidate;
  179. };
  180. class RESCUE_LOG
  181. {
  182. public:
  183. SCH_SYMBOL* symbol;
  184. wxString old_name;
  185. wxString new_name;
  186. };
  187. class RESCUER
  188. {
  189. public:
  190. RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic, SCH_SHEET_PATH* aCurrentSheet,
  191. EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType );
  192. virtual ~RESCUER()
  193. {
  194. }
  195. /**
  196. * Write the rescue library.
  197. *
  198. * Called after successful PerformAction()s. If this fails, undo the actions.
  199. *
  200. * @return True on success.
  201. */
  202. virtual bool WriteRescueLibrary( wxWindow *aParent ) = 0;
  203. virtual void OpenRescueLibrary() = 0;
  204. /**
  205. * Populate the RESCUER with all possible candidates.
  206. */
  207. virtual void FindCandidates() = 0;
  208. virtual void AddSymbol( LIB_SYMBOL* aNewSymbol ) = 0;
  209. /**
  210. * Display a dialog to allow the user to select rescues.
  211. *
  212. * @param aAskShowAgain - whether the "Never Show Again" button should be visible
  213. */
  214. virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) = 0;
  215. /**
  216. * Filter out duplicately named rescue candidates.
  217. */
  218. void RemoveDuplicates();
  219. /**
  220. * Return the number of rescue candidates found.
  221. */
  222. size_t GetCandidateCount() { return m_all_candidates.size(); }
  223. /**
  224. * Get the number of rescue candidates chosen by the user.
  225. */
  226. size_t GetChosenCandidateCount() { return m_chosen_candidates.size(); }
  227. /**
  228. * Get the list of symbols that need rescued.
  229. */
  230. std::vector<SCH_SYMBOL*>* GetSymbols() { return &m_symbols; }
  231. /**
  232. * Return the #SCH_PROJECT object for access to the symbol libraries.
  233. */
  234. PROJECT* GetPrj() { return m_prj; }
  235. SCHEMATIC* Schematic() { return m_schematic; }
  236. /**
  237. * Used by individual #RESCUE_CANDIDATE objects to log a rescue for undoing.
  238. */
  239. void LogRescue( SCH_SYMBOL *aSymbol, const wxString& aOldName, const wxString& aNewName );
  240. /**
  241. * Perform all chosen rescue actions, logging them to be undone if necessary.
  242. *
  243. * @return True on success
  244. */
  245. bool DoRescues();
  246. /**
  247. * Reverse the effects of all rescues on the project.
  248. */
  249. void UndoRescues();
  250. static bool RescueProject( wxWindow* aParent, RESCUER& aRescuer, bool aRunningOnDemand );
  251. protected:
  252. friend class DIALOG_RESCUE_EACH;
  253. std::vector<SCH_SYMBOL*> m_symbols;
  254. PROJECT* m_prj;
  255. SCHEMATIC* m_schematic;
  256. EDA_DRAW_PANEL_GAL::GAL_TYPE m_galBackEndType;
  257. SCH_SHEET_PATH* m_currentSheet;
  258. boost::ptr_vector<RESCUE_CANDIDATE> m_all_candidates;
  259. std::vector<RESCUE_CANDIDATE*> m_chosen_candidates;
  260. std::vector<RESCUE_LOG> m_rescue_log;
  261. };
  262. class LEGACY_RESCUER : public RESCUER
  263. {
  264. public:
  265. LEGACY_RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic, SCH_SHEET_PATH* aCurrentSheet,
  266. EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType ) :
  267. RESCUER( aProject, aSchematic, aCurrentSheet, aGalBackEndType )
  268. {
  269. }
  270. virtual ~LEGACY_RESCUER()
  271. {
  272. }
  273. virtual void FindCandidates() override;
  274. virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) override;
  275. virtual void OpenRescueLibrary() override;
  276. virtual bool WriteRescueLibrary( wxWindow *aParent ) override;
  277. virtual void AddSymbol( LIB_SYMBOL* aNewSymbol ) override;
  278. private:
  279. std::unique_ptr<SYMBOL_LIB> m_rescue_lib;
  280. };
  281. class SYMBOL_LIB_TABLE_RESCUER : public RESCUER
  282. {
  283. public:
  284. SYMBOL_LIB_TABLE_RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic,
  285. SCH_SHEET_PATH* aCurrentSheet,
  286. EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType );
  287. virtual ~SYMBOL_LIB_TABLE_RESCUER()
  288. {
  289. }
  290. virtual void FindCandidates() override;
  291. virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) override;
  292. virtual void OpenRescueLibrary() override;
  293. virtual bool WriteRescueLibrary( wxWindow* aParent ) override;
  294. virtual void AddSymbol( LIB_SYMBOL* aNewSymbol ) override;
  295. private:
  296. std::vector<std::unique_ptr<LIB_SYMBOL>> m_rescueLibSymbols;
  297. std::unique_ptr<std::map<std::string, UTF8>> m_properties; ///< Library plugin properties.
  298. };
  299. #endif // _LIB_CACHE_RESCUE_H_