1 /* This file is part of the KDE project
2 Copyright (C) 2011, 2012 by Dag Andersen (danders@get2net.dk)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "KoOdtFrameReportPicture.h"
21 #include "KReportRenderObjects.h"
22
23 #include <KoXmlWriter.h>
24 #include <KoOdfGraphicStyles.h>
25 #include <KoGenStyle.h>
26 #include <KoGenStyles.h>
27 #include <KReportUnit.h>
28 #include <KoStore.h>
29 #include <KoStoreDevice.h>
30
31 #include <QPainter>
32 #include "kreport_debug.h"
33 #include <QMimeDatabase>
34 #include <QMimeType>
35
KoOdtFrameReportPicture(OROPrimitive * primitive)36 KoOdtFrameReportPicture::KoOdtFrameReportPicture(OROPrimitive *primitive)
37 : KoOdtFrameReportPrimitive(primitive)
38 {
39 }
40
~KoOdtFrameReportPicture()41 KoOdtFrameReportPicture::~KoOdtFrameReportPicture()
42 {
43 }
44
picture() const45 OROPicture *KoOdtFrameReportPicture::picture() const
46 {
47 return dynamic_cast<OROPicture*>(m_primitive);
48 }
49
createBody(KoXmlWriter * bodyWriter) const50 void KoOdtFrameReportPicture::createBody(KoXmlWriter *bodyWriter) const
51 {
52 bodyWriter->startElement("draw:frame");
53 bodyWriter->addAttribute("draw:id", itemName());
54 bodyWriter->addAttribute("xml:id", itemName());
55 bodyWriter->addAttribute("draw:name", itemName());
56 bodyWriter->addAttribute("text:anchor-type", "page");
57 bodyWriter->addAttribute("text:anchor-page-number", pageNumber());
58 bodyWriter->addAttribute("draw:style-name", m_frameStyleName);
59
60 commonAttributes(bodyWriter);
61
62 bodyWriter->startElement("draw:image");
63 bodyWriter->addAttribute("xlink:href", "Pictures/" + pictureName());
64 bodyWriter->addAttribute("xlink:type", "simple");
65 bodyWriter->addAttribute("xlink:show", "embed");
66 bodyWriter->addAttribute("xlink:actuate", "onLoad");
67 bodyWriter->endElement();
68
69 bodyWriter->endElement(); // draw:frame
70 }
71
saveData(KoStore * store,KoXmlWriter * manifestWriter) const72 bool KoOdtFrameReportPicture::saveData(KoStore* store, KoXmlWriter* manifestWriter) const
73 {
74 QString name = "Pictures/" + pictureName();
75 if (!store->open(name)) {
76 return false;
77 }
78 KoStoreDevice device(store);
79 QImage image(m_primitive->size().toSize(), QImage::Format_ARGB32);
80 image.fill(0);
81 QPainter painter;
82 painter.begin(&image);
83 painter.setRenderHint(QPainter::Antialiasing);
84 painter.drawPicture(0, 0, *(picture()->picture()));
85 painter.end();
86 //kreportDebug()<<image.format();
87 bool ok = image.save(&device, "PNG");
88 if (ok) {
89 QMimeDatabase db;
90 const QString mimetype(db.mimeTypeForFile(name, QMimeDatabase::MatchExtension).name());
91 manifestWriter->addManifestEntry(name, mimetype);
92 }
93 ok = store->close() && ok;
94 return ok;
95 }
96