Update code from changes in private projects

This commit is contained in:
Alex Spataru
2023-10-12 22:42:07 -05:00
parent a834f5db60
commit 65f4173c27
2 changed files with 136 additions and 79 deletions
+86 -42
View File
@@ -20,60 +20,104 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "QrCodeGenerator.h"
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <QPainter> #include <QPainter>
#include <QTextStream>
#include <QSvgRenderer> #include <QSvgRenderer>
QrCodeGenerator::QrCodeGenerator(QObject *parent) : QObject(parent) {} #include "QrCodeGenerator.h"
QImage QrCodeGenerator::generateQr(const QString &data, const quint16 size, /**
const quint16 borderSize, * @brief Default constructor for the QrCodeGenerator class.
qrcodegen::QrCode::Ecc errorCorrection) { * @param parent QObject parent
QByteArray byteArray = data.toUtf8(); */
const qrcodegen::QrCode qrCode = qrcodegen::QrCode::encodeText(byteArray.constData(), QrCodeGenerator::QrCodeGenerator(QObject *parent)
errorCorrection); : QObject(parent)
return qrCodeToImage(qrCode, borderSize, size); {
// 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()) * @brief Generates a QR code image from a given text string.
throw std::overflow_error("Border too large"); * @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 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; * @brief Generates an SVG string representing a QR code.
sb << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" " * @param data The data to encode in the QR code.
"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"; * @param borderSize The size of the border around the QR code.
sb << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 "; * @param errorCorrection The level of error correction to apply.
sb << (qr.getSize() + border * 2) << " " << (qr.getSize() + border * 2) * @return QString containing the SVG representation of the QR code.
<< "\" stroke=\"none\">\n"; */
sb << "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n"; QString QrCodeGenerator::generateSvgQr(const QString &data, quint16 borderSize,
sb << "\t<path d=\""; qrcodegen::QrCode::Ecc errorCorrection)
for (int y = 0; y < qr.getSize(); y++) { {
for (int x = 0; x < qr.getSize(); x++) { auto b = data.toUtf8();
if (qr.getModule(x, y)) { const auto qrCode = qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
if (x != 0 || y != 0) return toSvgString(qrCode, borderSize);
sb << " "; }
sb << "M" << (x + border) << "," << (y + border) << "h1v1h-1z";
} /**
* @brief Converts a QR code to its SVG representation as a string.
* @param qr The QR code to convert.
* @param border The border size to use.
* @return QString containing the SVG representation of the QR code.
*/
QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border) const
{
QString str;
QTextStream sb(&str);
sb << R"(<?xml version="1.0" encoding="UTF-8"?>)"
<< R"(<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">)"
<< R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 )"
<< (qr.getSize() + border * 2) << " " << (qr.getSize() + border * 2)
<< R"(" stroke="none"><rect width="100%" height="100%" fill="#FFFFFF"/><path d=")";
for (int y = 0; y < qr.getSize(); y++)
{
for (int x = 0; x < qr.getSize(); x++)
{
if (qr.getModule(x, y))
{
sb << (x == 0 && y == 0 ? "" : " ") << "M" << (x + border) << "," << (y + border)
<< "h1v1h-1z";
}
}
} }
}
sb << "\" fill=\"#000000\"/>\n";
sb << "</svg>\n";
return QString::fromStdString(sb.str()); sb << R"(" fill="#000000"/></svg>)";
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, QImage QrCodeGenerator::qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border,
const quint16 size) const { quint16 size) const
auto svg = toSvgString(qrCode, border); {
QSvgRenderer render(svg.toUtf8()); QString svg = toSvgString(qrCode, border);
QImage image(size, size, QImage::Format_Mono); QSvgRenderer render(svg.toUtf8());
image.fill(Qt::white); QImage image(size, size, QImage::Format_Mono);
QPainter painter(&image); image.fill(Qt::white);
render.render(&painter); QPainter painter(&image);
return image; painter.setRenderHint(QPainter::Antialiasing);
render.render(&painter);
return image;
} }
+50 -37
View File
@@ -30,47 +30,60 @@
/** /**
* @class QrCodeGenerator * @class QrCodeGenerator
* @brief The QrCodeGenerator class is a simple C++ class that uses the qrcodegen library to * @brief The QrCodeGenerator class is a simple C++ class that uses the qrcodegen library
* generate QR codes from QStrings in Qt applications. * to generate QR codes from QStrings in Qt applications.
*/ */
class QrCodeGenerator : public QObject { class QrCodeGenerator : public QObject
{
public: public:
/** /**
* @brief Constructs a QrCodeGenerator object. * @brief Constructs a QrCodeGenerator object.
* @param parent The parent QObject. * @param parent The parent QObject.
*/ */
explicit QrCodeGenerator(QObject *parent = nullptr); explicit QrCodeGenerator(QObject *parent = nullptr);
/** /**
* @brief Generates a QR code from the given data and error correction level. * @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 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 size The desired width/height of the generated image (default: 500).
* @param borderSize The desired border width of the generated image (default: 1). * @param borderSize The desired border width of the generated image (default: 1).
* @param errorCorrection The desired error correction level (default: * @param errorCorrection The desired error correction level (default:
* qrcodegen::QrCode::Ecc::MEDIUM). * qrcodegen::QrCode::Ecc::MEDIUM).
* *
* @return QImage containing the generated QR code. * @return QImage containing the generated QR code.
*/ */
QImage generateQr(const QString &data, const quint16 size = 500, const quint16 borderSize = 1, QImage generateQr(const QString &data, const quint16 size = 1000, const quint16 borderSize = 1,
qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM); 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: private:
/** /**
* @brief Converts a qrcodegen::QrCode object to a SVG image. * @brief Converts a qrcodegen::QrCode object to a SVG image.
* @param qrCode The qrcodegen::QrCode object to convert. * @param qrCode The qrcodegen::QrCode object to convert.
* @param borderSize The desired border width of the generated image (default: 1). * @param border The desired border width of the generated image (default: 1).
* *
* @return QImage containing the QR code. * @return SVG containing the QR code.
*/ */
QString toSvgString(const qrcodegen::QrCode &qr, quint16 border) const; QString toSvgString(const qrcodegen::QrCode &qr, quint16 border) const;
/** /**
* @brief Converts a qrcodegen::QrCode object to a QImage. * @brief Converts a qrcodegen::QrCode object to a QImage.
* @param qrCode The qrcodegen::QrCode object to convert. * @param qrCode The qrcodegen::QrCode object to convert.
* @param size The desired width/height of the generated image. * @param size The desired width/height of the generated image.
* @param borderSize The desired border width of the generated image. * @param borderSize The desired border width of the generated image.
* *
* @return QImage containing the QR code. * @return QImage containing the QR code.
*/ */
QImage qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border, const quint16 size) const; QImage qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border, const quint16 size) const;
}; };