File indexing completed on 2024-05-05 04:43:22
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2010 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 "KReportOdtRenderer_p.h" 0019 #include "KReportRenderObjects.h" 0020 0021 #include <QTextDocument> 0022 #include <QTextTable> 0023 #include <QTextTableFormat> 0024 #include <QPainter> 0025 #include <QTextDocumentWriter> 0026 #include "kreport_debug.h" 0027 0028 namespace KReportPrivate { 0029 0030 KoReportODTRenderer::KoReportODTRenderer() : m_document(new QTextDocument()), m_cursor(m_document) 0031 { 0032 0033 } 0034 0035 KoReportODTRenderer::~KoReportODTRenderer() 0036 { 0037 delete m_document; 0038 } 0039 0040 bool KoReportODTRenderer::render(const KoReportRendererContext& context, ORODocument* document, int /*page*/) 0041 { 0042 QTextTableFormat tableFormat; 0043 tableFormat.setCellPadding(5); 0044 tableFormat.setHeaderRowCount(1); 0045 tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); 0046 tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100)); 0047 QTextTable *table = m_cursor.insertTable(1, 1, tableFormat); 0048 0049 long renderedSections = 0; 0050 0051 for (long s = 0; s < document->sections(); s++) { 0052 OROSection *section = document->section(s); 0053 section->sortPrimatives(OROSection::SortX); 0054 0055 if (section->type() == KReportSectionData::GroupHeader || section->type() == KReportSectionData::GroupFooter || 0056 section->type() == KReportSectionData::ReportHeader || section->type() == KReportSectionData::ReportFooter || 0057 section->type() == KReportSectionData::Detail){ 0058 //Add this section to the document 0059 0060 //Resize the table to accommodate all the primitives in the section 0061 if (table->columns() < section->primitives()) { 0062 table->appendColumns(section->primitives() - table->columns()); 0063 } 0064 0065 if (renderedSections > 0) { 0066 //We need to back a row, then forward a row to get at the start cell 0067 m_cursor.movePosition(QTextCursor::PreviousRow); 0068 m_cursor.movePosition(QTextCursor::NextRow); 0069 } else { 0070 //On the first row, ensure we are in the first cell after expanding the table 0071 while (m_cursor.movePosition(QTextCursor::PreviousCell)){} 0072 } 0073 //Render the objects in each section 0074 for (int i = 0; i < section->primitives(); i++) { 0075 //Colour the cell using hte section background colour 0076 OROPrimitive * prim = section->primitive(i); 0077 QTextTableCell cell = table->cellAt(m_cursor); 0078 QTextCharFormat format = cell.format(); 0079 format.setBackground(section->backgroundColor()); 0080 cell.setFormat(format); 0081 0082 if (prim->type() == OROTextBox::TextBox) { 0083 OROTextBox * tb = (OROTextBox*) prim; 0084 m_cursor.insertText(tb->text()); 0085 } else if (prim->type() == OROImage::Image) { 0086 OROImage * im = (OROImage*) prim; 0087 0088 m_cursor.insertImage(im->image().scaled(im->size().width(), im->size().height(), Qt::KeepAspectRatio)); 0089 0090 } else if (prim->type() == OROPicture::Picture) { 0091 OROPicture * im = (OROPicture*) prim; 0092 0093 QImage image(im->size().toSize(), QImage::Format_RGB32); 0094 QPainter painter(&image); 0095 im->picture()->play(&painter); 0096 0097 0098 m_cursor.insertImage(image); 0099 } else { 0100 kreportWarning() << "unhandled primitive type"; 0101 } 0102 m_cursor.movePosition(QTextCursor::NextCell); 0103 0104 } 0105 if (s < document->sections() - 1) { 0106 table->appendRows(1); 0107 } 0108 0109 renderedSections++; 0110 } 0111 } 0112 0113 QTextDocumentWriter writer(context.destinationUrl.toLocalFile()); 0114 return writer.write(m_document); 0115 } 0116 0117 }