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.

302 lines
7.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 CERN
  5. * Copyright (C) 2015 KiCad Developers, see change_log.txt for contributors.
  6. * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  7. *
  8. * This program is free software: you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation, either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "wx_html_report_panel.h"
  22. #include <wildcards_and_files_ext.h>
  23. #include <boost/foreach.hpp>
  24. WX_HTML_REPORT_PANEL::WX_HTML_REPORT_PANEL( wxWindow* parent,
  25. wxWindowID id,
  26. const wxPoint& pos,
  27. const wxSize& size,
  28. long style ) :
  29. WX_HTML_REPORT_PANEL_BASE( parent, id, pos, size, style ),
  30. m_reporter( this ),
  31. m_severities( -1 ),
  32. m_showAll( true ),
  33. m_lazyUpdate( false )
  34. {
  35. syncCheckboxes();
  36. m_htmlView->SetPage( addHeader( "" ) );
  37. }
  38. WX_HTML_REPORT_PANEL::~WX_HTML_REPORT_PANEL()
  39. {
  40. }
  41. void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
  42. {
  43. m_htmlView->SetMinSize( aMinSize );
  44. GetSizer()->SetSizeHints( this );
  45. }
  46. REPORTER& WX_HTML_REPORT_PANEL::Reporter()
  47. {
  48. return m_reporter;
  49. }
  50. void WX_HTML_REPORT_PANEL::Report( const wxString& aText, REPORTER::SEVERITY aSeverity )
  51. {
  52. REPORT_LINE line;
  53. line.message = aText;
  54. line.severity = aSeverity;
  55. m_report.push_back( line );
  56. m_html += generateHtml( line );
  57. if( !m_lazyUpdate )
  58. {
  59. m_htmlView->AppendToPage( generateHtml( line ) );
  60. scrollToBottom();
  61. }
  62. }
  63. void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
  64. {
  65. m_lazyUpdate = aLazyUpdate;
  66. }
  67. void WX_HTML_REPORT_PANEL::Flush()
  68. {
  69. m_htmlView->SetPage( addHeader( m_html ) );
  70. scrollToBottom();
  71. }
  72. void WX_HTML_REPORT_PANEL::scrollToBottom()
  73. {
  74. int x, y, xUnit, yUnit;
  75. m_htmlView->GetVirtualSize( &x, &y );
  76. m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
  77. m_htmlView->Scroll( 0, y / yUnit );
  78. }
  79. void WX_HTML_REPORT_PANEL::refreshView()
  80. {
  81. wxString html;
  82. BOOST_FOREACH( REPORT_LINE l, m_report )
  83. {
  84. html += generateHtml( l );
  85. }
  86. m_htmlView->SetPage( addHeader( html ) );
  87. scrollToBottom();
  88. }
  89. wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
  90. {
  91. wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
  92. wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
  93. wxString s = "<html><body bgcolor=\"" + bgcolor.GetAsString( wxC2S_HTML_SYNTAX ) +
  94. "\" text=\"" + fgcolor.GetAsString( wxC2S_HTML_SYNTAX ) + "\">";
  95. s += aBody;
  96. s += "</body></html>";
  97. return s;
  98. }
  99. wxString WX_HTML_REPORT_PANEL::generateHtml( const REPORT_LINE& aLine )
  100. {
  101. if( !m_showAll && ! ( m_severities & aLine.severity ) )
  102. return wxEmptyString;
  103. switch( aLine.severity )
  104. {
  105. case REPORTER::RPT_ERROR:
  106. return wxString( "<font color=\"red\" size=2>" ) + _( "<b>Error: </b></font><font size=2>" ) + aLine.message + wxString( "</font><br>" );
  107. case REPORTER::RPT_WARNING:
  108. return wxString( "<font color=\"orange\" size=2>" ) + _( "<b>Warning: </b></font><font size=2>" ) + aLine.message + wxString( "</font><br>" );
  109. case REPORTER::RPT_INFO:
  110. return wxString( "<font color=\"gray\" size=2>" ) + _( "<b>Info: </b>" ) + aLine.message + wxString( "</font><br>" );
  111. case REPORTER::RPT_ACTION:
  112. return wxString( "<font color=\"darkgreen\" size=2>" ) + aLine.message + wxString( "</font><br>" );
  113. default:
  114. return wxString( "<font size=2>" ) + aLine.message + wxString( "</font><br>" );
  115. }
  116. }
  117. wxString WX_HTML_REPORT_PANEL::generatePlainText( const REPORT_LINE& aLine )
  118. {
  119. switch( aLine.severity )
  120. {
  121. case REPORTER::RPT_ERROR:
  122. return _( "Error: " ) + aLine.message + wxT( "\n" );
  123. case REPORTER::RPT_WARNING:
  124. return _( "Warning: " ) + aLine.message + wxT( "\n" );
  125. case REPORTER::RPT_INFO:
  126. return _( "Info: " ) + aLine.message + wxT( "\n" );
  127. default:
  128. return aLine.message + wxT( "\n" );
  129. }
  130. }
  131. void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
  132. {
  133. if ( event.IsChecked() )
  134. m_showAll = true;
  135. else
  136. m_showAll = false;
  137. syncCheckboxes();
  138. refreshView();
  139. }
  140. void WX_HTML_REPORT_PANEL::syncCheckboxes()
  141. {
  142. m_checkBoxShowAll->SetValue( m_showAll );
  143. m_checkBoxShowWarnings->Enable( !m_showAll );
  144. m_checkBoxShowWarnings->SetValue( m_severities & REPORTER::RPT_WARNING );
  145. m_checkBoxShowErrors->Enable( !m_showAll );
  146. m_checkBoxShowErrors->SetValue( m_severities & REPORTER::RPT_ERROR );
  147. m_checkBoxShowInfos->Enable( !m_showAll );
  148. m_checkBoxShowInfos->SetValue( m_severities & REPORTER::RPT_INFO );
  149. m_checkBoxShowActions->Enable( !m_showAll );
  150. m_checkBoxShowActions->SetValue( m_severities & REPORTER::RPT_ACTION );
  151. }
  152. void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
  153. {
  154. if ( event.IsChecked() )
  155. m_severities |= REPORTER::RPT_WARNING;
  156. else
  157. m_severities &= ~REPORTER::RPT_WARNING;
  158. refreshView();
  159. }
  160. void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
  161. {
  162. if ( event.IsChecked() )
  163. m_severities |= REPORTER::RPT_ERROR;
  164. else
  165. m_severities &= ~REPORTER::RPT_ERROR;
  166. refreshView();
  167. }
  168. void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
  169. {
  170. if ( event.IsChecked() )
  171. m_severities |= REPORTER::RPT_INFO;
  172. else
  173. m_severities &= ~REPORTER::RPT_INFO;
  174. refreshView();
  175. }
  176. void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
  177. {
  178. if ( event.IsChecked() )
  179. m_severities |= REPORTER::RPT_ACTION;
  180. else
  181. m_severities &= ~REPORTER::RPT_ACTION;
  182. refreshView();
  183. }
  184. void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
  185. {
  186. wxFileName fn( "./report.txt" );
  187. wxFileDialog dlg( this, _( "Save report to file" ), fn.GetPath(), fn.GetName(),
  188. TextWildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  189. if( dlg.ShowModal() != wxID_OK )
  190. return;
  191. fn = dlg.GetPath();
  192. if( fn.GetExt().IsEmpty() )
  193. fn.SetExt( wxT( "txt" ) );
  194. wxFile f( fn.GetFullPath(), wxFile::write );
  195. if( !f.IsOpened() )
  196. {
  197. wxString msg;
  198. msg.Printf( _( "Cannot write report to file '%s'." ),
  199. fn.GetFullPath().GetData() );
  200. wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
  201. return;
  202. }
  203. BOOST_FOREACH( REPORT_LINE l, m_report )
  204. {
  205. f.Write( generatePlainText( l ) );
  206. }
  207. f.Close();
  208. }
  209. void WX_HTML_REPORT_PANEL::Clear()
  210. {
  211. m_html.clear();
  212. m_report.clear();
  213. }
  214. void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
  215. {
  216. m_box->GetStaticBox()->SetLabel( aLabel );
  217. }
  218. void WX_HTML_REPORT_PANEL::SetVisibleSeverities( int aSeverities )
  219. {
  220. if( aSeverities < 0 )
  221. m_showAll = true;
  222. else
  223. {
  224. m_showAll = false;
  225. m_severities = aSeverities;
  226. }
  227. syncCheckboxes();
  228. }
  229. int WX_HTML_REPORT_PANEL::GetVisibleSeverities()
  230. {
  231. return m_showAll ? m_severities | 0x80000000 : m_severities & ~0x80000000;
  232. }