From 65f4173c27f4619b5d6811894a59ccb5b11cb38f Mon Sep 17 00:00:00 2001 From: Alex Spataru Date: Thu, 12 Oct 2023 22:42:07 -0500 Subject: [PATCH] Update code from changes in private projects --- src/QrCodeGenerator.cpp | 128 +++++++++++++++++++++++++++------------- src/QrCodeGenerator.h | 87 +++++++++++++++------------ 2 files changed, 136 insertions(+), 79 deletions(-) diff --git a/src/QrCodeGenerator.cpp b/src/QrCodeGenerator.cpp index 4b4e124..8bf4ca3 100644 --- a/src/QrCodeGenerator.cpp +++ b/src/QrCodeGenerator.cpp @@ -20,60 +20,104 @@ * THE SOFTWARE. */ -#include "QrCodeGenerator.h" - #include #include - #include +#include #include -QrCodeGenerator::QrCodeGenerator(QObject *parent) : QObject(parent) {} +#include "QrCodeGenerator.h" -QImage QrCodeGenerator::generateQr(const QString &data, const quint16 size, - const quint16 borderSize, - qrcodegen::QrCode::Ecc errorCorrection) { - QByteArray byteArray = data.toUtf8(); - const qrcodegen::QrCode qrCode = qrcodegen::QrCode::encodeText(byteArray.constData(), - errorCorrection); - return qrCodeToImage(qrCode, borderSize, size); +/** + * @brief Default constructor for the QrCodeGenerator class. + * @param parent QObject parent + */ +QrCodeGenerator::QrCodeGenerator(QObject *parent) + : QObject(parent) +{ + // No implementation needed for default constructor. } -QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border) const { - if (border > INT_MAX / 2 || border * 2 > INT_MAX - qr.getSize()) - throw std::overflow_error("Border too large"); +/** + * @brief Generates a QR code image from a given text string. + * @param data The data to encode in the QR code. + * @param size The image size for the generated QR code. + * @param borderSize The size of the border around the QR code. + * @param errorCorrection The level of error correction to apply. + * @return QImage representing the generated QR code. + */ +QImage QrCodeGenerator::generateQr(const QString &data, quint16 size, quint16 borderSize, + qrcodegen::QrCode::Ecc errorCorrection) +{ + auto b = data.toUtf8(); + const auto qrCode = qrcodegen::QrCode::encodeText(b.constData(), errorCorrection); + return qrCodeToImage(qrCode, borderSize, size); +} - std::ostringstream sb; - sb << "\n"; - sb << "\n"; - sb << "\n"; - sb << "\t\n"; - sb << "\t)" + << R"()" + << R"(\n"; - sb << "\n"; - return QString::fromStdString(sb.str()); + sb << R"(" fill="#000000"/>)"; + return str; } +/** + * @brief Converts a QR code to a QImage. + * @param qrCode The QR code to convert. + * @param border The border size to use. + * @param size The image size to generate. + * @return QImage representing the QR code. + */ QImage QrCodeGenerator::qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border, - const quint16 size) const { - auto svg = toSvgString(qrCode, border); - QSvgRenderer render(svg.toUtf8()); - QImage image(size, size, QImage::Format_Mono); - image.fill(Qt::white); - QPainter painter(&image); - render.render(&painter); - return image; + quint16 size) const +{ + QString svg = toSvgString(qrCode, border); + QSvgRenderer render(svg.toUtf8()); + QImage image(size, size, QImage::Format_Mono); + image.fill(Qt::white); + QPainter painter(&image); + painter.setRenderHint(QPainter::Antialiasing); + render.render(&painter); + return image; } diff --git a/src/QrCodeGenerator.h b/src/QrCodeGenerator.h index ac57900..694d60b 100644 --- a/src/QrCodeGenerator.h +++ b/src/QrCodeGenerator.h @@ -30,47 +30,60 @@ /** * @class QrCodeGenerator - * @brief The QrCodeGenerator class is a simple C++ class that uses the qrcodegen library to - * generate QR codes from QStrings in Qt applications. + * @brief The QrCodeGenerator class is a simple C++ class that uses the qrcodegen library + * to generate QR codes from QStrings in Qt applications. */ -class QrCodeGenerator : public QObject { +class QrCodeGenerator : public QObject +{ public: - /** - * @brief Constructs a QrCodeGenerator object. - * @param parent The parent QObject. - */ - explicit QrCodeGenerator(QObject *parent = nullptr); + /** + * @brief Constructs a QrCodeGenerator object. + * @param parent The parent QObject. + */ + explicit QrCodeGenerator(QObject *parent = nullptr); - /** - * @brief Generates a QR code from the given data and error correction level. - * @param data The QString containing the data to encode in the QR code. - * @param size The desired width/height of the generated image (default: 500). - * @param borderSize The desired border width of the generated image (default: 1). - * @param errorCorrection The desired error correction level (default: - * qrcodegen::QrCode::Ecc::MEDIUM). - * - * @return QImage containing the generated QR code. - */ - QImage generateQr(const QString &data, const quint16 size = 500, const quint16 borderSize = 1, - qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM); + /** + * @brief Generates a QR code from the given data and error correction level. + * @param data The QString containing the data to encode in the QR code. + * @param size The desired width/height of the generated image (default: 500). + * @param borderSize The desired border width of the generated image (default: 1). + * @param errorCorrection The desired error correction level (default: + * qrcodegen::QrCode::Ecc::MEDIUM). + * + * @return QImage containing the generated QR code. + */ + QImage generateQr(const QString &data, const quint16 size = 1000, const quint16 borderSize = 1, + qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM); + + /** + * @brief Generates a QR code from the given data and error correction level. + * @param data The QString containing the data to encode in the QR code. + * @param borderSize The desired border width of the generated image (default: 1). + * @param errorCorrection The desired error correction level (default: + * qrcodegen::QrCode::Ecc::MEDIUM). + * + * @return QString string containing the generated QR code in SVG format. + */ + QString generateSvgQr(const QString &data, const quint16 borderSize = 1, + qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM); private: - /** - * @brief Converts a qrcodegen::QrCode object to a SVG image. - * @param qrCode The qrcodegen::QrCode object to convert. - * @param borderSize The desired border width of the generated image (default: 1). - * - * @return QImage containing the QR code. - */ - QString toSvgString(const qrcodegen::QrCode &qr, quint16 border) const; + /** + * @brief Converts a qrcodegen::QrCode object to a SVG image. + * @param qrCode The qrcodegen::QrCode object to convert. + * @param border The desired border width of the generated image (default: 1). + * + * @return SVG containing the QR code. + */ + QString toSvgString(const qrcodegen::QrCode &qr, quint16 border) const; - /** - * @brief Converts a qrcodegen::QrCode object to a QImage. - * @param qrCode The qrcodegen::QrCode object to convert. - * @param size The desired width/height of the generated image. - * @param borderSize The desired border width of the generated image. - * - * @return QImage containing the QR code. - */ - QImage qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border, const quint16 size) const; + /** + * @brief Converts a qrcodegen::QrCode object to a QImage. + * @param qrCode The qrcodegen::QrCode object to convert. + * @param size The desired width/height of the generated image. + * @param borderSize The desired border width of the generated image. + * + * @return QImage containing the QR code. + */ + QImage qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border, const quint16 size) const; };