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 The 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 <ki_exception.h>
23#include <string_utils.h>
24#include <template_fieldnames.h>
25#include <lib_symbol.h>
27#include <wx/log.h>
28
29static const wxString DescriptionFormat = wxS(
30 "<b>__NAME__</b>"
31 "__ALIASOF__"
32 "__DESC__"
33 "__KEY__"
34 "<hr><table border=0>"
35 "__FIELDS__"
36 "</table>" );
37
38static const wxString AliasOfFormat = wxS( "<br><i>" ) + _( "Derived from" ) +
39 wxS( " %s (%s)</i>" );
40static const wxString DescFormat = wxS( "<br>%s" );
41static const wxString KeywordsFormat = wxS( "<br>" ) + _( "Keywords" ) + wxS( ": %s" );
42static const wxString FieldFormat = wxS(
43 "<tr>"
44 " <td><b>__NAME__</b></td>"
45 " <td>__VALUE__</td>"
46 "</tr>" );
47static const wxString LinkFormat = wxS( "<a href=\"__HREF__\">__TEXT__</a>" );
48
49
51{
52public:
53 FOOTPRINT_INFO_GENERATOR( SYMBOL_LIBRARY_ADAPTER* aLibs, LIB_ID const& aLibId, int aUnit ) :
55 m_libs( aLibs ),
56 m_lib_id( aLibId ),
57 m_symbol( nullptr ),
58 m_unit( aUnit )
59 { }
60
65 {
66 wxCHECK_RET( m_libs, "Symbol library manager adapter pointer is not valid" );
67
68 if( !m_lib_id.IsValid() )
69 {
71 return;
72 }
73
74 try
75 {
76 m_symbol = m_libs->LoadSymbol( m_lib_id );
77 }
78 catch( const IO_ERROR& ioe )
79 {
80 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
81 m_lib_id.GetLibItemName().wx_str(),
82 m_lib_id.GetLibNickname().wx_str(),
83 ioe.What() );
85 return;
86 }
87
88 if( m_symbol )
89 {
95 }
96 else
97 {
99 }
100 }
101
105 wxString GetHtml() const
106 {
107 return m_html;
108 }
109
110protected:
112 {
113 m_html.Replace( wxS( "__NAME__" ), EscapeHTML( UnescapeString( m_symbol->GetName() ) ) );
114 }
115
117 {
118 if( m_symbol->IsRoot() )
119 {
120 m_html.Replace( "__ALIASOF__", wxEmptyString );
121 }
122 else
123 {
124 wxString root_name = _( "Unknown" );
125 wxString root_desc = wxS( "" );
126
127 std::shared_ptr< LIB_SYMBOL > parent = m_symbol->GetParent().lock();
128
129 if( parent )
130 {
131 root_name = parent->GetName();
132 root_desc = parent->GetDesc();
133 }
134
135 m_html.Replace( wxS( "__ALIASOF__" ), wxString::Format( AliasOfFormat,
136 EscapeHTML( UnescapeString( root_name ) ),
137 EscapeHTML( root_desc ) ) );
138 }
139 }
140
142 {
143 wxString esc_desc = EscapeHTML( UnescapeString( m_symbol->GetShownDescription() ) );
144
145 // Add line breaks
146 esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
147
148 // Add links
149 esc_desc = LinkifyHTML( esc_desc );
150
151 m_html.Replace( wxS( "__DESC__" ), wxString::Format( DescFormat, esc_desc ) );
152 }
153
155 {
156 wxString keywords = m_symbol->GetShownKeyWords();
157
158 if( keywords.empty() )
159 m_html.Replace( wxS( "__KEY__" ), wxEmptyString );
160 else
161 m_html.Replace( wxS( "__KEY__" ), wxString::Format( KeywordsFormat, EscapeHTML( keywords ) ) );
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 {
175 text = m_symbol->GetDatasheetField().GetShownText( false );
176
177 if( text.IsEmpty() || text == wxT( "~" ) )
178 {
179 fieldhtml.Replace( wxS( "__VALUE__" ), text );
180 }
181 else
182 {
183 wxString datasheetlink = LinkFormat;
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 FIELD_T::VALUE:
197 // showing the value just repeats the name, so that's not much use...
198 return wxEmptyString;
199
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
208 if( aField.HasHypertext() )
209 {
210 wxString link = LinkFormat;
211 link.Replace( wxS( "__HREF__" ), EscapeHTML( text ) );
212
213 if( text.Length() > 75 )
214 text = text.Left( 72 ) + wxT( "..." );
215
216 link.Replace( wxS( "__TEXT__" ), EscapeHTML( text ) );
217
218 fieldhtml.Replace( wxS( "__VALUE__" ), link );
219 }
220 else
221 {
222 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
223 }
224 }
225
226 return fieldhtml;
227 }
228
229
231 {
232 wxString fieldtable;
233 std::vector<SCH_FIELD*> fields;
234
235 m_symbol->GetFields( fields );
236
237 for( const SCH_FIELD* field: fields )
238 fieldtable += GetHtmlFieldRow( *field );
239
240 if( m_symbol->IsDerived() )
241 {
242 std::shared_ptr<LIB_SYMBOL> parent = m_symbol->GetParent().lock();
243
244 // Append all of the unique parent fields if this is a derived symbol.
245 if( parent )
246 {
247 std::vector<SCH_FIELD*> parentFields;
248
249 parent->GetFields( parentFields );
250
251 for( const SCH_FIELD* parentField : parentFields )
252 {
253 if( m_symbol->GetField( parentField->GetCanonicalName() ) )
254 continue;
255
256 fieldtable += GetHtmlFieldRow( *parentField );
257 }
258 }
259 }
260
261 m_html.Replace( wxS( "__FIELDS__" ), fieldtable );
262 }
263
265 {
266 m_html.Replace( wxS( "__NAME__" ), wxEmptyString );
267 m_html.Replace( wxS( "__ALIASOF__" ), wxEmptyString );
268 m_html.Replace( wxS( "__DESC__" ), wxEmptyString );
269 m_html.Replace( wxS( "__KEY__" ), wxEmptyString );
270 m_html.Replace( wxS( "__FIELDS__" ), wxEmptyString );
271 }
272
273private:
274 wxString m_html;
279};
280
281
282wxString GenerateAliasInfo( SYMBOL_LIBRARY_ADAPTER* aLibs, LIB_ID const& aLibId, int aUnit )
283{
284 FOOTPRINT_INFO_GENERATOR gen( aLibs, aLibId, aUnit );
285 gen.GenerateHtml();
286 return gen.GetHtml();
287}
const char * name
void GenerateHtml()
Generate the HTML internally.
SYMBOL_LIBRARY_ADAPTER * m_libs
wxString GetHtml() const
Return the generated HTML.
FOOTPRINT_INFO_GENERATOR(SYMBOL_LIBRARY_ADAPTER *aLibs, LIB_ID const &aLibId, int aUnit)
wxString GetHtmlFieldRow(const SCH_FIELD &aField) const
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
Define a library symbol object.
Definition lib_symbol.h:83
wxString GetName() const override
Definition lib_symbol.h:145
wxString GetDesc() override
Definition lib_symbol.h:148
wxString GetFullText(int unit=1) const
Return the text of a field.
bool HasHypertext() const override
Indicates that the item has at least one hypertext action.
FIELD_T GetId() const
Definition sch_field.h:120
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0, const wxString &aVariantName=wxEmptyString) const
An interface to the global shared library manager that is schematic-specific and linked to one projec...
#define _(s)
wxString GenerateAliasInfo(SYMBOL_LIBRARY_ADAPTER *aLibs, LIB_ID const &aLibId, int aUnit)
Return an HTML page describing a LIB_ID in a #SYMBOL_LIB_TABLE.
static const wxString KeywordsFormat
static const wxString DescriptionFormat
static const wxString DescFormat
static const wxString LinkFormat
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
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".