Skip to content

Commit 77851ab

Browse files
committed
GUI: Refactor actual QR code rendering into new QRImageWidget::setQR
1 parent 37f236a commit 77851ab

File tree

2 files changed

+61
-48
lines changed

2 files changed

+61
-48
lines changed

src/qt/receiverequestdialog.cpp

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,64 @@ QRImageWidget::QRImageWidget(QWidget *parent):
3737
contextMenu->addAction(copyImageAction);
3838
}
3939

40+
bool QRImageWidget::setQR(const QString& data, const QString& text)
41+
{
42+
#ifdef USE_QRCODE
43+
setText("");
44+
if (data.isEmpty()) return false;
45+
46+
// limit length
47+
if (data.length() > MAX_URI_LENGTH) {
48+
setText(tr("Resulting URI too long, try to reduce the text for label / message."));
49+
return false;
50+
}
51+
52+
QRcode *code = QRcode_encodeString(data.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
53+
54+
if (!code) {
55+
setText(tr("Error encoding URI into QR Code."));
56+
return false;
57+
}
58+
59+
QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
60+
qrImage.fill(0xffffff);
61+
unsigned char *p = code->data;
62+
for (int y = 0; y < code->width; ++y) {
63+
for (int x = 0; x < code->width; ++x) {
64+
qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
65+
++p;
66+
}
67+
}
68+
QRcode_free(code);
69+
70+
QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE + (text.isEmpty() ? 0 : 20), QImage::Format_RGB32);
71+
qrAddrImage.fill(0xffffff);
72+
QPainter painter(&qrAddrImage);
73+
painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
74+
75+
if (!text.isEmpty()) {
76+
QFont font = GUIUtil::fixedPitchFont();
77+
QRect paddedRect = qrAddrImage.rect();
78+
79+
// calculate ideal font size
80+
qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 20, text, font);
81+
font.setPointSizeF(font_size);
82+
83+
painter.setFont(font);
84+
paddedRect.setHeight(QR_IMAGE_SIZE+12);
85+
painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, text);
86+
}
87+
88+
painter.end();
89+
setPixmap(QPixmap::fromImage(qrAddrImage));
90+
91+
return true;
92+
#else
93+
setText(tr("QR code support not available."));
94+
return false;
95+
#endif
96+
}
97+
4098
QImage QRImageWidget::exportImage()
4199
{
42100
if(!pixmap())
@@ -150,55 +208,9 @@ void ReceiveRequestDialog::update()
150208
}
151209
ui->outUri->setText(html);
152210

153-
#ifdef USE_QRCODE
154-
ui->lblQRCode->setText("");
155-
if(!uri.isEmpty())
156-
{
157-
// limit URI length
158-
if (uri.length() > MAX_URI_LENGTH)
159-
{
160-
ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
161-
} else {
162-
QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
163-
if (!code)
164-
{
165-
ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
166-
return;
167-
}
168-
QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
169-
qrImage.fill(0xffffff);
170-
unsigned char *p = code->data;
171-
for (int y = 0; y < code->width; y++)
172-
{
173-
for (int x = 0; x < code->width; x++)
174-
{
175-
qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
176-
p++;
177-
}
178-
}
179-
QRcode_free(code);
180-
181-
QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE+20, QImage::Format_RGB32);
182-
qrAddrImage.fill(0xffffff);
183-
QPainter painter(&qrAddrImage);
184-
painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
185-
QFont font = GUIUtil::fixedPitchFont();
186-
QRect paddedRect = qrAddrImage.rect();
187-
188-
// calculate ideal font size
189-
qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 20, info.address, font);
190-
font.setPointSizeF(font_size);
191-
192-
painter.setFont(font);
193-
paddedRect.setHeight(QR_IMAGE_SIZE+12);
194-
painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, info.address);
195-
painter.end();
196-
197-
ui->lblQRCode->setPixmap(QPixmap::fromImage(qrAddrImage));
198-
ui->btnSaveAs->setEnabled(true);
199-
}
211+
if (ui->lblQRCode->setQR(uri, info.address)) {
212+
ui->btnSaveAs->setEnabled(true);
200213
}
201-
#endif
202214
}
203215

204216
void ReceiveRequestDialog::on_btnCopyURI_clicked()

src/qt/receiverequestdialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class QRImageWidget : public QLabel
2929

3030
public:
3131
explicit QRImageWidget(QWidget *parent = nullptr);
32+
bool setQR(const QString& data, const QString& text = "");
3233
QImage exportImage();
3334

3435
public Q_SLOTS:

0 commit comments

Comments
 (0)