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.

440 lines
11 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-2018 KiCad Developers, see AUTHORS.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 <algorithm>
  22. #include "wx_html_report_panel.h"
  23. #include <wildcards_and_files_ext.h>
  24. #include <gal/color4d.h>
  25. #include <wx/clipbrd.h>
  26. WX_HTML_REPORT_PANEL::WX_HTML_REPORT_PANEL( wxWindow* parent,
  27. wxWindowID id,
  28. const wxPoint& pos,
  29. const wxSize& size,
  30. long style ) :
  31. WX_HTML_REPORT_PANEL_BASE( parent, id, pos, size, style ),
  32. m_reporter( this ),
  33. m_severities( -1 ),
  34. m_lazyUpdate( false )
  35. {
  36. syncCheckboxes();
  37. m_htmlView->SetPage( addHeader( "" ) );
  38. Connect( wxEVT_COMMAND_MENU_SELECTED,
  39. wxMenuEventHandler( WX_HTML_REPORT_PANEL::onMenuEvent ), NULL, this );
  40. }
  41. WX_HTML_REPORT_PANEL::~WX_HTML_REPORT_PANEL()
  42. {
  43. }
  44. void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
  45. {
  46. m_fgSizer->SetMinSize( aMinSize );
  47. GetSizer()->SetSizeHints( this );
  48. }
  49. REPORTER& WX_HTML_REPORT_PANEL::Reporter()
  50. {
  51. return m_reporter;
  52. }
  53. void WX_HTML_REPORT_PANEL::Report( const wxString& aText, REPORTER::SEVERITY aSeverity,
  54. REPORTER::LOCATION aLocation )
  55. {
  56. REPORT_LINE line;
  57. line.message = aText;
  58. line.severity = aSeverity;
  59. if( aLocation == REPORTER::LOC_HEAD )
  60. m_reportHead.push_back( line );
  61. else if( aLocation == REPORTER::LOC_TAIL )
  62. m_reportTail.push_back( line );
  63. else
  64. m_report.push_back( line );
  65. if( !m_lazyUpdate )
  66. {
  67. m_htmlView->AppendToPage( generateHtml( line ) );
  68. scrollToBottom();
  69. }
  70. }
  71. void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
  72. {
  73. m_lazyUpdate = aLazyUpdate;
  74. }
  75. void WX_HTML_REPORT_PANEL::Flush( bool aSort )
  76. {
  77. wxString html;
  78. if( aSort )
  79. {
  80. std::sort( m_report.begin(), m_report.end(),
  81. []( const REPORT_LINE& a, const REPORT_LINE& b)
  82. {
  83. return a.severity < b.severity;
  84. });
  85. }
  86. for( const auto& line : m_reportHead )
  87. html += generateHtml( line );
  88. for( const auto& line : m_report )
  89. html += generateHtml( line );
  90. for( const auto& line : m_reportTail )
  91. html += generateHtml( line );
  92. m_htmlView->SetPage( addHeader( html ) );
  93. scrollToBottom();
  94. }
  95. void WX_HTML_REPORT_PANEL::scrollToBottom()
  96. {
  97. int x, y, xUnit, yUnit;
  98. m_htmlView->GetVirtualSize( &x, &y );
  99. m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
  100. m_htmlView->Scroll( 0, y / yUnit );
  101. updateBadges();
  102. }
  103. #define BADGE_SIZE 20
  104. #define BADGE_FONT_SIZE 10
  105. static wxBitmap makeBadge( REPORTER::SEVERITY aStyle, int aCount, wxWindow* aWindow )
  106. {
  107. wxSize size( BADGE_SIZE, BADGE_SIZE );
  108. wxBitmap bitmap( size );
  109. wxBrush brush;
  110. wxMemoryDC badgeDC;
  111. wxColour badgeColour;
  112. wxColour textColour;
  113. int fontSize = BADGE_FONT_SIZE;
  114. if( aCount > 99 )
  115. fontSize--;
  116. badgeDC.SelectObject( bitmap );
  117. brush.SetStyle( wxBRUSHSTYLE_SOLID );
  118. // We're one level deep in staticBoxes; each level is darkened by 210
  119. brush.SetColour( aWindow->GetParent()->GetBackgroundColour().MakeDisabled( 210 ) );
  120. badgeDC.SetBackground( brush );
  121. badgeDC.Clear();
  122. if( aCount == 0 )
  123. return bitmap;
  124. switch( aStyle )
  125. {
  126. case REPORTER::RPT_ERROR:
  127. badgeColour = *wxRED;
  128. textColour = *wxWHITE;
  129. break;
  130. case REPORTER::RPT_WARNING:
  131. badgeColour = wxColour( 235, 120, 80 ); // Orange
  132. textColour = *wxWHITE;
  133. break;
  134. case REPORTER::RPT_ACTION:
  135. badgeColour = *wxGREEN;
  136. textColour = *wxWHITE;
  137. break;
  138. case REPORTER::RPT_INFO:
  139. default:
  140. badgeColour = *wxLIGHT_GREY;
  141. textColour = *wxBLACK;
  142. break;
  143. }
  144. brush.SetStyle( wxBRUSHSTYLE_SOLID );
  145. brush.SetColour( badgeColour );
  146. badgeDC.SetBrush( brush );
  147. badgeDC.SetPen( wxPen( badgeColour, 0 ) );
  148. badgeDC.DrawCircle( size.x / 2 - 1, size.y / 2, ( std::max( size.x, size.y ) / 2 ) - 1 );
  149. wxFont font( fontSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD );
  150. badgeDC.SetFont( font );
  151. wxString text = wxString::Format( wxT( "%d" ), aCount );
  152. wxSize textExtent = badgeDC.GetTextExtent( text );
  153. badgeDC.SetTextForeground( textColour );
  154. badgeDC.DrawText( text, size.x / 2 - textExtent.x / 2 - 1, size.y / 2 - textExtent.y / 2 );
  155. return bitmap;
  156. }
  157. void WX_HTML_REPORT_PANEL::updateBadges()
  158. {
  159. int count = Count( REPORTER::RPT_ERROR );
  160. m_errorsBadge->SetBitmap( makeBadge( REPORTER::RPT_ERROR, count, m_errorsBadge ) );
  161. count = Count( REPORTER::RPT_WARNING );
  162. m_warningsBadge->SetBitmap( makeBadge( REPORTER::RPT_WARNING, count, m_warningsBadge ) );
  163. }
  164. wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
  165. {
  166. wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
  167. wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
  168. return wxString::Format( wxT( "<html><body bgcolor='%s' text='%s'>%s</body></html>" ),
  169. bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
  170. fgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
  171. aBody );
  172. }
  173. int WX_HTML_REPORT_PANEL::Count( int severityMask )
  174. {
  175. int count = 0;
  176. for( const REPORT_LINE& reportLine : m_report )
  177. if( severityMask & reportLine.severity )
  178. count++;
  179. return count;
  180. }
  181. wxString WX_HTML_REPORT_PANEL::generateHtml( const REPORT_LINE& aLine )
  182. {
  183. wxString retv;
  184. if( !( m_severities & aLine.severity ) )
  185. return retv;
  186. switch( aLine.severity )
  187. {
  188. case REPORTER::RPT_ERROR:
  189. retv = "<font color=\"red\" size=2><b>" + _( "Error: " ) + "</b></font><font size=2>" +
  190. aLine.message + "</font><br>";
  191. break;
  192. case REPORTER::RPT_WARNING:
  193. retv = "<font color=\"orange\" size=2><b>" + _( "Warning: " ) +
  194. "</b></font><font size=2>" + aLine.message + "</font><br>";
  195. break;
  196. case REPORTER::RPT_INFO:
  197. retv = "<font color=\"dark gray\" size=2><b>" + _( "Info: " ) + "</b>" + aLine.message +
  198. "</font><br>";
  199. break;
  200. case REPORTER::RPT_ACTION:
  201. retv = "<font color=\"dark green\" size=2>" + aLine.message + "</font><br>";
  202. break;
  203. default:
  204. retv = "<font size=2>" + aLine.message + "</font><br>";
  205. }
  206. return retv;
  207. }
  208. wxString WX_HTML_REPORT_PANEL::generatePlainText( const REPORT_LINE& aLine )
  209. {
  210. switch( aLine.severity )
  211. {
  212. case REPORTER::RPT_ERROR:
  213. return _( "Error: " ) + aLine.message + wxT( "\n" );
  214. case REPORTER::RPT_WARNING:
  215. return _( "Warning: " ) + aLine.message + wxT( "\n" );
  216. case REPORTER::RPT_INFO:
  217. return _( "Info: " ) + aLine.message + wxT( "\n" );
  218. default:
  219. return aLine.message + wxT( "\n" );
  220. }
  221. }
  222. void WX_HTML_REPORT_PANEL::onRightClick( wxMouseEvent& event )
  223. {
  224. wxMenu popup;
  225. popup.Append( wxID_COPY, "Copy" );
  226. PopupMenu( &popup );
  227. }
  228. void WX_HTML_REPORT_PANEL::onMenuEvent( wxMenuEvent& event )
  229. {
  230. if( event.GetId() == wxID_COPY )
  231. {
  232. if( wxTheClipboard->Open() )
  233. {
  234. bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
  235. wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
  236. wxTheClipboard->SetData( new wxTextDataObject( m_htmlView->SelectionToText() ) );
  237. wxTheClipboard->Close();
  238. wxTheClipboard->UsePrimarySelection( primarySelection );
  239. }
  240. }
  241. }
  242. void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
  243. {
  244. if( event.IsChecked() )
  245. m_severities = REPORTER::RPT_ALL;
  246. else
  247. m_severities = 0;
  248. syncCheckboxes();
  249. Flush( true );
  250. }
  251. void WX_HTML_REPORT_PANEL::syncCheckboxes()
  252. {
  253. m_checkBoxShowAll->SetValue( m_severities == REPORTER::RPT_ALL );
  254. m_checkBoxShowWarnings->SetValue( m_severities & REPORTER::RPT_WARNING );
  255. m_checkBoxShowErrors->SetValue( m_severities & REPORTER::RPT_ERROR );
  256. m_checkBoxShowInfos->SetValue( m_severities & REPORTER::RPT_INFO );
  257. m_checkBoxShowActions->SetValue( m_severities & REPORTER::RPT_ACTION );
  258. }
  259. void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
  260. {
  261. if( event.IsChecked() )
  262. m_severities |= REPORTER::RPT_WARNING;
  263. else
  264. m_severities &= ~REPORTER::RPT_WARNING;
  265. syncCheckboxes();
  266. Flush( true );
  267. }
  268. void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
  269. {
  270. if( event.IsChecked() )
  271. m_severities |= REPORTER::RPT_ERROR;
  272. else
  273. m_severities &= ~REPORTER::RPT_ERROR;
  274. syncCheckboxes();
  275. Flush( true );
  276. }
  277. void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
  278. {
  279. if( event.IsChecked() )
  280. m_severities |= REPORTER::RPT_INFO;
  281. else
  282. m_severities &= ~REPORTER::RPT_INFO;
  283. syncCheckboxes();
  284. Flush( true );
  285. }
  286. void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
  287. {
  288. if( event.IsChecked() )
  289. m_severities |= REPORTER::RPT_ACTION;
  290. else
  291. m_severities &= ~REPORTER::RPT_ACTION;
  292. syncCheckboxes();
  293. Flush( true );
  294. }
  295. void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
  296. {
  297. wxFileName fn( "./report.txt" );
  298. wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
  299. TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  300. if( dlg.ShowModal() != wxID_OK )
  301. return;
  302. fn = dlg.GetPath();
  303. if( fn.GetExt().IsEmpty() )
  304. fn.SetExt( "txt" );
  305. wxFile f( fn.GetFullPath(), wxFile::write );
  306. if( !f.IsOpened() )
  307. {
  308. wxString msg;
  309. msg.Printf( _( "Cannot write report to file \"%s\"." ),
  310. fn.GetFullPath().GetData() );
  311. wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
  312. return;
  313. }
  314. for( const REPORT_LINE& l : m_report )
  315. {
  316. f.Write( generatePlainText( l ) );
  317. }
  318. f.Close();
  319. }
  320. void WX_HTML_REPORT_PANEL::Clear()
  321. {
  322. m_report.clear();
  323. m_reportHead.clear();
  324. m_reportTail.clear();
  325. }
  326. void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
  327. {
  328. m_box->GetStaticBox()->SetLabel( aLabel );
  329. }
  330. void WX_HTML_REPORT_PANEL::SetVisibleSeverities( int aSeverities )
  331. {
  332. if( aSeverities < 0 )
  333. m_severities = REPORTER::RPT_ALL;
  334. else
  335. m_severities = aSeverities;
  336. syncCheckboxes();
  337. }
  338. int WX_HTML_REPORT_PANEL::GetVisibleSeverities()
  339. {
  340. return m_severities;
  341. }