KReport

KReportItemBarcode.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "KReportItemBarcode.h"
19#include "KReportUtils.h"
20
21#include <KProperty>
22#include <KPropertyListData>
23#include <KPropertySet>
24
25#include <QDomNode>
26
27#include "kreportplugin_debug.h"
28
29#include "barcodes.h"
30
31KReportItemBarcode::KReportItemBarcode()
32 : m_minWidthData(0), m_minWidthTotal(0), m_minHeight(0)
33{
34 createProperties();
35}
36
37KReportItemBarcode::KReportItemBarcode(const QDomNode & element)
38 : KReportItemBarcode()
39{
40 nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
41 setItemDataSource(element.toElement().attribute(QLatin1String("report:item-data-source")));
42 m_itemValue->setValue(element.toElement().attribute(QLatin1String("report:value")));
43 setZ(element.toElement().attribute(QLatin1String("report:z-index")).toDouble());
44 m_horizontalAlignment->setValue(element.toElement().attribute(QLatin1String("report:horizontal-align")));
45 m_maxLength->setValue(element.toElement().attribute(QLatin1String("report:barcode-max-length")).toInt());
46 m_format->setValue(element.toElement().attribute(QLatin1String("report:barcode-format")));
47 parseReportRect(element.toElement());
48}
49
50void KReportItemBarcode::setMaxLength(int i)
51{
52 if (i > 0) {
53 if (m_maxLength->value().toInt() != i) {
54 m_maxLength->setValue(i);
55 }
56 if (m_format->value().toString() == QLatin1String("3of9")) {
57 int C = i; // number of characters
58 int N = 2; // narrow mult for wide line
59 int X = 1; // narrow line width
60 int I = 1; // interchange line width
61 m_minWidthData = (((C + 2) * ((3 * N) + 6) * X) + ((C + 1) * I)) / 100.0;
62 //m_minHeight = m_minWidthData * 0.15;
63 /*if(min_height < 0.25)*/
64 m_minHeight = 0.25;
65 m_minWidthTotal = m_minWidthData + 0.22; // added a little buffer to make sure we don't loose any
66 // of our required quiet zone in conversions
67 } else if (m_format->value().toString() == QLatin1String("3of9+")) {
68 int C = i * 2; // number of characters
69 int N = 2; // narrow mult for wide line
70 int X = 1; // 1px narrow line
71 int I = 1; // 1px narrow line interchange
72 m_minWidthData = (((C + 2) * ((3 * N) + 6) * X) + ((C + 1) * I)) / 100.0;
73 //m_minHeight = m_minWidthData * 0.15;
74 /*if(min_height < 0.25)*/
75 m_minHeight = 0.25;
76 m_minWidthTotal = m_minWidthData + 0.22; // added a little buffer to make sure we don't loose any
77 // of our required quiet zone in conversions
78 } else if (m_format->value().toString() == QLatin1String("i2of5")) {
79 int C = i * 2; // number of characters
80 int N = 2; // narrow mult for wide line
81 int X = 1; // 1px narrow line
82 m_minWidthTotal = ((C * (2.0*N + 3.0) + 6.0 + N) * X);
83 m_minHeight = 0.25;
84 m_minWidthTotal = m_minWidthData + 0.22;
85 } else if (m_format->value().toString() == QLatin1String("128")) {
86 int C = i; // assuming 1:1 ratio of data passed in to data actually used in encoding
87 int X = 1; // 1px wide
88 m_minWidthData = (((11 * C) + 35) * X) / 100.0; // assuming CODE A or CODE B
89 //m_minHeight = m_minWidthData * 0.15;
90 /*if(min_height < 0.25)*/
91 m_minHeight = 0.25;
92 m_minWidthTotal = m_minWidthData + 0.22; // added a little bugger to make sure we don't loose any
93 // of our required quiet zone in conversions
94 } else if (m_format->value().toString() == QLatin1String("upc-a")) {
95 m_minWidthData = 0.95;
96 m_minWidthTotal = 1.15;
97 m_minHeight = 0.25;
98 } else if (m_format->value().toString() == QLatin1String("upc-e")) {
99 m_minWidthData = 0.52;
100 m_minWidthTotal = 0.70;
101 m_minHeight = 0.25;
102 } else if (m_format->value().toString() == QLatin1String("ean13")) {
103 m_minWidthData = 0.95;
104 m_minWidthTotal = 1.15;
105 m_minHeight = 0.25;
106 } else if (m_format->value().toString() == QLatin1String("ean8")) {
107 m_minWidthData = 0.67;
108 m_minWidthTotal = 0.90;
109 m_minHeight = 0.25;
110 } else {
111 kreportpluginWarning() << "Unknown format encountered: " << m_format->value().toString();
112 }
113 }
114}
115
116void KReportItemBarcode::createProperties()
117{
118 createDataSourceProperty();
119
120 m_itemValue = new KProperty("value", QString(), tr("Value"),
121 tr("Value used if not bound to a field"));
122
123 KPropertyListData *listData = new KPropertyListData(
124 { QLatin1String("left"), QLatin1String("center"), QLatin1String("right") },
125 QVariantList{ tr("Left"), tr("Center"), tr("Right") });
126 m_horizontalAlignment = new KProperty("horizontal-align", listData, QLatin1String("left"),
127 tr("Horizontal Alignment"));
128
129 listData = new KPropertyListData(
131 << QLatin1String("3of9") << QLatin1String("3of9+") << QLatin1String("128")
132 << QLatin1String("ean8") << QLatin1String("ean13") << QLatin1String("i2of5")
133 << QLatin1String("upc-a") << QLatin1String("upc-e"),
135 << tr("Code 3 of 9", "Barcode symbology, keep short")
136 << tr("Code 3 of 9 Ext.", "3 of 3 Extended: Barcode symbology, keep short")
137 << tr("Code 128", "Barcode symbology, keep short")
138 << tr("EAN-8", "Barcode symbology, keep short")
139 << tr("EAN-13", "Barcode symbology, keep short")
140 << tr("Interleaved 2 of 5", "Interleaved barcode 2 of 5: barcode symbology, keep short")
141 << tr("UPC-A", "Barcode symbology, keep short")
142 << tr("UPC-E", "Barcode symbology, keep short")
143 );
144 m_format = new KProperty("barcode-format", listData, QLatin1String("3of9"),
145 tr("Barcode Format"));
146
147 m_maxLength = new KProperty("barcode-max-length", 5, tr("Max Length"),
148 tr("Maximum Barcode Length"));
149
150 propertySet()->addProperty(m_itemValue);
151 propertySet()->addProperty(m_format);
152 propertySet()->addProperty(m_horizontalAlignment);
153 propertySet()->addProperty(m_maxLength);
154}
155
156KReportItemBarcode::~KReportItemBarcode()
157{
158}
159
160Qt::Alignment KReportItemBarcode::horizontalAlignment() const
161{
162 return KReportUtils::horizontalAlignment(m_horizontalAlignment->value().toString(),
164}
165
166QString KReportItemBarcode::format() const
167{
168 return m_format->value().toString();
169}
170
171int KReportItemBarcode::maxLength() const
172{
173 return m_maxLength->value().toInt();
174}
175
176void KReportItemBarcode::setFormat(const QString& f)
177{
178 m_format->setValue(f);
179}
180
181void KReportItemBarcode::setHorizontalAlignment(Qt::Alignment value)
182{
183 m_horizontalAlignment->setValue(KReportUtils::horizontalToString(value));
184}
185
186//RTTI
187QString KReportItemBarcode::typeName() const
188{
189 return QLatin1String("barcode");
190}
191
192int KReportItemBarcode::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
193 const QVariant &data, KReportScriptHandler *script)
194{
195 Q_UNUSED(section);
196 Q_UNUSED(script);
197
199 QSizeF siz = sceneSize(size());
200 pos += offset;
201
202 QRectF rect = QRectF(pos, siz);
203
204 QString val;
205
206 if (itemDataSource().isEmpty()) {
207 val = m_itemValue->value().toString();
208 } else {
209 val = data.toString();
210 }
211
212 if (page) {
213 QByteArray fmt = m_format->value().toByteArray();
214 Qt::Alignment align = horizontalAlignment();
215 if (fmt == "3of9")
216 render3of9(page, rect, val, align);
217 else if (fmt == "3of9+")
218 renderExtended3of9(page, rect, val, align);
219 else if (fmt == "i2of5")
220 renderI2of5(page, rect, val, align);
221 else if (fmt == "128")
222 renderCode128(page, rect, val, align);
223 else if (fmt == "ean13")
224 renderCodeEAN13(page, rect, val, align);
225 else if (fmt == "ean8")
226 renderCodeEAN8(page, rect, val, align);
227 else if (fmt == "upc-a")
228 renderCodeUPCA(page, rect, val, align);
229 else if (fmt == "upc-e")
230 renderCodeUPCE(page, rect, val, align);
231 else {
232 kreportpluginWarning() << "Unknown barcode format:" << fmt;
233 }
234 }
235 return 0;
236}
void addProperty(KProperty *property, const QByteArray &group="common")
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
QPointF position() const
Return the position in points.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
QSizeF size() const
Return the size in points.
QString itemDataSource() const
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
Represents a single a single row in a document and may contain zero or more OROPrimitives.
QString attribute(const QString &name, const QString &defValue) const const
QDomElement toElement() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
typedef Alignment
QByteArray toByteArray() const const
int toInt(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.