KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_info.cpp
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) 2011 Jean-Pierre Charras, <[email protected]>
5 * Copyright (C) 2013-2016 SoftPLC Corporation, Dick Hollenbeck <[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/*
27 * Functions to read footprint libraries and fill m_footprints by available footprints names
28 * and their documentation (comments and keywords)
29 */
30
31#include <footprint_info.h>
33#include <string_utils.h>
34#include <lib_id.h>
35#include <wx/tokenzr.h>
36
37FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aLibNickname,
38 const wxString& aFootprintName )
39{
40 if( aFootprintName.IsEmpty() )
41 return nullptr;
42
43 for( std::unique_ptr<FOOTPRINT_INFO>& fp : m_list )
44 {
45 if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
46 return fp.get();
47 }
48
49 return nullptr;
50}
51
52
53FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aFootprintName )
54{
55 if( aFootprintName.IsEmpty() )
56 return nullptr;
57
58 LIB_ID fpid;
59
60 wxCHECK_MSG( fpid.Parse( aFootprintName ) < 0, nullptr,
61 wxString::Format( wxT( "'%s' is not a valid LIB_ID." ), aFootprintName ) );
62
63 return GetFootprintInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
64}
65
66
67std::vector<SEARCH_TERM> FOOTPRINT_INFO::GetSearchTerms()
68{
69 std::vector<SEARCH_TERM> terms;
70
71 terms.emplace_back( SEARCH_TERM( GetLibNickname(), 4 ) );
72 terms.emplace_back( SEARCH_TERM( GetName(), 8 ) );
73 terms.emplace_back( SEARCH_TERM( GetLIB_ID().Format(), 16 ) );
74
75 wxStringTokenizer keywordTokenizer( GetKeywords(), " \t\r\n", wxTOKEN_STRTOK );
76
77 while( keywordTokenizer.HasMoreTokens() )
78 terms.emplace_back( SEARCH_TERM( keywordTokenizer.GetNextToken(), 4 ) );
79
80 // Also include keywords as one long string, just in case
81 terms.emplace_back( SEARCH_TERM( GetKeywords(), 1 ) );
82 terms.emplace_back( SEARCH_TERM( GetDesc(), 1 ) );
83
84 return terms;
85}
86
87
88bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
89{
90 return aLibrary == m_nickname;
91}
92
93
94bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs )
95{
96 int retv = StrNumCmp( lhs.m_nickname, rhs.m_nickname, false );
97
98 if( retv != 0 )
99 return retv < 0;
100
101 // Technically footprint names are not case sensitive because the file name is used
102 // as the footprint name. On windows this would be problematic because windows does
103 // not support case sensitive file names by default. This should not cause any issues
104 // and allow for a future change to use the name defined in the footprint file.
105 return StrNumCmp( lhs.m_fpname, rhs.m_fpname, false ) < 0;
106}
107
108
109void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )
110{
111 // @todo: go to a more HTML !<table>! ? centric output, possibly with recommendations
112 // for remedy of errors. Add numeric error codes to PARSE_ERROR, and switch on them for
113 // remedies, etc. Full access is provided to everything in every exception!
114
115 HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
116
117 dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
118
119 wxString msg;
120
121 while( std::unique_ptr<IO_ERROR> error = PopError() )
122 {
123 wxString tmp = EscapeHTML( error->Problem() );
124
125 // Preserve new lines in error messages so queued errors don't run together.
126 tmp.Replace( wxS( "\n" ), wxS( "<BR>" ) );
127 msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
128 }
129
130 dlg.AddHTML_Text( msg );
131
132 dlg.ShowModal();
133}
134
135
137{
138 wxString messages;
139
140 while( std::unique_ptr<IO_ERROR> error = PopError() )
141 {
142 if( !messages.IsEmpty() )
143 messages += wxS( "\n" );
144
145 messages += error->Problem();
146 }
147
148 return messages;
149}
int ShowModal() override
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
LIB_ID GetLIB_ID() const override
wxString GetName() const override
wxString m_fpname
Module name.
wxString GetLibNickname() const override
wxString GetKeywords()
std::vector< SEARCH_TERM > GetSearchTerms() override
wxString m_nickname
library as known in FP_LIB_TABLE
wxString GetDesc() override
void DisplayErrors(wxTopLevelWindow *aCaller=nullptr)
wxString GetErrorMessages()
Returns all accumulated errors as a newline-separated string for display in the status bar.
FOOTPRINT_INFO * GetFootprintInfo(const wxString &aFootprintName)
Get info for a footprint by id.
std::vector< std::unique_ptr< FOOTPRINT_INFO > > m_list
std::unique_ptr< IO_ERROR > PopError()
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:52
const UTF8 & GetLibItemName() const
Definition lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:87
#define _(s)
bool operator<(const FOOTPRINT_INFO &lhs, const FOOTPRINT_INFO &rhs)
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
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
A structure for storing weighted search terms.