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-2021 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 <string_utils.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 #include <wx/textctrl.h>
36 
37 WX_HTML_REPORT_PANEL::WX_HTML_REPORT_PANEL( wxWindow* parent, wxWindowID id, const wxPoint& pos,
38  const wxSize& size, long style ) :
39  WX_HTML_REPORT_PANEL_BASE( parent, id, pos, size, style ),
40  m_reporter( this ),
41  m_severities( -1 ),
42  m_lazyUpdate( false )
43 {
46  Flush();
47 
48  Connect( wxEVT_COMMAND_MENU_SELECTED,
49  wxMenuEventHandler( WX_HTML_REPORT_PANEL::onMenuEvent ), nullptr, this );
50 
51  m_htmlView->Bind( wxEVT_SYS_COLOUR_CHANGED,
52  wxSysColourChangedEventHandler( WX_HTML_REPORT_PANEL::onThemeChanged ),
53  this );
54 }
55 
56 
58 {
59 }
60 
61 
62 void WX_HTML_REPORT_PANEL::onThemeChanged( wxSysColourChangedEvent &aEvent )
63 {
64  Flush();
65 
66  aEvent.Skip();
67 }
68 
69 
70 void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
71 {
72  m_fgSizer->SetMinSize( aMinSize );
73  GetSizer()->SetSizeHints( this );
74 }
75 
76 
78 {
79  return m_reporter;
80 }
81 
82 
83 void WX_HTML_REPORT_PANEL::Report( const wxString& aText, SEVERITY aSeverity,
84  REPORTER::LOCATION aLocation )
85 {
86  REPORT_LINE line;
87  line.message = aText;
88  line.severity = aSeverity;
89 
90  if( aLocation == REPORTER::LOC_HEAD )
91  m_reportHead.push_back( line );
92  else if( aLocation == REPORTER::LOC_TAIL )
93  m_reportTail.push_back( line );
94  else
95  m_report.push_back( line );
96 
97  if( !m_lazyUpdate )
98  {
100  scrollToBottom();
101  }
102 }
103 
104 
105 void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
106 {
107  m_lazyUpdate = aLazyUpdate;
108 }
109 
110 
111 void WX_HTML_REPORT_PANEL::Flush( bool aSort )
112 {
113  wxString html;
114 
115  if( aSort )
116  {
117  std::sort( m_report.begin(), m_report.end(),
118  []( const REPORT_LINE& a, const REPORT_LINE& b)
119  {
120  return a.severity < b.severity;
121  });
122  }
123 
124  for( const auto& line : m_reportHead )
125  html += generateHtml( line );
126 
127  for( const auto& line : m_report )
128  html += generateHtml( line );
129 
130  for( const auto& line : m_reportTail )
131  html += generateHtml( line );
132 
133  m_htmlView->SetPage( html );
134  scrollToBottom();
135 }
136 
137 
139 {
140  int x, y, xUnit, yUnit;
141 
142  m_htmlView->GetVirtualSize( &x, &y );
143  m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
144  m_htmlView->Scroll( 0, y / yUnit );
145 
146  updateBadges();
147 }
148 
149 
151 {
152  int count = Count(RPT_SEVERITY_ERROR );
154 
155  count = Count(RPT_SEVERITY_WARNING );
157 }
158 
159 
160 int WX_HTML_REPORT_PANEL::Count( int severityMask )
161 {
162  int count = 0;
163 
164  for( const auto& reportLineArray : { m_report, m_reportHead, m_reportTail } )
165  {
166  for( const REPORT_LINE& reportLine : reportLineArray )
167  {
168  if( severityMask & reportLine.severity )
169  count++;
170  }
171  }
172 
173  return count;
174 }
175 
176 
178 {
179  wxString retv;
180 
181  if( !( m_severities & aLine.severity ) )
182  return retv;
183 
184  switch( aLine.severity )
185  {
186  case RPT_SEVERITY_ERROR:
187  retv = "<font color=\"red\" size=3>" + _( "Error:" ) + " </font>"
188  "<font size=3>" + aLine.message + "</font><br>";
189  break;
191  retv = "<font size=3>" + _( "Warning:" ) + wxS( " " ) + aLine.message + "</font><br>";
192  break;
193  case RPT_SEVERITY_INFO:
194  retv = "<font color=\"dark gray\" size=3>" + aLine.message + "</font><br>";
195  break;
196  case RPT_SEVERITY_ACTION:
197  retv = "<font color=\"dark green\" size=3>" + aLine.message + "</font><br>";
198  break;
199  default:
200  retv = "<font size=3>" + aLine.message + "</font><br>";
201  }
202 
203  return retv;
204 }
205 
206 
208 {
209  switch( aLine.severity )
210  {
211  case RPT_SEVERITY_ERROR: return _( "Error:" ) + wxS( " " ) + aLine.message + wxT( "\n" );
212  case RPT_SEVERITY_WARNING: return _( "Warning:" ) + wxS( " " ) + aLine.message + wxT( "\n" );
213  case RPT_SEVERITY_INFO: return _( "Info:" ) + wxS( " " ) + aLine.message + wxT( "\n" );
214  default: return aLine.message + wxT( "\n" );
215  }
216 }
217 
218 
219 void WX_HTML_REPORT_PANEL::onRightClick( wxMouseEvent& event )
220 {
221  wxMenu popup;
222  popup.Append( wxID_COPY, "Copy" );
223  PopupMenu( &popup );
224 }
225 
226 
227 void WX_HTML_REPORT_PANEL::onMenuEvent( wxMenuEvent& event )
228 {
229  if( event.GetId() == wxID_COPY )
230  {
231  wxLogNull doNotLog; // disable logging of failed clipboard actions
232 
233  if( wxTheClipboard->Open() )
234  {
235  bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
236  wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
237  wxTheClipboard->SetData( new wxTextDataObject( m_htmlView->SelectionToText() ) );
238  wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
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"
249 
250 
251 void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
252 {
253  if( event.IsChecked() )
255  else
257 
258  syncCheckboxes();
259  Flush( true );
260 }
261 
262 
264 {
270 }
271 
272 
273 void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
274 {
275  if( event.IsChecked() )
277  else
279 
280  syncCheckboxes();
281  Flush( true );
282 }
283 
284 
285 void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
286 {
287  if( event.IsChecked() )
289  else
291 
292  syncCheckboxes();
293  Flush( true );
294 }
295 
296 
297 void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
298 {
299  if( event.IsChecked() )
301  else
303 
304  syncCheckboxes();
305  Flush( true );
306 }
307 
308 
309 void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
310 {
311  if( event.IsChecked() )
313  else
315 
316  syncCheckboxes();
317  Flush( true );
318 }
319 
320 
321 void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
322 {
323  wxFileName fn;
324 
325  if( m_reportFileName.empty() )
326  fn = wxT( "./report.txt" );
327  else
328  fn = m_reportFileName;
329 
330  wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
331  TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
332 
333  if( dlg.ShowModal() != wxID_OK )
334  return;
335 
336  fn = dlg.GetPath();
337 
338  if( fn.GetExt().IsEmpty() )
339  fn.SetExt( "txt" );
340 
341  wxFFile f( fn.GetFullPath(), "wb" );
342 
343  if( !f.IsOpened() )
344  {
345  wxString msg;
346 
347  msg.Printf( _( "Cannot write report to file '%s'." ),
348  fn.GetFullPath().GetData() );
349  wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
350  return;
351  }
352 
353  for( REPORT_LINES section : { m_reportHead, m_report, m_reportTail } )
354  {
355  for( const REPORT_LINE& l : section )
356  {
357  wxString s = generatePlainText( l );
358 
360  f.Write( s );
361  }
362  }
363 
364  m_reportFileName = fn.GetFullPath();
365  f.Close();
366 }
367 
368 
370 {
371  m_report.clear();
372  m_reportHead.clear();
373  m_reportTail.clear();
374 }
375 
376 
377 void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
378 {
379  m_box->GetStaticBox()->SetLabel( aLabel );
380 }
381 
382 
384 {
385  if( aSeverities < 0 )
387  else
388  m_severities = aSeverities;
389 
390  syncCheckboxes();
391 }
392 
393 
395 {
396  return m_severities;
397 }
398 
399 
400 void WX_HTML_REPORT_PANEL::SetFileName( const wxString& aReportFileName )
401 {
402  m_reportFileName = aReportFileName;
403 }
404 
405 
407 {
408  return ( m_reportFileName );
409 }
410 
411 
412 void WX_HTML_REPORT_PANEL::SetShowSeverity( SEVERITY aSeverity, bool aValue )
413 {
414  switch( aSeverity )
415  {
416  case RPT_SEVERITY_INFO: m_checkBoxShowInfos->SetValue( aValue ); break;
417  case RPT_SEVERITY_ACTION: m_checkBoxShowActions->SetValue( aValue ); break;
418  case RPT_SEVERITY_WARNING: m_checkBoxShowWarnings->SetValue( aValue ); break;
419  default: m_checkBoxShowErrors->SetValue( aValue ); break;
420  }
421 }
void SetVisibleSeverities(int aSeverities)
void onCheckBoxShowInfos(wxCommandEvent &event) override
void onBtnSaveToFile(wxCommandEvent &event) override
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.
void UpdateNumber(int aNumber, SEVERITY aSeverity)
Update the number displayed on the badge.
void Flush(bool aSort=false)
Set the visible severity filter.
wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:144
void onCheckBoxShowWarnings(wxCommandEvent &event) override
void onCheckBoxShowErrors(wxCommandEvent &event) override
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
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
bool AppendToPage(const wxString &aSource)
Definition: html_window.cpp:57
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.
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:38
#define _(s)
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
wxString m_reportFileName
defaults to the not very useful /bin/report.txt
SEVERITY
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:79
void onThemeChanged(wxSysColourChangedEvent &aEvent)
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)
int m_severities
message severities to display (mask)
static int RPT_SEVERITY_ALL