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 #include <kicad_string.h>
30 #include <wx/ffile.h>
31 
33  wxWindowID id,
34  const wxPoint& pos,
35  const wxSize& size,
36  long style ) :
37  WX_HTML_REPORT_PANEL_BASE( parent, id, pos, size, style ),
38  m_reporter( this ),
39  m_severities( -1 ),
40  m_lazyUpdate( false )
41 {
43  m_htmlView->SetPage( addHeader( "" ) );
44 
45  Connect( wxEVT_COMMAND_MENU_SELECTED,
46  wxMenuEventHandler( WX_HTML_REPORT_PANEL::onMenuEvent ), NULL, this );
47 }
48 
49 
51 {
52 }
53 
54 
55 void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
56 {
57  m_fgSizer->SetMinSize( aMinSize );
58  GetSizer()->SetSizeHints( this );
59 }
60 
61 
63 {
64  return m_reporter;
65 }
66 
67 
68 void WX_HTML_REPORT_PANEL::Report( const wxString& aText, SEVERITY aSeverity,
69  REPORTER::LOCATION aLocation )
70 {
71  REPORT_LINE line;
72  line.message = aText;
73  line.severity = aSeverity;
74 
75  if( aLocation == REPORTER::LOC_HEAD )
76  m_reportHead.push_back( line );
77  else if( aLocation == REPORTER::LOC_TAIL )
78  m_reportTail.push_back( line );
79  else
80  m_report.push_back( line );
81 
82  if( !m_lazyUpdate )
83  {
84  m_htmlView->AppendToPage( generateHtml( line ) );
86  }
87 }
88 
89 
90 void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
91 {
92  m_lazyUpdate = aLazyUpdate;
93 }
94 
95 
96 void WX_HTML_REPORT_PANEL::Flush( bool aSort )
97 {
98  wxString html;
99 
100  if( aSort )
101  {
102  std::sort( m_report.begin(), m_report.end(),
103  []( const REPORT_LINE& a, const REPORT_LINE& b)
104  {
105  return a.severity < b.severity;
106  });
107  }
108 
109  for( const auto& line : m_reportHead )
110  html += generateHtml( line );
111 
112  for( const auto& line : m_report )
113  html += generateHtml( line );
114 
115  for( const auto& line : m_reportTail )
116  html += generateHtml( line );
117 
118  m_htmlView->SetPage( addHeader( html ) );
119  scrollToBottom();
120 }
121 
122 
124 {
125  int x, y, xUnit, yUnit;
126 
127  m_htmlView->GetVirtualSize( &x, &y );
128  m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
129  m_htmlView->Scroll( 0, y / yUnit );
130 
131  updateBadges();
132 }
133 
134 
136 {
137  int count = Count(RPT_SEVERITY_ERROR );
139 
140  count = Count(RPT_SEVERITY_WARNING );
142 }
143 
144 
145 wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
146 {
147  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
148  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
149 
150  return wxString::Format( wxT( "<html><body bgcolor='%s' text='%s'>%s</body></html>" ),
151  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
152  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
153  aBody );
154 }
155 
156 
157 int WX_HTML_REPORT_PANEL::Count( int severityMask )
158 {
159  int count = 0;
160 
161  for( const auto& reportLineArray : { m_report, m_reportHead, m_reportTail } )
162  {
163  for( const REPORT_LINE& reportLine : reportLineArray )
164  {
165  if( severityMask & reportLine.severity )
166  count++;
167  }
168  }
169 
170  return count;
171 }
172 
173 
175 {
176  wxString retv;
177 
178  if( !( m_severities & aLine.severity ) )
179  return retv;
180 
181  switch( aLine.severity )
182  {
183  case RPT_SEVERITY_ERROR:
184  retv = "<font color=\"red\" size=3>" + _( "Error:" ) + " </font>"
185  "<font size=3>" + aLine.message + "</font><br>";
186  break;
188  retv = "<font size=3>" + _( "Warning:" ) + wxS( " " ) + aLine.message + "</font><br>";
189  break;
190  case RPT_SEVERITY_INFO:
191  retv = "<font color=\"dark gray\" size=3>" + aLine.message + "</font><br>";
192  break;
193  case RPT_SEVERITY_ACTION:
194  retv = "<font color=\"dark green\" size=3>" + aLine.message + "</font><br>";
195  break;
196  default:
197  retv = "<font size=3>" + aLine.message + "</font><br>";
198  }
199 
200  return retv;
201 }
202 
203 
205 {
206  switch( aLine.severity )
207  {
208  case RPT_SEVERITY_ERROR:
209  return _( "Error:" ) + wxS( " " )+ aLine.message + wxT( "\n" );
211  return _( "Warning:" ) + wxS( " " )+ aLine.message + wxT( "\n" );
212  case RPT_SEVERITY_INFO:
213  return _( "Info:" ) + wxS( " " )+ aLine.message + wxT( "\n" );
214  default:
215  return aLine.message + wxT( "\n" );
216  }
217 }
218 
219 
220 void WX_HTML_REPORT_PANEL::onRightClick( wxMouseEvent& event )
221 {
222  wxMenu popup;
223  popup.Append( wxID_COPY, "Copy" );
224  PopupMenu( &popup );
225 }
226 
227 
228 void WX_HTML_REPORT_PANEL::onMenuEvent( wxMenuEvent& event )
229 {
230  if( event.GetId() == wxID_COPY )
231  {
232  wxLogNull doNotLog; // disable logging of failed clipboard actions
233 
234  if( wxTheClipboard->Open() )
235  {
236  bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
237  wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
238  wxTheClipboard->SetData( new wxTextDataObject( m_htmlView->SelectionToText() ) );
239  wxTheClipboard->Close();
240  wxTheClipboard->UsePrimarySelection( primarySelection );
241  }
242  }
243 }
244 
245 
246 // Don't globally define this; different facilities use different definitions of "ALL"
248 
249 
250 void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
251 {
252  if( event.IsChecked() )
254  else
256 
257  syncCheckboxes();
258  Flush( true );
259 }
260 
261 
263 {
269 }
270 
271 
272 void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
273 {
274  if( event.IsChecked() )
276  else
278 
279  syncCheckboxes();
280  Flush( true );
281 }
282 
283 
284 void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
285 {
286  if( event.IsChecked() )
288  else
290 
291  syncCheckboxes();
292  Flush( true );
293 }
294 
295 
296 void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
297 {
298  if( event.IsChecked() )
300  else
302 
303  syncCheckboxes();
304  Flush( true );
305 }
306 
307 
308 void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
309 {
310  if( event.IsChecked() )
312  else
314 
315  syncCheckboxes();
316  Flush( true );
317 }
318 
319 
320 void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
321 {
322  wxFileName fn;
323 
324  if( m_reportFileName.empty() )
325  fn = wxT( "./report.txt" );
326  else
327  fn = m_reportFileName;
328 
329  wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
330  TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
331 
332  if( dlg.ShowModal() != wxID_OK )
333  return;
334 
335  fn = dlg.GetPath();
336 
337  if( fn.GetExt().IsEmpty() )
338  fn.SetExt( "txt" );
339 
340  wxFFile f( fn.GetFullPath(), "wb" );
341 
342  if( !f.IsOpened() )
343  {
344  wxString msg;
345 
346  msg.Printf( _( "Cannot write report to file \"%s\"." ),
347  fn.GetFullPath().GetData() );
348  wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
349  return;
350  }
351 
352  for( REPORT_LINES section : { m_reportHead, m_report, m_reportTail } )
353  {
354  for( const REPORT_LINE& l : section )
355  {
356  wxString s = generatePlainText( l );
357 
359  f.Write( s );
360  }
361  }
362 
363  m_reportFileName = fn.GetFullPath();
364  f.Close();
365 }
366 
367 
369 {
370  m_report.clear();
371  m_reportHead.clear();
372  m_reportTail.clear();
373 }
374 
375 
376 void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
377 {
378  m_box->GetStaticBox()->SetLabel( aLabel );
379 }
380 
381 
383 {
384  if( aSeverities < 0 )
386  else
387  m_severities = aSeverities;
388 
389  syncCheckboxes();
390 }
391 
392 
394 {
395  return m_severities;
396 }
397 
398 
399 void WX_HTML_REPORT_PANEL::SetFileName( const wxString& aReportFileName )
400 {
401  m_reportFileName = aReportFileName;
402 }
403 
404 
406 {
407  return ( m_reportFileName );
408 }
409 
410 
411 void WX_HTML_REPORT_PANEL::SetShowSeverity( SEVERITY aSeverity, bool aValue )
412 {
413  switch( aSeverity )
414  {
415  case RPT_SEVERITY_INFO:
416  m_checkBoxShowInfos->SetValue( aValue );
417  break;
418 
419  case RPT_SEVERITY_ACTION:
420  m_checkBoxShowActions->SetValue( aValue );
421  break;
422 
424  m_checkBoxShowWarnings->SetValue( aValue );
425  break;
426 
427  default:
428  m_checkBoxShowErrors->SetValue( aValue );
429  break;
430  }
431 }
void SetVisibleSeverities(int aSeverities)
void onCheckBoxShowInfos(wxCommandEvent &event) override
void onBtnSaveToFile(wxCommandEvent &event) override
wxString addHeader(const wxString &aBody)
void SetLazyUpdate(bool aLazyUpdate)
Forces updating the HTML page, after the report is built in lazy mode If aSort = true,...
~WX_HTML_REPORT_PANEL()
Set the min size of the area which displays html messages:
void onCheckBoxShowActions(wxCommandEvent &event) override
wxString generatePlainText(const REPORT_LINE &aLine)
Class WX_HTML_REPORT_PANEL_BASE.
SEVERITY
Definition: ui_common.h:83
void UpdateNumber(int aNumber, SEVERITY aSeverity)
Update the number displayed on the badge.
void Flush(bool aSort=false)
Set the visible severity filter.
void onCheckBoxShowWarnings(wxCommandEvent &event) override
void onCheckBoxShowErrors(wxCommandEvent &event) override
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
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
... and at the beginning, regardless of sorting
#define NULL
WX_HTML_PANEL_REPORTER m_reporter
REPORT_LINES m_reportTail
Lines to print at the end, regardless of sorting.
int Count(int severityMask)
sets the frame label
Definition of file extensions used in Kicad.
wxString m_reportFileName
defaults to the not very useful /bin/report.txt
void SetShowSeverity(SEVERITY aSeverity, bool aValue)
Set the report full file name to the string.
REPORT_LINES m_report
copy of the report, stored for filtering
void Report(const wxString &aText, SEVERITY aSeverity, REPORTER::LOCATION aLocation=REPORTER::LOC_BODY)
Reports the string.
LOCATION
Location where the message is to be reported.
Definition: reporter.h:73
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void Clear()
return the number of messages matching the given severity mask.
#define _(s)
Definition: 3d_actions.cpp:33
std::vector< REPORT_LINE > REPORT_LINES
void onCheckBoxShowAll(wxCommandEvent &event) override
wxString TextFileWildcard()
wxString generateHtml(const REPORT_LINE &aLine)
void MsgPanelSetMinSize(const wxSize &aMinSize)
returns the reporter object that reports to this panel
void SetFileName(const wxString &aReportFileName)
void SetLabel(const wxString &aLabel) override
Sets the lasy update.
void onMenuEvent(wxMenuEvent &event)
bool ConvertSmartQuotesAndDashes(wxString *aString)
Converts curly quotes and em/en dashes to straight quotes and dashes.
Definition: string.cpp:43
int m_severities
message severities to display (mask)
static int RPT_SEVERITY_ALL