KiCad PCB EDA Suite
wx_html_report_panel.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) 2015 CERN
5  * Copyright (C) 2015-2017 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 
22 #include "wx_html_report_panel.h"
23 
25 #include <gal/color4d.h>
26 
27 
29  wxWindowID id,
30  const wxPoint& pos,
31  const wxSize& size,
32  long style ) :
33  WX_HTML_REPORT_PANEL_BASE( parent, id, pos, size, style ),
34  m_reporter( this ),
35  m_severities( -1 ),
36  m_showAll( true ),
37  m_lazyUpdate( false )
38 {
40  m_htmlView->SetPage( addHeader( "" ) );
41 }
42 
43 
45 {
46 }
47 
48 
49 void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
50 {
51  m_fgSizer->SetMinSize( aMinSize );
52  GetSizer()->SetSizeHints( this );
53 }
54 
55 
57 {
58  return m_reporter;
59 }
60 
61 
62 void WX_HTML_REPORT_PANEL::Report( const wxString& aText, REPORTER::SEVERITY aSeverity )
63 {
64  REPORT_LINE line;
65  line.message = aText;
66  line.severity = aSeverity;
67 
68  m_report.push_back( line );
69 
70  m_html += generateHtml( line );
71 
72  if( !m_lazyUpdate )
73  {
74  m_htmlView->AppendToPage( generateHtml( line ) );
76  }
77 }
78 
79 
80 void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
81 {
82  m_lazyUpdate = aLazyUpdate;
83 }
84 
85 
87 {
88  m_htmlView->SetPage( addHeader( m_html ) );
90 }
91 
92 
94 {
95  int x, y, xUnit, yUnit;
96 
97  m_htmlView->GetVirtualSize( &x, &y );
98  m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
99  m_htmlView->Scroll( 0, y / yUnit );
100 
101  updateBadges();
102 }
103 
104 
105 const static wxSize BADGE_SIZE_DU( 9, 9 );
106 const static int BADGE_FONT_SIZE = 9;
107 
108 static wxBitmap makeBadge( REPORTER::SEVERITY aStyle, int aCount, wxWindow* aWindow )
109 {
110  wxSize size( aWindow->ConvertDialogToPixels( BADGE_SIZE_DU ) );
111  wxBitmap bitmap( size );
112  wxBrush brush;
113  wxMemoryDC badgeDC;
114  wxColour badgeColour;
115  wxColour textColour;
116  int fontSize = BADGE_FONT_SIZE;
117 
118  if( aCount > 99 )
119  fontSize--;
120 
121  badgeDC.SelectObject( bitmap );
122 
123  brush.SetStyle( wxBRUSHSTYLE_SOLID );
124  // We're one level deep in staticBoxes; each level is darkened by 210
125  brush.SetColour( aWindow->GetParent()->GetBackgroundColour().MakeDisabled( 210 ) );
126  badgeDC.SetBackground( brush );
127  badgeDC.Clear();
128 
129  switch( aStyle )
130  {
131  case REPORTER::RPT_ERROR:
132  badgeColour = *wxRED;
133  textColour = *wxWHITE;
134  break;
136  badgeColour = *wxYELLOW;
137  textColour = *wxBLACK;
138  break;
140  badgeColour = *wxGREEN;
141  textColour = *wxWHITE;
142  break;
143  case REPORTER::RPT_INFO:
144  default:
145  badgeColour = *wxLIGHT_GREY;
146  textColour = *wxBLACK;
147  break;
148  }
149 
150  brush.SetStyle( wxBRUSHSTYLE_SOLID );
151  brush.SetColour( badgeColour );
152  badgeDC.SetBrush( brush );
153  badgeDC.SetPen( wxPen( badgeColour, 0 ) );
154  badgeDC.DrawCircle( size.x / 2 - 1, size.y / 2, ( std::max( size.x, size.y ) / 2 ) - 1 );
155 
156  wxFont font( BADGE_FONT_SIZE, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD );
157  wxString text = wxString::Format( wxT( "%d" ), aCount );
158  wxSize textExtent = badgeDC.GetTextExtent( text );
159 
160  badgeDC.SetFont( font );
161  badgeDC.SetTextForeground( textColour );
162  badgeDC.DrawText( text, size.x / 2 - textExtent.x / 2, size.y / 2 - textExtent.y / 2 + 2 );
163 
164  return bitmap;
165 }
166 
167 
169 {
170  int count = Count( REPORTER::RPT_ERROR );
171 
172  if( count > 0 )
173  {
174  m_errorsBadge->SetBitmap( makeBadge( REPORTER::RPT_ERROR, count, m_errorsBadge ) );
175  m_errorsBadge->Show( true );
176  }
177  else
178  m_errorsBadge->Show( false );
179 
180  count = Count( REPORTER::RPT_WARNING );
181 
182  if( count > 0 )
183  {
185  m_warningsBadge->Show( true );
186  }
187  else
188  m_warningsBadge->Show( false );
189 }
190 
191 
193 {
194  wxString html;
195 
196  for( const REPORT_LINE& l : m_report )
197  {
198  html += generateHtml( l );
199  }
200 
201  m_htmlView->SetPage( addHeader( html ) );
202  scrollToBottom();
203 }
204 
205 
206 wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
207 {
208  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
209  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
210  wxString s = "<html><body bgcolor=\"" + bgcolor.GetAsString( wxC2S_HTML_SYNTAX ) +
211  "\" text=\"" + fgcolor.GetAsString( wxC2S_HTML_SYNTAX ) + "\">";
212  s += aBody;
213  s += "</body></html>";
214 
215  return s;
216 }
217 
218 
219 int WX_HTML_REPORT_PANEL::Count( int severityMask )
220 {
221  int count = 0;
222 
223  for( const REPORT_LINE& reportLine : m_report )
224  if( severityMask & reportLine.severity )
225  count++;
226 
227  return count;
228 }
229 
230 
232 {
233  wxString retv;
234 
235  if( !m_showAll && ! ( m_severities & aLine.severity ) )
236  return retv;
237 
238  switch( aLine.severity )
239  {
240  case REPORTER::RPT_ERROR:
241  retv = "<font color=\"red\" size=2><b>" + _( "Error: " ) + "</b></font><font size=2>" +
242  aLine.message + "</font><br>";
243  break;
245  retv = "<font color=\"orange\" size=2><b>" + _( "Warning: " ) +
246  "</b></font><font size=2>" + aLine.message + "</font><br>";
247  break;
248  case REPORTER::RPT_INFO:
249  retv = "<font color=\"dark gray\" size=2><b>" + _( "Info: " ) + "</b>" + aLine.message +
250  "</font><br>";
251  break;
253  retv = "<font color=\"dark green\" size=2>" + aLine.message + "</font><br>";
254  break;
255  default:
256  retv = "<font size=2>" + aLine.message + "</font><br>";
257  }
258 
259  return retv;
260 }
261 
262 
264 {
265  switch( aLine.severity )
266  {
267  case REPORTER::RPT_ERROR:
268  return _( "Error: " ) + aLine.message + wxT( "\n" );
270  return _( "Warning: " ) + aLine.message + wxT( "\n" );
271  case REPORTER::RPT_INFO:
272  return _( "Info: " ) + aLine.message + wxT( "\n" );
273  default:
274  return aLine.message + wxT( "\n" );
275  }
276 }
277 
278 
279 void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
280 {
281  if ( event.IsChecked() )
282  m_showAll = true;
283  else
284  m_showAll = false;
285 
286  syncCheckboxes();
287  refreshView();
288 }
289 
290 
292 {
293  m_checkBoxShowAll->SetValue( m_showAll );
298 }
299 
300 
301 void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
302 {
303  if ( event.IsChecked() )
305  else
306  m_severities &= ~REPORTER::RPT_WARNING;
307 
308  refreshView();
309 }
310 
311 
312 void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
313 {
314  if ( event.IsChecked() )
316  else
317  m_severities &= ~REPORTER::RPT_ERROR;
318 
319  refreshView();
320 }
321 
322 
323 void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
324 {
325  if ( event.IsChecked() )
327  else
328  m_severities &= ~REPORTER::RPT_INFO;
329 
330  refreshView();
331 }
332 
333 
334 void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
335 {
336  if ( event.IsChecked() )
338  else
339  m_severities &= ~REPORTER::RPT_ACTION;
340 
341  refreshView();
342 }
343 
344 
345 void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
346 {
347  wxFileName fn( "./report.txt" );
348 
349  wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
350  TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
351 
352  if( dlg.ShowModal() != wxID_OK )
353  return;
354 
355  fn = dlg.GetPath();
356 
357  if( fn.GetExt().IsEmpty() )
358  fn.SetExt( "txt" );
359 
360  wxFile f( fn.GetFullPath(), wxFile::write );
361 
362  if( !f.IsOpened() )
363  {
364  wxString msg;
365 
366  msg.Printf( _( "Cannot write report to file \"%s\"." ),
367  fn.GetFullPath().GetData() );
368  wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
369  return;
370  }
371 
372  for( const REPORT_LINE& l : m_report )
373  {
374  f.Write( generatePlainText( l ) );
375  }
376 
377  f.Close();
378 }
379 
380 
382 {
383  m_html.clear();
384  m_report.clear();
385 }
386 
387 
388 void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
389 {
390  m_box->GetStaticBox()->SetLabel( aLabel );
391 }
392 
393 
395 {
396  if( aSeverities < 0 )
397  m_showAll = true;
398  else
399  {
400  m_showAll = false;
401  m_severities = aSeverities;
402  }
403 
404  syncCheckboxes();
405 }
406 
407 
409 {
410  return m_showAll ? m_severities | 0x80000000 : m_severities & ~0x80000000;
411 }
void SetVisibleSeverities(int aSeverities)
Set the visible severity filter.
void onCheckBoxShowInfos(wxCommandEvent &event) override
void onBtnSaveToFile(wxCommandEvent &event) override
wxString addHeader(const wxString &aBody)
void SetLazyUpdate(bool aLazyUpdate)
Sets the lasy update.
void onCheckBoxShowActions(wxCommandEvent &event) override
wxString generatePlainText(const REPORT_LINE &aLine)
Class WX_HTML_REPORT_PANEL_BASE.
static wxBitmap makeBadge(REPORTER::SEVERITY aStyle, int aCount, wxWindow *aWindow)
void onCheckBoxShowWarnings(wxCommandEvent &event) override
void onCheckBoxShowErrors(wxCommandEvent &event) override
static const int BADGE_FONT_SIZE
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
WX_HTML_REPORT_PANEL(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(500, 300), long style=wxTAB_TRAVERSAL)
SEVERITY
Severity of the reported messages.
Definition: reporter.h:65
void Report(const wxString &aText, REPORTER::SEVERITY aSeverity)
reports a string directly.
REPORTER & Reporter()
returns the reporter object that reports to this panel
WX_HTML_PANEL_REPORTER m_reporter
the reporter
bool m_showAll
show all messages flag (overrides m_severities)
int Count(int severityMask)
return the number of messages matching the given severity mask.
The common library.
REPORT_LINES m_report
copy of the report, stored for filtering
void Flush()
Forces updating the HTML page, after the report is built in lazy mode
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
void Clear()
clears the report panel
#define max(a, b)
Definition: auxiliary.h:86
void onCheckBoxShowAll(wxCommandEvent &event) override
static const wxSize BADGE_SIZE_DU(9, 9)
wxString TextFileWildcard()
wxString generateHtml(const REPORT_LINE &aLine)
void MsgPanelSetMinSize(const wxSize &aMinSize)
Set the min size of the area which displays html messages:
void SetLabel(const wxString &aLabel) override
sets the frame label
int m_severities
message severities to display (mask)