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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "KReportItemBarcode.h"
0019 #include "KReportUtils.h"
0020 
0021 #include <KProperty>
0022 #include <KPropertyListData>
0023 #include <KPropertySet>
0024 
0025 #include <QDomNode>
0026 
0027 #include "kreportplugin_debug.h"
0028 
0029 #include "barcodes.h"
0030 
0031 KReportItemBarcode::KReportItemBarcode()
0032     : m_minWidthData(0), m_minWidthTotal(0), m_minHeight(0)
0033 {
0034     createProperties();
0035 }
0036 
0037 KReportItemBarcode::KReportItemBarcode(const QDomNode & element)
0038     : KReportItemBarcode()
0039 {
0040     nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
0041     setItemDataSource(element.toElement().attribute(QLatin1String("report:item-data-source")));
0042     m_itemValue->setValue(element.toElement().attribute(QLatin1String("report:value")));
0043     setZ(element.toElement().attribute(QLatin1String("report:z-index")).toDouble());
0044     m_horizontalAlignment->setValue(element.toElement().attribute(QLatin1String("report:horizontal-align")));
0045     m_maxLength->setValue(element.toElement().attribute(QLatin1String("report:barcode-max-length")).toInt());
0046     m_format->setValue(element.toElement().attribute(QLatin1String("report:barcode-format")));
0047     parseReportRect(element.toElement());
0048 }
0049 
0050 void KReportItemBarcode::setMaxLength(int i)
0051 {
0052     if (i > 0) {
0053         if (m_maxLength->value().toInt() != i) {
0054             m_maxLength->setValue(i);
0055         }
0056         if (m_format->value().toString() == QLatin1String("3of9")) {
0057             int C = i; // number of characters
0058             int N = 2; // narrow mult for wide line
0059             int X = 1; // narrow line width
0060             int I = 1; // interchange line width
0061             m_minWidthData = (((C + 2) * ((3 * N) + 6) * X) + ((C + 1) * I)) / 100.0;
0062             //m_minHeight = m_minWidthData * 0.15;
0063             /*if(min_height < 0.25)*/
0064             m_minHeight = 0.25;
0065             m_minWidthTotal = m_minWidthData + 0.22; // added a little buffer to make sure we don't loose any
0066             // of our required quiet zone in conversions
0067         } else if (m_format->value().toString() == QLatin1String("3of9+")) {
0068             int C = i * 2; // number of characters
0069             int N = 2; // narrow mult for wide line
0070             int X = 1; // 1px narrow line
0071             int I = 1; // 1px narrow line interchange
0072             m_minWidthData = (((C + 2) * ((3 * N) + 6) * X) + ((C + 1) * I)) / 100.0;
0073             //m_minHeight = m_minWidthData * 0.15;
0074             /*if(min_height < 0.25)*/
0075             m_minHeight = 0.25;
0076             m_minWidthTotal = m_minWidthData + 0.22; // added a little buffer to make sure we don't loose any
0077             // of our required quiet zone in conversions
0078         } else if (m_format->value().toString() == QLatin1String("i2of5")) {
0079             int C = i * 2; // number of characters
0080             int N = 2; // narrow mult for wide line
0081             int X = 1; // 1px narrow line
0082             m_minWidthTotal = ((C * (2.0*N + 3.0) + 6.0 + N) * X);
0083             m_minHeight = 0.25;
0084             m_minWidthTotal = m_minWidthData + 0.22;
0085         } else if (m_format->value().toString() == QLatin1String("128")) {
0086             int C = i; // assuming 1:1 ratio of data passed in to data actually used in encoding
0087             int X = 1; // 1px wide
0088             m_minWidthData = (((11 * C) + 35) * X) / 100.0;       // assuming CODE A or CODE B
0089             //m_minHeight = m_minWidthData * 0.15;
0090             /*if(min_height < 0.25)*/
0091             m_minHeight = 0.25;
0092             m_minWidthTotal = m_minWidthData + 0.22; // added a little bugger to make sure we don't loose any
0093             // of our required quiet zone in conversions
0094         } else if (m_format->value().toString() == QLatin1String("upc-a")) {
0095             m_minWidthData = 0.95;
0096             m_minWidthTotal = 1.15;
0097             m_minHeight = 0.25;
0098         } else if (m_format->value().toString() == QLatin1String("upc-e")) {
0099             m_minWidthData = 0.52;
0100             m_minWidthTotal = 0.70;
0101             m_minHeight = 0.25;
0102         } else if (m_format->value().toString() == QLatin1String("ean13")) {
0103             m_minWidthData = 0.95;
0104             m_minWidthTotal = 1.15;
0105             m_minHeight = 0.25;
0106         } else if (m_format->value().toString() == QLatin1String("ean8")) {
0107             m_minWidthData = 0.67;
0108             m_minWidthTotal = 0.90;
0109             m_minHeight = 0.25;
0110         } else {
0111             kreportpluginWarning() << "Unknown format encountered: " << m_format->value().toString();
0112         }
0113     }
0114 }
0115 
0116 void KReportItemBarcode::createProperties()
0117 {
0118     createDataSourceProperty();
0119 
0120     m_itemValue = new KProperty("value", QString(), tr("Value"),
0121                                 tr("Value used if not bound to a field"));
0122 
0123     KPropertyListData *listData = new KPropertyListData(
0124         { QLatin1String("left"), QLatin1String("center"), QLatin1String("right") },
0125         QVariantList{ tr("Left"), tr("Center"), tr("Right") });
0126     m_horizontalAlignment = new KProperty("horizontal-align", listData, QLatin1String("left"),
0127                                           tr("Horizontal Alignment"));
0128 
0129     listData = new KPropertyListData(
0130     QStringList()
0131          << QLatin1String("3of9") << QLatin1String("3of9+") << QLatin1String("128")
0132          << QLatin1String("ean8") << QLatin1String("ean13") << QLatin1String("i2of5")
0133          << QLatin1String("upc-a") << QLatin1String("upc-e"),
0134     QStringList()
0135             << tr("Code 3 of 9", "Barcode symbology, keep short")
0136             << tr("Code 3 of 9 Ext.", "3 of 3 Extended: Barcode symbology, keep short")
0137             << tr("Code 128", "Barcode symbology, keep short")
0138             << tr("EAN-8", "Barcode symbology, keep short")
0139             << tr("EAN-13", "Barcode symbology, keep short")
0140             << tr("Interleaved 2 of 5", "Interleaved barcode 2 of 5: barcode symbology, keep short")
0141             << tr("UPC-A", "Barcode symbology, keep short")
0142             << tr("UPC-E", "Barcode symbology, keep short")
0143     );
0144     m_format = new KProperty("barcode-format", listData, QLatin1String("3of9"),
0145                              tr("Barcode Format"));
0146 
0147     m_maxLength = new KProperty("barcode-max-length", 5, tr("Max Length"),
0148                                 tr("Maximum Barcode Length"));
0149 
0150     propertySet()->addProperty(m_itemValue);
0151     propertySet()->addProperty(m_format);
0152     propertySet()->addProperty(m_horizontalAlignment);
0153     propertySet()->addProperty(m_maxLength);
0154 }
0155 
0156 KReportItemBarcode::~KReportItemBarcode()
0157 {
0158 }
0159 
0160 Qt::Alignment KReportItemBarcode::horizontalAlignment() const
0161 {
0162     return KReportUtils::horizontalAlignment(m_horizontalAlignment->value().toString(),
0163                                              Qt::AlignLeft);
0164 }
0165 
0166 QString KReportItemBarcode::format() const
0167 {
0168     return m_format->value().toString();
0169 }
0170 
0171 int KReportItemBarcode::maxLength() const
0172 {
0173     return m_maxLength->value().toInt();
0174 }
0175 
0176 void KReportItemBarcode::setFormat(const QString& f)
0177 {
0178     m_format->setValue(f);
0179 }
0180 
0181 void KReportItemBarcode::setHorizontalAlignment(Qt::Alignment value)
0182 {
0183     m_horizontalAlignment->setValue(KReportUtils::horizontalToString(value));
0184 }
0185 
0186 //RTTI
0187 QString KReportItemBarcode::typeName() const
0188 {
0189     return QLatin1String("barcode");
0190 }
0191 
0192 int KReportItemBarcode::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
0193                                           const QVariant &data, KReportScriptHandler *script)
0194 {
0195     Q_UNUSED(section);
0196     Q_UNUSED(script);
0197 
0198     QPointF pos = scenePosition(position());
0199     QSizeF siz = sceneSize(size());
0200     pos += offset;
0201 
0202     QRectF rect = QRectF(pos, siz);
0203 
0204     QString val;
0205 
0206     if (itemDataSource().isEmpty()) {
0207         val = m_itemValue->value().toString();
0208     } else {
0209         val = data.toString();
0210     }
0211 
0212     if (page) {
0213         QByteArray fmt = m_format->value().toByteArray();
0214         Qt::Alignment align = horizontalAlignment();
0215         if (fmt == "3of9")
0216             render3of9(page, rect, val, align);
0217         else if (fmt == "3of9+")
0218             renderExtended3of9(page, rect, val, align);
0219         else if (fmt == "i2of5")
0220             renderI2of5(page, rect, val, align);
0221         else if (fmt == "128")
0222             renderCode128(page, rect, val, align);
0223         else if (fmt == "ean13")
0224             renderCodeEAN13(page, rect, val, align);
0225         else if (fmt == "ean8")
0226             renderCodeEAN8(page, rect, val, align);
0227         else if (fmt == "upc-a")
0228             renderCodeUPCA(page, rect, val, align);
0229         else if (fmt == "upc-e")
0230             renderCodeUPCE(page, rect, val, align);
0231         else {
0232             kreportpluginWarning() << "Unknown barcode format:" << fmt;
0233         }
0234     }
0235     return 0;
0236 }