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
18 * along with this program. If not, see <https://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
61 FOOTPRINT_INFO_GENERATOR( const LIB_SYMBOL* aSymbol, int aUnit ) :
63 m_libs( nullptr ),
64 m_lib_id( aSymbol ? aSymbol->GetLibId() : LIB_ID() ),
65 m_symbol( aSymbol ),
66 m_unit( aUnit )
67 { }
68
73 {
74 if( m_symbol )
75 {
77 return;
78 }
79
80 wxCHECK_RET( m_libs, "Symbol library manager adapter pointer is not valid" );
81
82 if( !m_lib_id.IsValid() )
83 {
85 return;
86 }
87
88 try
89 {
90 m_symbol = m_libs->LoadSymbol( m_lib_id );
91 }
92 catch( const IO_ERROR& ioe )
93 {
94 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
95 m_lib_id.GetLibItemName().wx_str(),
96 m_lib_id.GetLibNickname().wx_str(),
97 ioe.What() );
99 return;
100 }
101
102 if( m_symbol )
104 else
106 }
107
111 wxString GetHtml() const
112 {
113 return m_html;
114 }
115
116protected:
118 {
119 SetHtmlName();
121 SetHtmlDesc();
124 }
125
127 {
128 m_html.Replace( wxS( "__NAME__" ), EscapeHTML( UnescapeString( m_symbol->GetName() ) ) );
129 }
130
132 {
133 if( m_symbol->IsRoot() )
134 {
135 m_html.Replace( "__ALIASOF__", wxEmptyString );
136 }
137 else
138 {
139 wxString root_name = _( "Unknown" );
140 wxString root_desc = wxS( "" );
141
142 std::shared_ptr< LIB_SYMBOL > parent = m_symbol->GetParent().lock();
143
144 if( parent )
145 {
146 root_name = parent->GetName();
147 root_desc = parent->GetDesc();
148 }
149
150 m_html.Replace( wxS( "__ALIASOF__" ), wxString::Format( AliasOfFormat,
151 EscapeHTML( UnescapeString( root_name ) ),
152 EscapeHTML( root_desc ) ) );
153 }
154 }
155
157 {
158 wxString esc_desc = EscapeHTML( UnescapeString( m_symbol->GetShownDescription( FOR_GUI ) ) );
159
160 // Add line breaks
161 esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
162
163 // Add links
164 esc_desc = LinkifyHTML( esc_desc );
165
166 m_html.Replace( wxS( "__DESC__" ), wxString::Format( DescFormat, esc_desc ) );
167 }
168
170 {
171 wxString keywords = m_symbol->GetShownKeyWords( FOR_GUI );
172
173 if( keywords.empty() )
174 m_html.Replace( wxS( "__KEY__" ), wxEmptyString );
175 else
176 m_html.Replace( wxS( "__KEY__" ), wxString::Format( KeywordsFormat, EscapeHTML( keywords ) ) );
177 }
178
179 wxString GetHtmlFieldRow( const SCH_FIELD& aField ) const
180 {
181 wxString name = aField.GetUntranslatedName();
182 wxString text;
183 wxString fieldhtml = FieldFormat;
184
185 fieldhtml.Replace( wxS( "__NAME__" ), EscapeHTML( name ) );
186
187 switch( aField.GetId() )
188 {
190 text = m_symbol->GetDatasheetField().GetShownText( FOR_GUI );
191
192 if( text.IsEmpty() || text == wxT( "~" ) )
193 {
194 fieldhtml.Replace( wxS( "__VALUE__" ), text );
195 }
196 else
197 {
198 wxString datasheetlink = LinkFormat;
199 datasheetlink.Replace( wxS( "__HREF__" ), EscapeHTML( text ) );
200
201 if( text.Length() > 75 )
202 text = text.Left( 72 ) + wxT( "..." );
203
204 datasheetlink.Replace( wxS( "__TEXT__" ), EscapeHTML( text ) );
205
206 fieldhtml.Replace( wxS( "__VALUE__" ), datasheetlink );
207 }
208
209 break;
210
211 case FIELD_T::VALUE:
212 // showing the value just repeats the name, so that's not much use...
213 return wxEmptyString;
214
216 text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
217 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
218 break;
219
220 default:
221 text = aField.GetShownText( FOR_GUI );
222
223 if( aField.HasHypertext() )
224 {
225 wxString link = LinkFormat;
226 link.Replace( wxS( "__HREF__" ), EscapeHTML( text ) );
227
228 if( text.Length() > 75 )
229 text = text.Left( 72 ) + wxT( "..." );
230
231 link.Replace( wxS( "__TEXT__" ), EscapeHTML( text ) );
232
233 fieldhtml.Replace( wxS( "__VALUE__" ), link );
234 }
235 else
236 {
237 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
238 }
239 }
240
241 return fieldhtml;
242 }
243
244
246 {
247 wxString fieldtable;
248 std::vector<SCH_FIELD*> fields;
249
250 m_symbol->GetFields( fields );
251
252 for( const SCH_FIELD* field: fields )
253 fieldtable += GetHtmlFieldRow( *field );
254
255 if( m_symbol->IsDerived() )
256 {
257 std::shared_ptr<LIB_SYMBOL> parent = m_symbol->GetParent().lock();
258
259 // Append all of the unique parent fields if this is a derived symbol.
260 if( parent )
261 {
262 std::vector<SCH_FIELD*> parentFields;
263
264 parent->GetFields( parentFields );
265
266 for( const SCH_FIELD* parentField : parentFields )
267 {
268 if( m_symbol->GetField( parentField->GetUntranslatedName() ) )
269 continue;
270
271 fieldtable += GetHtmlFieldRow( *parentField );
272 }
273 }
274 }
275
276 m_html.Replace( wxS( "__FIELDS__" ), fieldtable );
277 }
278
280 {
281 m_html.Replace( wxS( "__NAME__" ), wxEmptyString );
282 m_html.Replace( wxS( "__ALIASOF__" ), wxEmptyString );
283 m_html.Replace( wxS( "__DESC__" ), wxEmptyString );
284 m_html.Replace( wxS( "__KEY__" ), wxEmptyString );
285 m_html.Replace( wxS( "__FIELDS__" ), wxEmptyString );
286 }
287
288private:
289 wxString m_html;
294};
295
296
297wxString GenerateAliasInfo( SYMBOL_LIBRARY_ADAPTER* aLibs, LIB_ID const& aLibId, int aUnit )
298{
299 FOOTPRINT_INFO_GENERATOR gen( aLibs, aLibId, aUnit );
300 gen.GenerateHtml();
301 return gen.GetHtml();
302}
303
304
305wxString GenerateAliasInfo( const LIB_SYMBOL* aSymbol, int aUnit )
306{
307 FOOTPRINT_INFO_GENERATOR gen( aSymbol, aUnit );
308 gen.GenerateHtml();
309 return gen.GetHtml();
310}
const char * name
FOOTPRINT_INFO_GENERATOR(const LIB_SYMBOL *aSymbol, int aUnit)
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:45
Define a library symbol object.
Definition lib_symbol.h:114
wxString GetName() const override
Definition lib_symbol.h:183
wxString GetDesc() override
Definition lib_symbol.h:186
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:143
wxString GetUntranslatedName() const
Get the untranslated field name for storage, variable look-up, etc.
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
An interface to the global shared library manager that is schematic-specific and linked to one projec...
@ FOR_GUI
Definition common.h:93
#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_LIBRARY_ADAPTER.
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".