-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchtmlwriter.cpp
467 lines (418 loc) · 15.6 KB
/
chtmlwriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*------------------------------ Information ---------------------------*//**
*
* $HeadURL$
*
* @file chtmlwriter.cpp
*
* @author Jo2003
*
* @date 09.08.2013
*
* $Id$
*
*///------------------------- (c) 2013 by Jo2003 --------------------------
#include "chtmlwriter.h"
#include "templates.h"
#include "defdef.h"
#include <QFileInfo>
#include <QStringList>
#include <QDateTime>
//---------------------------------------------------------------------------
//
//! \brief constructs CHtmlWriter object
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param parent (QObject *) pointer to parent object
//
//! \return --
//---------------------------------------------------------------------------
CHtmlWriter::CHtmlWriter(QObject *parent) :
QObject(parent)
{
}
//---------------------------------------------------------------------------
//
//! \brief destroys CHtmlWriter object
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param --
//
//! \return --
//---------------------------------------------------------------------------
CHtmlWriter::~CHtmlWriter()
{
}
//---------------------------------------------------------------------------
//
//! \brief create whole html page
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) page content
//! \param title (const QString&) optional page title
//! \param css (const QString&) optional page style sheet
//
//! \return html page as string
//---------------------------------------------------------------------------
QString CHtmlWriter::htmlPage(const QString &content, const QString &title, const QString &css)
{
QString s =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">"
"<html>"
"<head>"
"<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />"
"<meta name=\"qrichtext\" content=\"1\" />"
"<title>" TMPL_TITLE "</title>"
"<style type=\"text/css\">"
TMPL_CSS
"</style>"
"</head>"
"<body>"
TMPL_CONT
"</body>"
"</html>\n";
s.replace(TMPL_TITLE, title.isEmpty() ? "Qt Site" : title);
s.replace(TMPL_CSS , css.isEmpty() ? "// no style ..." : css);
s.replace(TMPL_CONT , content);
return s;
}
//---------------------------------------------------------------------------
//
//! \brief create a html tag (open + close tag)
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param tag (const QString&) tag name e.g. span, div, etc.
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//! \param attr (const QString&) additional html attributes
//
//! \return html code of this tag
//---------------------------------------------------------------------------
QString CHtmlWriter::htmlTag(const QString &tag, const QString &content, const QString &style, const QString &attr)
{
QString s = "<" TMPL_NAME TMPL_CSS TMPL_ATTR">" TMPL_CONT "</" TMPL_NAME ">";
s.replace(TMPL_NAME, tag);
s.replace(TMPL_CSS , style.isEmpty() ? "" : QString(" style='" TMPL_CSS "'").replace(TMPL_CSS, style));
s.replace(TMPL_ATTR, attr.isEmpty() ? "" : (" " + attr));
s.replace(TMPL_CONT, content);
return s;
}
//---------------------------------------------------------------------------
//
//! \brief create a simple html tag (without close tag)
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param tag (const QString&) tag name e.g. img, br, etc.
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//
//! \return html code of this tag
//---------------------------------------------------------------------------
QString CHtmlWriter::simpleTag(const QString& tag, const QString &content, const QString& style)
{
QString s = "<" TMPL_NAME " " TMPL_CSS " " TMPL_CONT " />";
s.replace(TMPL_NAME, tag);
s.replace(TMPL_CSS , style.isEmpty() ? "" : QString("style='" TMPL_CSS "'").replace(TMPL_CSS, style));
s.replace(TMPL_CONT, content);
return s;
}
//---------------------------------------------------------------------------
//
//! \brief create a table cell
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//! \param colspan (int) td colspan attribute
//! \param align (const QString&) text align attribute
//! \param valign (const QString&) vertical align attribute
//
//! \return html code of table cell
//---------------------------------------------------------------------------
QString CHtmlWriter::tableCell (const QString &content, const QString& style, int colspan, const QString &align, const QString &valign)
{
QString s = "<td" TMPL_ATTR TMPL_ALIGN TMPL_VALIGN TMPL_CSS">" TMPL_CONT "</td>";
s.replace(TMPL_ALIGN , align.isEmpty() ? "" : QString(" align='" TMPL_ALIGN "'").replace(TMPL_ALIGN, align));
s.replace(TMPL_VALIGN, valign.isEmpty() ? "" : QString(" valign='" TMPL_VALIGN "'").replace(TMPL_VALIGN, valign));
s.replace(TMPL_ATTR , (colspan > 1) ? QString(" colspan='" TMPL_ATTR "'").replace(TMPL_ATTR, QString::number(colspan)) : "");
s.replace(TMPL_CSS , style.isEmpty() ? "" : QString(" style='" TMPL_CSS "'").replace(TMPL_CSS, style));
s.replace(TMPL_CONT , content);
return s;
}
//---------------------------------------------------------------------------
//
//! \brief create a table headline cell
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//! \param colspan (int) td colspan attribute
//! \param align (const QString&) text align attribute
//! \param valign (const QString&) vertical align attribute
//
//! \return html code of table cell
//---------------------------------------------------------------------------
QString CHtmlWriter::tableHead(const QString &content, const QString &style, int colspan, const QString &align, const QString &valign)
{
QString s = "<th" TMPL_ATTR TMPL_ALIGN TMPL_VALIGN TMPL_CSS">" TMPL_CONT "</th>";
s.replace(TMPL_ALIGN , align.isEmpty() ? "" : QString(" align='" TMPL_ALIGN "'").replace(TMPL_ALIGN, align));
s.replace(TMPL_VALIGN, valign.isEmpty() ? "" : QString(" valign='" TMPL_VALIGN "'").replace(TMPL_VALIGN, valign));
s.replace(TMPL_ATTR , (colspan > 1) ? QString(" colspan='" TMPL_ATTR "'").replace(TMPL_ATTR, QString::number(colspan)) : "");
s.replace(TMPL_CSS , style.isEmpty() ? "" : QString(" style='" TMPL_CSS "'").replace(TMPL_CSS, style));
s.replace(TMPL_CONT , content);
return s;
}
//---------------------------------------------------------------------------
//
//! \brief create a page divider
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//! \param align (const QString&) text align attribute
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::div(const QString &content, const QString &style, const QString &align)
{
QString s = "<div" TMPL_ALIGN TMPL_CSS">" TMPL_CONT "</div>";
s.replace(TMPL_ALIGN , align.isEmpty() ? "" : QString(" align='" TMPL_ALIGN "'").replace(TMPL_ALIGN, align));
s.replace(TMPL_CSS , style.isEmpty() ? "" : QString(" style='" TMPL_CSS "'").replace(TMPL_CSS, style));
s.replace(TMPL_CONT , content);
return s;
}
//---------------------------------------------------------------------------
//
//! \brief create a text divider
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::span (const QString &content, const QString& style)
{
return htmlTag("span", content, style);
}
//---------------------------------------------------------------------------
//
//! \brief create a paragraph
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::paragraph(const QString &content, const QString& style)
{
return htmlTag("p", content, style);
}
//---------------------------------------------------------------------------
//
//! \brief create a visible table
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::table(const QString &content, const QString &style)
{
QString s = "<table border='0' cellpadding='0' cellspacing='" DEF_CELLSPACING "' width='100%' " TMPL_CSS ">" TMPL_CONT "</table>";
s.replace(TMPL_CSS , style.isEmpty() ? "" : QString(" style='" TMPL_CSS "'").replace(TMPL_CSS, style));
s.replace(TMPL_CONT, content);
return s;
}
//---------------------------------------------------------------------------
//
//! \brief create a table row
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::tableRow(const QString &content, const QString &style)
{
return htmlTag("tr", content, style);
}
//---------------------------------------------------------------------------
//
//! \brief create a link
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param href (const QString&) link referer
//! \param content (const QString&) content within this tag
//! \param title (const QString&) title for this tag
//! \param style (const QString&) css for this tag
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::link(const QString &href, const QString &content, const QString& title, const QString& style)
{
QString s = "<a href='" TMPL_LINK "' title='" TMPL_TITLE "'" TMPL_CSS ">" TMPL_CONT "</a>";
s.replace(TMPL_LINK , href);
s.replace(TMPL_TITLE, title);
s.replace(TMPL_CONT , content);
s.replace(TMPL_CSS , style.isEmpty() ? "" : QString(" style='" TMPL_CSS "'").replace(TMPL_CSS, style));
return s;
}
//---------------------------------------------------------------------------
//
//! \brief create an image
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param src (const QString&) source url
//! \param width (int) optional width in px
//! \param height (int) optional height in px
//! \param style (const QString&) css for this tag
//! \param title (const QString&) title for this tag
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::image(const QString &src, int width, int height, const QString &style, const QString &title)
{
QFileInfo fi(src);
QString s = "src='" TMPL_LINK "' alt='" TMPL_NAME "' title='" TMPL_TITLE "'";
if (width)
{
s += " width='" TMPL_WIDTH "'";
s.replace(TMPL_WIDTH , QString::number(width));
}
if (height)
{
s += " height='" TMPL_HEIGHT "'";
s.replace(TMPL_HEIGHT, QString::number(height));
}
s.replace(TMPL_LINK , src);
s.replace(TMPL_TITLE , title);
s.replace(TMPL_NAME , fi.baseName());
return simpleTag("img", s, style);
}
//---------------------------------------------------------------------------
//
//! \brief create a table with one row and cell
//
//! \author Jo2003
//! \date 12.08.2013
//
//! \param content (const QString&) content within this tag
//! \param style (const QString&) css for this tag
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::oneCellPage (const QString& content, const QString& style)
{
QString s = tableCell(content, style);
s = tableRow(s);
s = table(s, TMPL_TAB_STYLE);
return htmlPage(s);
}
//---------------------------------------------------------------------------
//
//! \brief create short program info (tooltip)
//
//! \author Jo2003
//! \date 13.08.2013
//
//! \param name (const QString&) show name
//! \param prog (const QString&) program description
//! \param start (uint) unix timestamp of start time
//! \param end (uint) unix timestamp of end time
//! \param is (int) optional timeshift value in seconds
//! \param style (const QString&) css for this tag
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::createTooltip (const QString &name, const QString &prog, uint start, uint end, int ts, const QString &style)
{
// create tool tip with programm info ...
QString sStart = QDateTime::fromTime_t(start + ts).toString("dd. MMM yyyy, h:mm");
QString sEnd = end ? (" - " + QDateTime::fromTime_t(end + ts).toString("h:mm")) : "";
QString sLength = end ? (" (" + tr("%1 min.").arg((end - start) / 60) + ")") : "";
QStringList sl = prog.split("\n");
QString s = tableCell(htmlTag("b", name, "color: red;"), "", 1, "left" );
s += tableCell(span(sStart + sEnd + sLength, "color: #080;") , "", 1, "right");
s = tableRow(s);
s = table(s);
s += htmlTag("b", sl.at(0)) + "<br />";
if (sl.count() > 1) s += span(sl.at(1), "color: #666;") + "<br />";
return oneCellPage(s, style.isEmpty() ? TMPL_ONE_CELL : style);
}
//---------------------------------------------------------------------------
//
//! \brief create a table bitrate and timeshift info
//
//! \author Jo2003
//! \date 14.09.2014
//
//! \param v (const QVector<cparser::STimeShift> &) data vector
//
//! \return html code of tag
//---------------------------------------------------------------------------
QString CHtmlWriter::createBitrateTsTable(const QVector<cparser::STimeShift> &v)
{
QString tab;
QString row;
QMap<int, QVector<int> > dataMap;
QMap<int, QVector<int> >::iterator it;
int i;
for (i = 0; i < v.count(); i++)
{
dataMap[v.at(i).iTimeShift].append(v.at(i).iBitRate);
}
if ((it = dataMap.find(100)) != dataMap.end())
{
// delete 100 timeshift ...
dataMap.erase(it);
}
row = tableHead(tr("Timeshift"), "", 1, "center");
row += tableHead(tr("Mobile"), "", 1, "center");
row += tableHead(tr("Eco"), "", 1, "center");
row += tableHead(tr("Standard"), "", 1, "center");
row += tableHead(tr("Premium"), "", 1, "center");
tab = tableRow(row);
QList<int> keys = dataMap.keys();
for (i = 0; i < keys.size(); i++)
{
row = tableCell(QString::number(keys.at(i)), "", 1, "center");
row += tableCell((dataMap[keys.at(i)].contains(320) ? tr("+") : tr("-")), "", 1, "center");
row += tableCell((dataMap[keys.at(i)].contains(900) ? tr("+") : tr("-")), "", 1, "center");
row += tableCell((dataMap[keys.at(i)].contains(1500) ? tr("+") : tr("-")), "", 1, "center");
row += tableCell((dataMap[keys.at(i)].contains(2500) ? tr("+") : tr("-")), "", 1, "center");
tab += tableRow(row);
}
tab = QString("<br> <br>") + table(tab);
return tab;
}