KiCad PCB EDA Suite
class_library.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 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 2004-2019 KiCad Developers, see change_log.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #ifndef CLASS_LIBRARY_H
32 #define CLASS_LIBRARY_H
33 
34 #include <map>
35 #include <boost/ptr_container/ptr_vector.hpp>
36 #include <wx/filename.h>
37 
38 #include <sch_io_mgr.h>
39 #include <project.h>
40 
41 class LIB_PART;
42 class LIB_ID;
43 class LINE_READER;
44 class OUTPUTFORMATTER;
45 class SCH_LEGACY_PLUGIN;
46 class SCH_PLUGIN;
47 
48 
49 #define DOC_EXT "dcm"
50 
51 /*
52  * Part Library version and file header macros.
53  */
54 #define LIB_VERSION_MAJOR 2
55 #define LIB_VERSION_MINOR 4
56 
57 /* Must be the first line of part library (.lib) files. */
58 #define LIBFILE_IDENT "EESchema-LIBRARY Version"
59 
60 #define LIB_VERSION( major, minor ) ( major * 100 + minor )
61 
62 #define IS_LIB_CURRENT_VERSION( major, minor ) \
63  ( \
64  LIB_VERSION( major1, minor1 ) == \
65  LIB_VERSION( LIB_VERSION_MAJOR, LIB_VERSION_MINOR) \
66  )
67 
68 /*
69  * Library versions 2.4 and lower use the old separate library (.lib) and
70  * document (.dcm) files. Part libraries after 2.4 merged the library
71  * and document files into a single library file. This macro checks if the
72  * library version supports the old format
73  */
74 #define USE_OLD_DOC_FILE_FORMAT( major, minor ) \
75  ( LIB_VERSION( major, minor ) <= LIB_VERSION( 2, 4 ) )
76 
77 enum class SCH_LIB_TYPE
78 {
80  LT_SYMBOL
81 };
82 // Helper class to filter a list of libraries, and/or a list of PART_LIB
83 // in dialogs
85 {
86  wxArrayString m_allowedLibs;
87  bool m_filterPowerParts;
89  bool m_forceLoad; // When true, load a part lib from the lib
90  // which is given in m_allowedLibs[0]
91 
92 public:
94  {
95  m_filterPowerParts = false;
96  m_forceLoad = false;
97  }
98 
102  void AddLib( const wxString& aLibName )
103  {
104  m_allowedLibs.Add( aLibName );
105  m_forceLoad = false;
106  }
107 
108 
112  void LoadFrom( const wxString& aLibName )
113  {
114  m_allowedLibs.Clear();
115  m_allowedLibs.Add( aLibName );
116  m_forceLoad = true;
117  }
118 
123  {
124  m_allowedLibs.Clear();
125  m_forceLoad = false;
126  }
127 
131  void FilterPowerParts( bool aFilterEnable )
132  {
133  m_filterPowerParts = aFilterEnable;
134  }
135 
136  // Accessors
137 
141  bool GetFilterPowerParts() const { return m_filterPowerParts; }
142 
143 
147  const wxArrayString& GetAllowedLibList() const { return m_allowedLibs; }
148 
153  const wxString& GetLibSource() const
154  {
155  static wxString dummy;
156 
157  if( m_forceLoad && m_allowedLibs.GetCount() > 0 )
158  return m_allowedLibs[0];
159  else
160  return dummy;
161  }
162 };
163 
164 
165 /* Helpers for creating a list of part libraries. */
166 class PART_LIB;
167 class wxRegEx;
168 
173 {
174  bool operator() ( const wxString& aItem1, const wxString& aItem2 ) const
175  {
176  return aItem1 < aItem2;
177  }
178 };
179 
181 
182 typedef std::map< wxString, LIB_PART*, LibPartMapSort > LIB_PART_MAP;
183 typedef std::vector< LIB_PART* > LIB_PARTS;
184 typedef boost::ptr_vector< PART_LIB > PART_LIBS_BASE;
185 
186 
194 {
195 public:
196  KICAD_T Type() override { return PART_LIBS_T; }
197 
198  static int s_modify_generation;
199 
201  {
203  }
204 
207  int GetModifyHash();
208 
215  PART_LIB* AddLibrary( const wxString& aFileName );
216 
225  PART_LIB* AddLibrary( const wxString& aFileName, PART_LIBS::iterator& aIterator );
226 
234  void LoadAllLibraries( PROJECT* aProject, bool aShowProgress=true );
235 
239  static void LibNamesAndPaths( PROJECT* aProject, bool doSave,
240  wxString* aPaths, wxArrayString* aNames=NULL );
241 
248  static const wxString CacheName( const wxString& aFullProjectFilename );
249 
256  PART_LIB* FindLibrary( const wxString& aName );
257 
258  PART_LIB* FindLibraryByFullFileName( const wxString& aFullFileName );
259 
261 
268  wxArrayString GetLibraryNames( bool aSorted = true );
269 
280  LIB_PART* FindLibPart( const LIB_ID& aLibId, const wxString& aLibraryName = wxEmptyString );
281 
295  void FindLibraryNearEntries( std::vector<LIB_PART*>& aCandidates, const wxString& aEntryName,
296  const wxString& aLibraryName = wxEmptyString );
297 
298  int GetLibraryCount() { return size(); }
299 };
300 
301 
308 class PART_LIB
309 {
311  wxFileName fileName;
312  wxDateTime timeStamp;
315  wxString header;
316  bool isModified;
318 
319  SCH_IO_MGR::SCH_FILE_T m_pluginType;
320  std::unique_ptr< SCH_PLUGIN > m_plugin;
321  std::unique_ptr< PROPERTIES > m_properties;
322 
323 public:
324  PART_LIB( SCH_LIB_TYPE aType, const wxString& aFileName,
325  SCH_IO_MGR::SCH_FILE_T aPluginType = SCH_IO_MGR::SCH_LEGACY );
326  ~PART_LIB();
327 
331  int GetModHash() const { return m_mod_hash; }
332 
333  SCH_IO_MGR::SCH_FILE_T GetPluginType() const { return m_pluginType; }
334 
335  void SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType );
336 
337  void Create( const wxString& aFileName = wxEmptyString );
338 
339  void SetFileName( const wxString& aFileName ) { fileName = aFileName; }
340 
341  bool IsModified() const
342  {
343  return isModified;
344  }
345 
346  bool IsCache() const;
347 
348  void SetCache();
349 
350  bool IsBuffering() const;
351 
352  void EnableBuffering( bool aEnable = true );
353 
354  void Save( bool aSaveDocFile = true );
355 
359  bool IsReadOnly() const { return !fileName.IsFileWritable(); }
360 
366  void GetPartNames( wxArrayString& aNames ) const;
367 
373  void GetParts( std::vector<LIB_PART*>& aPart) const;
374 
381  LIB_PART* FindPart( const wxString& aName ) const;
382 
383  LIB_PART* FindPart( const LIB_ID& aLibId ) const;
384 
393  void AddPart( LIB_PART* aPart );
394 
406  LIB_PART* RemovePart( LIB_PART* aEntry );
407 
416  LIB_PART* ReplacePart( LIB_PART* aOldPart, LIB_PART* aNewPart );
417 
423  const wxString GetName() const { return fileName.GetName(); }
424 
430  wxString GetFullFileName() const { return fileName.GetFullPath(); }
431 
437  const wxString GetLogicalName() const
438  {
439  /* for now is the filename without path or extension.
440 
441  Technically the library should not know its logical name!
442  This will eventually come out of a pair of lookup tables using a
443  reverse lookup using the full name or library pointer as a key.
444  Search will be by project lookup table and then user lookup table if
445  not found.
446  */
447  return fileName.GetName();
448  }
449 
450 
458  static PART_LIB* LoadLibrary( const wxString& aFileName );
459 };
460 
461 
465 bool operator==( const PART_LIB& aLibrary, const wxString& aName );
466 bool operator!=( const PART_LIB& aLibrary, const wxString& aName );
467 
468 #endif // CLASS_LIBRARY_H
const wxString GetName() const
Return the file name without path or extension.
wxString GetFullFileName() const
Return the full file library name with path and extension.
bool IsModified() const
int m_mod_hash
incremented each time library is changed.
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
LIB_PART * ReplacePart(LIB_PART *aOldPart, LIB_PART *aNewPart)
Replace an existing part entry in the library.
SCH_LIB_TYPE type
Library type indicator.
std::unique_ptr< SCH_PLUGIN > m_plugin
static void LibNamesAndPaths(PROJECT *aProject, bool doSave, wxString *aPaths, wxArrayString *aNames=NULL)
Save or load the names of the currently configured part libraries (without paths).
void FilterPowerParts(bool aFilterEnable)
set the filtering of power parts
Container for project specific data.
Definition: project.h:62
const wxString & GetLibSource() const
bool m_filterPowerParts
true to filter (show only) power parts
Definition: class_library.h:88
void LoadAllLibraries(PROJECT *aProject, bool aShowProgress=true)
Load all of the project's libraries into this container, which should be cleared before calling it.
static int s_modify_generation
helper for GetModifyHash()
void Save(bool aSaveDocFile=true)
bool isModified
Library modification status.
PART_LIB * GetCacheLibrary()
void LoadFrom(const wxString &aLibName)
add a lib name to the allowed libraries
std::map< wxString, LIB_PART *, LibPartMapSort > LIB_PART_MAP
Part map used by part library object.
wxArrayString GetLibraryNames(bool aSorted=true)
Return the list of part library file names without path and extension.
int GetModifyHash()
Return the modification hash for all libraries.
wxString header
first line of loaded library.
void FindLibraryNearEntries(std::vector< LIB_PART * > &aCandidates, const wxString &aEntryName, const wxString &aLibraryName=wxEmptyString)
Search all libraries in the list for a LIB_PART using a case insensitive comparison.
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:306
int GetLibraryCount()
LIB_PART * RemovePart(LIB_PART *aEntry)
Safely remove aEntry from the library and return the next entry.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool operator==(const PART_LIB &aLibrary, const wxString &aName)
Case insensitive library name comparison.
PART_LIB * FindLibrary(const wxString &aName)
Find a part library by aName.
static const wxString CacheName(const wxString &aFullProjectFilename)
Return the name of the cache library after potentially fixing it from an older naming scheme.
void GetPartNames(wxArrayString &aNames) const
Load a string array with the names of all the entries in this library.
std::vector< LIB_PART * > LIB_PARTS
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
void AddLib(const wxString &aLibName)
add a lib name to the allowed libraries
bool IsBuffering() const
void EnableBuffering(bool aEnable=true)
int GetModHash() const
SCH_IO_MGR::SCH_FILE_T GetPluginType() const
void ClearLibList()
Clear the allowed libraries list (allows all libs)
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:153
const wxString GetLogicalName() const
Return the logical name of the library.
#define NULL
KICAD_T Type() override
wxDateTime timeStamp
Library save time and date.
PART_LIB * AddLibrary(const wxString &aFileName)
Allocate and adds a part library to the library list.
static PART_LIB * LoadLibrary(const wxString &aFileName)
Allocate and load a symbol library file.
Define a library symbol object.
Definition: lib_symbol.h:93
void SetCache()
boost::ptr_vector< PART_LIB > PART_LIBS_BASE
std::unique_ptr< PROPERTIES > m_properties
Library properties.
LIB_PART * FindLibPart(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Search all libraries in the list for a part.
SCH_IO_MGR::SCH_FILE_T m_pluginType
bool GetFilterPowerParts() const
LIB_PART * FindPart(const wxString &aName) const
Find LIB_PART by aName.
wxArrayString m_allowedLibs
a list of lib names to list some libraries if empty: no filter
Definition: class_library.h:86
A SCH_PLUGIN derivation for loading schematic files created before the new s-expression file format.
SCH_LIB_TYPE
Definition: class_library.h:77
wxFileName fileName
Library file name.
void GetParts(std::vector< LIB_PART * > &aPart) const
Load a vector with all the entries in this library.
A collection of PART_LIB objects.
void AddPart(LIB_PART *aPart)
Add aPart entry to library.
PART_LIB(SCH_LIB_TYPE aType, const wxString &aFileName, SCH_IO_MGR::SCH_FILE_T aPluginType=SCH_IO_MGR::SCH_LEGACY)
LIB_PART map sorting.
int versionMinor
Library minor version number.
bool operator()(const wxString &aItem1, const wxString &aItem2) const
const wxArrayString & GetAllowedLibList() const
PART_LIB * FindLibraryByFullFileName(const wxString &aFullFileName)
void SetPluginType(SCH_IO_MGR::SCH_FILE_T aPluginType)
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:73
bool IsReadOnly() const
void SetFileName(const wxString &aFileName)
void Create(const wxString &aFileName=wxEmptyString)
Object used to load, save, search, and otherwise manipulate symbol library files.
bool IsCache() const
int versionMajor
Library major version number.
bool operator!=(const PART_LIB &aLibrary, const wxString &aName)