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-2017 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2004-2017 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 <wx/filename.h>
35 
36 #include <class_libentry.h>
37 #include <sch_io_mgr.h>
38 
39 #include <project.h>
40 
41 #include <map>
42 
43 class LIB_ID;
44 class LINE_READER;
45 class OUTPUTFORMATTER;
46 class SCH_LEGACY_PLUGIN;
47 class SCH_PLUGIN;
48 
49 
50 #define DOC_EXT "dcm"
51 
52 /*
53  * Part Library version and file header macros.
54  */
55 #define LIB_VERSION_MAJOR 2
56 #define LIB_VERSION_MINOR 3
57 
58 /* Must be the first line of part library (.lib) files. */
59 #define LIBFILE_IDENT "EESchema-LIBRARY Version"
60 
61 #define LIB_VERSION( major, minor ) ( major * 100 + minor )
62 
63 #define IS_LIB_CURRENT_VERSION( major, minor ) \
64  ( \
65  LIB_VERSION( major1, minor1 ) == \
66  LIB_VERSION( LIB_VERSION_MAJOR, LIB_VERSION_MINOR) \
67  )
68 
69 /*
70  * Library versions 2.3 and lower use the old separate library (.lib) and
71  * document (.dcm) files. Part libraries after 2.3 merged the library
72  * and document files into a single library file. This macro checks if the
73  * library version supports the old format
74  */
75 #define USE_OLD_DOC_FILE_FORMAT( major, minor ) \
76  ( LIB_VERSION( major, minor ) <= LIB_VERSION( 2, 3 ) )
77 
78 // Helper class to filter a list of libraries, and/or a list of PART_LIB
79 // in dialogs
81 {
82  wxArrayString m_allowedLibs;
83  bool m_filterPowerParts;
85  bool m_forceLoad; // When true, load a part lib from the lib
86  // which is given in m_allowedLibs[0]
87 
88 public:
90  {
91  m_filterPowerParts = false;
92  m_forceLoad = false;
93  }
94 
98  void AddLib( const wxString& aLibName )
99  {
100  m_allowedLibs.Add( aLibName );
101  m_forceLoad = false;
102  }
103 
104 
108  void LoadFrom( const wxString& aLibName )
109  {
110  m_allowedLibs.Clear();
111  m_allowedLibs.Add( aLibName );
112  m_forceLoad = true;
113  }
114 
119  {
120  m_allowedLibs.Clear();
121  m_forceLoad = false;
122  }
123 
127  void FilterPowerParts( bool aFilterEnable )
128  {
129  m_filterPowerParts = aFilterEnable;
130  }
131 
132  // Accessors
133 
138  bool GetFilterPowerParts() const { return m_filterPowerParts; }
139 
140 
145  const wxArrayString& GetAllowedLibList() const { return m_allowedLibs; }
146 
152  const wxString& GetLibSource() const
153  {
154  static wxString dummy;
155 
156  if( m_forceLoad && m_allowedLibs.GetCount() > 0 )
157  return m_allowedLibs[0];
158  else
159  return dummy;
160  }
161 };
162 
163 
164 /* Helpers for creating a list of part libraries. */
165 class PART_LIB;
166 class wxRegEx;
167 
172 {
173  bool operator() ( const wxString& aItem1, const wxString& aItem2 ) const
174  {
175  return aItem1 < aItem2;
176  }
177 };
178 
180 
181 typedef std::map< wxString, LIB_ALIAS*, AliasMapSort > LIB_ALIAS_MAP;
182 typedef std::vector< LIB_ALIAS* > LIB_ALIASES;
183 typedef boost::ptr_vector< PART_LIB > PART_LIBS_BASE;
184 
185 
193 {
194 public:
195 
196  static int s_modify_generation;
197 
199  {
201  }
202 
205  int GetModifyHash();
206 
214  PART_LIB* AddLibrary( const wxString& aFileName );
215 
225  PART_LIB* AddLibrary( const wxString& aFileName, PART_LIBS::iterator& aIterator );
226 
232  void LoadAllLibraries( PROJECT* aProject, bool aShowProgress=true );
233 
239  static void LibNamesAndPaths( PROJECT* aProject, bool doSave,
240  wxString* aPaths, wxArrayString* aNames=NULL );
241 
248  static const wxString CacheName( const wxString& aFullProjectFilename );
249 
257  PART_LIB* FindLibrary( const wxString& aName );
258 
259  PART_LIB* FindLibraryByFullFileName( const wxString& aFullFileName );
260 
269  wxArrayString GetLibraryNames( bool aSorted = true );
270 
282  LIB_PART* FindLibPart( const LIB_ID& aLibId, const wxString& aLibraryName = wxEmptyString );
283 
294  LIB_ALIAS* FindLibraryAlias( const LIB_ID& aLibId,
295  const wxString& aLibraryName = wxEmptyString );
296 
310  void FindLibraryNearEntries( std::vector<LIB_ALIAS*>& aCandidates, const wxString& aEntryName,
311  const wxString& aLibraryName = wxEmptyString );
312 
313  int GetLibraryCount() { return size(); }
314 };
315 
316 
322 class PART_LIB
323 {
324  int type;
325  wxFileName fileName;
326  wxDateTime timeStamp;
329  wxString header;
330  bool isModified;
332 
334  std::unique_ptr< SCH_PLUGIN > m_plugin;
335  std::unique_ptr< PROPERTIES > m_properties;
336 
337 public:
338  PART_LIB( int aType, const wxString& aFileName,
340  ~PART_LIB();
341 
345  int GetModHash() const { return m_mod_hash; }
346 
348 
349  void SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType );
350 
351  void Create( const wxString& aFileName = wxEmptyString );
352 
353  void SetFileName( const wxString& aFileName ) { fileName = aFileName; }
354 
360  bool IsEmpty() const
361  {
362  return m_plugin->GetSymbolLibCount( fileName.GetFullPath() ) == 0;
363  }
364 
371  int GetCount() const
372  {
373  return (int) m_plugin->GetSymbolLibCount( fileName.GetFullPath() );
374  }
375 
376  bool IsModified() const
377  {
378  return isModified;
379  }
380 
381  bool IsCache() const;
382 
383  void SetCache();
384 
385  bool IsBuffering() const;
386 
387  void EnableBuffering( bool aEnable = true );
388 
389  void Save( bool aSaveDocFile = true );
390 
395  bool IsReadOnly() const { return !fileName.IsFileWritable(); }
396 
402  void GetAliasNames( wxArrayString& aNames );
403 
409  void GetAliases( std::vector<LIB_ALIAS*>& aAliases );
410 
416  void GetEntryTypePowerNames( wxArrayString& aNames );
417 
424  LIB_ALIAS* FindAlias( const wxString& aName );
425 
435  LIB_PART* FindPart( const wxString& aName );
436 
445  void AddPart( LIB_PART* aPart );
446 
458  LIB_ALIAS* RemoveAlias( LIB_ALIAS* aEntry );
459 
467  LIB_PART* ReplacePart( LIB_PART* aOldPart, LIB_PART* aNewPart );
468 
474  const wxString GetName() const { return fileName.GetName(); }
475 
482  wxString GetFullFileName() { return fileName.GetFullPath(); }
483 
489  const wxString GetLogicalName()
490  {
491  /* for now is the filename without path or extension.
492 
493  Technically the library should not know its logical name!
494  This will eventually come out of a pair of lookup tables using a
495  reverse lookup using the full name or library pointer as a key.
496  Search will be by project lookup table and then user lookup table if
497  not found.
498  */
499  return fileName.GetName();
500  }
501 
502 
512  static PART_LIB* LoadLibrary( const wxString& aFileName );
513 
519  bool HasPowerParts();
520 };
521 
522 
526 bool operator==( const PART_LIB& aLibrary, const wxString& aName );
527 bool operator!=( const PART_LIB& aLibrary, const wxString& aName );
528 
529 #endif // CLASS_LIBRARY_H
bool HasPowerParts()
Function HasPowerParts.
int m_mod_hash
incremented each time library is changed.
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
Part library alias object definition.
bool IsBuffering() const
LIB_PART * ReplacePart(LIB_PART *aOldPart, LIB_PART *aNewPart)
Replace an existing part entry in the library.
void GetAliasNames(wxArrayString &aNames)
Load a string array with the names of all the entries in this library.
std::unique_ptr< SCH_PLUGIN > m_plugin
static void LibNamesAndPaths(PROJECT *aProject, bool doSave, wxString *aPaths, wxArrayString *aNames=NULL)
Function LibNamesAndPaths either saves or loads the names of the currently configured part libraries ...
void FilterPowerParts(bool aFilterEnable)
set the filtering of power parts
Class PROJECT holds project specific data.
Definition: project.h:56
bool m_filterPowerParts
true to filter (show only) power parts
Definition: class_library.h:84
void LoadAllLibraries(PROJECT *aProject, bool aShowProgress=true)
Function LoadAllLibraries loads 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 operator()(const wxString &aItem1, const wxString &aItem2) const
void GetEntryTypePowerNames(wxArrayString &aNames)
Load a string array with the names of entries of type POWER in this library.
bool isModified
Library modification status.
wxString GetFullFileName()
Function GetFullFileName returns the full file library name with path and extension.
LIB_ALIAS * RemoveAlias(LIB_ALIAS *aEntry)
Safely remove aEntry from the library and return the next entry.
void LoadFrom(const wxString &aLibName)
add a lib name to the allowed libraries
bool IsCache() const
wxArrayString GetLibraryNames(bool aSorted=true)
Function GetLibraryNames returns the list of part library file names without path and extension...
const wxString GetLogicalName()
Function GetLogicalName returns the logical name of the library.
int GetModifyHash()
Return the modification hash for all libraries.
wxString header
first line of loaded library.
SCH_IO_MGR::SCH_FILE_T GetPluginType() const
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
int GetLibraryCount()
int type
Library type indicator.
Class LIB_ID.
Definition: lib_id.h:56
bool operator==(const PART_LIB &aLibrary, const wxString &aName)
Case insensitive library name comparison.
PART_LIB * FindLibrary(const wxString &aName)
Function FindLibrary finds a part library by aName.
static const wxString CacheName(const wxString &aFullProjectFilename)
Function cacheName returns the name of the cache library after potentially fixing it from an older na...
LIB_ALIAS map sorting.
LIB_ALIAS * FindLibraryAlias(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Function FindLibraryEntry searches all libraries in the list for an entry.
std::map< wxString, LIB_ALIAS *, AliasMapSort > LIB_ALIAS_MAP
Alias map used by part library object.
void AddLib(const wxString &aLibName)
add a lib name to the allowed libraries
Definition: class_library.h:98
void EnableBuffering(bool aEnable=true)
void ClearLibList()
Clear the allowed libraries list (allows all libs)
void GetAliases(std::vector< LIB_ALIAS * > &aAliases)
Load a vector with all the entries in this library.
Base class that schematic file and library loading and saving plugins should derive from...
Definition: sch_io_mgr.h:184
const wxArrayString & GetAllowedLibList() const
Function GetAllowedLibList.
Legacy Eeschema file formats prior to s-expression.
Definition: sch_io_mgr.h:54
Class LIB_ITEM definition.
wxDateTime timeStamp
Library save time and date.
PART_LIB * AddLibrary(const wxString &aFileName)
Function AddLibrary allocates and adds a part library to the library list.
const wxString GetName() const
Return the file name without path or extension.
static PART_LIB * LoadLibrary(const wxString &aFileName)
Function LoadLibrary allocates and loads a part library file.
Class LIB_PART defines a library part object.
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)
Function FindLibPart searches all libraries in the list for a part.
bool GetFilterPowerParts() const
Function GetFilterPowerParts.
SCH_IO_MGR::SCH_FILE_T m_pluginType
bool IsModified() const
std::vector< LIB_ALIAS * > LIB_ALIASES
wxArrayString m_allowedLibs
a list of lib names to list some libraries if empty: no filter
Definition: class_library.h:82
Class SCH_LEGACY_PLUGIN.
wxFileName fileName
Library file name.
Class PART_LIBS is a collection of PART_LIBs.
void AddPart(LIB_PART *aPart)
Add aPart entry to library.
bool IsEmpty() const
Get library entry status.
int GetModHash() const
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
int versionMinor
Library minor version number.
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:66
bool IsReadOnly() const
Function IsReadOnly.
const wxString & GetLibSource() const
Function GetLibSource.
PART_LIB(int aType, const wxString &aFileName, SCH_IO_MGR::SCH_FILE_T aPluginType=SCH_IO_MGR::SCH_LEGACY)
void SetFileName(const wxString &aFileName)
void Create(const wxString &aFileName=wxEmptyString)
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
int versionMajor
Library major version number.
void FindLibraryNearEntries(std::vector< LIB_ALIAS * > &aCandidates, const wxString &aEntryName, const wxString &aLibraryName=wxEmptyString)
Function FindLibraryNearEntries Searches all libraries in the list for an entry, using a case insensi...
LIB_ALIAS * FindAlias(const wxString &aName)
Find LIB_ALIAS by aName.
bool operator!=(const PART_LIB &aLibrary, const wxString &aName)
int GetCount() const
Function GetCount returns the number of entries in the library.
LIB_PART * FindPart(const wxString &aName)
Find part by aName.
SCH_FILE_T
A set of file types that the SCH_IO_MGR knows about, and for which there has been a plugin written...
Definition: sch_io_mgr.h:52