You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
6.7 KiB

  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-2020 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. #include <generate_alias_info.h>
  21. #include <kicad_string.h>
  22. #include <template_fieldnames.h>
  23. #include <class_libentry.h>
  24. #include <symbol_lib_table.h>
  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. static const wxString AliasOfFormat = "<br><i>" + _( "Alias of" ) + " %s (%s)</i>";
  34. static const wxString DescFormat = "<br>%s";
  35. static const wxString KeywordsFormat = "<br>" + _( "Key words:" ) + " %s";
  36. static const wxString FieldFormat =
  37. "<tr>"
  38. " <td><b>__NAME__</b></td>"
  39. " <td>__VALUE__</td>"
  40. "</tr>";
  41. static const wxString DatasheetLinkFormat = "<a href=\"__HREF__\">__TEXT__</a>";
  42. class FOOTPRINT_INFO_GENERATOR
  43. {
  44. wxString m_html;
  45. SYMBOL_LIB_TABLE* m_sym_lib_table;
  46. LIB_ID const m_lib_id;
  47. LIB_PART* m_symbol;
  48. int m_unit;
  49. public:
  50. FOOTPRINT_INFO_GENERATOR( SYMBOL_LIB_TABLE* aSymbolLibTable, LIB_ID const& aLibId, int aUnit )
  51. : m_html( DescriptionFormat ),
  52. m_sym_lib_table( aSymbolLibTable ),
  53. m_lib_id( aLibId ),
  54. m_symbol( nullptr ),
  55. m_unit( aUnit )
  56. { }
  57. /**
  58. * Generate the HTML internally.
  59. */
  60. void GenerateHtml()
  61. {
  62. wxCHECK_RET( m_sym_lib_table, "Symbol library table pointer is not valid" );
  63. if( !m_lib_id.IsValid() )
  64. return;
  65. try
  66. {
  67. m_symbol = const_cast< LIB_PART* >( m_sym_lib_table->LoadSymbol( m_lib_id ) );
  68. }
  69. catch( const IO_ERROR& ioe )
  70. {
  71. wxLogError( wxString::Format( _( "Error occurred loading symbol %s from library %s."
  72. "\n\n%s" ),
  73. m_lib_id.GetLibItemName().wx_str(),
  74. m_lib_id.GetLibNickname().wx_str(),
  75. ioe.What() ) );
  76. return;
  77. }
  78. if( m_symbol )
  79. {
  80. SetHtmlName();
  81. SetHtmlAliasOf();
  82. SetHtmlDesc();
  83. SetHtmlKeywords();
  84. SetHtmlFieldTable();
  85. }
  86. }
  87. /**
  88. * Return the generated HTML.
  89. */
  90. wxString GetHtml()
  91. {
  92. return m_html;
  93. }
  94. protected:
  95. void SetHtmlName()
  96. {
  97. m_html.Replace( "__NAME__", EscapedHTML( m_symbol->GetName() ) );
  98. }
  99. void SetHtmlAliasOf()
  100. {
  101. if( m_symbol->IsRoot() )
  102. {
  103. m_html.Replace( "__ALIASOF__", wxEmptyString );
  104. }
  105. else
  106. {
  107. wxString root_name = _( "Unknown" );
  108. wxString root_desc = "";
  109. std::shared_ptr< LIB_PART > parent = m_symbol->GetParent().lock();
  110. if( parent )
  111. {
  112. root_name = parent->GetName();
  113. root_desc = parent->GetDescription();
  114. }
  115. m_html.Replace(
  116. "__ALIASOF__", wxString::Format(
  117. AliasOfFormat, EscapedHTML( root_name ), EscapedHTML( root_desc ) ) );
  118. }
  119. }
  120. void SetHtmlDesc()
  121. {
  122. wxString raw_desc = m_symbol->GetDescription();
  123. m_html.Replace( "__DESC__", wxString::Format( DescFormat, EscapedHTML( raw_desc ) ) );
  124. }
  125. void SetHtmlKeywords()
  126. {
  127. wxString keywords = m_symbol->GetKeyWords();
  128. if( keywords.empty() )
  129. m_html.Replace( "__KEY__", wxEmptyString );
  130. else
  131. m_html.Replace( "__KEY__",
  132. wxString::Format( KeywordsFormat, EscapedHTML( keywords ) ) );
  133. }
  134. wxString GetHtmlFieldRow( LIB_FIELD const & aField )
  135. {
  136. wxString name = aField.GetCanonicalName();
  137. wxString text = aField.GetFullText( m_unit > 0 ? m_unit : 1 );
  138. wxString fieldhtml = FieldFormat;
  139. fieldhtml.Replace( "__NAME__", EscapedHTML( name ) );
  140. switch( aField.GetId() )
  141. {
  142. case DATASHEET:
  143. text = m_symbol->GetDatasheetField().GetText();
  144. if( text.IsEmpty() || text == wxT( "~" ) )
  145. {
  146. fieldhtml.Replace( "__VALUE__", text );
  147. }
  148. else
  149. {
  150. wxString datasheetlink = DatasheetLinkFormat;
  151. datasheetlink.Replace( "__HREF__", EscapedHTML( text ) );
  152. if( text.Length() > 75 )
  153. text = text.Left( 72 ) + wxT( "..." );
  154. datasheetlink.Replace( "__TEXT__", EscapedHTML( text ) );
  155. fieldhtml.Replace( "__VALUE__", datasheetlink );
  156. }
  157. break;
  158. case VALUE:
  159. // showing the value just repeats the name, so that's not much use...
  160. return wxEmptyString;
  161. default:
  162. fieldhtml.Replace( "__VALUE__", EscapedHTML( text ) );
  163. }
  164. return fieldhtml;
  165. }
  166. void SetHtmlFieldTable()
  167. {
  168. wxString fieldtable;
  169. LIB_FIELDS fields;
  170. m_symbol->GetFields( fields );
  171. for( auto const & field: fields )
  172. {
  173. fieldtable += GetHtmlFieldRow( field );
  174. }
  175. if( m_symbol->IsAlias() )
  176. {
  177. std::shared_ptr< LIB_PART > parent = m_symbol->GetParent().lock();
  178. // Append all of the unique parent fields if this is an alias.
  179. if( parent )
  180. {
  181. LIB_FIELDS parentFields;
  182. parent->GetFields( parentFields );
  183. for( auto const& parentField : parentFields )
  184. {
  185. if( m_symbol->FindField( parentField.GetCanonicalName() ) )
  186. continue;
  187. fieldtable += GetHtmlFieldRow( parentField );
  188. }
  189. }
  190. }
  191. m_html.Replace( "__FIELDS__", fieldtable );
  192. }
  193. };
  194. wxString GenerateAliasInfo( SYMBOL_LIB_TABLE* aSymLibTable, LIB_ID const& aLibId, int aUnit )
  195. {
  196. FOOTPRINT_INFO_GENERATOR gen( aSymLibTable, aLibId, aUnit );
  197. gen.GenerateHtml();
  198. return gen.GetHtml();
  199. }