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-2017 KiCad Developers, see change_log.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 "wx_html_report_panel.h"
23 
25 
26 
28  wxWindowID id,
29  const wxPoint& pos,
30  const wxSize& size,
31  long style ) :
32  WX_HTML_REPORT_PANEL_BASE( parent, id, pos, size, style ),
33  m_reporter( this ),
34  m_severities( -1 ),
35  m_showAll( true ),
36  m_lazyUpdate( false )
37 {
39  m_htmlView->SetPage( addHeader( "" ) );
40 }
41 
42 
44 {
45 }
46 
47 
48 void WX_HTML_REPORT_PANEL::MsgPanelSetMinSize( const wxSize& aMinSize )
49 {
50  m_htmlView->SetMinSize( aMinSize );
51  GetSizer()->SetSizeHints( this );
52 }
53 
54 
56 {
57  return m_reporter;
58 }
59 
60 
61 void WX_HTML_REPORT_PANEL::Report( const wxString& aText, REPORTER::SEVERITY aSeverity )
62 {
63  REPORT_LINE line;
64  line.message = aText;
65  line.severity = aSeverity;
66 
67  m_report.push_back( line );
68 
69  m_html += generateHtml( line );
70 
71  if( !m_lazyUpdate )
72  {
73  m_htmlView->AppendToPage( generateHtml( line ) );
75  }
76 }
77 
78 
79 void WX_HTML_REPORT_PANEL::SetLazyUpdate( bool aLazyUpdate )
80 {
81  m_lazyUpdate = aLazyUpdate;
82 }
83 
84 
86 {
87  m_htmlView->SetPage( addHeader( m_html ) );
89 }
90 
91 
93 {
94  int x, y, xUnit, yUnit;
95 
96  m_htmlView->GetVirtualSize( &x, &y );
97  m_htmlView->GetScrollPixelsPerUnit( &xUnit, &yUnit );
98  m_htmlView->Scroll( 0, y / yUnit );
99 }
100 
101 
103 {
104  wxString html;
105 
106  for( REPORT_LINE l : m_report )
107  {
108  html += generateHtml( l );
109  }
110 
111  m_htmlView->SetPage( addHeader( html ) );
112  scrollToBottom();
113 }
114 
115 
116 wxString WX_HTML_REPORT_PANEL::addHeader( const wxString& aBody )
117 {
118  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
119  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
120  wxString s = "<html><body bgcolor=\"" + bgcolor.GetAsString( wxC2S_HTML_SYNTAX ) +
121  "\" text=\"" + fgcolor.GetAsString( wxC2S_HTML_SYNTAX ) + "\">";
122  s += aBody;
123  s += "</body></html>";
124 
125  return s;
126 }
127 
128 
130 {
131  if( !m_showAll && ! ( m_severities & aLine.severity ) )
132  return wxEmptyString;
133 
134  switch( aLine.severity )
135  {
136  case REPORTER::RPT_ERROR:
137  return wxString( "<font color=\"red\" size=2>" ) + _( "<b>Error: </b></font><font size=2>" ) + aLine.message + wxString( "</font><br>" );
139  return wxString( "<font color=\"orange\" size=2>" ) + _( "<b>Warning: </b></font><font size=2>" ) + aLine.message + wxString( "</font><br>" );
140  case REPORTER::RPT_INFO:
141  return wxString( "<font color=\"gray\" size=2>" ) + _( "<b>Info: </b>" ) + aLine.message + wxString( "</font><br>" );
143  return wxString( "<font color=\"darkgreen\" size=2>" ) + aLine.message + wxString( "</font><br>" );
144  default:
145  return wxString( "<font size=2>" ) + aLine.message + wxString( "</font><br>" );
146  }
147 }
148 
149 
151 {
152  switch( aLine.severity )
153  {
154  case REPORTER::RPT_ERROR:
155  return _( "Error: " ) + aLine.message + wxT( "\n" );
157  return _( "Warning: " ) + aLine.message + wxT( "\n" );
158  case REPORTER::RPT_INFO:
159  return _( "Info: " ) + aLine.message + wxT( "\n" );
160  default:
161  return aLine.message + wxT( "\n" );
162  }
163 }
164 
165 
166 void WX_HTML_REPORT_PANEL::onCheckBoxShowAll( wxCommandEvent& event )
167 {
168  if ( event.IsChecked() )
169  m_showAll = true;
170  else
171  m_showAll = false;
172 
173  syncCheckboxes();
174  refreshView();
175 }
176 
177 
179 {
180  m_checkBoxShowAll->SetValue( m_showAll );
181  m_checkBoxShowWarnings->Enable( !m_showAll );
183  m_checkBoxShowErrors->Enable( !m_showAll );
185  m_checkBoxShowInfos->Enable( !m_showAll );
187  m_checkBoxShowActions->Enable( !m_showAll );
189 }
190 
191 
192 void WX_HTML_REPORT_PANEL::onCheckBoxShowWarnings( wxCommandEvent& event )
193 {
194  if ( event.IsChecked() )
196  else
197  m_severities &= ~REPORTER::RPT_WARNING;
198 
199  refreshView();
200 }
201 
202 
203 void WX_HTML_REPORT_PANEL::onCheckBoxShowErrors( wxCommandEvent& event )
204 {
205  if ( event.IsChecked() )
207  else
208  m_severities &= ~REPORTER::RPT_ERROR;
209 
210  refreshView();
211 }
212 
213 
214 void WX_HTML_REPORT_PANEL::onCheckBoxShowInfos( wxCommandEvent& event )
215 {
216  if ( event.IsChecked() )
218  else
219  m_severities &= ~REPORTER::RPT_INFO;
220 
221  refreshView();
222 }
223 
224 
225 void WX_HTML_REPORT_PANEL::onCheckBoxShowActions( wxCommandEvent& event )
226 {
227  if ( event.IsChecked() )
229  else
230  m_severities &= ~REPORTER::RPT_ACTION;
231 
232  refreshView();
233 }
234 
235 
236 void WX_HTML_REPORT_PANEL::onBtnSaveToFile( wxCommandEvent& event )
237 {
238  wxFileName fn( "./report.txt" );
239 
240  wxFileDialog dlg( this, _( "Save Report to File" ), fn.GetPath(), fn.GetFullName(),
241  TextFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
242 
243  if( dlg.ShowModal() != wxID_OK )
244  return;
245 
246  fn = dlg.GetPath();
247 
248  if( fn.GetExt().IsEmpty() )
249  fn.SetExt( wxT( "txt" ) );
250 
251  wxFile f( fn.GetFullPath(), wxFile::write );
252 
253  if( !f.IsOpened() )
254  {
255  wxString msg;
256 
257  msg.Printf( _( "Cannot write report to file '%s'." ),
258  fn.GetFullPath().GetData() );
259  wxMessageBox( msg, _( "File save error" ), wxOK | wxICON_ERROR, this );
260  return;
261  }
262 
263  for( REPORT_LINE l : m_report )
264  {
265  f.Write( generatePlainText( l ) );
266  }
267 
268  f.Close();
269 }
270 
271 
273 {
274  m_html.clear();
275  m_report.clear();
276 }
277 
278 
279 void WX_HTML_REPORT_PANEL::SetLabel( const wxString& aLabel )
280 {
281  m_box->GetStaticBox()->SetLabel( aLabel );
282 }
283 
284 
286 {
287  if( aSeverities < 0 )
288  m_showAll = true;
289  else
290  {
291  m_showAll = false;
292  m_severities = aSeverities;
293  }
294 
295  syncCheckboxes();
296 }
297 
298 
300 {
301  return m_showAll ? m_severities | 0x80000000 : m_severities & ~0x80000000;
302 }
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 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
WX_HTML_REPORT_PANEL(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(500, 300), long style=wxTAB_TRAVERSAL)
SEVERITY
Severity of the reported messages.
Definition: reporter.h:65
void Report(const wxString &aText, REPORTER::SEVERITY aSeverity)
reports a string directly.
REPORTER & Reporter()
returns the reporter object that reports to this panel
WX_HTML_PANEL_REPORTER m_reporter
the reporter
bool m_showAll
show all messages flag (overrides m_severities)
The common library.
REPORT_LINES m_report
copy of the report, stored for filtering
void Flush()
Forces updating the HTML page, after the report is built in lazy mode
void Clear()
clears the report panel
void onCheckBoxShowAll(wxCommandEvent &event) override
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
int m_severities
message severities to display (mask)