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 #include <wx/log.h>
32 #include <wx/filedlg.h>
33 #include <wx/msgdlg.h>
34 #include <wx/menu.h>
35 
37  wxWindowID id,
38  const wxPoint& pos,
39  const wxSize& size,
40  long style ) :
41  WX_HTML_REPORT_PANEL_BASE( parent, id, pos, size, style ),
42  m_reporter( this ),
43  m_severities( -1 ),
44  m_lazyUpdate( false )
45 {
47  m_htmlView->SetPage( addHeader( "" ) );
48 
49  Connect( wxEVT_COMMAND_MENU_SELECTED,
50  wxMenuEventHandler( WX_HTML_REPORT_PANEL::onMenuEvent ), NULL, this );
51 }
52 
53 
55 {
56 }
57 
58 
59 void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
60 {
61  m_fgSizer->SetMinSize( aMinSize );
62  GetSizer()->SetSizeHints( this );
63 }
64 
65 
67 {
68  return m_reporter;
69 }
70 
71 
72 void WX_HTML_REPORT_PANEL::Report( const wxString& aText, SEVERITY aSeverity,
73  REPORTER::LOCATION aLocation )
74 {
75  REPORT_LINE line;
76  line.message = aText;
77  line.severity = aSeverity;
78 
79  if( aLocation == REPORTER::LOC_HEAD )
80  m_reportHead.push_back( line );
81  else if( aLocation == REPORTER::LOC_TAIL )
82  m_reportTail.push_back( line );
83  else
84  m_report.push_back( line );
85 
86  if( !m_lazyUpdate )
87  {
88  m_htmlView->AppendToPage( generateHtml( line ) );
90  }
91 }
92 
93 
94 void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
95 {
96  m_lazyUpdate = aLazyUpdate;
97 }
98 
99 
100 void WX_HTML_REPORT_PANEL::Flush( bool aSort )
101 {
102  wxString html;
103 
104  if( aSort )
105  {
106  std::sort( m_report.begin(), m_report.end(),
107  []( const REPORT_LINE& a, const REPORT_LINE& b)
108  {
109  return a.severity < b.severity;
110  });
111  }
112 
113  for( const auto& line : m_reportHead )
114  html += generateHtml( line );
115 
116  for( const auto& line : m_report )
117  html += generateHtml( line );
118 
119  for( const auto& line : m_reportTail )
120  html += generateHtml( line );
121 
122  m_htmlView->SetPage( addHeader( html ) );
123  scrollToBottom();
124 }
125 
126 
128 {
129  int x, y, xUnit, yUnit;
130 
131  m_htmlView->GetVirtualSize( &x, &y );
132  m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
133  m_htmlView->Scroll( 0, y / yUnit );
134 
135  updateBadges();
136 }
137 
138 
140 {
141  int count = Count(RPT_SEVERITY_ERROR );
143 
144  count = Count(RPT_SEVERITY_WARNING );
146 }
147 
148 
149 wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
150 {
151  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
152  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
153 
154  return wxString::Format( wxT( "<html><body bgcolor='%s' text='%s'>%s</body></html>" ),
155  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
156  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
157  aBody );
158 }
159 
160 
161 int WX_HTML_REPORT_PANEL::Count( int severityMask )
162 {
163  int count = 0;
164 
165  for( const auto& reportLineArray : { m_report, m_reportHead, m_reportTail } )
166  {
167  for( const REPORT_LINE& reportLine : reportLineArray )
168  {
169  if( severityMask & reportLine.severity )
170  count++;
171  }
172  }
173 
174  return count;
175 }
176 
177 
179 {
180  wxString retv;
181 
182  if( !( m_severities & aLine.severity ) )
183  return retv;
184 
185  switch( aLine.severity )
186  {
187  case RPT_SEVERITY_ERROR:
188  retv = "<font color=\"red\" size=3>" + _( "Error:" ) + " </font>"
189  "<font size=3>" + aLine.message + "</font><br>";
190  break;
192  retv = "<font size=3>" + _( "Warning:" ) + wxS( " " ) + aLine.message + "</font><br>";
193  break;
194  case RPT_SEVERITY_INFO:
195  retv = "<font color=\"dark gray\" size=3>" + aLine.message + "</font><br>";
196  break;
197  case RPT_SEVERITY_ACTION:
198  retv = "<font color=\"dark green\" size=3>" + aLine.message + "</font><br>";
199  break;
200  default:
201  retv = "<font size=3>" + aLine.message + "</font><br>";
202  }
203 
204  return retv;
205 }
206 
207 
209 {
210  switch( aLine.severity )
211  {
212  case RPT_SEVERITY_ERROR:
213  return _( "Error:" ) + wxS( " " )+ aLine.message + wxT( "\n" );
215  return _( "Warning:" ) + wxS( " " )+ aLine.message + wxT( "\n" );
216  case RPT_SEVERITY_INFO:
217  return _( "Info:" ) + wxS( " " )+ aLine.message + wxT( "\n" );
218  default:
219  return aLine.message + wxT( "\n" );
220  }
221 }
222 
223 
224 void WX_HTML_REPORT_PANEL::onRightClick( wxMouseEvent& event )
225 {
226  wxMenu popup;
227  popup.Append( wxID_COPY, "Copy" );
228  PopupMenu( &popup );
229 }
230 
231 
232 void WX_HTML_REPORT_PANEL::onMenuEvent( wxMenuEvent& event )
233 {
234  if( event.GetId() == wxID_COPY )
235  {
236  wxLogNull doNotLog; // disable logging of failed clipboard actions
237 
238  if( wxTheClipboard->Open() )
239  {
240  bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
241  wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
242  wxTheClipboard->SetData( new wxTextDataObject( m_htmlView->SelectionToText() ) );
243  wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
244  wxTheClipboard->Close();
245  wxTheClipboard->UsePrimarySelection( primarySelection );
246  }
247  }
248 }
249 
250 
251 // Don't globally define this; different facilities use different definitions of "ALL"
253 
254 
255 void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
256 {
257  if( event.IsChecked() )
259  else
261 
262  syncCheckboxes();
263  Flush( true );
264 }
265 
266 
268 {
274 }
275 
276 
277 void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
278 {
279  if( event.IsChecked() )
281  else
283 
284  syncCheckboxes();
285  Flush( true );
286 }
287 
288 
289 void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
290 {
291  if( event.IsChecked() )
293  else
295 
296  syncCheckboxes();
297  Flush( true );
298 }
299 
300 
301 void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
302 {
303  if( event.IsChecked() )
305  else
307 
308  syncCheckboxes();
309  Flush( true );
310 }
311 
312 
313 void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
314 {
315  if( event.IsChecked() )
317  else
319 
320  syncCheckboxes();
321  Flush( true );
322 }
323 
324 
325 void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
326 {
327  wxFileName fn;
328 
329  if( m_reportFileName.empty() )
330  fn = wxT( "./report.txt" );
331  else
332  fn = m_reportFileName;
333 
334  wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
335  TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
336 
337  if( dlg.ShowModal() != wxID_OK )
338  return;
339 
340  fn = dlg.GetPath();
341 
342  if( fn.GetExt().IsEmpty() )
343  fn.SetExt( "txt" );
344 
345  wxFFile f( fn.GetFullPath(), "wb" );
346 
347  if( !f.IsOpened() )
348  {
349  wxString msg;
350 
351  msg.Printf( _( "Cannot write report to file '%s'." ),
352  fn.GetFullPath().GetData() );
353  wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
354  return;
355  }
356 
357  for( REPORT_LINES section : { m_reportHead, m_report, m_reportTail } )
358  {
359  for( const REPORT_LINE& l : section )
360  {
361  wxString s = generatePlainText( l );
362 
364  f.Write( s );
365  }
366  }
367 
368  m_reportFileName = fn.GetFullPath();
369  f.Close();
370 }
371 
372 
374 {
375  m_report.clear();
376  m_reportHead.clear();
377  m_reportTail.clear();
378 }
379 
380 
381 void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
382 {
383  m_box->GetStaticBox()->SetLabel( aLabel );
384 }
385 
386 
388 {
389  if( aSeverities < 0 )
391  else
392  m_severities = aSeverities;
393 
394  syncCheckboxes();
395 }
396 
397 
399 {
400  return m_severities;
401 }
402 
403 
404 void WX_HTML_REPORT_PANEL::SetFileName( const wxString& aReportFileName )
405 {
406  m_reportFileName = aReportFileName;
407 }
408 
409 
411 {
412  return ( m_reportFileName );
413 }
414 
415 
416 void WX_HTML_REPORT_PANEL::SetShowSeverity( SEVERITY aSeverity, bool aValue )
417 {
418  switch( aSeverity )
419  {
420  case RPT_SEVERITY_INFO:
421  m_checkBoxShowInfos->SetValue( aValue );
422  break;
423 
424  case RPT_SEVERITY_ACTION:
425  m_checkBoxShowActions->SetValue( aValue );
426  break;
427 
429  m_checkBoxShowWarnings->SetValue( aValue );
430  break;
431 
432  default:
433  m_checkBoxShowErrors->SetValue( aValue );
434  break;
435  }
436 }
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.
#define _(s)
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.
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 lazy update.
void onMenuEvent(wxMenuEvent &event)
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
Definition: string.cpp:114
int m_severities
message severities to display (mask)
static int RPT_SEVERITY_ALL