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 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>" + _( "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=\"__VALUE__\">__VALUE__</a>";
46 
47 
49 {
50  wxString m_html;
54  int m_unit;
55 
56 public:
57  ALIAS_INFO_GENERATOR( SYMBOL_LIB_TABLE* aSymbolLibTable, LIB_ID const& aLibId, int aUnit )
58  : m_html( DescriptionFormat ),
59  m_sym_lib_table( aSymbolLibTable ),
60  m_lib_id( aLibId ),
61  m_alias( 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_alias = const_cast< LIB_ALIAS* >( 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" ),
83  m_lib_id.GetLibItemName().wx_str(),
84  m_lib_id.GetLibNickname().wx_str(),
85  ioe.What() ) );
86  return;
87  }
88 
89  if( m_alias )
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_alias->GetName() ) );
111  }
112 
113 
115  {
116  if( m_alias->IsRoot() )
117  {
118  m_html.Replace( "__ALIASOF__", wxEmptyString );
119  }
120  else
121  {
122  wxString root_name = _( "Unknown" );
123  wxString root_desc = "";
124 
125  LIB_PART* root = m_alias->GetPart();
126  LIB_ALIAS* root_alias = root ? root->GetAlias( 0 ) : nullptr;
127 
128  if( root )
129  root_name = root->GetName();
130 
131  if( root_alias )
132  root_desc = root_alias->GetDescription();
133 
134  m_html.Replace(
135  "__ALIASOF__", wxString::Format(
136  AliasOfFormat, EscapedHTML( root_name ), EscapedHTML( root_desc ) ) );
137  }
138  }
139 
140 
141  void SetHtmlDesc()
142  {
143  wxString raw_desc = m_alias->GetDescription();
144 
145  m_html.Replace( "__DESC__", wxString::Format( DescFormat, EscapedHTML( raw_desc ) ) );
146  }
147 
148 
150  {
151  wxString keywords = m_alias->GetKeyWords();
152 
153  if( keywords.empty() )
154  m_html.Replace( "__KEY__", wxEmptyString );
155  else
156  m_html.Replace( "__KEY__",
157  wxString::Format( KeywordsFormat, EscapedHTML( keywords ) ) );
158  }
159 
160 
161  wxString GetHtmlFieldRow( LIB_FIELD const & aField )
162  {
163  wxString name = aField.GetName();
164  wxString text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
165  wxString fieldhtml = FieldFormat;
166 
167  fieldhtml.Replace( "__NAME__", EscapedHTML( name ) );
168 
169  switch( aField.GetId() )
170  {
171  case DATASHEET:
172  {
173  wxString datasheetlink = DatasheetLinkFormat;
174  datasheetlink.Replace( "__VALUE__", EscapedHTML( text ) );
175  fieldhtml.Replace( "__VALUE__", datasheetlink );
176  }
177  break;
178 
179  default:
180  fieldhtml.Replace( "__VALUE__", EscapedHTML( text ) );
181  }
182 
183  return fieldhtml;
184  }
185 
186 
188  {
189  wxString fieldtable;
190  LIB_FIELDS fields;
191  m_alias->GetPart()->GetFields( fields );
192 
193  for( auto const & field: fields )
194  {
195  fieldtable += GetHtmlFieldRow( field );
196  }
197 
198  m_html.Replace( "__FIELDS__", fieldtable );
199  }
200 };
201 
202 
203 wxString GenerateAliasInfo( SYMBOL_LIB_TABLE* aSymLibTable, LIB_ID const& aLibId, int aUnit )
204 {
205  ALIAS_INFO_GENERATOR gen( aSymLibTable, aLibId, aUnit );
206  gen.GenerateHtml();
207  return gen.GetHtml();
208 }
LIB_ALIAS * LoadSymbol(const wxString &aNickname, const wxString &aAliasName)
Load a LIB_ALIAS having aAliasName from the library given by aNickname.
bool IsValid() const
Function IsValid.
Definition: lib_id.h:177
Part library alias object definition.
SYMBOL_LIB_TABLE * m_sym_lib_table
name of datasheet
LIB_ALIAS * GetAlias(size_t aIndex)
static const wxString AliasOfFormat
wxString EscapedHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
Definition: string.cpp:170
Class LIB_FIELD is used in symbol libraries.
Definition: lib_field.h:60
static const wxString DescriptionFormat
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: lib_field.cpp:529
Class LIB_ID.
Definition: lib_id.h:56
bool IsRoot() const
wxString wx_str() const
Definition: utf8.cpp:48
void GenerateHtml()
Generate the HTML internally.
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
int GetId() const
Definition: lib_field.h:136
const wxString & GetName() const
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
Class LIB_ITEM definition.
wxString GetDescription() const
Class LIB_PART defines a library part object.
wxString GetHtmlFieldRow(LIB_FIELD const &aField)
wxString GetName(bool aTranslate=true) const
Returns the field name.
Definition: lib_field.cpp:605
wxString GetHtml()
Return the generated HTML.
LIB_PART * GetPart() const
Function GetPart gets the shared LIB_PART.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const wxString & GetName() const
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
ALIAS_INFO_GENERATOR(SYMBOL_LIB_TABLE *aSymbolLibTable, LIB_ID const &aLibId, int aUnit)
std::vector< LIB_FIELD > LIB_FIELDS
Definition: lib_field.h:261
const char * name
static const wxString FieldFormat
const UTF8 & GetLibNickname() const
Function GetLibNickname.
Definition: lib_id.h:108
wxString GetKeyWords() const
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
static const wxString DatasheetLinkFormat
void GetFields(LIB_FIELDS &aList)
Function GetFields returns a list of fields withing this part.