File indexing completed on 2024-05-12 04:01:31

0001 /*
0002     SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <sune@vuorela.dk>
0003     SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef PRISON_BARCODE_H
0008 #define PRISON_BARCODE_H
0009 
0010 #include "prison_export.h"
0011 
0012 #include "prison.h"
0013 #include <qglobal.h>
0014 
0015 #include <memory>
0016 
0017 class QByteArray;
0018 class QColor;
0019 class QImage;
0020 class QSizeF;
0021 class QString;
0022 
0023 namespace Prison
0024 {
0025 
0026 class AbstractBarcodePrivate;
0027 
0028 /**
0029  * A barcode generator for a fixed barcode format.
0030  *
0031  * @note This replaces Prison::createBarcode and AbstractBarcode* from KF5.
0032  * You can create Barcode instances directly now and specify the format in its
0033  * constructor.
0034  * Rather than checking for createBarcode returning a @c nullptr, check whether
0035  * the format is not Prison::Null.
0036  *
0037  * @since 6.0
0038  */
0039 class PRISON_EXPORT Barcode
0040 {
0041 public:
0042     Barcode(Barcode &&);
0043     ~Barcode();
0044     Barcode &operator=(Barcode &&);
0045 
0046     /** Barcode format of this barcode generator. */
0047     Prison::BarcodeType format() const;
0048 
0049     /**
0050      * Textual content encoded in this barcode.
0051      * This returns an empty QString if binary content is set.
0052      * @see byteArrayData()
0053      */
0054     QString data() const;
0055     /**
0056      * Binary data encoded in this barcode.
0057      * This returns an empty QByteArray if textual content is set.
0058      * @see data()
0059      * @since 5.85
0060      */
0061     QByteArray byteArrayData() const;
0062     /**
0063      * Sets textual data to be drawn as a barcode.
0064      * Only use this function if your content is textual, use the QByteArray overload
0065      * when your content contains non-textual binary content.
0066      * Calling this function does not do any repaints of anything, they are
0067      * your own responsibility.
0068      * @param data textual barcode content
0069      */
0070     void setData(const QString &data);
0071     /**
0072      * Sets binary data to be drawn as a barcode.
0073      * Prefer the QString overload if your content is purely textual, to reduce
0074      * the risk of encoding issues for non-ASCII content.
0075      * Calling this function does not do any repaints of anything, they are
0076      * your own responsibility.
0077      * @param data binary barcode content
0078      * @since 5.85
0079      */
0080     void setData(const QByteArray &data);
0081     /**
0082      * Creates a image with a barcode on
0083      * @return QImage with a barcode on, trying to match the requested \param size
0084      *
0085      * If one of the dimensions of @param size is smaller than the matching dimension in \ref minimumSize,
0086      * a null QImage will be returned
0087      */
0088     QImage toImage(const QSizeF &size);
0089 
0090     /**
0091      * The minimal amount of pixels needed to represent this barcode without loss of information.
0092      * That is, the size of the barcode image if each line or dot is just one pixel wide.
0093      * On normal screens that is not enough for barcode scanners to reliably detect the barcode
0094      * though.
0095      * @see preferredSize
0096      */
0097     QSizeF minimumSize() const;
0098 
0099     /**
0100      * The recommended size for this barcode when shown on a screen.
0101      * This is typically significantly larger than trueMinimumSize() so that
0102      * barcode scanners tend to reliably detect the code. As this depends
0103      * on the physical resolution of the output, you have to pass the device
0104      * pixel ration of the output screen here.
0105      * @param devicePixelRatio The device pixel ratio of the screen this is shown on.
0106      * @see trueMinimumSize
0107      * @since 5.69
0108      */
0109     QSizeF preferredSize(qreal devicePixelRatio) const;
0110 
0111     /**
0112      * @return the foreground color (by default black) to be used for the barcode.
0113      */
0114     QColor foregroundColor() const;
0115     /**
0116      * @return the background color (by default white) to be used for the barcode.
0117      */
0118     QColor backgroundColor() const;
0119     /**
0120      * sets the foreground color
0121      * @param foregroundcolor - the new foreground color
0122      */
0123     void setForegroundColor(const QColor &foregroundcolor);
0124     /**
0125      * sets the background color
0126      * @param backgroundcolor - the new background color
0127      */
0128     void setBackgroundColor(const QColor &backgroundcolor);
0129 
0130     /** Dimensions of the barcode. */
0131     enum Dimensions : uint8_t {
0132         NoDimensions, ///< Null barcode.
0133         OneDimension, ///< One-dimensional barcode.
0134         TwoDimensions, ///< 2D matrix code.
0135     };
0136 
0137     /** Returns the amount of dimensions of the barcode. */
0138     Dimensions dimensions() const;
0139 
0140     /** Create a new barcode generator.
0141      *
0142      *  If a format is requested that is not supported by the current build
0143      *  due to missing/disabled optional dependencies, Barcode::format() will
0144      *  return Prison::Null.
0145      */
0146     static std::optional<Prison::Barcode> create(Prison::BarcodeType type);
0147 
0148 private:
0149     friend class AbstractBarcodePrivate;
0150     explicit Barcode(std::unique_ptr<AbstractBarcodePrivate> &&d);
0151     std::unique_ptr<class AbstractBarcodePrivate> d;
0152 };
0153 }
0154 
0155 #endif