File indexing completed on 2024-05-19 04:44:32

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 "KReportDesignerItemField.h"
0020 #include "KReportItemField.h"
0021 #include "KReportDesigner.h"
0022 #include "kreportplugin_debug.h"
0023 #include "KReportLineStyle.h"
0024 
0025 #include <QDomDocument>
0026 #include <QPainter>
0027 #include <QGraphicsScene>
0028 #include <QGraphicsSceneMouseEvent>
0029 
0030 //
0031 // class ReportEntityField
0032 //
0033 
0034 void KReportDesignerItemField::init(QGraphicsScene *scene)
0035 {
0036     if (scene)
0037         scene->addItem(this);
0038 
0039     connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
0040             this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
0041 
0042     dataSourceProperty()->setListData(designer()->fieldKeys(), designer()->fieldNames());
0043     setZValue(z());
0044 
0045     updateRenderText(itemDataSource(), m_itemValue->value().toString(), QLatin1String("field"));
0046 }
0047 
0048 // methods (constructors)
0049 KReportDesignerItemField::KReportDesignerItemField(KReportDesigner *rw, QGraphicsScene *scene, const QPointF &pos)
0050         : KReportDesignerItemRectBase(rw, this)
0051 {
0052     Q_UNUSED(pos);
0053     init(scene);
0054     setSceneRect(properRect(*rw, getTextRect().width(), getTextRect().height()));
0055     nameProperty()->setValue(designer()->suggestEntityName(typeName()));
0056 }
0057 
0058 KReportDesignerItemField::KReportDesignerItemField(const QDomNode & element, KReportDesigner * d, QGraphicsScene * s)
0059         : KReportItemField(element), KReportDesignerItemRectBase(d, this)
0060 {
0061     init(s);
0062     setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
0063 }
0064 
0065 KReportDesignerItemField* KReportDesignerItemField::clone()
0066 {
0067     QDomDocument d;
0068     QDomElement e = d.createElement(QLatin1String("clone"));
0069     QDomNode n;
0070     buildXML(&d, &e);
0071     n = e.firstChild();
0072     return new KReportDesignerItemField(n, designer(), nullptr);
0073 }
0074 
0075 // methods (deconstructor)
0076 KReportDesignerItemField::~KReportDesignerItemField()
0077 {}
0078 
0079 QRectF KReportDesignerItemField::getTextRect() const
0080 {
0081     return QFontMetricsF(font()).boundingRect(QRectF(x(), y(), 0, 0), textFlags(), renderText());
0082 }
0083 
0084 
0085 
0086 void KReportDesignerItemField::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget *widget)
0087 {
0088     Q_UNUSED(option);
0089     Q_UNUSED(widget);
0090 
0091     // store any values we plan on changing so we can restore them
0092     QFont f = painter->font();
0093     QPen  p = painter->pen();
0094 
0095 
0096     painter->setFont(font());
0097     painter->setBackgroundMode(Qt::TransparentMode);
0098 
0099     QColor bg = m_backgroundColor->value().value<QColor>();
0100     bg.setAlphaF(m_backgroundOpacity->value().toReal() *0.01);
0101 
0102     painter->setPen(m_foregroundColor->value().value<QColor>());
0103 
0104     painter->fillRect(QGraphicsRectItem::rect(), bg);
0105     painter->drawText(rect(), textFlags(), renderText());
0106 
0107 
0108     if ((Qt::PenStyle)m_lineStyle->value().toInt() == Qt::NoPen || m_lineWeight->value().toInt() <= 0) {
0109         painter->setPen(QPen(Qt::lightGray));
0110     } else {
0111         painter->setPen(QPen(m_lineColor->value().value<QColor>(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt()));
0112     }
0113 
0114     painter->drawRect(rect());
0115 
0116 
0117     drawHandles(painter);
0118 
0119     // restore an values before we started just in case
0120     painter->setFont(f);
0121     painter->setPen(p);
0122 }
0123 
0124 void KReportDesignerItemField::buildXML(QDomDocument *doc, QDomElement *parent)
0125 {
0126     QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
0127 
0128     // properties
0129     addPropertyAsAttribute(&entity, nameProperty());
0130     addPropertyAsAttribute(&entity, dataSourceProperty());
0131     addPropertyAsAttribute(&entity, m_verticalAlignment);
0132     addPropertyAsAttribute(&entity, m_horizontalAlignment);
0133     addPropertyAsAttribute(&entity, m_wordWrap);
0134     addPropertyAsAttribute(&entity, m_canGrow);
0135     addPropertyAsAttribute(&entity, m_itemValue);
0136 
0137     entity.setAttribute(QLatin1String("report:z-index"), zValue());
0138 
0139     // bounding rect
0140     buildXMLRect(doc, &entity, this);
0141 
0142     //text style info
0143     buildXMLTextStyle(doc, &entity, textStyle());
0144 
0145     //Line Style
0146     buildXMLLineStyle(doc, &entity, lineStyle());
0147 
0148 
0149 #if 0 //Field Totals
0150     if (m_trackTotal) {
0151         QDomElement tracktotal = doc->createElement("tracktotal");
0152         if (m_trackBuiltinFormat)
0153             tracktotal.setAttribute("builtin", "true");
0154         if (_useSubTotal)
0155             tracktotal.setAttribute("subtotal", "true");
0156         tracktotal.appendChild(doc.createTextNode(_trackTotalFormat->value().toString()));
0157         entity.appendChild(tracktotal);
0158     }
0159 #endif
0160 
0161     parent->appendChild(entity);
0162 }
0163 
0164 void KReportDesignerItemField::slotPropertyChanged(KPropertySet &s, KProperty &p)
0165 {
0166     Q_UNUSED(s);
0167 
0168     if (p.name() == "name") {
0169         //For some reason p.oldValue returns an empty string
0170         if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
0171             p.setValue(oldName());
0172         } else {
0173             setOldName(p.value().toString());
0174         }
0175     }
0176 
0177     updateRenderText(itemDataSource(), m_itemValue->value().toString(), QLatin1String("field"));
0178 
0179     KReportDesignerItemRectBase::propertyChanged(s, p);
0180     if (designer())designer()->setModified(true);
0181 }