File indexing completed on 2024-05-05 04:43:22

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
0003  * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #include "KReportHTMLTableRenderer_p.h"
0020 #include "KReportRenderObjects.h"
0021 
0022 #include <QTemporaryFile>
0023 
0024 #include "kreport_debug.h"
0025 #include <QDir>
0026 #include <QPainter>
0027 #include <QString>
0028 
0029 namespace KReportPrivate {
0030 
0031 HTMLTableRenderer::HTMLTableRenderer()
0032 {
0033 }
0034 
0035 HTMLTableRenderer::~HTMLTableRenderer()
0036 {
0037 
0038 }
0039 
0040 bool HTMLTableRenderer::render(const KReportRendererContext& context, ORODocument *document, int page)
0041 {
0042     Q_UNUSED(page);
0043     QTemporaryFile tempHtmlFile; // auto removed by default on destruction
0044     if (!tempHtmlFile.open()) {
0045         kreportWarning() << "Couldn't create temporary file to write into";
0046         return false;
0047     }
0048 
0049     QTextStream out(&tempHtmlFile);
0050 
0051     QString dirSuffix = QLatin1String(".files");
0052     QDir tempDir;
0053     QFileInfo fi(tempHtmlFile);
0054 
0055     QString tempFileName = fi.absoluteFilePath();
0056     m_tempDirName = tempFileName + dirSuffix;
0057     m_actualDirName = context.url().fileName() + dirSuffix;
0058 
0059     if (!tempDir.mkpath(m_tempDirName))
0060         return false;
0061 
0062     out << renderTable(document);
0063 
0064     out.flush();
0065     tempHtmlFile.close();
0066 
0067     bool status = true; //! @todo port KIO;
0068 //    if (KIO::NetAccess::upload(tempFileName, context.destinationUrl, 0) && KIO::NetAccess::dircopy(QUrl(m_tempDirName),  QUrl(context.destinationUrl.url() + dirSuffix), 0)) {
0069 //        status = true;
0070 //    }
0071 
0072     // cleanup the temporary directory
0073     tempDir.setPath(m_tempDirName);
0074     QStringList fileList = tempDir.entryList();
0075     foreach(const QString& fileName, fileList) {
0076         tempDir.remove(fileName);
0077     }
0078     tempDir.rmdir(m_tempDirName);
0079 
0080     return status;
0081 }
0082 
0083 QString HTMLTableRenderer::renderTable(ORODocument *document)
0084 {
0085     QString html;
0086     QString body;
0087     QString tr;
0088 
0089     bool renderedPageHeader = false;
0090     bool renderedPageFooter = false;
0091 
0092     QDir d(m_tempDirName);
0093 
0094     // Render Each Section
0095     body = QLatin1String("<table>\n");
0096     for (int s = 0; s < document->sectionCount(); s++) {
0097         OROSection *section = document->section(s);
0098         section->sortPrimitives(Qt::Horizontal);
0099 
0100         if (section->type() == KReportSectionData::Type::GroupHeader
0101             || section->type() == KReportSectionData::Type::GroupFooter
0102             || section->type() == KReportSectionData::Type::Detail
0103             || section->type() == KReportSectionData::Type::ReportHeader
0104             || section->type() == KReportSectionData::Type::ReportFooter
0105             || (section->type() == KReportSectionData::Type::PageHeaderAny && !renderedPageHeader)
0106             || (section->type() == KReportSectionData::Type::PageFooterAny && !renderedPageFooter
0107                 && s > document->sectionCount() - 2))
0108         {   // render the page foot right at the end, it
0109             // will either be the last or second last
0110             // section if there is a report footer
0111             if (section->type() == KReportSectionData::Type::PageHeaderAny)
0112                 renderedPageHeader = true;
0113 
0114             if (section->type() == KReportSectionData::Type::PageFooterAny)
0115                 renderedPageFooter = true;
0116 
0117             tr = QLatin1String("<tr style=\"background-color: ") + section->backgroundColor().name() + QLatin1String("\">\n");
0118             //Render the objects in each section
0119             for (int i = 0; i < section->primitiveCount(); i++) {
0120                 OROPrimitive * prim = section->primitive(i);
0121 
0122                 if (OROTextBox *tb = dynamic_cast<OROTextBox*>(prim)) {
0123                     tr += QLatin1String("<td>") +
0124                           tb->text() +
0125                           QLatin1String("</td>\n");
0126                 } else if (OROImage *im  = dynamic_cast<OROImage*>(prim)) {
0127                     tr += QLatin1String("<td>"
0128                           "<img src=\"./") + m_actualDirName + QLatin1String("/object") + QString::number(s) + QString::number(i) + QLatin1String(".png\"></img>"
0129                           "</td>\n");
0130                     im->image().save(m_tempDirName + QLatin1String("/object") + QString::number(s) + QString::number(i) + QLatin1String(".png"));
0131                 } else if (OROPicture *im = dynamic_cast<OROPicture*>(prim)) {
0132                     tr += QLatin1String("<td>"
0133                           "<img src=\"./") + m_actualDirName + QLatin1String("/object") + QString::number(s) + QString::number(i) + QLatin1String(".png\"></img>"
0134                           "</td>\n");
0135                     QImage image(im->size().toSize(), QImage::Format_RGB32);
0136                     QPainter painter(&image);
0137                     im->picture()->play(&painter);
0138                     image.save(m_tempDirName + QLatin1String("/object") + QString::number(s) + QString::number(i) + QLatin1String(".png"));
0139                 } else {
0140                     kreportWarning() << "unhandled primitive type";
0141                 }
0142             }
0143             tr += QLatin1String("</tr>\n");
0144 
0145             if (tr.contains(QLatin1String("<td>"))) {
0146                 body += tr;
0147             }
0148         }
0149     }
0150     body += QLatin1String("</table>\n");
0151     html = QLatin1String("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
0152         "<html>\n"
0153         "<head>\n"
0154         "<title>")
0155       + document->title()
0156       + QLatin1String("</title>\n"
0157         "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n"
0158         "<meta name=\"generator\" content=\"Kexi\">\n"
0159         "</head>\n"
0160         "<body>\n")
0161       + body
0162       + QLatin1String("\n</body>\n"
0163         "</html>\n");
0164 
0165     return html;
0166 }
0167 
0168 }
0169