Warning, file /libraries/kreport/src/items/image/KReportDesignerItemImage.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
0003  * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #include "KReportDesignerItemImage.h"
0020 #include "KReportDesignerItemBase.h"
0021 #include "KReportDesigner.h"
0022 
0023 #include <KPropertySet>
0024 #include <QGraphicsScene>
0025 #include <QGraphicsSceneMouseEvent>
0026 #include <QDomDocument>
0027 #include <QPainter>
0028 
0029 //
0030 // ReportEntitiesImage
0031 //
0032 // contructors/deconstructors
0033 
0034 void KReportDesignerItemImage::init(QGraphicsScene *scene)
0035 {
0036     //kreportpluginDebug();
0037     if (scene)
0038         scene->addItem(this);
0039 
0040     connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
0041             this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
0042 
0043     dataSourceProperty()->setListData(designer()->fieldKeys(), designer()->fieldNames());
0044     setZValue(z());
0045 }
0046 
0047 KReportDesignerItemImage::KReportDesignerItemImage(KReportDesigner *rw, QGraphicsScene *scene, const QPointF &pos)
0048         : KReportDesignerItemRectBase(rw, this)
0049 {
0050     Q_UNUSED(pos);
0051     //kreportpluginDebug();
0052     init(scene);
0053     setSceneRect(properRect(*rw, KREPORT_ITEM_RECT_DEFAULT_WIDTH, KREPORT_ITEM_RECT_DEFAULT_WIDTH));
0054     nameProperty()->setValue(designer()->suggestEntityName(typeName()));
0055 }
0056 
0057 KReportDesignerItemImage::KReportDesignerItemImage(const QDomNode & element, KReportDesigner * rw, QGraphicsScene* scene)
0058         : KReportItemImage(element), KReportDesignerItemRectBase(rw, this)
0059 {
0060     init(scene);
0061     setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
0062 }
0063 
0064 KReportDesignerItemImage* KReportDesignerItemImage::clone()
0065 {
0066     QDomDocument d;
0067     QDomElement e = d.createElement(QLatin1String("clone"));
0068     QDomNode n;
0069     buildXML(&d, &e);
0070     n = e.firstChild();
0071     return new KReportDesignerItemImage(n, designer(), nullptr);
0072 }
0073 
0074 KReportDesignerItemImage::~KReportDesignerItemImage()
0075 {
0076     // do we need to clean anything up?
0077 }
0078 
0079 void KReportDesignerItemImage::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
0080 {
0081     Q_UNUSED(option);
0082     Q_UNUSED(widget);
0083 
0084     // store any values we plan on changing so we can restore them
0085     QPen  p = painter->pen();
0086 
0087     if (isInline()) {
0088         //QImage t_img = _image;
0089         QImage t_img = m_staticImage->value().value<QPixmap>().toImage();
0090         if (mode() == QLatin1String("stretch")) {
0091             t_img = t_img.scaled(rect().width(), rect().height(), Qt::KeepAspectRatio);
0092         }
0093         painter->drawImage(rect().left(), rect().top(), t_img, 0, 0, rect().width(), rect().height());
0094     } else {
0095         painter->drawText(rect(), 0, dataSourceAndObjectTypeName(itemDataSource(), QLatin1String("image")));
0096     }
0097 
0098     //Draw a border so user knows the object edge
0099     painter->setPen(QPen(Qt::lightGray));
0100     painter->drawRect(rect());
0101 
0102 
0103     drawHandles(painter);
0104 
0105     // restore an values before we started just in case
0106     painter->setPen(p);
0107 }
0108 
0109 void KReportDesignerItemImage::buildXML(QDomDocument *doc, QDomElement *parent)
0110 {
0111     QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
0112 
0113     // properties
0114     addPropertyAsAttribute(&entity, nameProperty());
0115     addPropertyAsAttribute(&entity, m_resizeMode);
0116     entity.setAttribute(QLatin1String("report:z-index"), z());
0117     buildXMLRect(doc, &entity, this);
0118 
0119 
0120     if (isInline()) {
0121         QDomElement map = doc->createElement(QLatin1String("report:inline-image-data"));
0122         map.appendChild(doc->createTextNode(QLatin1String(inlineImageData())));
0123         entity.appendChild(map);
0124     } else {
0125         addPropertyAsAttribute(&entity, dataSourceProperty());
0126     }
0127 
0128     parent->appendChild(entity);
0129 }
0130 
0131 void KReportDesignerItemImage::slotPropertyChanged(KPropertySet &s, KProperty &p)
0132 {
0133     if (p.name() == "name") {
0134         //For some reason p.oldValue returns an empty string
0135         if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
0136             p.setValue(oldName());
0137         } else {
0138             setOldName(p.value().toString());
0139         }
0140     }
0141 
0142     KReportDesignerItemRectBase::propertyChanged(s, p);
0143     if (designer()) designer()->setModified(true);
0144 }