KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_table_base.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) 2010-2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright (C) 2012 Wayne Stambaugh <[email protected]>
6 * Copyright (C) 2012-2021 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
26#ifndef _LIB_TABLE_BASE_H_
27#define _LIB_TABLE_BASE_H_
28
29#include <map>
30#include <boost/ptr_container/ptr_vector.hpp>
31#include <memory>
32#include <mutex>
33#include <shared_mutex>
34#include <project.h>
35#include <string_utf8_map.h>
36#include <richio.h>
37
38class OUTPUTFORMATTER;
39class LIB_TABLE_LEXER;
40class LIB_ID;
41class LIB_TABLE_ROW;
42class LIB_TABLE_GRID;
43class LIB_TABLE;
44class IO_ERROR;
45class wxWindow;
46
47
48typedef boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS;
49typedef LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER;
50typedef LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER;
51
52
59
60
66{
67public:
69 enabled( true ),
70 visible( true ),
71 m_loaded( false ),
72 m_parent( nullptr )
73 {
74 }
75
77 {
78 }
79
80 LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
81 const wxString& aDescr = wxEmptyString, LIB_TABLE* aParent = nullptr ) :
82 nickName( aNick ),
83 description( aDescr ),
84 enabled( true ),
85 visible( true ),
86 m_loaded( false ),
87 m_parent( aParent )
88 {
89 properties.reset();
90 SetOptions( aOptions );
91 SetFullURI( aURI );
92 }
93
94 bool operator==( const LIB_TABLE_ROW& r ) const;
95
96 bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
97
101 const wxString& GetNickName() const { return nickName; }
102
106 void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
107
111 bool GetIsLoaded() const { return m_loaded; }
112
116 void SetLoaded( bool aLoaded ) { m_loaded = aLoaded; };
117
121 bool GetIsEnabled() const { return enabled; }
122
126 void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }
127
128 bool GetIsVisible() const { return visible; }
129
130 void SetVisible( bool aVisible = true ) { visible = aVisible; }
131
132 virtual bool Refresh() { return false; }
133
137 virtual const wxString GetType() const = 0;
138
143 virtual void SetType( const wxString& aType ) = 0;
144
145 virtual bool SupportsSettingsDialog() const { return false; }
146
147 virtual void ShowSettingsDialog( wxWindow* aParent ) const {}
148
155 const wxString GetFullURI( bool aSubstituted = false ) const;
156
160 void SetFullURI( const wxString& aFullURI );
161
166 const wxString& GetOptions() const { return options; }
167
171 void SetOptions( const wxString& aOptions );
172
176 const wxString& GetDescr() const { return description; }
177
181 void SetDescr( const wxString& aDescr ) { description = aDescr; }
182
183 LIB_TABLE* GetParent() const { return m_parent; }
184
185 void SetParent( LIB_TABLE* aParent ) { m_parent = aParent; }
186
187 std::mutex& GetMutex() { return m_loadMutex; }
188
193 const STRING_UTF8_MAP* GetProperties() const { return properties.get(); }
194
203 void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
204
206 {
207 return do_clone();
208 }
209
210protected:
212 nickName( aRow.nickName ),
213 uri_user( aRow.uri_user ),
214 options( aRow.options ),
215 description( aRow.description ),
216 enabled( aRow.enabled ),
217 visible( aRow.visible ),
218 m_loaded( aRow.m_loaded ),
219 m_parent( aRow.m_parent )
220 {
221 if( aRow.properties )
222 properties = std::make_unique<STRING_UTF8_MAP>( *aRow.properties.get() );
223 else
224 properties.reset();
225 }
226
227 void operator=( const LIB_TABLE_ROW& aRow );
228
229private:
230 virtual LIB_TABLE_ROW* do_clone() const = 0;
231
232 void setProperties( STRING_UTF8_MAP* aProperties );
233
234 wxString nickName;
235 wxString uri_user;
236 wxString options;
237 wxString description;
238
239 bool enabled = true;
240 bool visible = true;
241 bool m_loaded = false;
243
244 std::unique_ptr<STRING_UTF8_MAP> properties;
245
246 std::mutex m_loadMutex;
247};
248
249
296{
297public:
310 virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
311
322 virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
323
332 LIB_TABLE( LIB_TABLE* aFallBackTable = nullptr );
333
334 virtual ~LIB_TABLE();
335
337 void Clear();
338
345 bool operator==( const LIB_TABLE& r ) const
346 {
347 if( m_rows.size() == r.m_rows.size() )
348 {
349 unsigned i;
350
351 for( i = 0; i < m_rows.size() && m_rows[i] == r.m_rows[i]; ++i )
352 ;
353
354 if( i == m_rows.size() )
355 return true;
356 }
357
358 return false;
359 }
360
361 bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
362
366 unsigned GetCount() const
367 {
368 return m_rows.size();
369 }
370
376 LIB_TABLE_ROW& At( unsigned aIndex )
377 {
378 return m_rows[aIndex];
379 }
380
384 const LIB_TABLE_ROW& At( unsigned aIndex ) const
385 {
386 return m_rows[aIndex];
387 }
388
397 bool IsEmpty( bool aIncludeFallback = true );
398
403 const wxString GetDescription( const wxString& aNickname );
404
411 bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
412
419 bool HasLibraryWithPath( const wxString& aPath ) const;
420
425 std::vector<wxString> GetLogicalLibs();
426
430 wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
431
444 bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
445
451 bool RemoveRow( const LIB_TABLE_ROW* aRow );
452
457 bool ReplaceRow( size_t aIndex, LIB_TABLE_ROW* aRow );
458
465 bool ChangeRowOrder( size_t aIndex, int aOffset );
466
470 void TransferRows( LIB_TABLE_ROWS& aRowsList );
471
476 const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
477
486 void Load( const wxString& aFileName );
487
493 void Save( const wxString& aFileName ) const;
494
507 static STRING_UTF8_MAP* ParseOptions( const std::string& aOptionsList );
508
519 static UTF8 FormatOptions( const STRING_UTF8_MAP* aProperties );
520
526 int GetVersion() const
527 {
528 return m_version;
529 }
530
531protected:
540 LIB_TABLE_ROW* findRow( const wxString& aNickname, bool aCheckIfEnabled = false ) const;
541
548 bool migrate();
549
550 void reindex();
551
552protected:
554
556 mutable int m_version;
557
559 // TODO: This should really be private; but the lib table grids re-use it
560 // (without using m_rowsMap).
562
564 std::map<wxString, LIB_TABLE_ROWS_ITER> m_rowsMap;
565
567 mutable std::shared_mutex m_mutex;
568};
569
570#endif // _LIB_TABLE_BASE_H_
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
bool visible
Whether the LIB_TABLE_ROW is visible in choosers.
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
std::mutex & GetMutex()
wxString nickName
const wxString & GetDescr() const
Return the description of the library referenced by this row.
virtual LIB_TABLE_ROW * do_clone() const =0
bool GetIsLoaded() const
std::unique_ptr< STRING_UTF8_MAP > properties
void SetVisible(bool aVisible=true)
void operator=(const LIB_TABLE_ROW &aRow)
wxString options
void SetNickName(const wxString &aNickName)
Change the logical name of this library, useful for an editor.
wxString uri_user
what user entered from UI or loaded from disk
LIB_TABLE * GetParent() const
bool operator!=(const LIB_TABLE_ROW &r) const
virtual const wxString GetType() const =0
Return the type of library represented by this row.
wxString description
void SetEnabled(bool aEnabled=true)
Change the enabled status of this library.
void SetDescr(const wxString &aDescr)
Change the description of the library referenced by this row.
void Format(OUTPUTFORMATTER *out, int nestLevel) const
Serialize this object as utf8 text to an OUTPUTFORMATTER, and tries to make it look good using multip...
virtual void ShowSettingsDialog(wxWindow *aParent) const
bool enabled
Whether the LIB_TABLE_ROW is enabled.
void SetParent(LIB_TABLE *aParent)
bool m_loaded
Whether the LIB_TABLE_ROW is loaded.
virtual bool Refresh()
void setProperties(STRING_UTF8_MAP *aProperties)
LIB_TABLE_ROW(const LIB_TABLE_ROW &aRow)
void SetLoaded(bool aLoaded)
Mark the row as being a loaded library.
virtual ~LIB_TABLE_ROW()
LIB_TABLE * m_parent
Pointer to the table this row lives in (maybe null)
virtual void SetType(const wxString &aType)=0
Change the type of library represented by this row that must be implemented in the derived object to ...
const wxString & GetNickName() const
std::mutex m_loadMutex
LIB_TABLE_ROW(const wxString &aNick, const wxString &aURI, const wxString &aOptions, const wxString &aDescr=wxEmptyString, LIB_TABLE *aParent=nullptr)
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
LIB_TABLE_ROW * clone() const
bool operator==(const LIB_TABLE_ROW &r) const
bool GetIsEnabled() const
void SetOptions(const wxString &aOptions)
Change the library options strings.
const STRING_UTF8_MAP * GetProperties() const
Return the constant #PROPERTIES for this library (LIB_TABLE_ROW).
virtual bool SupportsSettingsDialog() const
bool GetIsVisible() const
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
bool ReplaceRow(size_t aIndex, LIB_TABLE_ROW *aRow)
Replaces the Nth row with the given new row.
std::shared_mutex m_mutex
Mutex to protect access to the rows vector.
const wxString GetDescription(const wxString &aNickname)
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
std::map< wxString, LIB_TABLE_ROWS_ITER > m_rowsMap
this is a non-owning index into the LIB_TABLE_ROWS table
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
int m_version
Versioning to handle importing old tables.
LIB_TABLE_ROW & At(unsigned aIndex)
Get the 'n'th LIB_TABLE_ROW object.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
void TransferRows(LIB_TABLE_ROWS &aRowsList)
Takes ownership of another list of rows; the original list will be freed.
bool operator==(const LIB_TABLE &r) const
Compares this table against another.
LIB_TABLE_ROWS m_rows
Owning set of rows.
virtual ~LIB_TABLE()
bool migrate()
Updates the env vars from older version of KiCad, provided they do not currently resolve to anything.
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
bool HasLibraryWithPath(const wxString &aPath) const
Test for the existence of aPath in the library table.
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.
bool RemoveRow(const LIB_TABLE_ROW *aRow)
Removes a row from the table and frees the pointer.
void Clear()
Delete all rows.
LIB_TABLE * m_fallBack
wxString GetFullURI(const wxString &aLibNickname, bool aExpandEnvVars=true) const
Return the full URI of the library mapped to aLibNickname.
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.
unsigned GetCount() const
Get the number of rows contained in the table.
virtual void Parse(LIB_TABLE_LEXER *aLexer)=0
Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objec...
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
bool ChangeRowOrder(size_t aIndex, int aOffset)
Moves a row within the table.
bool operator!=(const LIB_TABLE &r) const
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
static STRING_UTF8_MAP * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a #PROPERTIES object which is returned.
const LIB_TABLE_ROW & At(unsigned aIndex) const
Get the 'n'th LIB_TABLE_ROW object.
LIB_TABLE_ROW * findRow(const wxString &aNickname, bool aCheckIfEnabled=false) const
Return a LIB_TABLE_ROW if aNickname is found in this table or in any chained fallBack table fragment,...
int GetVersion() const
Returns the version number (0 if unset)
static UTF8 FormatOptions(const STRING_UTF8_MAP *aProperties)
Returns a list of options from the aProperties parameter.
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:73
A name/value tuple with unique names and optional values.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
LIB_TABLE_ROW * new_clone(const LIB_TABLE_ROW &aRow)
Allows boost pointer containers to make clones of the data stored in them.
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER