KReport

KReportDesignerItemImage.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
3 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "KReportDesignerItemImage.h"
20#include "KReportDesignerItemBase.h"
21#include "KReportDesigner.h"
22
23#include <KPropertySet>
24#include <QGraphicsScene>
25#include <QGraphicsSceneMouseEvent>
26#include <QDomDocument>
27#include <QPainter>
28
29//
30// ReportEntitiesImage
31//
32// contructors/deconstructors
33
34void KReportDesignerItemImage::init(QGraphicsScene *scene)
35{
36 //kreportpluginDebug();
37 if (scene)
38 scene->addItem(this);
39
40 connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
41 this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
42
43 dataSourceProperty()->setListData(designer()->fieldKeys(), designer()->fieldNames());
44 setZValue(z());
45}
46
47KReportDesignerItemImage::KReportDesignerItemImage(KReportDesigner *rw, QGraphicsScene *scene, const QPointF &pos)
49{
50 Q_UNUSED(pos);
51 //kreportpluginDebug();
52 init(scene);
53 setSceneRect(properRect(*rw, KREPORT_ITEM_RECT_DEFAULT_WIDTH, KREPORT_ITEM_RECT_DEFAULT_WIDTH));
54 nameProperty()->setValue(designer()->suggestEntityName(typeName()));
55}
56
57KReportDesignerItemImage::KReportDesignerItemImage(const QDomNode & element, KReportDesigner * rw, QGraphicsScene* scene)
58 : KReportItemImage(element), KReportDesignerItemRectBase(rw, this)
59{
60 init(scene);
61 setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
62}
63
64KReportDesignerItemImage* KReportDesignerItemImage::clone()
65{
68 QDomNode n;
69 buildXML(&d, &e);
70 n = e.firstChild();
71 return new KReportDesignerItemImage(n, designer(), nullptr);
72}
73
74KReportDesignerItemImage::~KReportDesignerItemImage()
75{
76 // do we need to clean anything up?
77}
78
79void KReportDesignerItemImage::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
80{
81 Q_UNUSED(option);
82 Q_UNUSED(widget);
83
84 // store any values we plan on changing so we can restore them
85 QPen p = painter->pen();
86
87 if (isInline()) {
88 //QImage t_img = _image;
89 QImage t_img = m_staticImage->value().value<QPixmap>().toImage();
90 if (mode() == QLatin1String("stretch")) {
91 t_img = t_img.scaled(rect().width(), rect().height(), Qt::KeepAspectRatio);
92 }
93 painter->drawImage(rect().left(), rect().top(), t_img, 0, 0, rect().width(), rect().height());
94 } else {
95 painter->drawText(rect(), 0, dataSourceAndObjectTypeName(itemDataSource(), QLatin1String("image")));
96 }
97
98 //Draw a border so user knows the object edge
99 painter->setPen(QPen(Qt::lightGray));
100 painter->drawRect(rect());
101
102
103 drawHandles(painter);
104
105 // restore an values before we started just in case
106 painter->setPen(p);
107}
108
109void KReportDesignerItemImage::buildXML(QDomDocument *doc, QDomElement *parent)
110{
111 QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
112
113 // properties
114 addPropertyAsAttribute(&entity, nameProperty());
115 addPropertyAsAttribute(&entity, m_resizeMode);
116 entity.setAttribute(QLatin1String("report:z-index"), z());
117 buildXMLRect(doc, &entity, this);
118
119
120 if (isInline()) {
121 QDomElement map = doc->createElement(QLatin1String("report:inline-image-data"));
122 map.appendChild(doc->createTextNode(QLatin1String(inlineImageData())));
123 entity.appendChild(map);
124 } else {
125 addPropertyAsAttribute(&entity, dataSourceProperty());
126 }
127
128 parent->appendChild(entity);
129}
130
131void KReportDesignerItemImage::slotPropertyChanged(KPropertySet &s, KProperty &p)
132{
133 if (p.name() == "name") {
134 //For some reason p.oldValue returns an empty string
135 if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
136 p.setValue(oldName());
137 } else {
138 setOldName(p.value().toString());
139 }
140 }
141
142 KReportDesignerItemRectBase::propertyChanged(s, p);
143 if (designer()) designer()->setModified(true);
144}
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QByteArray name() const
void setListData(const QStringList &keys, const QStringList &names)
Base class for rectangular report items used within the designer GUI.
The ReportDesigner is the main widget for designing a report.
void setModified(bool modified)
Sets the modified status, defaulting to true for modified.
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
qreal z() const
Return the z-value in points.
QString itemDataSource() const
QString typeName(const QJsonObject &obj)
QCA_EXPORT void init()
QDomElement createElement(const QString &tagName)
QDomText createTextNode(const QString &value)
void setAttribute(const QString &name, const QString &value)
QDomNode appendChild(const QDomNode &newChild)
QDomNode firstChild() const const
QGraphicsScene * scene() const const
void setZValue(qreal z)
QRectF rect() const const
void addItem(QGraphicsItem *item)
QImage scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void drawImage(const QPoint &point, const QImage &image)
void drawRect(const QRect &rectangle)
void drawText(const QPoint &position, const QString &text)
const QPen & pen() const const
void setPen(Qt::PenStyle style)
KeepAspectRatio
lightGray
QTextStream & left(QTextStream &stream)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
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.