Skip to content

Commit

Permalink
GUI: Receive: Split up long addresses across multiple lines, in QR co…
Browse files Browse the repository at this point in the history
…de image
  • Loading branch information
luke-jr authored and instagibbs committed Apr 10, 2019
1 parent 25600f9 commit 273aa76
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/qt/receiverequestdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,29 @@ void ReceiveRequestDialog::update()
}
QRcode_free(code);

QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE+20, QImage::Format_RGB32);
QFont font = GUIUtil::fixedPitchFont();
font.setPixelSize(12);
QFontMetrics fm(font);
const int lines = (fm.width(info.address) / QR_IMAGE_SIZE) + 1;
QString split_address = info.address;
const int chars_per_line = (info.address.length() + lines - 1) / lines;
for (int i = 0; i < lines; ++i) {
split_address.insert((chars_per_line * i) + i, '\n');
}

QImage qrAddrImage = QImage(QR_IMAGE_SIZE, QR_IMAGE_SIZE + 16 + fm.height(), QImage::Format_RGB32);
qrAddrImage.fill(0xffffff);
QPainter painter(&qrAddrImage);
painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
QFont font = GUIUtil::fixedPitchFont();
QRect paddedRect = qrAddrImage.rect();
paddedRect.setHeight(QR_IMAGE_SIZE + 8 + fm.height());
painter.drawText(paddedRect, Qt::AlignBottom | Qt::AlignCenter | Qt::TextWordWrap, split_address);

// calculate ideal font size
qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 20, info.address, font);
font.setPointSizeF(font_size);

painter.setFont(font);
paddedRect.setHeight(QR_IMAGE_SIZE+12);
painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, info.address);
painter.end();

ui->lblQRCode->setPixmap(QPixmap::fromImage(qrAddrImage));
Expand Down

0 comments on commit 273aa76

Please sign in to comment.