KReport

KReportDesignerItemImage.cpp
1 /* This file is part of the KDE project
2  * Copyright (C) 2001-2007 by OpenMFG, LLC ([email protected])
3  * Copyright (C) 2007-2008 by Adam Pigg ([email protected])
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 
34 void 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 
47 KReportDesignerItemImage::KReportDesignerItemImage(KReportDesigner *rw, QGraphicsScene *scene, const QPointF &pos)
48  : KReportDesignerItemRectBase(rw, this)
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 
57 KReportDesignerItemImage::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 
64 KReportDesignerItemImage* KReportDesignerItemImage::clone()
65 {
66  QDomDocument d;
67  QDomElement e = d.createElement(QLatin1String("clone"));
68  QDomNode n;
69  buildXML(&d, &e);
70  n = e.firstChild();
71  return new KReportDesignerItemImage(n, designer(), nullptr);
72 }
73 
74 KReportDesignerItemImage::~KReportDesignerItemImage()
75 {
76  // do we need to clean anything up?
77 }
78 
79 void 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 
109 void 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 
131 void 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 }
QByteArray name() const
void setPen(const QColor &color)
QDomNode firstChild() const const
void addItem(QGraphicsItem *item)
void drawRect(const QRectF &rectangle)
QCA_EXPORT void init()
QTextStream & left(QTextStream &stream)
QDomText createTextNode(const QString &value)
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
const QPen & pen() const const
QDomElement createElement(const QString &tagName)
void drawText(const QPointF &position, const QString &text)
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
void setAttribute(const QString &name, const QString &value)
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
KeepAspectRatio
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QDomNode appendChild(const QDomNode &newChild)
Base class for rectangular report items used within the designer GUI.
QVariant value() const
The ReportDesigner is the main widget for designing a report.
QFuture< void > map(Sequence &sequence, MapFunctor function)
lightGray
QString toString() const const
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.