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-2017 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 
46 #include <class_libentry.h>
47 #include <sch_legacy_plugin.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 
56 
58 {
61 };
62 
63 
65 {
66 protected:
67  wxString m_requested_name;
68  wxString m_new_name;
70 
71 public:
72  virtual ~RESCUE_CANDIDATE() {}
73 
77  virtual wxString GetRequestedName() const { return m_requested_name; }
78 
82  virtual wxString GetNewName() const { return m_new_name; }
83 
88  virtual LIB_PART* GetCacheCandidate() const { return NULL; }
89 
94  virtual LIB_PART* GetLibCandidate() const { return m_lib_candidate; }
95 
99  virtual wxString GetActionDescription() const = 0;
100 
106  virtual bool PerformAction( RESCUER* aRescuer ) = 0;
107 };
108 
109 
111 {
112 public:
119  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
120 
127  RESCUE_CASE_CANDIDATE( const wxString& aRequestedName, const wxString& aNewName,
128  LIB_PART* aLibCandidate );
129 
131 
132  virtual wxString GetActionDescription() const override;
133 
134  virtual bool PerformAction( RESCUER* aRescuer ) override;
135 };
136 
137 
139 {
141 
142 public:
149  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
150 
158  RESCUE_CACHE_CANDIDATE( const wxString& aRequestedName, const wxString& aNewName,
159  LIB_PART* aCacheCandidate, LIB_PART* aLibCandidate );
160 
162 
163  virtual LIB_PART* GetCacheCandidate() const override { return m_cache_candidate; }
164 
165  virtual wxString GetActionDescription() const override;
166 
167  virtual bool PerformAction( RESCUER* aRescuer ) override;
168 };
169 
170 
172 {
176 
177 public:
184  static void FindRescues( RESCUER& aRescuer, boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates );
185 
193  RESCUE_SYMBOL_LIB_TABLE_CANDIDATE( const LIB_ID& aRequestedId, const LIB_ID& aNewId,
194  LIB_PART* aCacheCandidate, LIB_PART* aLibCandidate);
195 
197 
198  virtual LIB_PART* GetCacheCandidate() const override { return m_cache_candidate; }
199 
200  virtual wxString GetActionDescription() const override;
201 
202  virtual bool PerformAction( RESCUER* aRescuer ) override;
203 };
204 
205 
207 {
208 public:
210  wxString old_name;
211  wxString new_name;
212 };
213 
214 
215 class RESCUER
216 {
217 protected:
218  friend class DIALOG_RESCUE_EACH;
219 
220  std::vector<SCH_COMPONENT*> m_components;
223 
224  boost::ptr_vector<RESCUE_CANDIDATE> m_all_candidates;
225  std::vector<RESCUE_CANDIDATE*> m_chosen_candidates;
226 
227  std::vector<RESCUE_LOG> m_rescue_log;
228 
229 public:
230  RESCUER( SCH_EDIT_FRAME& aEditFrame, PROJECT& aProject );
231 
238  virtual bool WriteRescueLibrary( SCH_EDIT_FRAME *aEditFrame ) = 0;
239 
240  virtual void OpenRescueLibrary() = 0;
241 
245  virtual void FindCandidates() = 0;
246 
247  virtual void AddPart( LIB_PART* aNewPart ) = 0;
248 
254  virtual void InvokeDialog( bool aAskShowAgain ) = 0;
255 
257 
261  void RemoveDuplicates();
262 
266  size_t GetCandidateCount() { return m_all_candidates.size(); }
267 
271  size_t GetChosenCandidateCount() { return m_chosen_candidates.size(); }
272 
276  std::vector<SCH_COMPONENT*>* GetComponents() { return &m_components; }
277 
281  PROJECT* GetPrj() { return m_prj; }
282 
286  wxString GetPartNameSuffix();
287 
291  void LogRescue( SCH_COMPONENT *aComponent, const wxString& aOldName,
292  const wxString& aNewName );
293 
299  bool DoRescues();
300 
304  void UndoRescues();
305 };
306 
307 
308 class LEGACY_RESCUER : public RESCUER
309 {
310 private:
311  std::unique_ptr<PART_LIB> m_rescue_lib;
312 
313 public:
314  LEGACY_RESCUER( SCH_EDIT_FRAME& aEditFrame, PROJECT& aProject ) :
315  RESCUER( aEditFrame, aProject )
316  {
317  }
318 
319  virtual void FindCandidates() override;
320 
321  virtual void InvokeDialog( bool aAskShowAgain ) override;
322 
323  virtual void OpenRescueLibrary() override;
324 
325  virtual bool WriteRescueLibrary( SCH_EDIT_FRAME *aEditFrame ) override;
326 
327  virtual void AddPart( LIB_PART* aNewPart ) override;
328 };
329 
330 
332 {
333 private:
335 
336  std::unique_ptr< PROPERTIES > m_properties;
337 
338 public:
339  SYMBOL_LIB_TABLE_RESCUER( SCH_EDIT_FRAME& aEditFrame, PROJECT& aProject );
340 
341  virtual void FindCandidates() override;
342 
343  virtual void InvokeDialog( bool aAskShowAgain ) override;
344 
345  virtual void OpenRescueLibrary() override;
346 
347  virtual bool WriteRescueLibrary( SCH_EDIT_FRAME *aEditFrame ) override;
348 
349  virtual void AddPart( LIB_PART* aNewPart ) override;
350 };
351 
352 #endif // _LIB_CACHE_RESCUE_H_
RESCUER(SCH_EDIT_FRAME &aEditFrame, PROJECT &aProject)
virtual bool WriteRescueLibrary(SCH_EDIT_FRAME *aEditFrame) override
Writes out the rescue library.
virtual void FindCandidates() override
Populate the RESCUER with all possible candidates.
virtual LIB_PART * GetLibCandidate() const
Get the part the would be loaded from the libraries, if possible, or else NULL.
virtual wxString GetActionDescription() const override
Get a description of the action proposed, for displaying in the UI.
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 * GetCacheCandidate() const
Get the part that can be loaded from the project cache, if possible, or else NULL.
virtual void InvokeDialog(bool aAskShowAgain)=0
Display a dialog to allow the user to select rescues.
Class PROJECT holds project specific data.
Definition: project.h:56
PROJECT * GetPrj()
Return the #SCH_PROJECT object for access to the symbol libraries.
virtual wxString GetActionDescription() const =0
Get a description of the action proposed, for displaying in the UI.
SCH_EDIT_FRAME * GetFrame()
virtual bool WriteRescueLibrary(SCH_EDIT_FRAME *aEditFrame) override
Writes out the rescue library.
virtual void OpenRescueLibrary() override
Schematic editor (Eeschema) main window.
Definition: schframe.h:118
virtual ~RESCUE_CANDIDATE()
Class LIB_ID.
Definition: lib_id.h:56
SYMBOL_LIB_TABLE_RESCUER(SCH_EDIT_FRAME &aEditFrame, PROJECT &aProject)
LIB_PART * m_lib_candidate
SCH_COMPONENT * component
boost::ptr_vector< RESCUE_CANDIDATE > m_all_candidates
void RemoveDuplicates()
Filter out duplicately named rescue candidates.
LEGACY_RESCUER(SCH_EDIT_FRAME &aEditFrame, PROJECT &aProject)
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
Class LIB_ITEM definition.
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.
RESCUE_TYPE
Class LIB_PART defines a library part object.
PROJECT * m_prj
std::vector< SCH_COMPONENT * > m_components
virtual bool WriteRescueLibrary(SCH_EDIT_FRAME *aEditFrame)=0
Writes out the rescue library.
virtual void OpenRescueLibrary()=0
A SCH_PLUGIN derivation for loading schematic files created before the new s-expression file format...
virtual void InvokeDialog(bool aAskShowAgain) override
Display a dialog to allow the user to select rescues.
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:513
wxString GetPartNameSuffix()
Return the suffix to add to rescued parts.
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
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.
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
virtual void AddPart(LIB_PART *aNewPart)=0
std::vector< SCH_COMPONENT * > * GetComponents()
Get the list of symbols that need rescued.
std::vector< RESCUE_LOG > m_rescue_log
SCH_EDIT_FRAME * m_edit_frame
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 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.
virtual void InvokeDialog(bool aAskShowAgain) override
Display a dialog to allow the user to select rescues.
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.
virtual wxString GetRequestedName() const
Get the name that was originally requested in the schematic.
virtual wxString GetNewName() const
Get the name we're proposing changing it to.
wxString new_name
virtual void FindCandidates() override
Populate the RESCUER with all possible candidates.