PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusHTMLGenerator.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2  Program: Plus
3  Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4  See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #include "PlusConfigure.h"
8 #include "vtkPlusHTMLGenerator.h"
9 #include "vtkObjectFactory.h"
10 #include "vtksys/SystemTools.hxx"
11 #include "vtkTable.h"
12 #include "vtkVariant.h"
13 
14 //----------------------------------------------------------------------------
16 
17 //----------------------------------------------------------------------------
19 {
20  this->Title = NULL;
21  this->OutputDirectory = NULL;
22  this->BaseFilename = NULL;
23  this->SetBaseFilename("PlusReport");
24  this->SetTitle("");
25  this->HtmlBody.clear();
27 }
28 
29 //----------------------------------------------------------------------------
31 {
32 
33 }
34 
35 //----------------------------------------------------------------------------
36 void vtkPlusHTMLGenerator::PrintSelf(ostream& os, vtkIndent indent)
37 {
38  this->Superclass::PrintSelf(os, indent);
39 }
40 
41 //----------------------------------------------------------------------------
43 {
44  LOG_TRACE("vtkPlusHTMLGenerator::AddHorizontalLine");
45  this->HtmlBody << "<hr />" << std::endl;
46 }
47 
48 //----------------------------------------------------------------------------
49 void vtkPlusHTMLGenerator::AddImage(const char* imageSource, const char* alt, const int widthPx/*=0*/, const int heightPx/*=0*/)
50 {
51  LOG_TRACE("vtkPlusHTMLGenerator::AddImage");
52  if (imageSource == NULL)
53  {
54  LOG_ERROR("Unable to add imageSource to HTML document - imageSource is NULL!");
55  return;
56  }
57 
58  if (alt == NULL)
59  {
60  LOG_ERROR("Unable to add alterantive text to HTML document - alt is NULL!");
61  return;
62  }
63 
64  if (widthPx == 0 && heightPx == 0)
65  {
66  this->HtmlBody << "<img src='" << imageSource << "' alt='" << alt << "' />" << std::endl;
67  }
68  else
69  {
70  this->HtmlBody << "<img src='" << imageSource << "' alt='" << alt << "' width='" << widthPx << "' height='" << heightPx << "' />" << std::endl;
71  }
72 }
73 
74 //----------------------------------------------------------------------------
75 void vtkPlusHTMLGenerator::AddParagraph(const char* paragraph)
76 {
77  LOG_TRACE("vtkPlusHTMLGenerator::AddParagraph");
78  if (paragraph == NULL)
79  {
80  LOG_ERROR("Unable to add paragraph to HTML document - paragraph is NULL!");
81  return;
82  }
83 
84  this->HtmlBody << "<p>" << paragraph << "</p>" << std::endl;
85 }
86 
87 //----------------------------------------------------------------------------
88 void vtkPlusHTMLGenerator::AddLink(const char* linkText, const char* url)
89 {
90  LOG_TRACE("vtkPlusHTMLGenerator::AddLink");
91  if (linkText == NULL)
92  {
93  LOG_ERROR("Unable to add link to HTML document - linkText is NULL!");
94  return;
95  }
96 
97  if (url == NULL)
98  {
99  LOG_ERROR("Unable to add link to HTML document - url is NULL!");
100  return;
101  }
102 
103  this->HtmlBody << "<a href='" << url << "'>" << linkText << "</a>" << std::endl;
104 }
105 
106 //----------------------------------------------------------------------------
107 void vtkPlusHTMLGenerator::AddText(const char* text, HEADINGS h, const char* style/*=NULL*/)
108 {
109  LOG_TRACE("vtkPlusHTMLGenerator::AddText");
110  if (text == NULL)
111  {
112  LOG_ERROR("Unable to add text to HTML document - input text is NULL!");
113  return;
114  }
115 
116  std::string openTag, closeTag;
117  switch (h)
118  {
119  case H1:
120  {
121  if (style == NULL)
122  {
123  openTag = "<h1>";
124  }
125  else
126  {
127  openTag = "<h1 style='" + std::string(style) + "'>";
128  }
129  closeTag = "</h1>";
130  }
131  break;
132 
133  case H2:
134  {
135  if (style == NULL)
136  {
137  openTag = "<h2>";
138  }
139  else
140  {
141  openTag = "<h2 style='" + std::string(style) + "'>";
142  }
143  closeTag = "</h2>";
144  }
145  break;
146 
147  case H3:
148  {
149  if (style == NULL)
150  {
151  openTag = "<h3>";
152  }
153  else
154  {
155  openTag = "<h3 style='" + std::string(style) + "'>";
156  }
157 
158  closeTag = "</h3>";
159  }
160  break;
161 
162  case H4:
163  {
164  if (style == NULL)
165  {
166  openTag = "<h4>";
167  }
168  else
169  {
170  openTag = "<h4 style='" + std::string(style) + "'>";
171  }
172 
173  closeTag = "</h4>";
174  }
175  break;
176 
177  case H5:
178  {
179  if (style == NULL)
180  {
181  openTag = "<h5>";
182  }
183  else
184  {
185  openTag = "<h5 style='" + std::string(style) + "'>";
186  }
187 
188  closeTag = "</h5>";
189  }
190  break;
191 
192  case H6:
193  {
194  if (style == NULL)
195  {
196  openTag = "<h6>";
197  }
198  else
199  {
200  openTag = "<h6 style='" + std::string(style) + "'>";
201  }
202 
203  closeTag = "</h6>";
204  }
205  break;
206  }
207 
208  this->HtmlBody << openTag << text << closeTag << std::endl;
209 }
210 
211 //----------------------------------------------------------------------------
212 void vtkPlusHTMLGenerator::AddTable(vtkTable* inputTable, int borderPx)
213 {
214  LOG_TRACE("vtkPlusHTMLGenerator::AddTable");
215  if (inputTable == NULL)
216  {
217  LOG_ERROR("Unable to add table to HTML document - table is NULL");
218  return;
219  }
220 
221  std::ostringstream openTag;
222  openTag << "<table border='" << borderPx << "'>";
223  std::string closeTag = "</table>";
224 
225  std::ostringstream table;
226 
227  // Create table header
228  table << "<tr>";
229  for (int c = 0; c < inputTable->GetNumberOfColumns(); ++c)
230  {
231  table << "<th>" << inputTable->GetColumnName(c) << "</th>";
232  }
233  table << "</tr>";
234 
235  for (int r = 0; r < inputTable->GetNumberOfRows(); ++r)
236  {
237  table << "<tr>";
238  for (int c = 0; c < inputTable->GetNumberOfColumns(); ++c)
239  {
240  table << "<td>" << inputTable->GetValue(r, c).ToString() << "</td>";
241  }
242  table << "</tr>";
243  }
244 
245  this->HtmlBody << openTag.str() << table.str() << closeTag << std::endl;
246 }
247 
248 //----------------------------------------------------------------------------
250 {
251  LOG_TRACE("vtkPlusHTMLGenerator::GetHtmlBody");
252  return this->HtmlBody.str();
253 }
254 
255 //----------------------------------------------------------------------------
257 {
258  LOG_TRACE("vtkPlusHTMLGenerator::GetHtmlPage");
259  std::ostringstream page;
260  page << "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>" << std::endl;
261  page << "<html>" << std::endl;
262  page << "<head>" << std::endl;
263  page << "<title>" << this->GetTitle() << "</title>" << std::endl;
264  page << "</head>" << std::endl;
265  page << "<body>" << std::endl;
266  page << this->GetHtmlBody();
267  page << "</body>" << std::endl;
268  page << "</html>";
269  page << std::ends;
270 
271  return page.str();
272 }
273 
274 //----------------------------------------------------------------------------
275 void vtkPlusHTMLGenerator::SaveHtmlPage(const char* fileName)
276 {
277  LOG_TRACE("vtkPlusHTMLGenerator::SaveHtmlPage");
278  std::ofstream htmlpage;
279  htmlpage.open(fileName, ios::out);
280  htmlpage << this->GetHtmlPage();
281  htmlpage.close();
282 }
283 
284 //----------------------------------------------------------------------------
286 {
287  std::string fullPath;
288  if (GetOutputDirectory())
289  {
290  fullPath += GetOutputDirectory();
291  fullPath += "/";
292  }
293  fullPath += std::string(this->GetBaseFilename()) + "-" + vtkPlusConfig::GetInstance()->GetApplicationStartTimestamp() + ".html";
294  LOG_INFO("Write HTML report to " << fullPath);
295  SaveHtmlPage(fullPath.c_str());
296  return fullPath;
297 }
298 
299 //----------------------------------------------------------------------------
300 std::string vtkPlusHTMLGenerator::AddImageAutoFilename(const char* filenamePostfix, const char* description, const int widthPx/*=0*/, const int heightPx/*=0*/)
301 {
302  std::string fullPath;
303  if (GetOutputDirectory())
304  {
305  fullPath += GetOutputDirectory();
306  fullPath += "/";
307  }
308  fullPath += std::string(this->GetBaseFilename()) + "-" + vtkPlusConfig::GetInstance()->GetApplicationStartTimestamp() + "-" + filenamePostfix;
309  AddImage(fullPath.c_str(), description, widthPx, heightPx);
310  return fullPath;
311 }
virtual char * GetOutputDirectory()
virtual void AddText(const char *text, HEADINGS h, const char *style=NULL)
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
virtual void SaveHtmlPage(const char *fileName)
virtual void AddImage(const char *imageSource, const char *alt, const int widthPx=0, const int heightPx=0)
static vtkPlusConfig * GetInstance()
virtual void AddLink(const char *linkText, const char *url)
PhidgetLCD_Font int int const char * text
Definition: phidget22.h:4287
virtual void SetTitle(const char *)
virtual void SetBaseFilename(const char *)
virtual std::string SaveHtmlPageAutoFilename()
virtual void AddHorizontalLine()
std::ostringstream HtmlBody
virtual void AddParagraph(const char *paragraph)
virtual void AddTable(vtkTable *table, int borderPx)
virtual std::string GetHtmlBody()
virtual std::string AddImageAutoFilename(const char *filenamePostfix, const char *description, const int widthPx=0, const int heightPx=0)
virtual char * GetBaseFilename()
virtual char * GetTitle()
class for generating basic html tags
vtkStandardNewMacro(vtkPlusHTMLGenerator)
virtual void SetOutputDirectory(const char *)
virtual std::string GetHtmlPage()