KReport

KReportDesignerItemBarcode.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
3 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "KReportDesignerItemBarcode.h"
20#include "KReportDesignerItemBase.h"
21#include "KReportDesigner.h"
22
23#include "barcodepaint.h"
24
25#include <KProperty>
26#include <KPropertySet>
27
28#include <QGraphicsScene>
29#include <QGraphicsSceneMouseEvent>
30#include <QDomDocument>
31#include <QPainter>
32#include "kreportplugin_debug.h"
33
34void KReportDesignerItemBarcode::init(QGraphicsScene *scene)
35{
36 if (scene)
37 scene->addItem(this);
38
39 connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
40 this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
41
42 setMaxLength(5);
43 setZ(z());
44
45 updateRenderText(m_itemValue->value().toString().isEmpty() ? m_format->value().toString() : QString(), m_itemValue->value().toString(), QString());
46
47}
48// methods (constructors)
49KReportDesignerItemBarcode::KReportDesignerItemBarcode(KReportDesigner * rw, QGraphicsScene* scene, const QPointF &pos)
51{
52 Q_UNUSED(pos);
53 init(scene);
54 setSceneRect(properRect(*rw, m_minWidthTotal*dpiX(), m_minHeight*dpiY()));
55 nameProperty()->setValue(designer()->suggestEntityName(typeName()));
56}
57
58KReportDesignerItemBarcode::KReportDesignerItemBarcode(const QDomNode & element, KReportDesigner * rw, QGraphicsScene* scene)
59 : KReportItemBarcode(element), KReportDesignerItemRectBase(rw, this)
60{
61 init(scene);
62 setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
63}
64
65KReportDesignerItemBarcode* KReportDesignerItemBarcode::clone()
66{
69 QDomNode n;
70 buildXML(&d, &e);
71 n = e.firstChild();
72 return new KReportDesignerItemBarcode(n, designer(), nullptr);
73}
74
75// methods (deconstructor)
76KReportDesignerItemBarcode::~KReportDesignerItemBarcode()
77{}
78
79QRectF KReportDesignerItemBarcode::getTextRect() const
80{
81 QFont fnt = QFont();
82 return QFontMetricsF(fnt)
83 .boundingRect(QRectF(x(), y(), 0, 0), 0,
84 dataSourceAndObjectTypeName(itemDataSource(), QLatin1String("barcode")));
85}
86
87void KReportDesignerItemBarcode::paint(QPainter* painter,
88 const QStyleOptionGraphicsItem* option,
89 QWidget* widget)
90{
91 Q_UNUSED(option);
92 Q_UNUSED(widget);
93
94 // store any values we plan on changing so we can restore them
95 QPen p = painter->pen();
96
97 painter->setBackground(Qt::white);
98
99 //Draw a border so user knows the object edge
100 painter->setPen(QPen(QColor(224, 224, 224)));
101 painter->drawRect(rect());
102
103 drawHandles(painter);
104
105 QByteArray fmt = m_format->value().toByteArray();
106 if (fmt == "i2of5") {
107 renderI2of5(rect().toRect(), renderText(), horizontalAlignment(), painter);
108 } else if (fmt == "3of9") {
109 render3of9(rect().toRect(), renderText(), horizontalAlignment(), painter);
110 } else if (fmt == "3of9+") {
111 renderExtended3of9(rect().toRect(), renderText(), horizontalAlignment(), painter);
112 } else if (fmt == "128") {
113 renderCode128(rect().toRect(), renderText(), horizontalAlignment(), painter);
114 } else if (fmt == "upc-a") {
115 renderCodeUPCA(rect().toRect(), renderText(), horizontalAlignment(), painter);
116 } else if (fmt == "upc-e") {
117 renderCodeUPCE(rect().toRect(), renderText(), horizontalAlignment(), painter);
118 } else if (fmt == "ean13") {
119 renderCodeEAN13(rect().toRect(), renderText(), horizontalAlignment(), painter);
120 } else if (fmt == "ean8") {
121 renderCodeEAN8(rect().toRect(), renderText(), horizontalAlignment(), painter);
122 }
123
124 painter->setPen(Qt::black);
125 painter->drawText(rect(), 0, dataSourceAndObjectTypeName(itemDataSource(),
126 QLatin1String("barcode")));
127
128 // restore an values before we started just in case
129 painter->setPen(p);
130}
131
132void KReportDesignerItemBarcode::buildXML(QDomDocument *doc, QDomElement *parent)
133{
134 //kreportpluginDebug();
135 QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
136
137 // properties
138 addPropertyAsAttribute(&entity, nameProperty());
139 addPropertyAsAttribute(&entity, dataSourceProperty());
140 addPropertyAsAttribute(&entity, m_horizontalAlignment);
141 addPropertyAsAttribute(&entity, m_format);
142 addPropertyAsAttribute(&entity, m_maxLength);
143 entity.setAttribute(QLatin1String("report:z-index"), zValue());
144 addPropertyAsAttribute(&entity, m_itemValue);
145
146 // bounding rect
147 buildXMLRect(doc, &entity, this);
148
149 parent->appendChild(entity);
150}
151
152void KReportDesignerItemBarcode::slotPropertyChanged(KPropertySet &s, KProperty &p)
153{
154 if (p.name() == "name") {
155 //For some reason p.oldValue returns an empty string
156 if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
157 p.setValue(oldName());
158 } else {
159 setOldName(p.value().toString());
160 }
161 }
162
163 updateRenderText(m_itemValue->value().toString().isEmpty() ? m_format->value().toString() : QString(), m_itemValue->value().toString(), QString());
164
165 KReportDesignerItemRectBase::propertyChanged(s, p);
166 if (designer()) designer()->setModified(true);
167}
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QByteArray name() const
void updateRenderText(const QString &itemDataSource, const QString &itemStaticValue, const QString &itemType)
Updates the text that is shown for the item in the report designer If itemDataSource is set then it i...
Base class for rectangular report items used within the designer GUI.
The ReportDesigner is the main widget for designing a report.
void setModified(bool modified)
Sets the modified status, defaulting to true for modified.
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
void setZ(qreal z)
Sets the z-value for the element.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
qreal z() const
Return the z-value in points.
QString itemDataSource() const
KOSM_EXPORT const char * typeName(Type type)
QCA_EXPORT void init()
QDomElement createElement(const QString &tagName)
void setAttribute(const QString &name, const QString &value)
QDomNode firstChild() const const
QRectF boundingRect(QChar ch) const const
QGraphicsScene * scene() const const
qreal x() const const
qreal y() const const
qreal zValue() const const
QRectF rect() const const
void addItem(QGraphicsItem *item)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void drawRect(const QRect &rectangle)
void drawText(const QPoint &position, const QString &text)
const QPen & pen() const const
void setBackground(const QBrush &brush)
void setPen(Qt::PenStyle style)
bool isEmpty() const const
QByteArray toByteArray() 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.