Skip to content

Commit bbc91b7

Browse files
committed
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: <img width="332" alt="bildschirmfoto 2018-01-12 um 11 25 40" src="https://user-images.githubusercontent.com/178464/34896144-c0c65d76-f78c-11e7-93e1-94dc8e203269.png"> <img width="322" alt="bildschirmfoto 2018-01-12 um 11 25 46" src="https://user-images.githubusercontent.com/178464/34896145-c0edfe1c-f78c-11e7-8c09-c15155e2160e.png"> Examples Ubuntu non HIDPI: <img width="314" alt="bildschirmfoto 2018-01-12 um 11 27 51" src="https://user-images.githubusercontent.com/178464/34896151-c88347f4-f78c-11e7-8a03-df8049dcfed6.png"> <img width="322" alt="bildschirmfoto 2018-01-12 um 11 27 42" src="https://user-images.githubusercontent.com/178464/34896152-c8bb881c-f78c-11e7-89d2-6f04ec608a19.png"> Tree-SHA512: d749763fb748b146f77fd8d88fb7d29b07a46cde0b0f303a4006ae9cc3521b3c2e8ab43b828e243514109379898b198552e17b8f316c5a869b0cc8246b054b86
2 parents 44080a9 + 59f9e2a commit bbc91b7

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/qt/guiutil.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,18 @@ QString formatBytes(uint64_t bytes)
995995
return QString(QObject::tr("%1 GB")).arg(bytes / 1024 / 1024 / 1024);
996996
}
997997

998+
qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize, qreal font_size) {
999+
while(font_size >= minPointSize) {
1000+
font.setPointSizeF(font_size);
1001+
QFontMetrics fm(font);
1002+
if (fm.width(text) < width) {
1003+
break;
1004+
}
1005+
font_size -= 0.5;
1006+
}
1007+
return font_size;
1008+
}
1009+
9981010
void ClickableLabel::mouseReleaseEvent(QMouseEvent *event)
9991011
{
10001012
Q_EMIT clicked(event->pos());

src/qt/guiutil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ namespace GUIUtil
201201

202202
QString formatBytes(uint64_t bytes);
203203

204+
qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14);
205+
204206
class ClickableLabel : public QLabel
205207
{
206208
Q_OBJECT

src/qt/receiverequestdialog.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ void ReceiveRequestDialog::update()
183183
QPainter painter(&qrAddrImage);
184184
painter.drawImage(0, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
185185
QFont font = GUIUtil::fixedPitchFont();
186-
font.setPixelSize(12);
187-
painter.setFont(font);
188186
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);
189193
paddedRect.setHeight(QR_IMAGE_SIZE+12);
190194
painter.drawText(paddedRect, Qt::AlignBottom|Qt::AlignCenter, info.address);
191195
painter.end();

0 commit comments

Comments
 (0)