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-2019 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 <class_libentry.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>" + _( "Key words:" ) + " %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  {
77  m_symbol = const_cast< LIB_PART* >( m_sym_lib_table->LoadSymbol( m_lib_id ) );
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()
103  {
104  return m_html;
105  }
106 
107 protected:
108  void SetHtmlName()
109  {
110  m_html.Replace( "__NAME__", EscapedHTML( 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(
134  "__ALIASOF__", wxString::Format(
135  AliasOfFormat, EscapedHTML( root_name ), EscapedHTML( 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, EscapedHTML( 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__",
156  wxString::Format( KeywordsFormat, EscapedHTML( keywords ) ) );
157  }
158 
159 
160  wxString GetHtmlFieldRow( LIB_FIELD const & aField )
161  {
162  wxString name = aField.GetName();
163  wxString text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
164  wxString fieldhtml = FieldFormat;
165 
166  fieldhtml.Replace( "__NAME__", EscapedHTML( name ) );
167 
168  switch( aField.GetId() )
169  {
170  case DATASHEET:
171  text = m_symbol->GetDocFileName();
172 
173  if( text.IsEmpty() || text == wxT( "~" ) )
174  {
175  fieldhtml.Replace( "__VALUE__", text );
176  }
177  else
178  {
179  wxString datasheetlink = DatasheetLinkFormat;
180  datasheetlink.Replace( "__HREF__", EscapedHTML( text ) );
181 
182  if( text.Length() > 75 )
183  text = text.Left( 72 ) + wxT( "..." );
184 
185  datasheetlink.Replace( "__TEXT__", EscapedHTML( text ) );
186 
187  fieldhtml.Replace( "__VALUE__", datasheetlink );
188  }
189 
190  break;
191 
192  case VALUE:
193  // showing the value just repeats the name, so that's not much use...
194  return wxEmptyString;
195 
196  default:
197  fieldhtml.Replace( "__VALUE__", EscapedHTML( text ) );
198  }
199 
200  return fieldhtml;
201  }
202 
203 
205  {
206  wxString fieldtable;
207  LIB_FIELDS fields;
208  m_symbol->GetFields( fields );
209 
210  for( auto const & field: fields )
211  {
212  fieldtable += GetHtmlFieldRow( field );
213  }
214 
215  if( m_symbol->IsAlias() )
216  {
217  std::shared_ptr< LIB_PART > parent = m_symbol->GetParent().lock();
218 
219  // Append all of the unique parent fields if this is an alias.
220  if( parent )
221  {
222  LIB_FIELDS parentFields;
223 
224  parent->GetFields( parentFields );
225 
226  for( auto const& parentField : parentFields )
227  {
228  if( m_symbol->FindField( parentField.GetName() ) )
229  continue;
230 
231  fieldtable += GetHtmlFieldRow( parentField );
232  }
233  }
234  }
235 
236  m_html.Replace( "__FIELDS__", fieldtable );
237  }
238 };
239 
240 
241 wxString GenerateAliasInfo( SYMBOL_LIB_TABLE* aSymLibTable, LIB_ID const& aLibId, int aUnit )
242 {
243  FOOTPRINT_INFO_GENERATOR gen( aSymLibTable, aLibId, aUnit );
244  gen.GenerateHtml();
245  return gen.GetHtml();
246 }
wxString GetName(bool aTranslate=true) const
Returns the field name.
Definition: lib_field.cpp:366
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
void GenerateHtml()
Generate the HTML internally.
name of datasheet
wxString GetName() const override
static const wxString AliasOfFormat
wxString EscapedHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:286
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
wxString GetHtmlFieldRow(LIB_FIELD const &aField)
int GetId() const
Definition: lib_field.h:135
wxString GetKeyWords() const
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool IsValid() const
Definition: lib_id.h:171
#define VALUE
LIB_FIELD * FindField(const wxString &aFieldName)
Find a field within this part matching aFieldName and returns it or NULL if not found.
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:33
Class LIB_PIN definition.
PART_REF & GetParent()
Define a library symbol object.
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:97
wxString GetDocFileName() const
wxString GetDescription() override
wxString GetHtml()
Return the generated HTML.
const char * name
Definition: DXF_plotter.cpp:60
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:31
std::vector< LIB_FIELD > LIB_FIELDS
Definition: lib_field.h:214
bool IsAlias() const
wxString wx_str() const
Definition: utf8.cpp:51
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: lib_field.cpp:300
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.
static const wxString FieldFormat
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
static const wxString DatasheetLinkFormat
void GetFields(LIB_FIELDS &aList)
Return a list of fields within this part.