KiCad PCB EDA Suite
project_rescue.h
Go to the documentation of this file.
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 (C) 2015-2020 KiCad Developers, see change_log.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 
25 #ifndef _LIB_CACHE_RESCUE_H_
26 #define _LIB_CACHE_RESCUE_H_
27 
28 /* This code handles the case where an old schematic was made before
29  * various changes were made, either to KiCad or to the libraries, and
30  * the project needs to be recovered. The function of note is a member
31  * of SCH_EDIT_FRAME, defined thus:
32  *
33  * bool SCH_EDIT_FRAME::RescueProject( bool aSilentIfNone );
34  *
35  * When this is called, a list of problematic components is compiled. If
36  * this list is empty, then the function displays a notification and returns
37  * (if aSilentIfNone is true, the notification is silenced).
38  */
39 
40 #include <vector>
41 #include <wx/string.h>
42 #include <boost/ptr_container/ptr_vector.hpp>
43 
44 #include <properties.h>
45 #include <class_libentry.h>
46 #include <sch_legacy_plugin.h>
47 #include <class_draw_panel_gal.h>
48 
49 
50 class LIB_PART;
51 class SCH_COMPONENT;
52 class RESCUER;
53 class SCH_EDIT_FRAME;
54 class SCH_LEGACY_PLUGIN;
55 class SCH_SHEET_PATH;
56 class SCHEMATIC;
57 
58 
60 {
63 };
64 
65 
67 {
68 protected:
69  wxString m_requested_name;
70  wxString m_new_name;
72  int m_unit;
73  int m_convert;
74 
75 public:
76  virtual ~RESCUE_CANDIDATE() {}
77 
81  virtual wxString GetRequestedName() const { return m_requested_name; }
82 
86  virtual wxString GetNewName() const { return m_new_name; }
87 
92  virtual LIB_PART* GetCacheCandidate() const { return NULL; }
93 
98  virtual LIB_PART* GetLibCandidate() const { return m_lib_candidate; }
99 
100  int GetUnit() const { return m_unit; }
101 
102  int GetConvert() const { return m_convert; }
103 
107  virtual wxString GetActionDescription() const = 0;
108 
114  virtual bool PerformAction( RESCUER* aRescuer ) = 0;
115 };
116 
117 
119 {
120 public:
127  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
128 
137  RESCUE_CASE_CANDIDATE( const wxString& aRequestedName, const wxString& aNewName,
138  LIB_PART* aLibCandidate, int aUnit = 0, int aConvert = 0 );
139 
141 
142  virtual wxString GetActionDescription() const override;
143 
144  virtual bool PerformAction( RESCUER* aRescuer ) override;
145 };
146 
147 
149 {
151 
152 public:
159  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
160 
170  RESCUE_CACHE_CANDIDATE( const wxString& aRequestedName, const wxString& aNewName,
171  LIB_PART* aCacheCandidate, LIB_PART* aLibCandidate,
172  int aUnit = 0, int aConvert = 0 );
173 
175 
176  virtual LIB_PART* GetCacheCandidate() const override { return m_cache_candidate; }
177 
178  virtual wxString GetActionDescription() const override;
179 
180  virtual bool PerformAction( RESCUER* aRescuer ) override;
181 };
182 
183 
185 {
189 
190 public:
197  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
198 
208  RESCUE_SYMBOL_LIB_TABLE_CANDIDATE( const LIB_ID& aRequestedId, const LIB_ID& aNewId,
209  LIB_PART* aCacheCandidate, LIB_PART* aLibCandidate,
210  int aUnit = 0, int aConvert = 0 );
211 
213 
214  virtual LIB_PART* GetCacheCandidate() const override { return m_cache_candidate; }
215 
216  virtual wxString GetActionDescription() const override;
217 
218  virtual bool PerformAction( RESCUER* aRescuer ) override;
219 };
220 
221 
223 {
224 public:
226  wxString old_name;
227  wxString new_name;
228 };
229 
230 
231 class RESCUER
232 {
233 protected:
234  friend class DIALOG_RESCUE_EACH;
235 
236  std::vector<SCH_COMPONENT*> m_components;
241 
242  boost::ptr_vector<RESCUE_CANDIDATE> m_all_candidates;
243  std::vector<RESCUE_CANDIDATE*> m_chosen_candidates;
244 
245  std::vector<RESCUE_LOG> m_rescue_log;
246 
247 public:
248  RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic, SCH_SHEET_PATH* aCurrentSheet,
249  EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType );
250 
251  virtual ~RESCUER()
252  {
253  }
254 
261  virtual bool WriteRescueLibrary( wxWindow *aParent ) = 0;
262 
263  virtual void OpenRescueLibrary() = 0;
264 
268  virtual void FindCandidates() = 0;
269 
270  virtual void AddPart( LIB_PART* aNewPart ) = 0;
271 
277  virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) = 0;
278 
282  void RemoveDuplicates();
283 
287  size_t GetCandidateCount() { return m_all_candidates.size(); }
288 
292  size_t GetChosenCandidateCount() { return m_chosen_candidates.size(); }
293 
297  std::vector<SCH_COMPONENT*>* GetComponents() { return &m_components; }
298 
302  PROJECT* GetPrj() { return m_prj; }
303 
305 
309  void LogRescue( SCH_COMPONENT *aComponent, const wxString& aOldName,
310  const wxString& aNewName );
311 
317  bool DoRescues();
318 
322  void UndoRescues();
323 
324  static bool RescueProject( wxWindow* aParent, RESCUER& aRescuer, bool aRunningOnDemand );
325 };
326 
327 
328 class LEGACY_RESCUER : public RESCUER
329 {
330 private:
331  std::unique_ptr<PART_LIB> m_rescue_lib;
332 
333 public:
334  LEGACY_RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic, SCH_SHEET_PATH* aCurrentSheet,
335  EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType ) :
336  RESCUER( aProject, aSchematic, aCurrentSheet, aGalBackEndType )
337  {
338  }
339 
340  virtual ~LEGACY_RESCUER()
341  {
342  }
343 
344  virtual void FindCandidates() override;
345 
346  virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) override;
347 
348  virtual void OpenRescueLibrary() override;
349 
350  virtual bool WriteRescueLibrary( wxWindow *aParent ) override;
351 
352  virtual void AddPart( LIB_PART* aNewPart ) override;
353 };
354 
355 
357 {
358 private:
360 
361  std::unique_ptr< PROPERTIES > m_properties;
362 
363 public:
364  SYMBOL_LIB_TABLE_RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic,
365  SCH_SHEET_PATH* aCurrentSheet,
366  EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType );
367 
369  {
370  }
371 
372  virtual void FindCandidates() override;
373 
374  virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) override;
375 
376  virtual void OpenRescueLibrary() override;
377 
378  virtual bool WriteRescueLibrary( wxWindow* aParent ) override;
379 
380  virtual void AddPart( LIB_PART* aNewPart ) override;
381 };
382 
383 #endif // _LIB_CACHE_RESCUE_H_
virtual void InvokeDialog(wxWindow *aParent, bool aAskShowAgain) override
Display a dialog to allow the user to select rescues.
EDA_DRAW_PANEL_GAL::GAL_TYPE m_galBackEndType
virtual void FindCandidates() override
Populate the RESCUER with all possible candidates.
virtual bool WriteRescueLibrary(wxWindow *aParent)=0
Writes out the rescue library.
virtual ~RESCUER()
virtual wxString GetActionDescription() const override
Get a description of the action proposed, for displaying in the UI.
SCHEMATIC * Schematic()
virtual void FindCandidates()=0
Populate the RESCUER with all possible candidates.
size_t GetChosenCandidateCount()
Get the number of resuce candidates chosen by the user.
virtual LIB_PART * GetLibCandidate() const
Get the part the would be loaded from the libraries, if possible, or else NULL.
PROJECT holds project specific data.
Definition: project.h:61
PROJECT * GetPrj()
Return the #SCH_PROJECT object for access to the symbol libraries.
virtual wxString GetNewName() const
Get the name we're proposing changing it to.
Holds all the data relating to one schematic A schematic may consist of one or more sheets (and one r...
Definition: schematic.h:42
RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
virtual wxString GetActionDescription() const =0
Get a description of the action proposed, for displaying in the UI.
virtual wxString GetRequestedName() const
Get the name that was originally requested in the schematic.
virtual void OpenRescueLibrary() override
virtual void InvokeDialog(wxWindow *aParent, bool aAskShowAgain)=0
Display a dialog to allow the user to select rescues.
Schematic editor (Eeschema) main window.
virtual ~RESCUE_CANDIDATE()
int GetConvert() const
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LEGACY_RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType)
SYMBOL_LIB_TABLE_RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
LIB_PART * m_lib_candidate
SCH_COMPONENT * component
boost::ptr_vector< RESCUE_CANDIDATE > m_all_candidates
void RemoveDuplicates()
Filter out duplicately named rescue candidates.
static bool RescueProject(wxWindow *aParent, RESCUER &aRescuer, bool aRunningOnDemand)
SCH_SHEET_PATH * m_currentSheet
#define NULL
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_CACHE_CANDIDATE objectss into a vector.
SCH_PLUGIN::SCH_PLUGIN_RELEASER m_pi
int GetUnit() const
virtual bool PerformAction(RESCUER *aRescuer) override
Perform the actual rescue action.
virtual wxString GetActionDescription() const override
Get a description of the action proposed, for displaying in the UI.
size_t GetCandidateCount()
Returen the number of rescue candidates found.
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_SYMBOL_LIB_TABLE_CANDIDATE objects into a vector.
virtual wxString GetActionDescription() const override
Get a description of the action proposed, for displaying in the UI.
std::unique_ptr< PROPERTIES > m_properties
Library plugin properties.
virtual LIB_PART * GetCacheCandidate() const
Get the part that can be loaded from the project cache, if possible, or else NULL.
RESCUE_TYPE
Define a library symbol object.
PROJECT * m_prj
std::vector< SCH_COMPONENT * > m_components
SCH_SHEET_PATH.
virtual void OpenRescueLibrary()=0
A SCH_PLUGIN derivation for loading schematic files created before the new s-expression file format.
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:469
virtual LIB_PART * GetCacheCandidate() const override
Get the part that can be loaded from the project cache, if possible, or else NULL.
std::vector< RESCUE_CANDIDATE * > m_chosen_candidates
virtual bool WriteRescueLibrary(wxWindow *aParent) override
Writes out the rescue library.
std::unique_ptr< PART_LIB > m_rescue_lib
virtual void OpenRescueLibrary() override
virtual LIB_PART * GetCacheCandidate() const override
Get the part that can be loaded from the project cache, if possible, or else NULL.
bool DoRescues()
Perform all chosen rescue actions, logging them to be undone if necessary.
SCHEMATIC * m_schematic
virtual ~LEGACY_RESCUER()
Schematic symbol object.
Definition: sch_component.h:80
virtual void AddPart(LIB_PART *aNewPart)=0
virtual bool WriteRescueLibrary(wxWindow *aParent) override
Writes out the rescue library.
std::vector< SCH_COMPONENT * > * GetComponents()
Get the list of symbols that need rescued.
std::vector< RESCUE_LOG > m_rescue_log
void LogRescue(SCH_COMPONENT *aComponent, const wxString &aOldName, const wxString &aNewName)
Used by individual RESCUE_CANDIDATE objects to log a rescue for undoing.
wxString m_requested_name
virtual void InvokeDialog(wxWindow *aParent, bool aAskShowAgain) override
Display a dialog to allow the user to select rescues.
virtual bool PerformAction(RESCUER *aRescuer) override
Perform the actual rescue action.
virtual void AddPart(LIB_PART *aNewPart) override
wxString old_name
virtual bool PerformAction(RESCUER *aRescuer)=0
Perform the actual rescue action.
virtual bool PerformAction(RESCUER *aRescuer) override
Perform the actual rescue action.
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_CASE_CANDIDATE objects into a vector.
virtual void AddPart(LIB_PART *aNewPart) override
void UndoRescues()
Reverse the effects of all rescues on the project.
wxString new_name
virtual void FindCandidates() override
Populate the RESCUER with all possible candidates.