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 The KiCad Developers, see AUTHORS.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 <richio.h>
36#include <kicommon.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
68{
69public:
70 virtual ~LIB_TABLE_IO() = default;
71
78 virtual std::unique_ptr<LINE_READER> GetReader( const wxString& aURI ) const = 0;
79
83 virtual bool CanSaveToUri( const wxString& aURI ) const = 0;
84
91 virtual bool UrisAreEquivalent( const wxString& aURI1, const wxString& aURI2 ) const = 0;
92
96 virtual std::unique_ptr<OUTPUTFORMATTER> GetWriter( const wxString& aURI ) const = 0;
97};
98
99
106{
107public:
108 FILE_LIB_TABLE_IO() = default;
109
110 std::unique_ptr<LINE_READER> GetReader( const wxString& aURI ) const override;
111
112 bool CanSaveToUri( const wxString& aURI ) const override;
113
114 bool UrisAreEquivalent( const wxString& aURI1, const wxString& aURI2 ) const override;
115
116 std::unique_ptr<OUTPUTFORMATTER> GetWriter( const wxString& aURI ) const override;
117};
118
119
125{
126public:
128 enabled( true ),
129 visible( true ),
130 m_loaded( false ),
131 m_parent( nullptr )
132 {
133 }
134
136 {
137 }
138
139 LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
140 const wxString& aDescr = wxEmptyString, LIB_TABLE* aParent = nullptr ) :
141 nickName( aNick ),
142 description( aDescr ),
143 enabled( true ),
144 visible( true ),
145 m_loaded( false ),
146 m_parent( aParent )
147 {
148 SetOptions( aOptions );
149 SetFullURI( aURI );
150 }
151
152 bool operator==( const LIB_TABLE_ROW& r ) const;
153
154 bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
155
159 const wxString& GetNickName() const { return nickName; }
160
164 void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
165
169 bool GetIsLoaded() const { return m_loaded; }
170
174 void SetLoaded( bool aLoaded ) { m_loaded = aLoaded; };
175
179 bool GetIsEnabled() const { return enabled; }
180
184 void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }
185
186 bool GetIsVisible() const { return visible; }
187
188 void SetVisible( bool aVisible = true ) { visible = aVisible; }
189
190 virtual bool LibraryExists() const = 0;
191
192 virtual bool Refresh() { return false; }
193
197 virtual const wxString GetType() const = 0;
198
203 virtual void SetType( const wxString& aType ) = 0;
204
205 virtual bool SupportsSettingsDialog() const { return false; }
206
207 virtual void ShowSettingsDialog( wxWindow* aParent ) const {}
208
215 const wxString GetFullURI( bool aSubstituted = false ) const;
216
220 void SetFullURI( const wxString& aFullURI );
221
226 const wxString& GetOptions() const { return options; }
227
231 void SetOptions( const wxString& aOptions );
232
236 const wxString& GetDescr() const { return description; }
237
241 void SetDescr( const wxString& aDescr ) { description = aDescr; }
242
243 LIB_TABLE* GetParent() const { return m_parent; }
244
245 void SetParent( LIB_TABLE* aParent ) { m_parent = aParent; }
246
247 std::mutex& GetMutex() { return m_loadMutex; }
248
253 const std::map<std::string, UTF8>& GetProperties() const { return properties; }
254
263 void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
264
266 {
267 return do_clone();
268 }
269
270protected:
272 nickName( aRow.nickName ),
273 uri_user( aRow.uri_user ),
274 options( aRow.options ),
275 description( aRow.description ),
276 enabled( aRow.enabled ),
277 visible( aRow.visible ),
278 m_loaded( aRow.m_loaded ),
279 m_parent( aRow.m_parent ),
280 properties( aRow.properties )
281 {
282 }
283
284 void operator=( const LIB_TABLE_ROW& aRow );
285
286private:
287 virtual LIB_TABLE_ROW* do_clone() const = 0;
288
289 void setProperties( const std::map<std::string, UTF8>& aProperties );
290
291private:
292 wxString nickName;
293 wxString uri_user;
294 wxString options;
295 wxString description;
296
297 bool enabled = true;
298 bool visible = true;
299 bool m_loaded = false;
301
302 std::map<std::string, UTF8> properties;
303
304 std::mutex m_loadMutex;
305};
306
307
354{
355public:
368 virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
369
380 virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
381
392 LIB_TABLE( LIB_TABLE* aFallBackTable = nullptr,
393 std::unique_ptr<LIB_TABLE_IO> aTableIo = nullptr );
394
395 virtual ~LIB_TABLE();
396
403 bool operator==( const LIB_TABLE& r ) const
404 {
405 if( m_rows.size() == r.m_rows.size() )
406 {
407 unsigned i;
408
409 for( i = 0; i < m_rows.size() && m_rows[i] == r.m_rows[i]; ++i )
410 ;
411
412 if( i == m_rows.size() )
413 return true;
414 }
415
416 return false;
417 }
418
419 bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
420
424 unsigned GetCount() const
425 {
426 return m_rows.size();
427 }
428
434 LIB_TABLE_ROW& At( unsigned aIndex )
435 {
436 return m_rows[aIndex];
437 }
438
442 const LIB_TABLE_ROW& At( unsigned aIndex ) const
443 {
444 return m_rows[aIndex];
445 }
446
455 bool IsEmpty( bool aIncludeFallback = true );
456
461 const wxString GetDescription( const wxString& aNickname );
462
469 bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
470
477 bool HasLibraryWithPath( const wxString& aPath ) const;
478
483 std::vector<wxString> GetLogicalLibs();
484
488 wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
489
502 bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
503
509 bool RemoveRow( const LIB_TABLE_ROW* aRow );
510
515 bool ReplaceRow( size_t aIndex, LIB_TABLE_ROW* aRow );
516
523 bool ChangeRowOrder( size_t aIndex, int aOffset );
524
528 void TransferRows( LIB_TABLE_ROWS& aRowsList );
529
534 const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
535
544 void Load( const wxString& aFileName );
545
551 void Save( const wxString& aFileName ) const;
552
565 static std::map<std::string, UTF8> ParseOptions( const std::string& aOptionsList );
566
577 static UTF8 FormatOptions( const std::map<std::string, UTF8>* aProperties );
578
584 int GetVersion() const
585 {
586 return m_version;
587 }
588
589protected:
590 /*
591 * Do not make this public. It MUST be called with a lock already in place.
592 */
593 void clear();
594
603 LIB_TABLE_ROW* findRow( const wxString& aNickname, bool aCheckIfEnabled = false ) const;
604
608 bool doInsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
609
616 bool migrate();
617
618 /*
619 * Do not make this public. It MUST be called with a lock already in place.
620 */
621 void reindex();
622
623protected:
624 // Injected I/O interface
625 std::unique_ptr<LIB_TABLE_IO> m_io;
626
628
630 mutable int m_version;
631
633 // TODO: This should really be private; but the lib table grids re-use it
634 // (without using m_rowsMap).
636
638 std::map<wxString, LIB_TABLE_ROWS_ITER> m_rowsMap;
639
641 mutable std::shared_mutex m_mutex;
642};
643
644#endif // _LIB_TABLE_BASE_H_
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
Read a model file and creates a generic display structure.
bool operator==(const wxAuiPaneInfo &aLhs, const wxAuiPaneInfo &aRhs)
bool CanSaveToUri(const wxString &aURI) const override
Check if the given URI is writable.
bool UrisAreEquivalent(const wxString &aURI1, const wxString &aURI2) const override
Compare two URIs for equivalence.
FILE_LIB_TABLE_IO()=default
std::unique_ptr< LINE_READER > GetReader(const wxString &aURI) const override
Create a reader for the given URI.
std::unique_ptr< OUTPUTFORMATTER > GetWriter(const wxString &aURI) const override
Save the given table to the given URI.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
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...
LIB_TABLE_IO abstracts the file I/O operations for the library table loading and saving.
virtual std::unique_ptr< OUTPUTFORMATTER > GetWriter(const wxString &aURI) const =0
Save the given table to the given URI.
virtual ~LIB_TABLE_IO()=default
virtual std::unique_ptr< LINE_READER > GetReader(const wxString &aURI) const =0
Create a reader for the given URI.
virtual bool CanSaveToUri(const wxString &aURI) const =0
Check if the given URI is writable.
virtual bool UrisAreEquivalent(const wxString &aURI1, const wxString &aURI2) const =0
Compare two URIs for equivalence.
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()
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
void SetVisible(bool aVisible=true)
void operator=(const LIB_TABLE_ROW &aRow)
void SetNickName(const wxString &aNickName)
Change the logical name of this library, useful for an editor.
void setProperties(const std::map< std::string, UTF8 > &aProperties)
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.
virtual void ShowSettingsDialog(wxWindow *aParent) const
bool enabled
Whether the LIB_TABLE_ROW is enabled.
void SetParent(LIB_TABLE *aParent)
const std::map< std::string, UTF8 > & GetProperties() const
Return the constant #PROPERTIES for this library (LIB_TABLE_ROW).
bool m_loaded
Whether the LIB_TABLE_ROW is loaded.
virtual bool Refresh()
virtual bool LibraryExists() const =0
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
std::map< std::string, UTF8 > properties
LIB_TABLE_ROW(const wxString &aNick, const wxString &aURI, const wxString &aOptions, const wxString &aDescr=wxEmptyString, LIB_TABLE *aParent=nullptr)
LIB_TABLE_ROW * clone() const
bool GetIsEnabled() const
void SetOptions(const wxString &aOptions)
Change the library options strings.
virtual bool SupportsSettingsDialog() const
bool GetIsVisible() const
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
std::shared_mutex m_mutex
Mutex to protect access to the rows vector.
std::map< wxString, LIB_TABLE_ROWS_ITER > m_rowsMap
this is a non-owning index into the LIB_TABLE_ROWS 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 operator==(const LIB_TABLE &r) const
Compares this table against another.
LIB_TABLE_ROWS m_rows
Owning set of rows.
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.
LIB_TABLE(LIB_TABLE *aFallBackTable=nullptr, std::unique_ptr< LIB_TABLE_IO > aTableIo=nullptr)
Build a library table by pre-pending this table fragment in front of aFallBackTable.
LIB_TABLE * m_fallBack
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...
std::unique_ptr< LIB_TABLE_IO > m_io
bool operator!=(const LIB_TABLE &r) const
const LIB_TABLE_ROW & At(unsigned aIndex) const
Get the 'n'th LIB_TABLE_ROW object.
int GetVersion() const
Returns the version number (0 if unset)
An interface used to output 8 bit text in a convenient way.
Definition richio.h:323
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition project.h:98
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:72
#define KICOMMON_API
Definition kicommon.h:28
boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER
KICOMMON_API LIB_TABLE_ROW * new_clone(const LIB_TABLE_ROW &aRow)
Allows boost pointer containers to make clones of the data stored in them.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition ptree.cpp:198