File indexing completed on 2024-05-12 04:43:19

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 "KReportDesignerItemBarcode.h"
0020 #include "KReportDesignerItemBase.h"
0021 #include "KReportDesigner.h"
0022 
0023 #include "barcodepaint.h"
0024 
0025 #include <KProperty>
0026 #include <KPropertySet>
0027 
0028 #include <QGraphicsScene>
0029 #include <QGraphicsSceneMouseEvent>
0030 #include <QDomDocument>
0031 #include <QPainter>
0032 #include "kreportplugin_debug.h"
0033 
0034 void KReportDesignerItemBarcode::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     setMaxLength(5);
0043     setZ(z());
0044 
0045     updateRenderText(m_itemValue->value().toString().isEmpty() ?  m_format->value().toString() : QString(), m_itemValue->value().toString(), QString());
0046 
0047 }
0048 // methods (constructors)
0049 KReportDesignerItemBarcode::KReportDesignerItemBarcode(KReportDesigner * rw, QGraphicsScene* scene, const QPointF &pos)
0050         : KReportDesignerItemRectBase(rw, this)
0051 {
0052     Q_UNUSED(pos);
0053     init(scene);
0054     setSceneRect(properRect(*rw, m_minWidthTotal*dpiX(), m_minHeight*dpiY()));
0055     nameProperty()->setValue(designer()->suggestEntityName(typeName()));
0056 }
0057 
0058 KReportDesignerItemBarcode::KReportDesignerItemBarcode(const QDomNode & element, KReportDesigner * rw, QGraphicsScene* scene)
0059         : KReportItemBarcode(element), KReportDesignerItemRectBase(rw, this)
0060 {
0061     init(scene);
0062     setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
0063 }
0064 
0065 KReportDesignerItemBarcode* KReportDesignerItemBarcode::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 KReportDesignerItemBarcode(n, designer(), nullptr);
0073 }
0074 
0075 // methods (deconstructor)
0076 KReportDesignerItemBarcode::~KReportDesignerItemBarcode()
0077 {}
0078 
0079 QRectF KReportDesignerItemBarcode::getTextRect() const
0080 {
0081     QFont fnt = QFont();
0082     return QFontMetricsF(fnt)
0083             .boundingRect(QRectF(x(), y(), 0, 0), 0,
0084                           dataSourceAndObjectTypeName(itemDataSource(), QLatin1String("barcode")));
0085 }
0086 
0087 void KReportDesignerItemBarcode::paint(QPainter* painter,
0088                                         const QStyleOptionGraphicsItem* option,
0089                                         QWidget* widget)
0090 {
0091     Q_UNUSED(option);
0092     Q_UNUSED(widget);
0093 
0094     // store any values we plan on changing so we can restore them
0095     QPen  p = painter->pen();
0096 
0097     painter->setBackground(Qt::white);
0098 
0099     //Draw a border so user knows the object edge
0100     painter->setPen(QPen(QColor(224, 224, 224)));
0101     painter->drawRect(rect());
0102 
0103     drawHandles(painter);
0104 
0105     QByteArray fmt = m_format->value().toByteArray();
0106     if (fmt == "i2of5") {
0107         renderI2of5(rect().toRect(), renderText(), horizontalAlignment(), painter);
0108     } else if (fmt == "3of9") {
0109         render3of9(rect().toRect(), renderText(), horizontalAlignment(), painter);
0110     } else if (fmt == "3of9+") {
0111         renderExtended3of9(rect().toRect(), renderText(), horizontalAlignment(), painter);
0112     } else if (fmt == "128") {
0113         renderCode128(rect().toRect(), renderText(), horizontalAlignment(), painter);
0114     } else if (fmt == "upc-a") {
0115         renderCodeUPCA(rect().toRect(), renderText(), horizontalAlignment(), painter);
0116     } else if (fmt == "upc-e") {
0117         renderCodeUPCE(rect().toRect(), renderText(), horizontalAlignment(), painter);
0118     } else if (fmt == "ean13") {
0119         renderCodeEAN13(rect().toRect(), renderText(), horizontalAlignment(), painter);
0120     } else if (fmt == "ean8") {
0121         renderCodeEAN8(rect().toRect(), renderText(), horizontalAlignment(), painter);
0122     }
0123 
0124     painter->setPen(Qt::black);
0125     painter->drawText(rect(), 0, dataSourceAndObjectTypeName(itemDataSource(),
0126                                                              QLatin1String("barcode")));
0127 
0128     // restore an values before we started just in case
0129     painter->setPen(p);
0130 }
0131 
0132 void KReportDesignerItemBarcode::buildXML(QDomDocument *doc, QDomElement *parent)
0133 {
0134     //kreportpluginDebug();
0135     QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
0136 
0137     // properties
0138     addPropertyAsAttribute(&entity, nameProperty());
0139     addPropertyAsAttribute(&entity, dataSourceProperty());
0140     addPropertyAsAttribute(&entity, m_horizontalAlignment);
0141     addPropertyAsAttribute(&entity, m_format);
0142     addPropertyAsAttribute(&entity, m_maxLength);
0143     entity.setAttribute(QLatin1String("report:z-index"), zValue());
0144     addPropertyAsAttribute(&entity, m_itemValue);
0145 
0146     // bounding rect
0147     buildXMLRect(doc, &entity, this);
0148 
0149     parent->appendChild(entity);
0150 }
0151 
0152 void KReportDesignerItemBarcode::slotPropertyChanged(KPropertySet &s, KProperty &p)
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     updateRenderText(m_itemValue->value().toString().isEmpty() ?  m_format->value().toString() : QString(), m_itemValue->value().toString(), QString());
0164 
0165     KReportDesignerItemRectBase::propertyChanged(s, p);
0166     if (designer()) designer()->setModified(true);
0167 }