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

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