KReport

KReportItemImage.cpp
1 /* This file is part of the KDE project
2  * Copyright (C) 2007-2008 by Adam Pigg ([email protected])
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 
30 KReportItemImage::KReportItemImage()
31 {
32  createProperties();
33 }
34 
35 KReportItemImage::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 
61 KReportItemImage::~KReportItemImage()
62 {
63 }
64 
65 bool KReportItemImage::isInline() const
66 {
67  return !(inlineImageData().isEmpty());
68 }
69 
70 QByteArray 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 
82 void 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));
95  const QPixmap pix(QPixmap::fromImage(QImage::fromData(binaryStream), Qt::ColorOnly));
96  m_staticImage->setValue(pix);
97  }
98 
99 }
100 
101 QString KReportItemImage::mode() const
102 {
103  return m_resizeMode->value().toString();
104 }
105 
106 void KReportItemImage::setMode(const QString &m)
107 {
108  if (mode() != m) {
109  m_resizeMode->setValue(m);
110  }
111 }
112 
113 void 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 
127 QString KReportItemImage::typeName() const
128 {
129  return QLatin1String("image");
130 }
131 
132 int 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 
int count() const const
QDomNode firstChild() const const
QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags)
QDomElement toElement() const const
Defines an image. An image is a bitmap.
void setZ(qreal z)
Sets the z-value for the element.
QByteArray toLatin1() const const
QByteArray toByteArray() const const
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
bool loadFromData(const uchar *data, int len, const char *format)
QString nodeValue() const const
QPointF position() const
KeepAspectRatio
QDomNode item(int index) const const
bool isEmpty() const const
bool save(const QString &fileName, const char *format, int quality) const const
QByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options)
QDomNodeList childNodes() const const
QString nodeName() const const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
ColorOnly
Represents a single a single row in a document and may contain zero or more OROPrimitives.
QString attribute(const QString &name, const QString &defValue) const const
SmoothTransformation
QString tr(const char *sourceText, const char *disambiguation, int n)
QImage fromData(const uchar *data, int size, const char *format)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:17:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.