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-2021 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 symbols 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 <lib_symbol.h>
47 #include <class_draw_panel_gal.h>
48 
49 
50 class LIB_SYMBOL;
51 class SCH_SYMBOL;
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 public:
69  virtual ~RESCUE_CANDIDATE() {}
70 
74  virtual wxString GetRequestedName() const { return m_requested_name; }
75 
79  virtual wxString GetNewName() const { return m_new_name; }
80 
85  virtual LIB_SYMBOL* GetCacheCandidate() const { return nullptr; }
86 
91  virtual LIB_SYMBOL* GetLibCandidate() const { return m_lib_candidate; }
92 
93  int GetUnit() const { return m_unit; }
94 
95  int GetConvert() const { return m_convert; }
96 
100  virtual wxString GetActionDescription() const = 0;
101 
107  virtual bool PerformAction( RESCUER* aRescuer ) = 0;
108 
109 protected:
111  wxString m_new_name;
113  int m_unit;
115 };
116 
117 
119 {
120 public:
127  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
128 
138  RESCUE_CASE_CANDIDATE( const wxString& aRequestedName, const wxString& aNewName,
139  LIB_SYMBOL* aLibCandidate, int aUnit = 0, int aConvert = 0 );
140 
142 
143  virtual wxString GetActionDescription() const override;
144 
145  virtual bool PerformAction( RESCUER* aRescuer ) override;
146 };
147 
148 
150 {
152 
153 public:
160  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
161 
172  RESCUE_CACHE_CANDIDATE( const wxString& aRequestedName, const wxString& aNewName,
173  LIB_SYMBOL* aCacheCandidate, LIB_SYMBOL* aLibCandidate,
174  int aUnit = 0, int aConvert = 0 );
175 
177 
178  virtual LIB_SYMBOL* GetCacheCandidate() const override { return m_cache_candidate; }
179 
180  virtual wxString GetActionDescription() const override;
181 
182  virtual bool PerformAction( RESCUER* aRescuer ) override;
183 };
184 
185 
187 {
188 public:
195  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
196 
207  RESCUE_SYMBOL_LIB_TABLE_CANDIDATE( const LIB_ID& aRequestedId, const LIB_ID& aNewId,
208  LIB_SYMBOL* aCacheCandidate, LIB_SYMBOL* aLibCandidate,
209  int aUnit = 0, int aConvert = 0 );
210 
212 
213  virtual LIB_SYMBOL* GetCacheCandidate() const override { return m_cache_candidate; }
214 
215  virtual wxString GetActionDescription() const override;
216 
217  virtual bool PerformAction( RESCUER* aRescuer ) override;
218 
219 private:
223 };
224 
225 
227 {
228 public:
230  wxString old_name;
231  wxString new_name;
232 };
233 
234 
235 class RESCUER
236 {
237 public:
238  RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic, SCH_SHEET_PATH* aCurrentSheet,
239  EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType );
240 
241  virtual ~RESCUER()
242  {
243  }
244 
252  virtual bool WriteRescueLibrary( wxWindow *aParent ) = 0;
253 
254  virtual void OpenRescueLibrary() = 0;
255 
259  virtual void FindCandidates() = 0;
260 
261  virtual void AddSymbol( LIB_SYMBOL* aNewSymbol ) = 0;
262 
268  virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) = 0;
269 
273  void RemoveDuplicates();
274 
278  size_t GetCandidateCount() { return m_all_candidates.size(); }
279 
283  size_t GetChosenCandidateCount() { return m_chosen_candidates.size(); }
284 
288  std::vector<SCH_SYMBOL*>* GetSymbols() { return &m_symbols; }
289 
293  PROJECT* GetPrj() { return m_prj; }
294 
296 
300  void LogRescue( SCH_SYMBOL *aSymbol, const wxString& aOldName, const wxString& aNewName );
301 
307  bool DoRescues();
308 
312  void UndoRescues();
313 
314  static bool RescueProject( wxWindow* aParent, RESCUER& aRescuer, bool aRunningOnDemand );
315 
316 protected:
317  friend class DIALOG_RESCUE_EACH;
318 
319  std::vector<SCH_SYMBOL*> m_symbols;
324 
325  boost::ptr_vector<RESCUE_CANDIDATE> m_all_candidates;
326  std::vector<RESCUE_CANDIDATE*> m_chosen_candidates;
327 
328  std::vector<RESCUE_LOG> m_rescue_log;
329 };
330 
331 
332 class LEGACY_RESCUER : public RESCUER
333 {
334 public:
335  LEGACY_RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic, SCH_SHEET_PATH* aCurrentSheet,
336  EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType ) :
337  RESCUER( aProject, aSchematic, aCurrentSheet, aGalBackEndType )
338  {
339  }
340 
341  virtual ~LEGACY_RESCUER()
342  {
343  }
344 
345  virtual void FindCandidates() override;
346 
347  virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) override;
348 
349  virtual void OpenRescueLibrary() override;
350 
351  virtual bool WriteRescueLibrary( wxWindow *aParent ) override;
352 
353  virtual void AddSymbol( LIB_SYMBOL* aNewSymbol ) override;
354 
355 private:
356  std::unique_ptr<SYMBOL_LIB> m_rescue_lib;
357 };
358 
359 
361 {
362 public:
363  SYMBOL_LIB_TABLE_RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic,
364  SCH_SHEET_PATH* aCurrentSheet,
365  EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType );
366 
368  {
369  }
370 
371  virtual void FindCandidates() override;
372 
373  virtual void InvokeDialog( wxWindow* aParent, bool aAskShowAgain ) override;
374 
375  virtual void OpenRescueLibrary() override;
376 
377  virtual bool WriteRescueLibrary( wxWindow* aParent ) override;
378 
379  virtual void AddSymbol( LIB_SYMBOL* aNewSymbol ) override;
380 
381 private:
383 
384  std::unique_ptr< PROPERTIES > m_properties;
385 };
386 
387 #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
Write 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 rescue candidates chosen by the user.
virtual void AddSymbol(LIB_SYMBOL *aNewSymbol) override
Container for project specific data.
Definition: project.h:62
std::unique_ptr< SYMBOL_LIB > m_rescue_lib
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.
Definition: schematic.h:59
RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
virtual LIB_SYMBOL * GetCacheCandidate() const override
Get the part that can be loaded from the project cache, if possible, or else NULL.
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.
LIB_SYMBOL * m_cache_candidate
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 LIB_SYMBOL * GetCacheCandidate() const
Get the part that can be loaded from the project cache, if possible, or else NULL.
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)
std::vector< SCH_SYMBOL * > m_symbols
Define a library symbol object.
Definition: lib_symbol.h:96
SYMBOL_LIB_TABLE_RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
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
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_CACHE_CANDIDATE objects 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()
Return 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.
RESCUE_TYPE
PROJECT * m_prj
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
LIB_SYMBOL * m_lib_candidate
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:479
std::vector< RESCUE_CANDIDATE * > m_chosen_candidates
virtual bool WriteRescueLibrary(wxWindow *aParent) override
Write the rescue library.
virtual void OpenRescueLibrary() override
Schematic symbol object.
Definition: sch_symbol.h:78
bool DoRescues()
Perform all chosen rescue actions, logging them to be undone if necessary.
SCH_SYMBOL * symbol
std::vector< SCH_SYMBOL * > * GetSymbols()
Get the list of symbols that need rescued.
void LogRescue(SCH_SYMBOL *aSymbol, const wxString &aOldName, const wxString &aNewName)
Used by individual RESCUE_CANDIDATE objects to log a rescue for undoing.
SCHEMATIC * m_schematic
virtual ~LEGACY_RESCUER()
virtual bool WriteRescueLibrary(wxWindow *aParent) override
Write the rescue library.
std::vector< RESCUE_LOG > m_rescue_log
wxString m_requested_name
virtual void AddSymbol(LIB_SYMBOL *aNewSymbol)=0
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.
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.
virtual LIB_SYMBOL * GetLibCandidate() const
Get the part the would be loaded from the libraries, if possible, or else NULL.
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_CASE_CANDIDATE objects into a vector.
void UndoRescues()
Reverse the effects of all rescues on the project.
virtual void AddSymbol(LIB_SYMBOL *aNewSymbol) override
wxString new_name
virtual LIB_SYMBOL * GetCacheCandidate() const override
Get the part that can be loaded from the project cache, if possible, or else NULL.
virtual void FindCandidates() override
Populate the RESCUER with all possible candidates.