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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "KReportKSpreadRenderer.h"
0019 
0020 #include "ods/KoSimpleOdsDocument.h"
0021 #include "ods/KoSimpleOdsCell.h"
0022 #include "ods/KoSimpleOdsSheet.h"
0023 #include "KReportRenderObjects.h"
0024 
0025 #include "kreport_debug.h"
0026 
0027 KoReportKSpreadRenderer::KoReportKSpreadRenderer()
0028 {
0029 }
0030 
0031 
0032 KoReportKSpreadRenderer::~KoReportKSpreadRenderer()
0033 {
0034 }
0035 
0036 bool KoReportKSpreadRenderer::render(const KoReportRendererContext& context, ORODocument* document, int page)
0037 {
0038     Q_UNUSED(page);
0039     KoSimpleOdsDocument *doc = new KoSimpleOdsDocument();
0040     KoSimpleOdsSheet *sht = new KoSimpleOdsSheet();
0041 
0042     //kreportDebug() << "Setting name to:" << document->title();
0043     sht->setName(document->title());
0044 
0045     bool renderedPageHeader = false;
0046     bool renderedPageFooter = false;
0047 
0048     // Render Each Section
0049     for (int s = 0; s < document->sectionCount(); s++) {
0050         OROSection *section = document->section(s);
0051         section->sortPrimitives(OROSection::SortX);
0052 
0053         if (section->type() == KReportSectionData::GroupHeader ||
0054                 section->type() == KReportSectionData::GroupFooter ||
0055                 section->type() == KReportSectionData::Detail ||
0056                 section->type() == KReportSectionData::ReportHeader ||
0057                 section->type() == KReportSectionData::ReportFooter ||
0058                 (section->type() == KReportSectionData::PageHeaderAny && !renderedPageHeader) ||
0059                 (section->type() == KReportSectionData::PageFooterAny && !renderedPageFooter && s > document->sectionCount() - 2)) { //render the page foot right at the end, it will either be the last or second last section if there is a report footer
0060             if (section->type() == KReportSectionData::PageHeaderAny)
0061                 renderedPageHeader = true;
0062 
0063             if (section->type() == KReportSectionData::PageFooterAny)
0064                 renderedPageFooter = true;
0065 
0066             //Render the objects in each section
0067             for (int i = 0; i < section->primitiveCount(); i++) {
0068                 OROPrimitive * prim = section->primitive(i);
0069 
0070                 if (prim->type() == OROTextBox::TextBox) {
0071                     OROTextBox * tb = (OROTextBox*) prim;
0072 
0073                     sht->addCell(s, i, new KoSimpleOdsCell(tb->text()));
0074                 }
0075                 /*
0076                 else if (prim->type() == OROImage::Image)
0077                 {
0078                  kreportDebug() << "Saving an image";
0079                  OROImage * im = ( OROImage* ) prim;
0080                  tr += "<td>"
0081                        "<img src=\"./" + fi.fileName() + "/object" + QString::number(s) + QString::number(i) + ".png\"></img>"
0082                        "</td>\n";
0083                  im->image().save(saveDir + "/object" + QString::number(s) + QString::number(i) + ".png");
0084                 }
0085                 else if (prim->type() == OROPicture::Picture)
0086                 {
0087                  kreportDebug() << "Saving a picture";
0088                  OROPicture * im = ( OROPicture* ) prim;
0089 
0090                  tr += "<td>"
0091                        "<img src=\"./" + fi.fileName() + "/object" + QString::number(s) + QString::number(i) + ".png\"></img>"
0092                        "</td>\n";
0093                  QImage image(im->size().toSize(), QImage::Format_RGB32);
0094                  QPainter painter(&image);
0095                  im->picture()->play(&painter);
0096                  image.save(saveDir + "/object" + QString::number(s) + QString::number(i) + ".png");
0097                 }*/
0098                 else {
0099                     kreportWarning() << "unhandled primitive type";
0100                 }
0101             }
0102         }
0103     }
0104 
0105     doc->addSheet(sht);
0106 
0107     bool status;
0108     if (doc->saveDocument(context.destinationUrl.path()) == QFile::NoError) {
0109         status = true;
0110     }
0111     else {
0112         status = false;
0113     }
0114 
0115     delete doc;
0116     return status;
0117 }