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 #define BADGE_SIZE 20
135 #define BADGE_FONT_SIZE 10
136 
137 static wxBitmap makeBadge( REPORTER::SEVERITY aStyle, int aCount, wxWindow* aWindow )
138 {
139  wxSize size( BADGE_SIZE, BADGE_SIZE );
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  if( aCount == 0 )
159  return bitmap;
160 
161  switch( aStyle )
162  {
163  case REPORTER::RPT_ERROR:
164  badgeColour = *wxRED;
165  textColour = *wxWHITE;
166  break;
168  badgeColour = wxColour( 235, 120, 80 ); // Orange
169  textColour = *wxWHITE;
170  break;
172  badgeColour = *wxGREEN;
173  textColour = *wxWHITE;
174  break;
175  case REPORTER::RPT_INFO:
176  default:
177  badgeColour = *wxLIGHT_GREY;
178  textColour = *wxBLACK;
179  break;
180  }
181 
182  brush.SetStyle( wxBRUSHSTYLE_SOLID );
183  brush.SetColour( badgeColour );
184  badgeDC.SetBrush( brush );
185  badgeDC.SetPen( wxPen( badgeColour, 0 ) );
186  badgeDC.DrawCircle( size.x / 2 - 1, size.y / 2, ( std::max( size.x, size.y ) / 2 ) - 1 );
187 
188  wxFont font( fontSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD );
189  badgeDC.SetFont( font );
190 
191  wxString text = wxString::Format( wxT( "%d" ), aCount );
192  wxSize textExtent = badgeDC.GetTextExtent( text );
193 
194  badgeDC.SetTextForeground( textColour );
195  badgeDC.DrawText( text, size.x / 2 - textExtent.x / 2 - 1, size.y / 2 - textExtent.y / 2 );
196 
197  return bitmap;
198 }
199 
200 
202 {
203  int count = Count( REPORTER::RPT_ERROR );
204  m_errorsBadge->SetBitmap( makeBadge( REPORTER::RPT_ERROR, count, m_errorsBadge ) );
205 
206  count = Count( REPORTER::RPT_WARNING );
208 }
209 
210 
211 wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
212 {
213  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
214  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
215 
216  return wxString::Format( wxT( "<html><body bgcolor='%s' text='%s'>%s</body></html>" ),
217  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
218  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
219  aBody );
220 }
221 
222 
223 int WX_HTML_REPORT_PANEL::Count( int severityMask )
224 {
225  int count = 0;
226 
227  for( const REPORT_LINE& reportLine : m_report )
228  if( severityMask & reportLine.severity )
229  count++;
230 
231  return count;
232 }
233 
234 
236 {
237  wxString retv;
238 
239  if( !m_showAll && ! ( m_severities & aLine.severity ) )
240  return retv;
241 
242  switch( aLine.severity )
243  {
244  case REPORTER::RPT_ERROR:
245  retv = "<font color=\"red\" size=2><b>" + _( "Error: " ) + "</b></font><font size=2>" +
246  aLine.message + "</font><br>";
247  break;
249  retv = "<font color=\"orange\" size=2><b>" + _( "Warning: " ) +
250  "</b></font><font size=2>" + aLine.message + "</font><br>";
251  break;
252  case REPORTER::RPT_INFO:
253  retv = "<font color=\"dark gray\" size=2><b>" + _( "Info: " ) + "</b>" + aLine.message +
254  "</font><br>";
255  break;
257  retv = "<font color=\"dark green\" size=2>" + aLine.message + "</font><br>";
258  break;
259  default:
260  retv = "<font size=2>" + aLine.message + "</font><br>";
261  }
262 
263  return retv;
264 }
265 
266 
268 {
269  switch( aLine.severity )
270  {
271  case REPORTER::RPT_ERROR:
272  return _( "Error: " ) + aLine.message + wxT( "\n" );
274  return _( "Warning: " ) + aLine.message + wxT( "\n" );
275  case REPORTER::RPT_INFO:
276  return _( "Info: " ) + aLine.message + wxT( "\n" );
277  default:
278  return aLine.message + wxT( "\n" );
279  }
280 }
281 
282 
283 void WX_HTML_REPORT_PANEL::onRightClick( wxMouseEvent& event )
284 {
285  wxMenu popup;
286  popup.Append( wxID_COPY, "Copy" );
287  PopupMenu( &popup );
288 }
289 
290 
291 void WX_HTML_REPORT_PANEL::onMenuEvent( wxMenuEvent& event )
292 {
293  if( event.GetId() == wxID_COPY )
294  {
295  if( wxTheClipboard->Open() )
296  {
297  bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
298  wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
299  wxTheClipboard->SetData( new wxTextDataObject( m_htmlView->SelectionToText() ) );
300  wxTheClipboard->Close();
301  wxTheClipboard->UsePrimarySelection( primarySelection );
302  }
303  }
304 }
305 
306 
307 void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
308 {
309  if( event.IsChecked() )
310  m_showAll = true;
311  else
312  m_showAll = false;
313 
314  syncCheckboxes();
315  Flush( true );
316 }
317 
318 
320 {
321  m_checkBoxShowAll->SetValue( m_showAll );
326 }
327 
328 
329 void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
330 {
331  if( event.IsChecked() )
333  else
334  m_severities &= ~REPORTER::RPT_WARNING;
335 
336  Flush( true );
337 }
338 
339 
340 void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
341 {
342  if( event.IsChecked() )
344  else
345  m_severities &= ~REPORTER::RPT_ERROR;
346 
347  Flush( true );
348 }
349 
350 
351 void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
352 {
353  if( event.IsChecked() )
355  else
356  m_severities &= ~REPORTER::RPT_INFO;
357 
358  Flush( true );
359 }
360 
361 
362 void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
363 {
364  if( event.IsChecked() )
366  else
367  m_severities &= ~REPORTER::RPT_ACTION;
368 
369  Flush( true );
370 }
371 
372 
373 void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
374 {
375  wxFileName fn( "./report.txt" );
376 
377  wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
378  TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
379 
380  if( dlg.ShowModal() != wxID_OK )
381  return;
382 
383  fn = dlg.GetPath();
384 
385  if( fn.GetExt().IsEmpty() )
386  fn.SetExt( "txt" );
387 
388  wxFile f( fn.GetFullPath(), wxFile::write );
389 
390  if( !f.IsOpened() )
391  {
392  wxString msg;
393 
394  msg.Printf( _( "Cannot write report to file \"%s\"." ),
395  fn.GetFullPath().GetData() );
396  wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
397  return;
398  }
399 
400  for( const REPORT_LINE& l : m_report )
401  {
402  f.Write( generatePlainText( l ) );
403  }
404 
405  f.Close();
406 }
407 
408 
410 {
411  m_report.clear();
412  m_reportHead.clear();
413  m_reportTail.clear();
414 }
415 
416 
417 void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
418 {
419  m_box->GetStaticBox()->SetLabel( aLabel );
420 }
421 
422 
424 {
425  if( aSeverities < 0 )
426  m_showAll = true;
427  else
428  {
429  m_showAll = false;
430  m_severities = aSeverities;
431  }
432 
433  syncCheckboxes();
434 }
435 
436 
438 {
439  return m_showAll ? m_severities | 0x80000000 : m_severities & ~0x80000000;
440 }
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
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
#define BADGE_SIZE
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
#define BADGE_FONT_SIZE
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)