KReport

KReportDesignerItemBarcode.cpp
1 /* This file is part of the KDE project
2  * Copyright (C) 2001-2007 by OpenMFG, LLC ([email protected])
3  * Copyright (C) 2007-2008 by Adam Pigg ([email protected])
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 
34 void 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)
49 KReportDesignerItemBarcode::KReportDesignerItemBarcode(KReportDesigner * rw, QGraphicsScene* scene, const QPointF &pos)
50  : KReportDesignerItemRectBase(rw, this)
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 
58 KReportDesignerItemBarcode::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 
65 KReportDesignerItemBarcode* KReportDesignerItemBarcode::clone()
66 {
67  QDomDocument d;
68  QDomElement e = d.createElement(QLatin1String("clone"));
69  QDomNode n;
70  buildXML(&d, &e);
71  n = e.firstChild();
72  return new KReportDesignerItemBarcode(n, designer(), nullptr);
73 }
74 
75 // methods (deconstructor)
76 KReportDesignerItemBarcode::~KReportDesignerItemBarcode()
77 {}
78 
79 QRectF 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 
87 void 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 
132 void 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 
152 void 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 }
QByteArray name() const
void setPen(const QColor &color)
QDomNode firstChild() const const
void addItem(QGraphicsItem *item)
void drawRect(const QRectF &rectangle)
QCA_EXPORT void init()
void setBackground(const QBrush &brush)
const QPen & pen() const const
QDomElement createElement(const QString &tagName)
void drawText(const QPointF &position, const QString &text)
void setAttribute(const QString &name, const QString &value)
QRectF boundingRect(const QString &text) const const
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
QString suggestEntityName(const QString &name) const
Return a unique name that can be used by the entity.
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QDomNode appendChild(const QDomNode &newChild)
Base class for rectangular report items used within the designer GUI.
QVariant value() const
The ReportDesigner is the main widget for designing a report.
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:17:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.