Prison

barcode.cpp
1/*
2 SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <sune@vuorela.dk>
3 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
4 SPDX-License-Identifier: MIT
5*/
6
7#include <config-prison.h>
8
9#include "barcode.h"
10
11#include "abstractbarcode_p.h"
12#include "aztecbarcode_p.h"
13#include "code128barcode_p.h"
14#include "code39barcode_p.h"
15#include "code93barcode_p.h"
16#include "datamatrixbarcode_p.h"
17#include "pdf417barcode_p.h"
18#include "qrcodebarcode_p.h"
19#if HAVE_ZXING
20#include "zxingonedbarcode_p.h"
21#endif
22
23#include <QColor>
24#include <QPainter>
25#include <QVariant>
26
27using namespace Prison;
28
29std::optional<Barcode> Barcode::create(Prison::BarcodeType format)
30{
31 std::unique_ptr<AbstractBarcodePrivate> d;
32 switch (format) {
33 case Prison::QRCode:
34 d = std::make_unique<QRCodeBarcode>();
35 break;
37#if HAVE_DMTX
38 d = std::make_unique<DataMatrixBarcode>();
39#endif
40 break;
41 case Prison::Aztec:
42 d = std::make_unique<AztecBarcode>();
43 break;
44 case Prison::Code39:
45 d = std::make_unique<Code39Barcode>();
46 break;
47 case Prison::Code93:
48 d = std::make_unique<Code93Barcode>();
49 break;
50 case Prison::Code128:
51 d = std::make_unique<Code128Barcode>();
52 break;
53 case Prison::PDF417:
54#if HAVE_ZXING
55 d = std::make_unique<Pdf417Barcode>();
56#endif
57 break;
58 case Prison::EAN13:
59#if HAVE_ZXING
60 d = std::make_unique<ZXingOneDBarcode<ZXing::BarcodeFormat::EAN13>>();
61#endif
62 break;
63 }
64
65 if (d) {
66 d->m_format = format;
67 return Barcode(std::move(d));
68 }
69 return std::nullopt;
70}
71
72Barcode::Barcode(std::unique_ptr<AbstractBarcodePrivate> &&dd)
73 : d(std::move(dd))
74{
75}
76
77Barcode::Barcode(Barcode &&) = default;
78Barcode::~Barcode() = default;
79Barcode &Barcode::operator=(Barcode &&) = default;
80
82{
83 return d->m_format;
84}
85
87{
88 return d->m_data.userType() == QMetaType::QString ? d->m_data.toString() : QString();
89}
90
92{
93 return d->m_data.userType() == QMetaType::QByteArray ? d->m_data.toByteArray() : QByteArray();
94}
95
97{
98 d->recompute();
99 if (d->m_cache.isNull() || d->sizeTooSmall(size)) {
100 return QImage();
101 }
102
103 // scale to the requested size, using only full integer factors to keep the code readable
104 int scaleX = std::max<int>(1, size.width() / d->m_cache.width());
105 int scaleY = std::max<int>(1, size.height() / d->m_cache.height());
106 if (dimensions() == TwoDimensions) {
107 scaleX = scaleY = std::min(scaleX, scaleY);
108 }
109
110 QImage out(d->m_cache.width() * scaleX, d->m_cache.height() * scaleY, d->m_cache.format());
111 QPainter p(&out);
113 p.drawImage(out.rect(), d->m_cache, d->m_cache.rect());
114 return out;
115}
116
117void Barcode::setData(const QString &data)
118{
119 if (d) {
120 d->m_data = data;
121 d->m_cache = QImage();
122 }
123}
124
126{
127 d->m_data = data;
128 d->m_cache = QImage();
129}
130
132{
133 d->recompute();
134 return d->m_cache.size();
135}
136
137QSizeF Barcode::preferredSize(qreal devicePixelRatio) const
138{
139 d->recompute();
140 return d->preferredSize(devicePixelRatio);
141}
142
144{
145 return d->m_background;
146}
147
149{
150 return d->m_foreground;
151}
152
153void Barcode::setBackgroundColor(const QColor &backgroundcolor)
154{
155 if (backgroundcolor != backgroundColor()) {
156 d->m_background = backgroundcolor;
157 d->m_cache = QImage();
158 }
159}
160
161void Barcode::setForegroundColor(const QColor &foregroundcolor)
162{
163 if (foregroundcolor != foregroundColor()) {
164 d->m_foreground = foregroundcolor;
165 d->m_cache = QImage();
166 }
167}
168
170{
171 return d->m_dimension;
172}
A barcode generator for a fixed barcode format.
Definition barcode.h:40
void setData(const QString &data)
Sets textual data to be drawn as a barcode.
Definition barcode.cpp:117
QSizeF preferredSize(qreal devicePixelRatio) const
The recommended size for this barcode when shown on a screen.
Definition barcode.cpp:137
QImage toImage(const QSizeF &size)
Creates a image with a barcode on.
Definition barcode.cpp:96
QColor backgroundColor() const
Definition barcode.cpp:143
QSizeF minimumSize() const
The minimal amount of pixels needed to represent this barcode without loss of information.
Definition barcode.cpp:131
QByteArray byteArrayData() const
Binary data encoded in this barcode.
Definition barcode.cpp:91
QColor foregroundColor() const
Definition barcode.cpp:148
Prison::BarcodeType format() const
Barcode format of this barcode generator.
Definition barcode.cpp:81
static std::optional< Prison::Barcode > create(Prison::BarcodeType type)
Create a new barcode generator.
Definition barcode.cpp:29
Dimensions dimensions() const
Returns the amount of dimensions of the barcode.
Definition barcode.cpp:169
void setBackgroundColor(const QColor &backgroundcolor)
sets the background color
Definition barcode.cpp:153
QString data() const
Textual content encoded in this barcode.
Definition barcode.cpp:86
Dimensions
Dimensions of the barcode.
Definition barcode.h:131
@ TwoDimensions
2D matrix code.
Definition barcode.h:134
void setForegroundColor(const QColor &foregroundcolor)
sets the foreground color
Definition barcode.cpp:161
Provides classes and methods for generating barcodes.
Definition barcode.h:24
BarcodeType
possible supported barcode types
Definition prison.h:22
@ PDF417
PDF417 barcode.
Definition prison.h:36
@ QRCode
QRCode 2d barcode.
Definition prison.h:24
@ Code39
Code39 barcode.
Definition prison.h:30
@ EAN13
EAN13 barcode.
Definition prison.h:38
@ DataMatrix
DataMatrix 2d barcode.
Definition prison.h:26
@ Aztec
Aztec 2d barcode.
Definition prison.h:28
@ Code128
Code 128 barcode.
Definition prison.h:34
@ Code93
Code93 barcode.
Definition prison.h:32
QRect rect() const const
SmoothPixmapTransform
void drawImage(const QPoint &point, const QImage &image)
void setRenderHint(RenderHint hint, bool on)
qreal height() const const
qreal width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:46:53 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.