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 
25 static const wxString DescriptionFormat =
26  "<b>__NAME__</b>"
27  "__ALIASOF__"
28  "__DESC__"
29  "__KEY__"
30  "<hr><table border=0>"
31  "__FIELDS__"
32  "</table>";
33 
34 static const wxString AliasOfFormat = "<br><i>" + _( "Alias of " ) + "%s (%s)</i>";
35 static const wxString DescFormat = "<br>%s";
36 static const wxString KeywordsFormat = "<br>" + _( "Keywords:" ) + " %s";
37 static const wxString FieldFormat =
38  "<tr>"
39  " <td><b>__NAME__</b></td>"
40  " <td>__VALUE__</td>"
41  "</tr>";
42 static const wxString DatasheetLinkFormat = "<a href=\"__VALUE__\">__VALUE__</a>";
43 
44 
46 {
47  wxString m_html;
48  LIB_ALIAS const * m_part;
49  int m_unit;
50 
51 public:
52  ALIAS_INFO_GENERATOR( LIB_ALIAS const * aAlias, int aUnit )
53  : m_html( DescriptionFormat ),
54  m_part( aAlias ),
55  m_unit( aUnit )
56  { }
57 
61  void GenerateHtml()
62  {
63  SetHtmlName();
65  SetHtmlDesc();
68  }
69 
73  wxString GetHtml()
74  {
75  return m_html;
76  }
77 
78 protected:
79  void SetHtmlName()
80  {
81  m_html.Replace( "__NAME__", EscapedHTML( m_part->GetName() ) );
82  }
83 
84 
86  {
87  if( m_part->IsRoot() )
88  {
89  m_html.Replace( "__ALIASOF__", wxEmptyString );
90  }
91  else
92  {
93  wxString root_name = _( "Unknown" );
94  wxString root_desc = "";
95 
96  LIB_PART* root = m_part->GetPart();
97  LIB_ALIAS* root_alias = root ? root->GetAlias( 0 ) : nullptr;
98 
99  if( root )
100  root_name = root->GetName();
101 
102  if( root_alias )
103  root_desc = root_alias->GetDescription();
104 
105  m_html.Replace(
106  "__ALIASOF__", wxString::Format(
107  AliasOfFormat, EscapedHTML( root_name ), EscapedHTML( root_desc ) ) );
108  }
109  }
110 
111 
112  void SetHtmlDesc()
113  {
114  wxString raw_desc = m_part->GetDescription();
115 
116  m_html.Replace( "__DESC__", wxString::Format( DescFormat, EscapedHTML( raw_desc ) ) );
117  }
118 
119 
121  {
122  wxString keywords = m_part->GetKeyWords();
123 
124  if( keywords.empty() )
125  m_html.Replace( "__KEY__", wxEmptyString );
126  else
127  m_html.Replace( "__KEY__",
128  wxString::Format( KeywordsFormat, EscapedHTML( keywords ) ) );
129  }
130 
131 
132  wxString GetHtmlFieldRow( LIB_FIELD const & aField )
133  {
134  wxString name = aField.GetName();
135  wxString text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
136  wxString fieldhtml = FieldFormat;
137 
138  fieldhtml.Replace( "__NAME__", EscapedHTML( name ) );
139 
140  switch( aField.GetId() )
141  {
142  case DATASHEET:
143  {
144  wxString datasheetlink = DatasheetLinkFormat;
145  datasheetlink.Replace( "__VALUE__", EscapedHTML( text ) );
146  fieldhtml.Replace( "__VALUE__", datasheetlink );
147  }
148  break;
149 
150  default:
151  fieldhtml.Replace( "__VALUE__", EscapedHTML( text ) );
152  }
153 
154  return fieldhtml;
155  }
156 
157 
159  {
160  wxString fieldtable;
161  LIB_FIELDS fields;
162  m_part->GetPart()->GetFields( fields );
163 
164  for( auto const & field: fields )
165  {
166  fieldtable += GetHtmlFieldRow( field );
167  }
168 
169  m_html.Replace( "__FIELDS__", fieldtable );
170  }
171 };
172 
173 
174 wxString GenerateAliasInfo( LIB_ALIAS const * aAlias, int aUnit )
175 {
176  ALIAS_INFO_GENERATOR gen( aAlias, aUnit );
177  gen.GenerateHtml();
178  return gen.GetHtml();
179 }
Part library alias object definition.
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:533
ALIAS_INFO_GENERATOR(LIB_ALIAS const *aAlias, int aUnit)
bool IsRoot() const
void GenerateHtml()
Generate the HTML internally.
static const wxString KeywordsFormat
static const wxString DescFormat
int GetId() const
Definition: lib_field.h:136
const wxString & GetName() const
wxString GenerateAliasInfo(LIB_ALIAS const *aAlias, int aUnit)
Return an HTML page describing a LIB_ALIAS.
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:609
wxString GetHtml()
Return the generated HTML.
LIB_PART * GetPart() const
Function GetPart gets the shared LIB_PART.
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
std::vector< LIB_FIELD > LIB_FIELDS
Definition: lib_field.h:261
const char * name
static const wxString FieldFormat
wxString GetKeyWords() const
static const wxString DatasheetLinkFormat
LIB_ALIAS const * m_part
void GetFields(LIB_FIELDS &aList)
Function GetFields returns a list of fields withing this part.