From 7fb5b5b3a5403687d843d80914f6904c174dc4d3 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 15 Jan 2018 23:33:19 +0100 Subject: [PATCH] Merge #12173: [Qt] Use flexible font size for QRCode image address 59f9e2a Use flexible font size for QRCode image address (Jonas Schnelli) Pull request description: Bech32 addresses are currently cut off in the QRCode image in the GUI receive tab. This adds a simple font size calculation logic that "must" (down to 4pt) fix into the given image width. Examples OSX HiDPI: bildschirmfoto 2018-01-12 um 11 25 40 bildschirmfoto 2018-01-12 um 11 25 46 Examples Ubuntu non HIDPI: bildschirmfoto 2018-01-12 um 11 27 51 bildschirmfoto 2018-01-12 um 11 27 42 Tree-SHA512: d749763fb748b146f77fd8d88fb7d29b07a46cde0b0f303a4006ae9cc3521b3c2e8ab43b828e243514109379898b198552e17b8f316c5a869b0cc8246b054b86 --- src/qt/guiutil.cpp | 12 ++++++++++++ src/qt/guiutil.h | 2 ++ src/qt/receiverequestdialog.cpp | 8 ++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index 2959576004..fbdede2948 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -1095,6 +1095,18 @@ QString formatBytes(uint64_t bytes) return QString(QObject::tr("%1 GB")).arg(bytes / 1024 / 1024 / 1024); } +qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize, qreal font_size) { + while(font_size >= minPointSize) { + font.setPointSizeF(font_size); + QFontMetrics fm(font); + if (fm.width(text) < width) { + break; + } + font_size -= 0.5; + } + return font_size; +} + void ClickableLabel::mouseReleaseEvent(QMouseEvent *event) { Q_EMIT clicked(event->pos()); diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h index d87e6f4a47..91ec08cca1 100644 --- a/src/qt/guiutil.h +++ b/src/qt/guiutil.h @@ -251,6 +251,8 @@ namespace GUIUtil QString formatBytes(uint64_t bytes); + qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14); + class ClickableLabel : public QLabel { Q_OBJECT diff --git a/src/qt/receiverequestdialog.cpp b/src/qt/receiverequestdialog.cpp index 2bf87330a6..b63eccf29e 100644 --- a/src/qt/receiverequestdialog.cpp +++ b/src/qt/receiverequestdialog.cpp @@ -183,9 +183,13 @@ void ReceiveRequestDialog::update() QPainter painter(&qrAddrImage); painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE)); QFont font = GUIUtil::fixedPitchFont(); - font.setPixelSize(12); - painter.setFont(font); QRect paddedRect = qrAddrImage.rect(); + + // 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();