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.

217 lines
5.9 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 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>" + _( "Keywords:" ) + " %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 ALIAS_INFO_GENERATOR
  43. {
  44. wxString m_html;
  45. SYMBOL_LIB_TABLE* m_sym_lib_table;
  46. LIB_ID const m_lib_id;
  47. LIB_ALIAS* m_alias;
  48. int m_unit;
  49. public:
  50. ALIAS_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_alias( 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_alias = const_cast< LIB_ALIAS* >( 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_alias )
  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_alias->GetName() ) );
  98. }
  99. void SetHtmlAliasOf()
  100. {
  101. if( m_alias->IsRoot() )
  102. {
  103. m_html.Replace( "__ALIASOF__", wxEmptyString );
  104. }
  105. else
  106. {
  107. wxString root_name = _( "Unknown" );
  108. wxString root_desc = "";
  109. LIB_PART* root = m_alias->GetPart();
  110. LIB_ALIAS* root_alias = root ? root->GetAlias( 0 ) : nullptr;
  111. if( root )
  112. root_name = root->GetName();
  113. if( root_alias )
  114. root_desc = root_alias->GetDescription();
  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_alias->GetDescription();
  123. m_html.Replace( "__DESC__", wxString::Format( DescFormat, EscapedHTML( raw_desc ) ) );
  124. }
  125. void SetHtmlKeywords()
  126. {
  127. wxString keywords = m_alias->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.GetName();
  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. {
  144. if( text.IsEmpty() )
  145. text = m_alias->GetDocFileName();
  146. wxString datasheetlink = DatasheetLinkFormat;
  147. datasheetlink.Replace( "__HREF__", EscapedHTML( text ) );
  148. if( text.Length() > 75 )
  149. text = text.Left( 72 ) + wxT( "..." );
  150. datasheetlink.Replace( "__TEXT__", EscapedHTML( text ) );
  151. fieldhtml.Replace( "__VALUE__", datasheetlink );
  152. }
  153. break;
  154. default:
  155. fieldhtml.Replace( "__VALUE__", EscapedHTML( text ) );
  156. }
  157. return fieldhtml;
  158. }
  159. void SetHtmlFieldTable()
  160. {
  161. wxString fieldtable;
  162. LIB_FIELDS fields;
  163. m_alias->GetPart()->GetFields( fields );
  164. for( auto const & field: fields )
  165. {
  166. fieldtable += GetHtmlFieldRow( field );
  167. }
  168. m_html.Replace( "__FIELDS__", fieldtable );
  169. }
  170. };
  171. wxString GenerateAliasInfo( SYMBOL_LIB_TABLE* aSymLibTable, LIB_ID const& aLibId, int aUnit )
  172. {
  173. ALIAS_INFO_GENERATOR gen( aSymLibTable, aLibId, aUnit );
  174. gen.GenerateHtml();
  175. return gen.GetHtml();
  176. }