File indexing completed on 2024-05-12 15:49:07

0001 /*
0002     SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <sune@vuorela.dk>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef PRISON_ABSTRACTBARCODE_H
0008 #define PRISON_ABSTRACTBARCODE_H
0009 #include <QImage>
0010 #include <QSizeF>
0011 #include <QString>
0012 
0013 #include "prison_export.h"
0014 
0015 #include <memory>
0016 
0017 class QColor;
0018 
0019 namespace Prison
0020 {
0021 /**
0022  * base class for barcode generators
0023  * To add your own barcode generator, subclass this class
0024  * and reimplement toImage(const QSizeF&) to do the actual
0025  * work of generating the barcode.
0026  *
0027  * The barcode is cached in AbstractBarcode when painting and
0028  * the size and the data doesn't change. Using the same AbstractBarcode
0029  * to paint on several surfaces, if they aren't of the exact same size
0030  * will break the caching
0031  */
0032 class PRISON_EXPORT AbstractBarcode
0033 {
0034 public:
0035 #if PRISON_ENABLE_DEPRECATED_SINCE(5, 69)
0036     /**
0037      * creates a barcode generator without any data
0038      * @deprecated since 5.69 Use Prison::createBarcode instead.
0039      */
0040     PRISON_DEPRECATED_VERSION(5, 69, "Use Prison::createBarcode()")
0041     AbstractBarcode();
0042 #endif
0043 
0044     virtual ~AbstractBarcode();
0045     /**
0046      * Textual content encoded in this barcode.
0047      * This returns an empty QString if binary content is set.
0048      * @see byteArrayData()
0049      */
0050     QString data() const;
0051     /**
0052      * Binary data encoded in this barcode.
0053      * This returns an empty QByteArray if textual content is set.
0054      * @see data()
0055      * @since 5.85
0056      */
0057     QByteArray byteArrayData() const;
0058     /**
0059      * Sets textual data to be drawn as a barcode.
0060      * Only use this function if your content is textual, use the QByteArray overload
0061      * when your content contains non-textual binary content.
0062      * Calling this function does not do any repaints of anything, they are
0063      * your own responsibility.
0064      * @param data textual barcode content
0065      */
0066     void setData(const QString &data);
0067     /**
0068      * Sets binary data to be drawn as a barcode.
0069      * Prefer the QString overload if your content is purely textual, to reduce
0070      * the risk of encoding issues for non-ASCII content.
0071      * Calling this function does not do any repaints of anything, they are
0072      * your own responsibility.
0073      * @param data binary barcode content
0074      * @since 5.85
0075      */
0076     void setData(const QByteArray &data);
0077     /**
0078      * Creates a image with a barcode on
0079      * @return QImage with a barcode on, trying to match the requested \param size
0080      *
0081      * If one of the dimensions of @param size is smaller than the matching dimension in \ref minimumSize,
0082      * a null QImage will be returned
0083      */
0084     QImage toImage(const QSizeF &size);
0085 
0086 #if PRISON_ENABLE_DEPRECATED_SINCE(5, 72)
0087     /**
0088      * The minimal size of this barcode.
0089      * @note This isn't the absolute minimum, but closer to the result of preferredSize(1).
0090      * @return the minimal size for this barcode.
0091      * @deprecated Since 5.69, use preferredSize() or trueMinimumSize().
0092      */
0093     PRISON_DEPRECATED_VERSION_BELATED(5, 72, 5, 69, "Use preferredSize() or trueMinimumSize()")
0094     QSizeF minimumSize() const;
0095 #endif
0096 
0097     /**
0098      * The minimal amount of pixels needed to represent this barcode without loss of information.
0099      * That is, the size of the barcode image if each line or dot is just one pixel wide.
0100      * On normal screens that is not enough for barcode scanners to reliably detect the barcode
0101      * though.
0102      * @see preferredSize
0103      * @since 5.69
0104      */
0105     QSizeF trueMinimumSize() const; // TODO KF6: rename to minimumSize
0106 
0107     /**
0108      * The recommended size for this barcode when shown on a screen.
0109      * This is typically significantly larger than trueMinimumSize() so that
0110      * barcode scanners tend to reliably detect the code. As this depends
0111      * on the physical resolution of the output, you have to pass the device
0112      * pixel ration of the output screen here.
0113      * @param devicePixelRatio The device pixel ratio of the screen this is shown on.
0114      * @see trueMinimumSize
0115      * @since 5.69
0116      */
0117     QSizeF preferredSize(qreal devicePixelRatio) const; // TODO KF6: make virtual
0118 
0119     /**
0120      * @return the foreground color (by default black) to be used for the barcode.
0121      */
0122     const QColor &foregroundColor() const;
0123     /**
0124      * @return the background color (by default white) to be used for the barcode.
0125      */
0126     const QColor &backgroundColor() const;
0127     /**
0128      * sets the foreground color
0129      * @param foregroundcolor - the new foreground color
0130      */
0131     void setForegroundColor(const QColor &foregroundcolor);
0132     /**
0133      * sets the background color
0134      * @param backgroundcolor - the new background color
0135      */
0136     void setBackgroundColor(const QColor &backgroundcolor);
0137 
0138     /** Dimensions of the barcode.
0139      *  @since 5.69
0140      */
0141     enum Dimensions : uint8_t {
0142         NoDimensions, ///< Null barcode.
0143         OneDimension, ///< One-dimensional barcode.
0144         TwoDimensions, ///< 2D matrix code.
0145     };
0146 
0147     /** Returns the amount of dimensions of the barcode.
0148      *  @since 5.69
0149      */
0150     Dimensions dimensions() const;
0151 
0152 protected:
0153     ///@cond internal
0154     explicit AbstractBarcode(Dimensions dim);
0155     ///@endcond
0156 
0157 #if PRISON_ENABLE_DEPRECATED_SINCE(5, 69)
0158     /**
0159      * Sets the minimum size for this barcode.
0160      * Some barcodes have minimum sizes for when they are readable and such
0161      * @param minimumSize QSizeF holding the minimum size for this barcode
0162      * @deprecated since 5.69, function is a no-op, no need to call this anymore.
0163      */
0164     PRISON_DEPRECATED_VERSION(5, 69, "no need to call this anymore")
0165     void setMinimumSize(const QSizeF &minimumSize);
0166 #endif
0167 
0168     /**
0169      * Doing the actual painting of the image
0170      * @param size unused - will be removed in KF6
0171      * @return image with barcode, or null image
0172      */
0173     // TODO KF6: remove the size argument
0174     virtual QImage paintImage(const QSizeF &size) = 0;
0175 
0176 private:
0177     friend class AbstractBarcodePrivate;
0178     /**
0179      * d-pointer
0180      */
0181     std::unique_ptr<class AbstractBarcodePrivate> const d;
0182 };
0183 } // namespace
0184 
0185 #endif // PRISON_ABSTRACTBARCODE_H