KiCad PCB EDA Suite
Loading...
Searching...
No Matches
generate_alias_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) 2017 Chris Pavlina <[email protected]>
5 * Copyright (C) 2017-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <generate_alias_info.h>
22#include <string_utils.h>
23#include <template_fieldnames.h>
24#include <lib_symbol.h>
25#include <symbol_lib_table.h>
26#include <wx/log.h>
27
28static const wxString DescriptionFormat = wxS(
29 "<b>__NAME__</b>"
30 "__ALIASOF__"
31 "__DESC__"
32 "__KEY__"
33 "<hr><table border=0>"
34 "__FIELDS__"
35 "</table>" );
36
37static const wxString AliasOfFormat = wxS( "<br><i>" ) + _( "Derived from" ) + wxS( " %s (%s)</i>" );
38static const wxString DescFormat = wxS( "<br>%s" );
39static const wxString KeywordsFormat = wxS( "<br>" ) + _( "Keywords" ) + wxS( ": %s" );
40static const wxString FieldFormat = wxS(
41 "<tr>"
42 " <td><b>__NAME__</b></td>"
43 " <td>__VALUE__</td>"
44 "</tr>" );
45static const wxString DatasheetLinkFormat = wxS( "<a href=\"__HREF__\">__TEXT__</a>" );
46
47
49{
50 wxString m_html;
54 int m_unit;
55
56public:
57 FOOTPRINT_INFO_GENERATOR( SYMBOL_LIB_TABLE* aSymbolLibTable, LIB_ID const& aLibId, int aUnit )
59 m_sym_lib_table( aSymbolLibTable ),
60 m_lib_id( aLibId ),
61 m_symbol( nullptr ),
62 m_unit( aUnit )
63 { }
64
69 {
70 wxCHECK_RET( m_sym_lib_table, "Symbol library table pointer is not valid" );
71
72 if( !m_lib_id.IsValid() )
73 return;
74
75 try
76 {
78 }
79 catch( const IO_ERROR& ioe )
80 {
81 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
84 ioe.What() );
85 return;
86 }
87
88 if( m_symbol )
89 {
95 }
96 }
97
101 wxString GetHtml() const
102 {
103 return m_html;
104 }
105
106protected:
108 {
109 m_html.Replace( wxS( "__NAME__" ), EscapeHTML( UnescapeString( m_symbol->GetName() ) ) );
110 }
111
112
114 {
115 if( m_symbol->IsRoot() )
116 {
117 m_html.Replace( "__ALIASOF__", wxEmptyString );
118 }
119 else
120 {
121 wxString root_name = _( "Unknown" );
122 wxString root_desc = wxS( "" );
123
124 std::shared_ptr< LIB_SYMBOL > parent = m_symbol->GetParent().lock();
125
126 if( parent )
127 {
128 root_name = parent->GetName();
129 root_desc = parent->GetDesc();
130 }
131
132 m_html.Replace( wxS( "__ALIASOF__" ), wxString::Format( AliasOfFormat,
133 EscapeHTML( UnescapeString( root_name ) ),
134 EscapeHTML( root_desc ) ) );
135 }
136 }
137
138
140 {
141 wxString esc_desc = EscapeHTML( UnescapeString( m_symbol->GetDescription() ) );
142
143 // Add line breaks
144 esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
145
146 // Add links
147 esc_desc = LinkifyHTML( esc_desc );
148
149 m_html.Replace( wxS( "__DESC__" ), wxString::Format( DescFormat, esc_desc ) );
150 }
151
152
154 {
155 wxString keywords = m_symbol->GetKeyWords();
156
157 if( keywords.empty() )
158 m_html.Replace( wxS( "__KEY__" ), wxEmptyString );
159 else
160 m_html.Replace( wxS( "__KEY__" ), wxString::Format( KeywordsFormat, EscapeHTML( keywords ) ) );
161 }
162
163
164 wxString GetHtmlFieldRow( const SCH_FIELD& aField ) const
165 {
166 wxString name = aField.GetCanonicalName();
167 wxString text;
168 wxString fieldhtml = FieldFormat;
169
170 fieldhtml.Replace( wxS( "__NAME__" ), EscapeHTML( name ) );
171
172 switch( aField.GetId() )
173 {
174 case DATASHEET_FIELD:
176
177 if( text.IsEmpty() || text == wxT( "~" ) )
178 {
179 fieldhtml.Replace( wxS( "__VALUE__" ), text );
180 }
181 else
182 {
183 wxString datasheetlink = DatasheetLinkFormat;
184 datasheetlink.Replace( wxS( "__HREF__" ), EscapeHTML( text ) );
185
186 if( text.Length() > 75 )
187 text = text.Left( 72 ) + wxT( "..." );
188
189 datasheetlink.Replace( wxS( "__TEXT__" ), EscapeHTML( text ) );
190
191 fieldhtml.Replace( wxS( "__VALUE__" ), datasheetlink );
192 }
193
194 break;
195
196 case VALUE_FIELD:
197 // showing the value just repeats the name, so that's not much use...
198 return wxEmptyString;
199
200 case REFERENCE_FIELD:
201 text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
202 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
203 break;
204
205 default:
206 text = aField.GetShownText( false );
207 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
208 }
209
210 return fieldhtml;
211 }
212
213
215 {
216 wxString fieldtable;
217 std::vector<SCH_FIELD*> fields;
218
219 m_symbol->GetFields( fields );
220
221 for( const SCH_FIELD* field: fields )
222 fieldtable += GetHtmlFieldRow( *field );
223
224 if( m_symbol->IsAlias() )
225 {
226 std::shared_ptr<LIB_SYMBOL> parent = m_symbol->GetParent().lock();
227
228 // Append all of the unique parent fields if this is an alias.
229 if( parent )
230 {
231 std::vector<SCH_FIELD*> parentFields;
232
233 parent->GetFields( parentFields );
234
235 for( const SCH_FIELD* parentField : parentFields )
236 {
237 if( m_symbol->FindField( parentField->GetCanonicalName() ) )
238 continue;
239
240 fieldtable += GetHtmlFieldRow( *parentField );
241 }
242 }
243 }
244
245 m_html.Replace( wxS( "__FIELDS__" ), fieldtable );
246 }
247};
248
249
250wxString GenerateAliasInfo( SYMBOL_LIB_TABLE* aSymLibTable, LIB_ID const& aLibId, int aUnit )
251{
252 FOOTPRINT_INFO_GENERATOR gen( aSymLibTable, aLibId, aUnit );
253 gen.GenerateHtml();
254 return gen.GetHtml();
255}
const char * name
Definition: DXF_plotter.cpp:57
SYMBOL_LIB_TABLE * m_sym_lib_table
FOOTPRINT_INFO_GENERATOR(SYMBOL_LIB_TABLE *aSymbolLibTable, LIB_ID const &aLibId, int aUnit)
void GenerateHtml()
Generate the HTML internally.
wxString GetHtml() const
Return the generated HTML.
wxString GetHtmlFieldRow(const SCH_FIELD &aField) const
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
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 a library symbol object.
Definition: lib_symbol.h:77
wxString GetDescription() const override
Definition: lib_symbol.h:157
wxString GetKeyWords() const override
Definition: lib_symbol.h:170
void GetFields(std::vector< SCH_FIELD * > &aList)
Return a list of fields within this symbol.
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition: lib_symbol.h:193
bool IsAlias() const
Definition: lib_symbol.h:194
SCH_FIELD & GetDatasheetField() const
Return reference to the datasheet field.
wxString GetName() const override
Definition: lib_symbol.h:136
SCH_FIELD * FindField(const wxString &aFieldName, bool aCaseInsensitive=false)
Find a field within this symbol matching aFieldName and returns it or NULL if not found.
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:105
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: sch_field.cpp:321
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: sch_field.cpp:1193
int GetId() const
Definition: sch_field.h:133
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:200
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
wxString wx_str() const
Definition: utf8.cpp:45
#define _(s)
static const wxString KeywordsFormat
static const wxString DatasheetLinkFormat
static const wxString DescriptionFormat
static const wxString DescFormat
wxString GenerateAliasInfo(SYMBOL_LIB_TABLE *aSymLibTable, LIB_ID const &aLibId, int aUnit)
Return an HTML page describing a LIB_ID in a SYMBOL_LIB_TABLE.
static const wxString AliasOfFormat
static const wxString FieldFormat
static const wxString KeywordsFormat
static const wxString DescriptionFormat
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
wxString UnescapeString(const wxString &aSource)
wxString LinkifyHTML(wxString aStr)
Wraps links in HTML tags.
@ DATASHEET_FIELD
name of datasheet
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".