v2.0.0
Loading...
Searching...
No Matches
report.cpp
Go to the documentation of this file.
1//=============================================================================================================
19
20//=============================================================================================================
21// INCLUDES
22//=============================================================================================================
23
24#include "report.h"
25
26//=============================================================================================================
27// QT INCLUDES
28//=============================================================================================================
29
30#include <QFile>
31#include <QTextStream>
32#include <QDebug>
33#include <QDateTime>
34
35//=============================================================================================================
36// USED NAMESPACES
37//=============================================================================================================
38
39using namespace UTILSLIB;
40
41//=============================================================================================================
42// DEFINE MEMBER METHODS
43//=============================================================================================================
44
45Report::Report(const QString& sTitle)
46 : m_sTitle(sTitle)
47{
48}
49
50//=============================================================================================================
51
52void Report::addText(const QString& sTitle, const QString& sContent)
53{
54 ReportSection section;
55 section.title = sTitle;
56 section.htmlContent = "<p>" + sContent.toHtmlEscaped() + "</p>";
57 m_sections.append(section);
58}
59
60//=============================================================================================================
61
62void Report::addTable(const QString& sTitle,
63 const QStringList& headers,
64 const QList<QStringList>& rows)
65{
66 QString html = "<table>\n<thead><tr>\n";
67 for (const auto& h : headers) {
68 html += " <th>" + h.toHtmlEscaped() + "</th>\n";
69 }
70 html += "</tr></thead>\n<tbody>\n";
71
72 for (const auto& row : rows) {
73 html += "<tr>\n";
74 for (const auto& cell : row) {
75 html += " <td>" + cell.toHtmlEscaped() + "</td>\n";
76 }
77 html += "</tr>\n";
78 }
79 html += "</tbody>\n</table>";
80
81 ReportSection section;
82 section.title = sTitle;
83 section.htmlContent = html;
84 m_sections.append(section);
85}
86
87//=============================================================================================================
88
89void Report::addKeyValue(const QString& sTitle,
90 const QList<QPair<QString, QString>>& pairs)
91{
92 QString html = "<dl>\n";
93 for (const auto& pair : pairs) {
94 html += " <dt>" + pair.first.toHtmlEscaped() + "</dt>\n";
95 html += " <dd>" + pair.second.toHtmlEscaped() + "</dd>\n";
96 }
97 html += "</dl>";
98
99 ReportSection section;
100 section.title = sTitle;
101 section.htmlContent = html;
102 m_sections.append(section);
103}
104
105//=============================================================================================================
106
107void Report::addCode(const QString& sTitle, const QString& sCode)
108{
109 ReportSection section;
110 section.title = sTitle;
111 section.htmlContent = "<pre><code>" + sCode.toHtmlEscaped() + "</code></pre>";
112 m_sections.append(section);
113}
114
115//=============================================================================================================
116
117QString Report::toHtml() const
118{
119 QString html;
120 html += "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n";
121 html += "<meta charset=\"UTF-8\">\n";
122 html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
123 html += "<title>" + m_sTitle.toHtmlEscaped() + "</title>\n";
124 html += "<style>\n";
125 html += "body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; ";
126 html += "max-width: 960px; margin: 0 auto; padding: 20px; color: #333; }\n";
127 html += "h1 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; }\n";
128 html += "h2 { color: #2980b9; margin-top: 30px; }\n";
129 html += "table { border-collapse: collapse; width: 100%; margin: 10px 0; }\n";
130 html += "th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }\n";
131 html += "th { background-color: #3498db; color: white; }\n";
132 html += "tr:nth-child(even) { background-color: #f2f2f2; }\n";
133 html += "dl { margin: 10px 0; }\n";
134 html += "dt { font-weight: bold; margin-top: 8px; }\n";
135 html += "dd { margin-left: 20px; color: #555; }\n";
136 html += "pre { background-color: #f8f8f8; border: 1px solid #ddd; padding: 12px; ";
137 html += "border-radius: 4px; overflow-x: auto; }\n";
138 html += "code { font-family: 'SF Mono', Menlo, Monaco, monospace; font-size: 0.9em; }\n";
139 html += ".footer { margin-top: 40px; padding-top: 10px; border-top: 1px solid #ddd; ";
140 html += "color: #999; font-size: 0.85em; }\n";
141 html += "</style>\n</head>\n<body>\n";
142
143 html += "<h1>" + m_sTitle.toHtmlEscaped() + "</h1>\n";
144
145 for (const auto& section : m_sections) {
146 html += "<h2>" + section.title.toHtmlEscaped() + "</h2>\n";
147 html += section.htmlContent + "\n";
148 }
149
150 html += "<div class=\"footer\">\n";
151 html += "<p>Generated by MNE-CPP on " + QDateTime::currentDateTime().toString(Qt::ISODate) + "</p>\n";
152 html += "</div>\n";
153 html += "</body>\n</html>\n";
154
155 return html;
156}
157
158//=============================================================================================================
159
160bool Report::save(const QString& sPath) const
161{
162 QFile file(sPath);
163 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
164 qWarning() << "[Report::save] Cannot open:" << sPath;
165 return false;
166 }
167
168 QTextStream out(&file);
169 out << toHtml();
170 file.close();
171 return true;
172}
HTML report builder — mne-cpp's equivalent of mne.Report for summarising analysis pipelines.
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
A section in the report (title + HTML content).
Definition report.h:56
Report(const QString &sTitle="MNE-CPP Report")
Construct a report with a title.
Definition report.cpp:45
bool save(const QString &sPath) const
Save the report to an HTML file.
Definition report.cpp:160
void addCode(const QString &sTitle, const QString &sCode)
Add a preformatted code block.
Definition report.cpp:107
QString toHtml() const
Generate the full HTML string.
Definition report.cpp:117
void addKeyValue(const QString &sTitle, const QList< QPair< QString, QString > > &pairs)
Add a key-value section.
Definition report.cpp:89
void addText(const QString &sTitle, const QString &sContent)
Add a text section.
Definition report.cpp:52
void addTable(const QString &sTitle, const QStringList &headers, const QList< QStringList > &rows)
Add a table section.
Definition report.cpp:62