Prison

barcode.h
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#ifndef PRISON_BARCODE_H
8#define PRISON_BARCODE_H
9
10#include "prison_export.h"
11
12#include "prison.h"
13#include <qglobal.h>
14
15#include <memory>
16
17class QByteArray;
18class QColor;
19class QImage;
20class QSizeF;
21class QString;
22
23namespace Prison
24{
25
26class AbstractBarcodePrivate;
27
28/**
29 * A barcode generator for a fixed barcode format.
30 *
31 * @note This replaces Prison::createBarcode and AbstractBarcode* from KF5.
32 * You can create Barcode instances directly now and specify the format in its
33 * constructor.
34 * Rather than checking for createBarcode returning a @c nullptr, check whether
35 * the format is not Prison::Null.
36 *
37 * @since 6.0
38 */
39class PRISON_EXPORT Barcode
40{
41public:
42 Barcode(Barcode &&);
43 ~Barcode();
44 Barcode &operator=(Barcode &&);
45
46 /** Barcode format of this barcode generator. */
47 Prison::BarcodeType format() const;
48
49 /**
50 * Textual content encoded in this barcode.
51 * This returns an empty QString if binary content is set.
52 * @see byteArrayData()
53 */
54 QString data() const;
55 /**
56 * Binary data encoded in this barcode.
57 * This returns an empty QByteArray if textual content is set.
58 * @see data()
59 * @since 5.85
60 */
61 QByteArray byteArrayData() const;
62 /**
63 * Sets textual data to be drawn as a barcode.
64 * Only use this function if your content is textual, use the QByteArray overload
65 * when your content contains non-textual binary content.
66 * Calling this function does not do any repaints of anything, they are
67 * your own responsibility.
68 * @param data textual barcode content
69 */
70 void setData(const QString &data);
71 /**
72 * Sets binary data to be drawn as a barcode.
73 * Prefer the QString overload if your content is purely textual, to reduce
74 * the risk of encoding issues for non-ASCII content.
75 * Calling this function does not do any repaints of anything, they are
76 * your own responsibility.
77 * @param data binary barcode content
78 * @since 5.85
79 */
80 void setData(const QByteArray &data);
81 /**
82 * Creates a image with a barcode on
83 * @return QImage with a barcode on, trying to match the requested \param size
84 *
85 * If one of the dimensions of @param size is smaller than the matching dimension in \ref minimumSize,
86 * a null QImage will be returned
87 */
88 QImage toImage(const QSizeF &size);
89
90 /**
91 * The minimal amount of pixels needed to represent this barcode without loss of information.
92 * That is, the size of the barcode image if each line or dot is just one pixel wide.
93 * On normal screens that is not enough for barcode scanners to reliably detect the barcode
94 * though.
95 * @see preferredSize
96 */
97 QSizeF minimumSize() const;
98
99 /**
100 * The recommended size for this barcode when shown on a screen.
101 * This is typically significantly larger than trueMinimumSize() so that
102 * barcode scanners tend to reliably detect the code. As this depends
103 * on the physical resolution of the output, you have to pass the device
104 * pixel ration of the output screen here.
105 * @param devicePixelRatio The device pixel ratio of the screen this is shown on.
106 * @see trueMinimumSize
107 * @since 5.69
108 */
109 QSizeF preferredSize(qreal devicePixelRatio) const;
110
111 /**
112 * @return the foreground color (by default black) to be used for the barcode.
113 */
114 QColor foregroundColor() const;
115 /**
116 * @return the background color (by default white) to be used for the barcode.
117 */
118 QColor backgroundColor() const;
119 /**
120 * sets the foreground color
121 * @param foregroundcolor - the new foreground color
122 */
123 void setForegroundColor(const QColor &foregroundcolor);
124 /**
125 * sets the background color
126 * @param backgroundcolor - the new background color
127 */
128 void setBackgroundColor(const QColor &backgroundcolor);
129
130 /** Dimensions of the barcode. */
131 enum Dimensions : uint8_t {
132 NoDimensions, ///< Null barcode.
133 OneDimension, ///< One-dimensional barcode.
134 TwoDimensions, ///< 2D matrix code.
135 };
136
137 /** Returns the amount of dimensions of the barcode. */
138 Dimensions dimensions() const;
139
140 /** Create a new barcode generator.
141 *
142 * If a format is requested that is not supported by the current build
143 * due to missing/disabled optional dependencies, Barcode::format() will
144 * return Prison::Null.
145 */
146 static std::optional<Prison::Barcode> create(Prison::BarcodeType type);
147
148private:
149 friend class AbstractBarcodePrivate;
150 explicit Barcode(std::unique_ptr<AbstractBarcodePrivate> &&d);
151 std::unique_ptr<class AbstractBarcodePrivate> d;
152};
153}
154
155#endif
A barcode generator for a fixed barcode format.
Definition barcode.h:40
Dimensions
Dimensions of the barcode.
Definition barcode.h:131
@ NoDimensions
Null barcode.
Definition barcode.h:132
@ TwoDimensions
2D matrix code.
Definition barcode.h:134
@ OneDimension
One-dimensional barcode.
Definition barcode.h:133
Provides classes and methods for generating barcodes.
Definition barcode.h:24
BarcodeType
possible supported barcode types
Definition prison.h:22
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.