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 <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" ) +
38 wxS( " %s (%s)</i>" );
39static const wxString DescFormat = wxS( "<br>%s" );
40static const wxString KeywordsFormat = wxS( "<br>" ) + _( "Keywords" ) + wxS( ": %s" );
41static const wxString FieldFormat = wxS(
42 "<tr>"
43 " <td><b>__NAME__</b></td>"
44 " <td>__VALUE__</td>"
45 "</tr>" );
46static const wxString LinkFormat = wxS( "<a href=\"__HREF__\">__TEXT__</a>" );
47
48
50{
51public:
52 FOOTPRINT_INFO_GENERATOR( SYMBOL_LIB_TABLE* aSymbolLibTable, LIB_ID const& aLibId, int aUnit ) :
54 m_sym_lib_table( aSymbolLibTable ),
55 m_lib_id( aLibId ),
56 m_symbol( nullptr ),
57 m_unit( aUnit )
58 { }
59
64 {
65 wxCHECK_RET( m_sym_lib_table, "Symbol library table pointer is not valid" );
66
67 if( !m_lib_id.IsValid() )
68 return;
69
70 try
71 {
73 }
74 catch( const IO_ERROR& ioe )
75 {
76 wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ),
79 ioe.What() );
80 return;
81 }
82
83 if( m_symbol )
84 {
90 }
91 }
92
96 wxString GetHtml() const
97 {
98 return m_html;
99 }
100
101protected:
103 {
104 m_html.Replace( wxS( "__NAME__" ), EscapeHTML( UnescapeString( m_symbol->GetName() ) ) );
105 }
106
108 {
109 if( m_symbol->IsRoot() )
110 {
111 m_html.Replace( "__ALIASOF__", wxEmptyString );
112 }
113 else
114 {
115 wxString root_name = _( "Unknown" );
116 wxString root_desc = wxS( "" );
117
118 std::shared_ptr< LIB_SYMBOL > parent = m_symbol->GetParent().lock();
119
120 if( parent )
121 {
122 root_name = parent->GetName();
123 root_desc = parent->GetDesc();
124 }
125
126 m_html.Replace( wxS( "__ALIASOF__" ), wxString::Format( AliasOfFormat,
127 EscapeHTML( UnescapeString( root_name ) ),
128 EscapeHTML( root_desc ) ) );
129 }
130 }
131
133 {
134 wxString esc_desc = EscapeHTML( UnescapeString( m_symbol->GetDescription() ) );
135
136 // Add line breaks
137 esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
138
139 // Add links
140 esc_desc = LinkifyHTML( esc_desc );
141
142 m_html.Replace( wxS( "__DESC__" ), wxString::Format( DescFormat, esc_desc ) );
143 }
144
146 {
147 wxString keywords = m_symbol->GetKeyWords();
148
149 if( keywords.empty() )
150 m_html.Replace( wxS( "__KEY__" ), wxEmptyString );
151 else
152 m_html.Replace( wxS( "__KEY__" ), wxString::Format( KeywordsFormat, EscapeHTML( keywords ) ) );
153 }
154
155 wxString GetHtmlFieldRow( const SCH_FIELD& aField ) const
156 {
157 wxString name = aField.GetCanonicalName();
158 wxString text;
159 wxString fieldhtml = FieldFormat;
160
161 fieldhtml.Replace( wxS( "__NAME__" ), EscapeHTML( name ) );
162
163 switch( aField.GetId() )
164 {
165 case FIELD_T::DATASHEET:
167
168 if( text.IsEmpty() || text == wxT( "~" ) )
169 {
170 fieldhtml.Replace( wxS( "__VALUE__" ), text );
171 }
172 else
173 {
174 wxString datasheetlink = LinkFormat;
175 datasheetlink.Replace( wxS( "__HREF__" ), EscapeHTML( text ) );
176
177 if( text.Length() > 75 )
178 text = text.Left( 72 ) + wxT( "..." );
179
180 datasheetlink.Replace( wxS( "__TEXT__" ), EscapeHTML( text ) );
181
182 fieldhtml.Replace( wxS( "__VALUE__" ), datasheetlink );
183 }
184
185 break;
186
187 case FIELD_T::VALUE:
188 // showing the value just repeats the name, so that's not much use...
189 return wxEmptyString;
190
191 case FIELD_T::REFERENCE:
192 text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
193 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
194 break;
195
196 default:
197 text = aField.GetShownText( false );
198
199 if( aField.IsHypertext() )
200 {
201 wxString link = LinkFormat;
202 link.Replace( wxS( "__HREF__" ), EscapeHTML( text ) );
203
204 if( text.Length() > 75 )
205 text = text.Left( 72 ) + wxT( "..." );
206
207 link.Replace( wxS( "__TEXT__" ), EscapeHTML( text ) );
208
209 fieldhtml.Replace( wxS( "__VALUE__" ), link );
210 }
211 else
212 {
213 fieldhtml.Replace( wxS( "__VALUE__" ), EscapeHTML( text ) );
214 }
215 }
216
217 return fieldhtml;
218 }
219
220
222 {
223 wxString fieldtable;
224 std::vector<SCH_FIELD*> fields;
225
226 m_symbol->GetFields( fields );
227
228 for( const SCH_FIELD* field: fields )
229 fieldtable += GetHtmlFieldRow( *field );
230
231 if( m_symbol->IsDerived() )
232 {
233 std::shared_ptr<LIB_SYMBOL> parent = m_symbol->GetParent().lock();
234
235 // Append all of the unique parent fields if this is a derived symbol.
236 if( parent )
237 {
238 std::vector<SCH_FIELD*> parentFields;
239
240 parent->GetFields( parentFields );
241
242 for( const SCH_FIELD* parentField : parentFields )
243 {
244 if( m_symbol->GetField( parentField->GetCanonicalName() ) )
245 continue;
246
247 fieldtable += GetHtmlFieldRow( *parentField );
248 }
249 }
250 }
251
252 m_html.Replace( wxS( "__FIELDS__" ), fieldtable );
253 }
254
255private:
256 wxString m_html;
261};
262
263
264wxString GenerateAliasInfo( SYMBOL_LIB_TABLE* aSymLibTable, LIB_ID const& aLibId, int aUnit )
265{
266 FOOTPRINT_INFO_GENERATOR gen( aSymLibTable, aLibId, aUnit );
267 gen.GenerateHtml();
268 return gen.GetHtml();
269}
const char * name
Definition: DXF_plotter.cpp:62
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:85
wxString GetDescription() const override
Definition: lib_symbol.h:170
wxString GetKeyWords() const override
Definition: lib_symbol.h:183
void GetFields(std::vector< SCH_FIELD * > &aList, bool aVisibleOnly=false) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
SCH_FIELD & GetDatasheetField()
Return reference to the datasheet field.
Definition: lib_symbol.h:346
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition: lib_symbol.h:206
bool IsDerived() const
Definition: lib_symbol.h:207
SCH_FIELD * GetField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName; return nullptr if not found.
wxString GetName() const override
Definition: lib_symbol.h:149
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:118
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: sch_field.cpp:301
bool IsHypertext() const override
Allow items to support hypertext actions when hovered/clicked.
Definition: sch_field.cpp:1018
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,...
Definition: sch_field.cpp:1117
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:191
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 DescriptionFormat
static const wxString DescFormat
static const wxString LinkFormat
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.