Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Qt] Use flexible font size for QRCode image address #12173

Merged
merged 1 commit into from Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/qt/guiutil.cpp
Expand Up @@ -995,6 +995,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());
Expand Down
2 changes: 2 additions & 0 deletions src/qt/guiutil.h
Expand Up @@ -201,6 +201,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
Expand Down
8 changes: 6 additions & 2 deletions src/qt/receiverequestdialog.cpp
Expand Up @@ -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();
Expand Down