Prison

abstractbarcode.h
1 /*
2  SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <[email protected]>
3 
4  SPDX-License-Identifier: MIT
5 */
6 
7 #ifndef PRISON_ABSTRACTBARCODE_H
8 #define PRISON_ABSTRACTBARCODE_H
9 #include <QImage>
10 #include <QSizeF>
11 #include <QString>
12 
13 #include "prison_export.h"
14 
15 #include <memory>
16 
17 class QColor;
18 
19 namespace Prison
20 {
21 /**
22  * base class for barcode generators
23  * To add your own barcode generator, subclass this class
24  * and reimplement toImage(const QSizeF&) to do the actual
25  * work of generating the barcode.
26  *
27  * The barcode is cached in AbstractBarcode when painting and
28  * the size and the data doesn't change. Using the same AbstractBarcode
29  * to paint on several surfaces, if they aren't of the exact same size
30  * will break the caching
31  */
32 class PRISON_EXPORT AbstractBarcode
33 {
34 public:
35 #if PRISON_ENABLE_DEPRECATED_SINCE(5, 69)
36  /**
37  * creates a barcode generator without any data
38  * @deprecated since 5.69 Use Prison::createBarcode instead.
39  */
40  PRISON_DEPRECATED_VERSION(5, 69, "Use Prison::createBarcode()")
42 #endif
43 
44  virtual ~AbstractBarcode();
45  /**
46  * Textual content encoded in this barcode.
47  * This returns an empty QString if binary content is set.
48  * @see byteArrayData()
49  */
50  QString data() const;
51  /**
52  * Binary data encoded in this barcode.
53  * This returns an empty QByteArray if textual content is set.
54  * @see data()
55  * @since 5.85
56  */
57  QByteArray byteArrayData() const;
58  /**
59  * Sets textual data to be drawn as a barcode.
60  * Only use this function if your content is textual, use the QByteArray overload
61  * when your content contains non-textual binary content.
62  * Calling this function does not do any repaints of anything, they are
63  * your own responsibility.
64  * @param data textual barcode content
65  */
66  void setData(const QString &data);
67  /**
68  * Sets binary data to be drawn as a barcode.
69  * Prefer the QString overload if your content is purely textual, to reduce
70  * the risk of encoding issues for non-ASCII content.
71  * Calling this function does not do any repaints of anything, they are
72  * your own responsibility.
73  * @param data binary barcode content
74  * @since 5.85
75  */
76  void setData(const QByteArray &data);
77  /**
78  * Creates a image with a barcode on
79  * @return QImage with a barcode on, trying to match the requested \param size
80  *
81  * If one of the dimensions of @param size is smaller than the matching dimension in \ref minimumSize,
82  * a null QImage will be returned
83  */
84  QImage toImage(const QSizeF &size);
85 
86 #if PRISON_ENABLE_DEPRECATED_SINCE(5, 72)
87  /**
88  * The minimal size of this barcode.
89  * @note This isn't the absolute minimum, but closer to the result of preferredSize(1).
90  * @return the minimal size for this barcode.
91  * @deprecated Since 5.69, use preferredSize() or trueMinimumSize().
92  */
93  PRISON_DEPRECATED_VERSION_BELATED(5, 72, 5, 69, "Use preferredSize() or trueMinimumSize()")
94  QSizeF minimumSize() const;
95 #endif
96 
97  /**
98  * The minimal amount of pixels needed to represent this barcode without loss of information.
99  * That is, the size of the barcode image if each line or dot is just one pixel wide.
100  * On normal screens that is not enough for barcode scanners to reliably detect the barcode
101  * though.
102  * @see preferredSize
103  * @since 5.69
104  */
105  QSizeF trueMinimumSize() const; // TODO KF6: rename to minimumSize
106 
107  /**
108  * The recommended size for this barcode when shown on a screen.
109  * This is typically significantly larger than trueMinimumSize() so that
110  * barcode scanners tend to reliably detect the code. As this depends
111  * on the physical resolution of the output, you have to pass the device
112  * pixel ration of the output screen here.
113  * @param devicePixelRatio The device pixel ratio of the screen this is shown on.
114  * @see trueMinimumSize
115  * @since 5.69
116  */
117  QSizeF preferredSize(qreal devicePixelRatio) const; // TODO KF6: make virtual
118 
119  /**
120  * @return the foreground color (by default black) to be used for the barcode.
121  */
122  const QColor &foregroundColor() const;
123  /**
124  * @return the background color (by default white) to be used for the barcode.
125  */
126  const QColor &backgroundColor() const;
127  /**
128  * sets the foreground color
129  * @param foregroundcolor - the new foreground color
130  */
131  void setForegroundColor(const QColor &foregroundcolor);
132  /**
133  * sets the background color
134  * @param backgroundcolor - the new background color
135  */
136  void setBackgroundColor(const QColor &backgroundcolor);
137 
138  /** Dimensions of the barcode.
139  * @since 5.69
140  */
141  enum Dimensions : uint8_t {
142  NoDimensions, ///< Null barcode.
143  OneDimension, ///< One-dimensional barcode.
144  TwoDimensions, ///< 2D matrix code.
145  };
146 
147  /** Returns the amount of dimensions of the barcode.
148  * @since 5.69
149  */
150  Dimensions dimensions() const;
151 
152 protected:
153  ///@cond internal
154  explicit AbstractBarcode(Dimensions dim);
155  ///@endcond
156 
157 #if PRISON_ENABLE_DEPRECATED_SINCE(5, 69)
158  /**
159  * Sets the minimum size for this barcode.
160  * Some barcodes have minimum sizes for when they are readable and such
161  * @param minimumSize QSizeF holding the minimum size for this barcode
162  * @deprecated since 5.69, function is a no-op, no need to call this anymore.
163  */
164  PRISON_DEPRECATED_VERSION(5, 69, "no need to call this anymore")
165  void setMinimumSize(const QSizeF &minimumSize);
166 #endif
167 
168  /**
169  * Doing the actual painting of the image
170  * @param size unused - will be removed in KF6
171  * @return image with barcode, or null image
172  */
173  // TODO KF6: remove the size argument
174  virtual QImage paintImage(const QSizeF &size) = 0;
175 
176 private:
177  friend class AbstractBarcodePrivate;
178  /**
179  * d-pointer
180  */
181  std::unique_ptr<class AbstractBarcodePrivate> const d;
182 };
183 } // namespace
184 
185 #endif // PRISON_ABSTRACTBARCODE_H
Dimensions
Dimensions of the barcode.
@ OneDimension
One-dimensional barcode.
@ NoDimensions
Null barcode.
@ TwoDimensions
2D matrix code.
base class for barcode generators To add your own barcode generator, subclass this class and reimplem...
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 03:56:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.