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#include <kicommon.h>
38
39class OUTPUTFORMATTER;
40class LIB_TABLE_LEXER;
41class LIB_ID;
42class LIB_TABLE_ROW;
43class LIB_TABLE_GRID;
44class LIB_TABLE;
45class IO_ERROR;
46class wxWindow;
47
48
49typedef boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS;
50typedef LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER;
51typedef LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER;
52
53
60
61
67{
68public:
70 enabled( true ),
71 visible( true ),
72 m_loaded( false ),
73 m_parent( nullptr )
74 {
75 }
76
78 {
79 }
80
81 LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
82 const wxString& aDescr = wxEmptyString, LIB_TABLE* aParent = nullptr ) :
83 nickName( aNick ),
84 description( aDescr ),
85 enabled( true ),
86 visible( true ),
87 m_loaded( false ),
88 m_parent( aParent )
89 {
90 properties.reset();
91 SetOptions( aOptions );
92 SetFullURI( aURI );
93 }
94
95 bool operator==( const LIB_TABLE_ROW& r ) const;
96
97 bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
98
102 const wxString& GetNickName() const { return nickName; }
103
107 void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
108
112 bool GetIsLoaded() const { return m_loaded; }
113
117 void SetLoaded( bool aLoaded ) { m_loaded = aLoaded; };
118
122 bool GetIsEnabled() const { return enabled; }
123
127 void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }
128
129 bool GetIsVisible() const { return visible; }
130
131 void SetVisible( bool aVisible = true ) { visible = aVisible; }
132
133 virtual bool Refresh() { return false; }
134
138 virtual const wxString GetType() const = 0;
139
144 virtual void SetType( const wxString& aType ) = 0;
145
146 virtual bool SupportsSettingsDialog() const { return false; }
147
148 virtual void ShowSettingsDialog( wxWindow* aParent ) const {}
149
156 const wxString GetFullURI( bool aSubstituted = false ) const;
157
161 void SetFullURI( const wxString& aFullURI );
162
167 const wxString& GetOptions() const { return options; }
168
172 void SetOptions( const wxString& aOptions );
173
177 const wxString& GetDescr() const { return description; }
178
182 void SetDescr( const wxString& aDescr ) { description = aDescr; }
183
184 LIB_TABLE* GetParent() const { return m_parent; }
185
186 void SetParent( LIB_TABLE* aParent ) { m_parent = aParent; }
187
188 std::mutex& GetMutex() { return m_loadMutex; }
189
194 const STRING_UTF8_MAP* GetProperties() const { return properties.get(); }
195
204 void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
205
207 {
208 return do_clone();
209 }
210
211protected:
213 nickName( aRow.nickName ),
214 uri_user( aRow.uri_user ),
215 options( aRow.options ),
216 description( aRow.description ),
217 enabled( aRow.enabled ),
218 visible( aRow.visible ),
219 m_loaded( aRow.m_loaded ),
220 m_parent( aRow.m_parent )
221 {
222 if( aRow.properties )
223 properties = std::make_unique<STRING_UTF8_MAP>( *aRow.properties.get() );
224 else
225 properties.reset();
226 }
227
228 void operator=( const LIB_TABLE_ROW& aRow );
229
230private:
231 virtual LIB_TABLE_ROW* do_clone() const = 0;
232
233 void setProperties( STRING_UTF8_MAP* aProperties );
234
235 wxString nickName;
236 wxString uri_user;
237 wxString options;
238 wxString description;
239
240 bool enabled = true;
241 bool visible = true;
242 bool m_loaded = false;
244
245 std::unique_ptr<STRING_UTF8_MAP> properties;
246
247 std::mutex m_loadMutex;
248};
249
250
297{
298public:
311 virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
312
323 virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
324
333 LIB_TABLE( LIB_TABLE* aFallBackTable = nullptr );
334
335 virtual ~LIB_TABLE();
336
338 void Clear();
339
346 bool operator==( const LIB_TABLE& r ) const
347 {
348 if( m_rows.size() == r.m_rows.size() )
349 {
350 unsigned i;
351
352 for( i = 0; i < m_rows.size() && m_rows[i] == r.m_rows[i]; ++i )
353 ;
354
355 if( i == m_rows.size() )
356 return true;
357 }
358
359 return false;
360 }
361
362 bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
363
367 unsigned GetCount() const
368 {
369 return m_rows.size();
370 }
371
377 LIB_TABLE_ROW& At( unsigned aIndex )
378 {
379 return m_rows[aIndex];
380 }
381
385 const LIB_TABLE_ROW& At( unsigned aIndex ) const
386 {
387 return m_rows[aIndex];
388 }
389
398 bool IsEmpty( bool aIncludeFallback = true );
399
404 const wxString GetDescription( const wxString& aNickname );
405
412 bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
413
420 bool HasLibraryWithPath( const wxString& aPath ) const;
421
426 std::vector<wxString> GetLogicalLibs();
427
431 wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
432
445 bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
446
452 bool RemoveRow( const LIB_TABLE_ROW* aRow );
453
458 bool ReplaceRow( size_t aIndex, LIB_TABLE_ROW* aRow );
459
466 bool ChangeRowOrder( size_t aIndex, int aOffset );
467
471 void TransferRows( LIB_TABLE_ROWS& aRowsList );
472
477 const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
478
487 void Load( const wxString& aFileName );
488
494 void Save( const wxString& aFileName ) const;
495
508 static STRING_UTF8_MAP* ParseOptions( const std::string& aOptionsList );
509
520 static UTF8 FormatOptions( const STRING_UTF8_MAP* aProperties );
521
527 int GetVersion() const
528 {
529 return m_version;
530 }
531
532protected:
541 LIB_TABLE_ROW* findRow( const wxString& aNickname, bool aCheckIfEnabled = false ) const;
542
549 bool migrate();
550
551 void reindex();
552
553protected:
555
557 mutable int m_version;
558
560 // TODO: This should really be private; but the lib table grids re-use it
561 // (without using m_rowsMap).
563
565 std::map<wxString, LIB_TABLE_ROWS_ITER> m_rowsMap;
566
568 mutable std::shared_mutex m_mutex;
569};
570
571#endif // _LIB_TABLE_BASE_H_
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
reads a model file and creates a generic display structure
bool operator==(const wxAuiPaneInfo &aLhs, const wxAuiPaneInfo &aRhs)
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.
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.
virtual void ShowSettingsDialog(wxWindow *aParent) const
void SetParent(LIB_TABLE *aParent)
virtual bool Refresh()
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)
LIB_TABLE_ROW * clone() const
bool GetIsEnabled() const
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.
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 * 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...
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: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: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:200