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 return;
70
71 try
72 {
73 m_symbol = m_libs->LoadSymbol( m_lib_id );
74 }
75 catch( const IO_ERROR& ioe )
76 {
77 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
78 m_lib_id.GetLibItemName().wx_str(),
79 m_lib_id.GetLibNickname().wx_str(),
80 ioe.What() );
81 return;
82 }
83
84 if( m_symbol )
85 {
91 }
92 }
93
97 wxString GetHtml() const
98 {
99 return m_html;
100 }
101
102protected:
104 {
105 m_html.Replace( wxS( "__NAME__" ), EscapeHTML( UnescapeString( m_symbol->GetName() ) ) );
106 }
107
109 {
110 if( m_symbol->IsRoot() )
111 {
112 m_html.Replace( "__ALIASOF__", wxEmptyString );
113 }
114 else
115 {
116 wxString root_name = _( "Unknown" );
117 wxString root_desc = wxS( "" );
118
119 std::shared_ptr< LIB_SYMBOL > parent = m_symbol->GetParent().lock();
120
121 if( parent )
122 {
123 root_name = parent->GetName();
124 root_desc = parent->GetDesc();
125 }
126
127 m_html.Replace( wxS( "__ALIASOF__" ), wxString::Format( AliasOfFormat,
128 EscapeHTML( UnescapeString( root_name ) ),
129 EscapeHTML( root_desc ) ) );
130 }
131 }
132
134 {
135 wxString esc_desc = EscapeHTML( UnescapeString( m_symbol->GetShownDescription() ) );
136
137 // Add line breaks
138 esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
139
140 // Add links
141 esc_desc = LinkifyHTML( esc_desc );
142
143 m_html.Replace( wxS( "__DESC__" ), wxString::Format( DescFormat, esc_desc ) );
144 }
145
147 {
148 wxString keywords = m_symbol->GetShownKeyWords();
149
150 if( keywords.empty() )
151 m_html.Replace( wxS( "__KEY__" ), wxEmptyString );
152 else
153 m_html.Replace( wxS( "__KEY__" ), wxString::Format( KeywordsFormat, EscapeHTML( keywords ) ) );
154 }
155
156 wxString GetHtmlFieldRow( const SCH_FIELD& aField ) const
157 {
158 wxString name = aField.GetCanonicalName();
159 wxString text;
160 wxString fieldhtml = FieldFormat;
161
162 fieldhtml.Replace( wxS( "__NAME__" ), EscapeHTML( name ) );
163
164 switch( aField.GetId() )
165 {
167 text = m_symbol->GetDatasheetField().GetShownText( false );
168
169 if( text.IsEmpty() || text == wxT( "~" ) )
170 {
171 fieldhtml.Replace( wxS( "__VALUE__" ), text );
172 }
173 else
174 {
175 wxString datasheetlink = LinkFormat;
176 datasheetlink.Replace( wxS( "__HREF__" ), EscapeHTML( text ) );
177
178 if( text.Length() > 75 )
179 text = text.Left( 72 ) + wxT( "..." );
180
181 datasheetlink.Replace( wxS( "__TEXT__" ), EscapeHTML( text ) );
182
183 fieldhtml.Replace( wxS( "__VALUE__" ), datasheetlink );
184 }
185
186 break;
187
188 case FIELD_T::VALUE:
189 // showing the value just repeats the name, so that's not much use...
190 return wxEmptyString;
191
193 text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
194 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
195 break;
196
197 default:
198 text = aField.GetShownText( false );
199
200 if( aField.IsHypertext() )
201 {
202 wxString link = LinkFormat;
203 link.Replace( wxS( "__HREF__" ), EscapeHTML( text ) );
204
205 if( text.Length() > 75 )
206 text = text.Left( 72 ) + wxT( "..." );
207
208 link.Replace( wxS( "__TEXT__" ), EscapeHTML( text ) );
209
210 fieldhtml.Replace( wxS( "__VALUE__" ), link );
211 }
212 else
213 {
214 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
215 }
216 }
217
218 return fieldhtml;
219 }
220
221
223 {
224 wxString fieldtable;
225 std::vector<SCH_FIELD*> fields;
226
227 m_symbol->GetFields( fields );
228
229 for( const SCH_FIELD* field: fields )
230 fieldtable += GetHtmlFieldRow( *field );
231
232 if( m_symbol->IsDerived() )
233 {
234 std::shared_ptr<LIB_SYMBOL> parent = m_symbol->GetParent().lock();
235
236 // Append all of the unique parent fields if this is a derived symbol.
237 if( parent )
238 {
239 std::vector<SCH_FIELD*> parentFields;
240
241 parent->GetFields( parentFields );
242
243 for( const SCH_FIELD* parentField : parentFields )
244 {
245 if( m_symbol->GetField( parentField->GetCanonicalName() ) )
246 continue;
247
248 fieldtable += GetHtmlFieldRow( *parentField );
249 }
250 }
251 }
252
253 m_html.Replace( wxS( "__FIELDS__" ), fieldtable );
254 }
255
256private:
257 wxString m_html;
262};
263
264
265wxString GenerateAliasInfo( SYMBOL_LIBRARY_ADAPTER* aLibs, LIB_ID const& aLibId, int aUnit )
266{
267 FOOTPRINT_INFO_GENERATOR gen( aLibs, aLibId, aUnit );
268 gen.GenerateHtml();
269 return gen.GetHtml();
270}
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:85
wxString GetName() const override
Definition lib_symbol.h:148
wxString GetDesc() override
Definition lib_symbol.h:151
wxString GetFullText(int unit=1) const
Return the text of a field.
bool IsHypertext() const override
Allow items to support hypertext actions when hovered/clicked.
FIELD_T GetId() const
Definition sch_field.h:116
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
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".