KReport

KReportItemImage.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "KReportItemImage.h"
19#include "KReportUtils.h"
20#include "KReportRenderObjects.h"
21#include "kreportplugin_debug.h"
22
23#include <KPropertyListData>
24#include <KPropertySet>
25
26#include <QBuffer>
27#include <QDomNodeList>
28
29
30KReportItemImage::KReportItemImage()
31{
32 createProperties();
33}
34
35KReportItemImage::KReportItemImage(const QDomNode & element)
36 : KReportItemImage()
37{
38 nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
39 setItemDataSource(element.toElement().attribute(QLatin1String("report:item-data-source")));
40 m_resizeMode->setValue(element.toElement().attribute(QLatin1String("report:resize-mode"), QLatin1String("stretch")));
41 setZ(element.toElement().attribute(QLatin1String("report:z-index")).toDouble());
42
43 parseReportRect(element.toElement());
44
45 QDomNodeList nl = element.childNodes();
46 QString n;
47 QDomNode node;
48 for (int i = 0; i < nl.count(); i++) {
49 node = nl.item(i);
50 n = node.nodeName();
51
52 if (n == QLatin1String("report:inline-image-data")) {
53
54 setInlineImageData(node.firstChild().nodeValue().toLatin1());
55 } else {
56 kreportpluginWarning() << "while parsing image element encountered unknown element: " << n;
57 }
58 }
59}
60
61KReportItemImage::~KReportItemImage()
62{
63}
64
65bool KReportItemImage::isInline() const
66{
67 return !(inlineImageData().isEmpty());
68}
69
70QByteArray KReportItemImage::inlineImageData() const
71{
72 QPixmap pixmap = m_staticImage->value().value<QPixmap>();
73 QByteArray ba;
74 QBuffer buffer(&ba);
75 buffer.open(QIODevice::ReadWrite);
76 pixmap.save(&buffer, "PNG"); // writes pixmap into ba in PNG format,
77 //! @todo should I remember the format used, or save as PNG as its lossless?
78
79 return buffer.buffer().toBase64();
80}
81
82void KReportItemImage::setInlineImageData(const QByteArray &dat, const QString &fn)
83{
84 if (!fn.isEmpty()) {
85 QPixmap pix(fn);
86 if (!pix.isNull())
87 m_staticImage->setValue(pix);
88 else {
89 QPixmap blank(1, 1);
90 blank.fill();
91 m_staticImage->setValue(blank);
92 }
93 } else {
94 const QByteArray binaryStream(QByteArray::fromBase64(dat));
96 m_staticImage->setValue(pix);
97 }
98
99}
100
101QString KReportItemImage::mode() const
102{
103 return m_resizeMode->value().toString();
104}
105
106void KReportItemImage::setMode(const QString &m)
107{
108 if (mode() != m) {
109 m_resizeMode->setValue(m);
110 }
111}
112
113void KReportItemImage::createProperties()
114{
115 createDataSourceProperty();
116
117 KPropertyListData *listData = new KPropertyListData(
118 { QLatin1String("clip"), QLatin1String("stretch") },
119 QVariantList{ tr("Clip"), tr("Stretch") });
120 m_resizeMode = new KProperty("resize-mode", listData, QLatin1String("clip"), tr("Resize Mode"));
121 m_staticImage = new KProperty("static-image", QPixmap(), tr("Value"), tr("Value used if not bound to a field"));
122
123 propertySet()->addProperty(m_resizeMode);
124 propertySet()->addProperty(m_staticImage);
125}
126
127QString KReportItemImage::typeName() const
128{
129 return QLatin1String("image");
130}
131
132int KReportItemImage::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
133 const QVariant &data, KReportScriptHandler *script)
134{
135 Q_UNUSED(script)
136
137 QByteArray uudata;
138 QByteArray imgdata;
139 if (!isInline()) {
140 imgdata = data.toByteArray();
141 } else {
142 uudata = inlineImageData();
143 imgdata = QByteArray::fromBase64(uudata);
144 }
145
146 QImage img;
147 img.loadFromData(imgdata);
148 OROImage * id = new OROImage();
149 id->setImage(img);
150 if (mode().toLower() == QLatin1String("stretch")) {
151 id->setScaled(true);
152 id->setAspectRatioMode(Qt::KeepAspectRatio);
153 id->setTransformationMode(Qt::SmoothTransformation);
154 }
155
156 id->setPosition(scenePosition(position()) + offset);
157 id->setSize(sceneSize(size()));
158 if (page) {
159 page->insertPrimitive(id);
160 }
161
162 if (section) {
163 OROImage *i2 = dynamic_cast<OROImage*>(id->clone());
164 if (i2) {
165 i2->setPosition(scenePosition(position()));
166 section->addPrimitive(i2);
167 }
168 }
169
170 if (!page) {
171 delete id;
172 }
173
174 return 0; //Item doesn't stretch the section height
175}
176
177
void addProperty(KProperty *property, const QByteArray &group="common")
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
QPointF position() const
Return the position in points.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
QSizeF size() const
Return the size in points.
Defines an image. An image is a bitmap.
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
Represents a single a single row in a document and may contain zero or more OROPrimitives.
QByteArray fromBase64(const QByteArray &base64, Base64Options options)
bool isEmpty() const const
QString attribute(const QString &name, const QString &defValue) const const
QDomNodeList childNodes() const const
QDomNode firstChild() const const
QString nodeName() const const
QString nodeValue() const const
QDomElement toElement() const const
int count() const const
QDomNode item(int index) const const
QImage fromData(QByteArrayView data, const char *format)
bool loadFromData(QByteArrayView data, const char *format)
QString tr(const char *sourceText, const char *disambiguation, int n)
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
bool save(QIODevice *device, const char *format, int quality) const const
bool isEmpty() const const
QByteArray toLatin1() const const
KeepAspectRatio
ColorOnly
SmoothTransformation
QByteArray toByteArray() const const
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:21:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.