KiCad PCB EDA Suite
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 <pavlina.chris@gmail.com>
5  * Copyright (C) 2017-2021 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 <kicad_string.h>
23 #include <template_fieldnames.h>
24 #include <lib_symbol.h>
25 #include <symbol_lib_table.h>
26 
27 
28 static const wxString DescriptionFormat =
29  "<b>__NAME__</b>"
30  "__ALIASOF__"
31  "__DESC__"
32  "__KEY__"
33  "<hr><table border=0>"
34  "__FIELDS__"
35  "</table>";
36 
37 static const wxString AliasOfFormat = "<br><i>" + _( "Alias of" ) + " %s (%s)</i>";
38 static const wxString DescFormat = "<br>%s";
39 static const wxString KeywordsFormat = "<br>" + _( "Keywords" ) + ": %s";
40 static const wxString FieldFormat =
41  "<tr>"
42  " <td><b>__NAME__</b></td>"
43  " <td>__VALUE__</td>"
44  "</tr>";
45 static const wxString DatasheetLinkFormat = "<a href=\"__HREF__\">__TEXT__</a>";
46 
47 
49 {
50  wxString m_html;
54  int m_unit;
55 
56 public:
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 
68  void GenerateHtml()
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( wxString::Format( _( "Error occurred loading symbol %s from library %s."
82  "\n\n%s" ),
85  ioe.What() ) );
86  return;
87  }
88 
89  if( m_symbol )
90  {
91  SetHtmlName();
93  SetHtmlDesc();
96  }
97  }
98 
102  wxString GetHtml() const
103  {
104  return m_html;
105  }
106 
107 protected:
108  void SetHtmlName()
109  {
110  m_html.Replace( "__NAME__", EscapeHTML( m_symbol->GetName() ) );
111  }
112 
113 
115  {
116  if( m_symbol->IsRoot() )
117  {
118  m_html.Replace( "__ALIASOF__", wxEmptyString );
119  }
120  else
121  {
122  wxString root_name = _( "Unknown" );
123  wxString root_desc = "";
124 
125  std::shared_ptr< LIB_PART > parent = m_symbol->GetParent().lock();
126 
127  if( parent )
128  {
129  root_name = parent->GetName();
130  root_desc = parent->GetDescription();
131  }
132 
133  m_html.Replace( "__ALIASOF__", wxString::Format( AliasOfFormat,
134  EscapeHTML( root_name ),
135  EscapeHTML( root_desc ) ) );
136  }
137  }
138 
139 
140  void SetHtmlDesc()
141  {
142  wxString raw_desc = m_symbol->GetDescription();
143 
144  m_html.Replace( "__DESC__", wxString::Format( DescFormat, EscapeHTML( raw_desc ) ) );
145  }
146 
147 
149  {
150  wxString keywords = m_symbol->GetKeyWords();
151 
152  if( keywords.empty() )
153  m_html.Replace( "__KEY__", wxEmptyString );
154  else
155  m_html.Replace( "__KEY__", wxString::Format( KeywordsFormat, EscapeHTML( keywords ) ) );
156  }
157 
158 
159  wxString GetHtmlFieldRow( const LIB_FIELD& aField ) const
160  {
161  wxString name = aField.GetCanonicalName();
162  wxString text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
163  wxString fieldhtml = FieldFormat;
164 
165  fieldhtml.Replace( "__NAME__", EscapeHTML( name ) );
166 
167  switch( aField.GetId() )
168  {
169  case DATASHEET_FIELD:
170  text = m_symbol->GetDatasheetField().GetText();
171 
172  if( text.IsEmpty() || text == wxT( "~" ) )
173  {
174  fieldhtml.Replace( "__VALUE__", text );
175  }
176  else
177  {
178  wxString datasheetlink = DatasheetLinkFormat;
179  datasheetlink.Replace( "__HREF__", EscapeHTML( text ) );
180 
181  if( text.Length() > 75 )
182  text = text.Left( 72 ) + wxT( "..." );
183 
184  datasheetlink.Replace( "__TEXT__", EscapeHTML( text ) );
185 
186  fieldhtml.Replace( "__VALUE__", datasheetlink );
187  }
188 
189  break;
190 
191  case VALUE_FIELD:
192  // showing the value just repeats the name, so that's not much use...
193  return wxEmptyString;
194 
195  default:
196  fieldhtml.Replace( "__VALUE__", EscapeHTML( text ) );
197  }
198 
199  return fieldhtml;
200  }
201 
202 
204  {
205  wxString fieldtable;
206  std::vector<LIB_FIELD*> fields;
207 
208  m_symbol->GetFields( fields );
209 
210  for( const LIB_FIELD* field: fields )
211  fieldtable += GetHtmlFieldRow( *field );
212 
213  if( m_symbol->IsAlias() )
214  {
215  std::shared_ptr<LIB_PART> parent = m_symbol->GetParent().lock();
216 
217  // Append all of the unique parent fields if this is an alias.
218  if( parent )
219  {
220  std::vector<LIB_FIELD*> parentFields;
221 
222  parent->GetFields( parentFields );
223 
224  for( const LIB_FIELD* parentField : parentFields )
225  {
226  if( m_symbol->FindField( parentField->GetCanonicalName() ) )
227  continue;
228 
229  fieldtable += GetHtmlFieldRow( *parentField );
230  }
231  }
232  }
233 
234  m_html.Replace( "__FIELDS__", fieldtable );
235  }
236 };
237 
238 
239 wxString GenerateAliasInfo( SYMBOL_LIB_TABLE* aSymLibTable, LIB_ID const& aLibId, int aUnit )
240 {
241  FOOTPRINT_INFO_GENERATOR gen( aSymLibTable, aLibId, aUnit );
242  gen.GenerateHtml();
243  return gen.GetHtml();
244 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
void GenerateHtml()
Generate the HTML internally.
LIB_FIELD & GetDatasheetField()
Return reference to the datasheet field.
wxString GetName() const override
Definition: lib_symbol.h:129
static const wxString AliasOfFormat
Field object used in symbol libraries.
Definition: lib_field.h:59
LIB_PART * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_PART having aName from the library given by aNickname.
static const wxString DescriptionFormat
int GetId() const
Definition: lib_field.h:138
wxString GetKeyWords() const
Definition: lib_symbol.h:151
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:168
name of datasheet
#define gen(mvar, chg_bit, get, set, add, sub, changed)
wxString GetHtml() const
Return the generated HTML.
LIB_FIELD * FindField(const wxString &aFieldName)
Find a field within this part matching aFieldName and returns it or NULL if not found.
Definition: lib_symbol.cpp:953
Field Value of part, i.e. "3.3K".
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 KeywordsFormat
static const wxString DescFormat
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
PART_REF & GetParent()
Definition: lib_symbol.h:120
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: lib_field.cpp:373
Define a library symbol object.
Definition: lib_symbol.h:93
void GetFields(std::vector< LIB_FIELD * > &aList)
Return a list of fields within this part.
Definition: lib_symbol.cpp:905
SYMBOL_LIB_TABLE * m_sym_lib_table
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:92
wxString GetDescription() override
Definition: lib_symbol.h:138
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
const char * name
Definition: DXF_plotter.cpp:59
#define _(s)
Definition: 3d_actions.cpp:33
bool IsAlias() const
Definition: lib_symbol.h:168
wxString wx_str() const
Definition: utf8.cpp:51
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: lib_field.cpp:296
FOOTPRINT_INFO_GENERATOR(SYMBOL_LIB_TABLE *aSymbolLibTable, LIB_ID const &aLibId, int aUnit)
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition: lib_symbol.h:167
static const wxString FieldFormat
wxString GetHtmlFieldRow(const LIB_FIELD &aField) const
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:346
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
static const wxString DatasheetLinkFormat
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133