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_lazyUpdate( false )
39 {
41  m_htmlView->SetPage( addHeader( "" ) );
42 
43  Connect( wxEVT_COMMAND_MENU_SELECTED,
44  wxMenuEventHandler( WX_HTML_REPORT_PANEL::onMenuEvent ), NULL, this );
45 }
46 
47 
49 {
50 }
51 
52 
53 void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
54 {
55  m_fgSizer->SetMinSize( aMinSize );
56  GetSizer()->SetSizeHints( this );
57 }
58 
59 
61 {
62  return m_reporter;
63 }
64 
65 
66 void WX_HTML_REPORT_PANEL::Report( const wxString& aText, REPORTER::SEVERITY aSeverity,
67  REPORTER::LOCATION aLocation )
68 {
69  REPORT_LINE line;
70  line.message = aText;
71  line.severity = aSeverity;
72 
73  if( aLocation == REPORTER::LOC_HEAD )
74  m_reportHead.push_back( line );
75  else if( aLocation == REPORTER::LOC_TAIL )
76  m_reportTail.push_back( line );
77  else
78  m_report.push_back( line );
79 
80  if( !m_lazyUpdate )
81  {
82  m_htmlView->AppendToPage( generateHtml( line ) );
84  }
85 }
86 
87 
88 void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
89 {
90  m_lazyUpdate = aLazyUpdate;
91 }
92 
93 
94 void WX_HTML_REPORT_PANEL::Flush( bool aSort )
95 {
96  wxString html;
97 
98  if( aSort )
99  {
100  std::sort( m_report.begin(), m_report.end(),
101  []( const REPORT_LINE& a, const REPORT_LINE& b)
102  {
103  return a.severity < b.severity;
104  });
105  }
106 
107  for( auto line : m_reportHead )
108  html += generateHtml( line );
109 
110  for( auto line : m_report )
111  html += generateHtml( line );
112 
113  for( auto line : m_reportTail )
114  html += generateHtml( line );
115 
116  m_htmlView->SetPage( addHeader( html ) );
117  scrollToBottom();
118 }
119 
120 
122 {
123  int x, y, xUnit, yUnit;
124 
125  m_htmlView->GetVirtualSize( &x, &y );
126  m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
127  m_htmlView->Scroll( 0, y / yUnit );
128 
129  updateBadges();
130 }
131 
132 
133 #define BADGE_SIZE 20
134 #define BADGE_FONT_SIZE 10
135 
136 static wxBitmap makeBadge( REPORTER::SEVERITY aStyle, int aCount, wxWindow* aWindow )
137 {
138  wxSize size( BADGE_SIZE, BADGE_SIZE );
139  wxBitmap bitmap( size );
140  wxBrush brush;
141  wxMemoryDC badgeDC;
142  wxColour badgeColour;
143  wxColour textColour;
144  int fontSize = BADGE_FONT_SIZE;
145 
146  if( aCount > 99 )
147  fontSize--;
148 
149  badgeDC.SelectObject( bitmap );
150 
151  brush.SetStyle( wxBRUSHSTYLE_SOLID );
152  // We're one level deep in staticBoxes; each level is darkened by 210
153  brush.SetColour( aWindow->GetParent()->GetBackgroundColour().MakeDisabled( 210 ) );
154  badgeDC.SetBackground( brush );
155  badgeDC.Clear();
156 
157  if( aCount == 0 )
158  return bitmap;
159 
160  switch( aStyle )
161  {
162  case REPORTER::RPT_ERROR:
163  badgeColour = *wxRED;
164  textColour = *wxWHITE;
165  break;
167  badgeColour = wxColour( 235, 120, 80 ); // Orange
168  textColour = *wxWHITE;
169  break;
171  badgeColour = *wxGREEN;
172  textColour = *wxWHITE;
173  break;
174  case REPORTER::RPT_INFO:
175  default:
176  badgeColour = *wxLIGHT_GREY;
177  textColour = *wxBLACK;
178  break;
179  }
180 
181  brush.SetStyle( wxBRUSHSTYLE_SOLID );
182  brush.SetColour( badgeColour );
183  badgeDC.SetBrush( brush );
184  badgeDC.SetPen( wxPen( badgeColour, 0 ) );
185  badgeDC.DrawCircle( size.x / 2 - 1, size.y / 2, ( std::max( size.x, size.y ) / 2 ) - 1 );
186 
187  wxFont font( fontSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD );
188  badgeDC.SetFont( font );
189 
190  wxString text = wxString::Format( wxT( "%d" ), aCount );
191  wxSize textExtent = badgeDC.GetTextExtent( text );
192 
193  badgeDC.SetTextForeground( textColour );
194  badgeDC.DrawText( text, size.x / 2 - textExtent.x / 2 - 1, size.y / 2 - textExtent.y / 2 );
195 
196  return bitmap;
197 }
198 
199 
201 {
202  int count = Count( REPORTER::RPT_ERROR );
203  m_errorsBadge->SetBitmap( makeBadge( REPORTER::RPT_ERROR, count, m_errorsBadge ) );
204 
205  count = Count( REPORTER::RPT_WARNING );
207 }
208 
209 
210 wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
211 {
212  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
213  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
214 
215  return wxString::Format( wxT( "<html><body bgcolor='%s' text='%s'>%s</body></html>" ),
216  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
217  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
218  aBody );
219 }
220 
221 
222 int WX_HTML_REPORT_PANEL::Count( int severityMask )
223 {
224  int count = 0;
225 
226  for( const REPORT_LINE& reportLine : m_report )
227  if( severityMask & reportLine.severity )
228  count++;
229 
230  return count;
231 }
232 
233 
235 {
236  wxString retv;
237 
238  if( !( m_severities & aLine.severity ) )
239  return retv;
240 
241  switch( aLine.severity )
242  {
243  case REPORTER::RPT_ERROR:
244  retv = "<font color=\"red\" size=2><b>" + _( "Error: " ) + "</b></font><font size=2>" +
245  aLine.message + "</font><br>";
246  break;
248  retv = "<font color=\"orange\" size=2><b>" + _( "Warning: " ) +
249  "</b></font><font size=2>" + aLine.message + "</font><br>";
250  break;
251  case REPORTER::RPT_INFO:
252  retv = "<font color=\"dark gray\" size=2><b>" + _( "Info: " ) + "</b>" + aLine.message +
253  "</font><br>";
254  break;
256  retv = "<font color=\"dark green\" size=2>" + aLine.message + "</font><br>";
257  break;
258  default:
259  retv = "<font size=2>" + aLine.message + "</font><br>";
260  }
261 
262  return retv;
263 }
264 
265 
267 {
268  switch( aLine.severity )
269  {
270  case REPORTER::RPT_ERROR:
271  return _( "Error: " ) + aLine.message + wxT( "\n" );
273  return _( "Warning: " ) + aLine.message + wxT( "\n" );
274  case REPORTER::RPT_INFO:
275  return _( "Info: " ) + aLine.message + wxT( "\n" );
276  default:
277  return aLine.message + wxT( "\n" );
278  }
279 }
280 
281 
282 void WX_HTML_REPORT_PANEL::onRightClick( wxMouseEvent& event )
283 {
284  wxMenu popup;
285  popup.Append( wxID_COPY, "Copy" );
286  PopupMenu( &popup );
287 }
288 
289 
290 void WX_HTML_REPORT_PANEL::onMenuEvent( wxMenuEvent& event )
291 {
292  if( event.GetId() == wxID_COPY )
293  {
294  if( wxTheClipboard->Open() )
295  {
296  bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
297  wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
298  wxTheClipboard->SetData( new wxTextDataObject( m_htmlView->SelectionToText() ) );
299  wxTheClipboard->Close();
300  wxTheClipboard->UsePrimarySelection( primarySelection );
301  }
302  }
303 }
304 
305 
306 void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
307 {
308  if( event.IsChecked() )
310  else
311  m_severities = 0;
312 
313  syncCheckboxes();
314  Flush( true );
315 }
316 
317 
319 {
325 }
326 
327 
328 void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
329 {
330  if( event.IsChecked() )
332  else
333  m_severities &= ~REPORTER::RPT_WARNING;
334 
335  syncCheckboxes();
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  syncCheckboxes();
348  Flush( true );
349 }
350 
351 
352 void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
353 {
354  if( event.IsChecked() )
356  else
357  m_severities &= ~REPORTER::RPT_INFO;
358 
359  syncCheckboxes();
360  Flush( true );
361 }
362 
363 
364 void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
365 {
366  if( event.IsChecked() )
368  else
369  m_severities &= ~REPORTER::RPT_ACTION;
370 
371  syncCheckboxes();
372  Flush( true );
373 }
374 
375 
376 void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
377 {
378  wxFileName fn( "./report.txt" );
379 
380  wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
381  TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
382 
383  if( dlg.ShowModal() != wxID_OK )
384  return;
385 
386  fn = dlg.GetPath();
387 
388  if( fn.GetExt().IsEmpty() )
389  fn.SetExt( "txt" );
390 
391  wxFile f( fn.GetFullPath(), wxFile::write );
392 
393  if( !f.IsOpened() )
394  {
395  wxString msg;
396 
397  msg.Printf( _( "Cannot write report to file \"%s\"." ),
398  fn.GetFullPath().GetData() );
399  wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
400  return;
401  }
402 
403  for( const REPORT_LINE& l : m_report )
404  {
405  f.Write( generatePlainText( l ) );
406  }
407 
408  f.Close();
409 }
410 
411 
413 {
414  m_report.clear();
415  m_reportHead.clear();
416  m_reportTail.clear();
417 }
418 
419 
420 void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
421 {
422  m_box->GetStaticBox()->SetLabel( aLabel );
423 }
424 
425 
427 {
428  if( aSeverities < 0 )
430  else
431  m_severities = aSeverities;
432 
433  syncCheckboxes();
434 }
435 
436 
438 {
439  return m_severities;
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,...
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.
int Count(int severityMask)
return the number of messages matching the given severity mask.
Definition of file extensions used in Kicad.
#define _(s)
REPORT_LINES m_report
copy of the report, stored for filtering
LOCATION
Location where the message is to be reported.
Definition: reporter.h:90
static constexpr int RPT_ALL
Definition: reporter.h:82
#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)