1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-11-16 20:19:13 +01:00
doorlockd-mirror/doorlockd/client/qrwidget.cpp

64 lines
1.3 KiB
C++
Raw Normal View History

2015-09-25 13:11:36 +02:00
#include <exception>
#include <memory>
#include <QPainter>
#include <QDebug>
#include <qrencode.h>
#include "qrwidget.h"
QRWidget::QRWidget(QWidget* parent) :
QWidget(parent),
_data(" ")
{
}
void QRWidget::setQRData(const std::string &data)
2015-09-25 13:11:36 +02:00
{
2015-09-28 17:03:03 +02:00
_data = data;
2015-09-25 13:11:36 +02:00
update();
}
void QRWidget::paintEvent(QPaintEvent*)
{
QPainter painter(this);
std::unique_ptr<QRcode, void(*)(QRcode*)> qr(
QRcode_encodeString(_data.c_str(), 1, QR_ECLEVEL_L, QR_MODE_8, 1),
2015-09-25 13:11:36 +02:00
[] (QRcode* ptr) {
if (ptr)
QRcode_free(ptr);
});
if (!qr)
throw std::runtime_error("Error generating QR Code");
QColor fg("black");
QColor bg("white");
painter.setBrush(bg);
painter.setPen(Qt::NoPen);
painter.drawRect(0,0,width(),height());
painter.setBrush(fg);
const int s=qr->width>0?qr->width:1;
const double w=width();
const double h=height();
const double aspect=w/h;
const double scale=((aspect>1.0)?h:w)/s;
for(int y=0;y<s;y++) {
const int yy=y*s;
for(int x=0;x<s;x++) {
const int xx=yy+x;
const unsigned char b=qr->data[xx];
if(b &0x01) {
const double rx1=x*scale, ry1=y*scale;
QRectF r(rx1, ry1, scale, scale);
painter.drawRects(&r,1);
}
}
}
}