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-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 
22 #include <algorithm>
23 
24 #include "wx_html_report_panel.h"
25 
27 #include <gal/color4d.h>
28 #include <wx/clipbrd.h>
29 
31  wxWindowID id,
32  const wxPoint& pos,
33  const wxSize& size,
34  long style ) :
35  WX_HTML_REPORT_PANEL_BASE( parent, id, pos, size, style ),
36  m_reporter( this ),
37  m_severities( -1 ),
38  m_showAll( true ),
39  m_lazyUpdate( false )
40 {
42  m_htmlView->SetPage( addHeader( "" ) );
43 
44  Connect( wxEVT_COMMAND_MENU_SELECTED,
45  wxMenuEventHandler( WX_HTML_REPORT_PANEL::onMenuEvent ), NULL, this );
46 }
47 
48 
50 {
51 }
52 
53 
54 void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
55 {
56  m_fgSizer->SetMinSize( aMinSize );
57  GetSizer()->SetSizeHints( this );
58 }
59 
60 
62 {
63  return m_reporter;
64 }
65 
66 
67 void WX_HTML_REPORT_PANEL::Report( const wxString& aText, REPORTER::SEVERITY aSeverity,
68  REPORTER::LOCATION aLocation )
69 {
70  REPORT_LINE line;
71  line.message = aText;
72  line.severity = aSeverity;
73 
74  if( aLocation == REPORTER::LOC_HEAD )
75  m_reportHead.push_back( line );
76  else if( aLocation == REPORTER::LOC_TAIL )
77  m_reportTail.push_back( line );
78  else
79  m_report.push_back( line );
80 
81  if( !m_lazyUpdate )
82  {
83  m_htmlView->AppendToPage( generateHtml( line ) );
85  }
86 }
87 
88 
89 void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
90 {
91  m_lazyUpdate = aLazyUpdate;
92 }
93 
94 
95 void WX_HTML_REPORT_PANEL::Flush( bool aSort )
96 {
97  wxString html;
98 
99  if( aSort )
100  {
101  std::sort( m_report.begin(), m_report.end(),
102  []( const REPORT_LINE& a, const REPORT_LINE& b)
103  {
104  return a.severity < b.severity;
105  });
106  }
107 
108  for( auto line : m_reportHead )
109  html += generateHtml( line );
110 
111  for( auto line : m_report )
112  html += generateHtml( line );
113 
114  for( auto line : m_reportTail )
115  html += generateHtml( line );
116 
117  m_htmlView->SetPage( addHeader( html ) );
118  scrollToBottom();
119 }
120 
121 
123 {
124  int x, y, xUnit, yUnit;
125 
126  m_htmlView->GetVirtualSize( &x, &y );
127  m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
128  m_htmlView->Scroll( 0, y / yUnit );
129 
130  updateBadges();
131 }
132 
133 
134 const static wxSize BADGE_SIZE_DU( 9, 9 );
135 const static int BADGE_FONT_SIZE = 9;
136 
137 static wxBitmap makeBadge( REPORTER::SEVERITY aStyle, int aCount, wxWindow* aWindow )
138 {
139  wxSize size( aWindow->ConvertDialogToPixels( BADGE_SIZE_DU ) );
140  wxBitmap bitmap( size );
141  wxBrush brush;
142  wxMemoryDC badgeDC;
143  wxColour badgeColour;
144  wxColour textColour;
145  int fontSize = BADGE_FONT_SIZE;
146 
147  if( aCount > 99 )
148  fontSize--;
149 
150  badgeDC.SelectObject( bitmap );
151 
152  brush.SetStyle( wxBRUSHSTYLE_SOLID );
153  // We're one level deep in staticBoxes; each level is darkened by 210
154  brush.SetColour( aWindow->GetParent()->GetBackgroundColour().MakeDisabled( 210 ) );
155  badgeDC.SetBackground( brush );
156  badgeDC.Clear();
157 
158  switch( aStyle )
159  {
160  case REPORTER::RPT_ERROR:
161  badgeColour = *wxRED;
162  textColour = *wxWHITE;
163  break;
165  badgeColour = *wxYELLOW;
166  textColour = *wxBLACK;
167  break;
169  badgeColour = *wxGREEN;
170  textColour = *wxWHITE;
171  break;
172  case REPORTER::RPT_INFO:
173  default:
174  badgeColour = *wxLIGHT_GREY;
175  textColour = *wxBLACK;
176  break;
177  }
178 
179  brush.SetStyle( wxBRUSHSTYLE_SOLID );
180  brush.SetColour( badgeColour );
181  badgeDC.SetBrush( brush );
182  badgeDC.SetPen( wxPen( badgeColour, 0 ) );
183  badgeDC.DrawCircle( size.x / 2 - 1, size.y / 2, ( std::max( size.x, size.y ) / 2 ) - 1 );
184 
185  wxFont font( BADGE_FONT_SIZE, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD );
186  wxString text = wxString::Format( wxT( "%d" ), aCount );
187  wxSize textExtent = badgeDC.GetTextExtent( text );
188 
189  badgeDC.SetFont( font );
190  badgeDC.SetTextForeground( textColour );
191  badgeDC.DrawText( text, size.x / 2 - textExtent.x / 2, size.y / 2 - textExtent.y / 2 + 2 );
192 
193  return bitmap;
194 }
195 
196 
198 {
199  int count = Count( REPORTER::RPT_ERROR );
200 
201  if( count > 0 )
202  {
203  m_errorsBadge->SetBitmap( makeBadge( REPORTER::RPT_ERROR, count, m_errorsBadge ) );
204  m_errorsBadge->Show( true );
205  }
206  else
207  m_errorsBadge->Show( false );
208 
209  count = Count( REPORTER::RPT_WARNING );
210 
211  if( count > 0 )
212  {
214  m_warningsBadge->Show( true );
215  }
216  else
217  m_warningsBadge->Show( false );
218 }
219 
220 
221 wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
222 {
223  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
224  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
225 
226  return wxString::Format( wxT( "<html><body bgcolor='%s' text='%s'>%s</body></html>" ),
227  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
228  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
229  aBody );
230 }
231 
232 
233 int WX_HTML_REPORT_PANEL::Count( int severityMask )
234 {
235  int count = 0;
236 
237  for( const REPORT_LINE& reportLine : m_report )
238  if( severityMask & reportLine.severity )
239  count++;
240 
241  return count;
242 }
243 
244 
246 {
247  wxString retv;
248 
249  if( !m_showAll && ! ( m_severities & aLine.severity ) )
250  return retv;
251 
252  switch( aLine.severity )
253  {
254  case REPORTER::RPT_ERROR:
255  retv = "<font color=\"red\" size=2><b>" + _( "Error: " ) + "</b></font><font size=2>" +
256  aLine.message + "</font><br>";
257  break;
259  retv = "<font color=\"orange\" size=2><b>" + _( "Warning: " ) +
260  "</b></font><font size=2>" + aLine.message + "</font><br>";
261  break;
262  case REPORTER::RPT_INFO:
263  retv = "<font color=\"dark gray\" size=2><b>" + _( "Info: " ) + "</b>" + aLine.message +
264  "</font><br>";
265  break;
267  retv = "<font color=\"dark green\" size=2>" + aLine.message + "</font><br>";
268  break;
269  default:
270  retv = "<font size=2>" + aLine.message + "</font><br>";
271  }
272 
273  return retv;
274 }
275 
276 
278 {
279  switch( aLine.severity )
280  {
281  case REPORTER::RPT_ERROR:
282  return _( "Error: " ) + aLine.message + wxT( "\n" );
284  return _( "Warning: " ) + aLine.message + wxT( "\n" );
285  case REPORTER::RPT_INFO:
286  return _( "Info: " ) + aLine.message + wxT( "\n" );
287  default:
288  return aLine.message + wxT( "\n" );
289  }
290 }
291 
292 
293 void WX_HTML_REPORT_PANEL::onRightClick( wxMouseEvent& event )
294 {
295  wxMenu popup;
296  popup.Append( wxID_COPY, "Copy" );
297  PopupMenu( &popup );
298 }
299 
300 
301 void WX_HTML_REPORT_PANEL::onMenuEvent( wxMenuEvent& event )
302 {
303  if( event.GetId() == wxID_COPY )
304  {
305  if( wxTheClipboard->Open() )
306  {
307  bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
308  wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
309  wxTheClipboard->SetData( new wxTextDataObject( m_htmlView->SelectionToText() ) );
310  wxTheClipboard->Close();
311  wxTheClipboard->UsePrimarySelection( primarySelection );
312  }
313  }
314 }
315 
316 
317 void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
318 {
319  if( event.IsChecked() )
320  m_showAll = true;
321  else
322  m_showAll = false;
323 
324  syncCheckboxes();
325  Flush( true );
326 }
327 
328 
330 {
331  m_checkBoxShowAll->SetValue( m_showAll );
336 }
337 
338 
339 void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
340 {
341  if( event.IsChecked() )
343  else
344  m_severities &= ~REPORTER::RPT_WARNING;
345 
346  Flush( true );
347 }
348 
349 
350 void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
351 {
352  if( event.IsChecked() )
354  else
355  m_severities &= ~REPORTER::RPT_ERROR;
356 
357  Flush( true );
358 }
359 
360 
361 void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
362 {
363  if( event.IsChecked() )
365  else
366  m_severities &= ~REPORTER::RPT_INFO;
367 
368  Flush( true );
369 }
370 
371 
372 void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
373 {
374  if( event.IsChecked() )
376  else
377  m_severities &= ~REPORTER::RPT_ACTION;
378 
379  Flush( true );
380 }
381 
382 
383 void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
384 {
385  wxFileName fn( "./report.txt" );
386 
387  wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
388  TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
389 
390  if( dlg.ShowModal() != wxID_OK )
391  return;
392 
393  fn = dlg.GetPath();
394 
395  if( fn.GetExt().IsEmpty() )
396  fn.SetExt( "txt" );
397 
398  wxFile f( fn.GetFullPath(), wxFile::write );
399 
400  if( !f.IsOpened() )
401  {
402  wxString msg;
403 
404  msg.Printf( _( "Cannot write report to file \"%s\"." ),
405  fn.GetFullPath().GetData() );
406  wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
407  return;
408  }
409 
410  for( const REPORT_LINE& l : m_report )
411  {
412  f.Write( generatePlainText( l ) );
413  }
414 
415  f.Close();
416 }
417 
418 
420 {
421  m_report.clear();
422  m_reportHead.clear();
423  m_reportTail.clear();
424 }
425 
426 
427 void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
428 {
429  m_box->GetStaticBox()->SetLabel( aLabel );
430 }
431 
432 
434 {
435  if( aSeverities < 0 )
436  m_showAll = true;
437  else
438  {
439  m_showAll = false;
440  m_severities = aSeverities;
441  }
442 
443  syncCheckboxes();
444 }
445 
446 
448 {
449  return m_showAll ? m_severities | 0x80000000 : m_severities & ~0x80000000;
450 }
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.
void Flush(bool aSort=false)
Forces updating the HTML page, after the report is built in lazy mode If aSort = true, the body messages will be ordered by severity
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
void onRightClick(wxMouseEvent &event) override
WX_HTML_REPORT_PANEL(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(500, 300), long style=wxTAB_TRAVERSAL)
REPORT_LINES m_reportHead
Lines to print at the very beginning of the report, regardless of sorting
SEVERITY
Severity of the reported messages.
Definition: reporter.h:74
REPORTER & Reporter()
returns the reporter object that reports to this panel
WX_HTML_PANEL_REPORTER m_reporter
the reporter
REPORT_LINES m_reportTail
Lines to print at the very end of the report, regardless of sorting
void Report(const wxString &aText, REPORTER::SEVERITY aSeverity, REPORTER::LOCATION aLocation=REPORTER::LOC_BODY)
Reports the string.
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
LOCATION
Location where the message is to be reported.
Definition: reporter.h:88
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
void onMenuEvent(wxMenuEvent &event)
int m_severities
message severities to display (mask)