Skip to content

Commit

Permalink
GUI: Enable customisation of QR Code font
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed May 11, 2024
1 parent 55bd5d8 commit fefa5b4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/qt/forms/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,31 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_qrFont">
<item>
<widget class="QLabel" name="qrFontLabel">
<property name="text">
<string>Font in QR Codes: </string>
</property>
<property name="buddy">
<cstring>qrFont</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="qrFont"/>
</item>
<item>
<widget class="QLabel" name="qrFont_preview">
<property name="text">
<string notr="true">1NS17iag9jJgT…
bc1p2q3rvn3gp…</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_Display">
<property name="orientation">
Expand Down
10 changes: 10 additions & 0 deletions src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ OptionsDialog::OptionsDialog(QWidget* parent, bool enableWallet)
}

setupFontOptions(ui->moneyFont, ui->moneyFont_preview);
setupFontOptions(ui->qrFont, ui->qrFont_preview);
#ifndef USE_QRCODE
ui->qrFontLabel->setVisible(false);
ui->qrFont->setVisible(false);
ui->qrFont_preview->setVisible(false);
#endif

GUIUtil::handleCloseWindowShortcut(this);
}
Expand Down Expand Up @@ -245,6 +251,9 @@ void OptionsDialog::setModel(OptionsModel *_model)
const auto& font_for_money = _model->data(_model->index(OptionsModel::FontForMoney, 0), Qt::EditRole).value<OptionsModel::FontChoice>();
setFontChoice(ui->moneyFont, font_for_money);

const auto& font_for_qrcodes = _model->data(_model->index(OptionsModel::FontForQRCodes, 0), Qt::EditRole).value<OptionsModel::FontChoice>();
setFontChoice(ui->qrFont, font_for_qrcodes);

updateDefaultProxyNets();
}

Expand Down Expand Up @@ -384,6 +393,7 @@ void OptionsDialog::on_openBitcoinConfButton_clicked()
void OptionsDialog::on_okButton_clicked()
{
model->setData(model->index(OptionsModel::FontForMoney, 0), ui->moneyFont->itemData(ui->moneyFont->currentIndex()));
model->setData(model->index(OptionsModel::FontForQRCodes, 0), ui->qrFont->itemData(ui->qrFont->currentIndex()));

mapper->submit();
accept();
Expand Down
16 changes: 16 additions & 0 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ bool OptionsModel::Init(bilingual_str& error)
}
Q_EMIT fontForMoneyChanged(getFontForMoney());

if (settings.contains("FontForQRCodes")) {
m_font_qrcodes = FontChoiceFromString(settings.value("FontForQRCodes").toString());
}
Q_EMIT fontForQRCodesChanged(getFontChoiceForQRCodes());

m_mask_values = settings.value("mask_values", false).toBool();

return true;
Expand Down Expand Up @@ -466,6 +471,8 @@ QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) con
return QString::fromStdString(SettingToString(setting(), ""));
case FontForMoney:
return QVariant::fromValue(m_font_money);
case FontForQRCodes:
return QVariant::fromValue(m_font_qrcodes);
case CoinControlFeatures:
return fCoinControlFeatures;
case EnablePSBTControls:
Expand Down Expand Up @@ -649,6 +656,15 @@ bool OptionsModel::setOption(OptionID option, const QVariant& value, const std::
Q_EMIT fontForMoneyChanged(getFontForMoney());
break;
}
case FontForQRCodes:
{
const auto& new_font = value.value<FontChoice>();
if (m_font_qrcodes == new_font) break;
settings.setValue("FontForQRCodes", FontChoiceToString(new_font));
m_font_qrcodes = new_font;
Q_EMIT fontForQRCodesChanged(new_font);
break;
}
case CoinControlFeatures:
fCoinControlFeatures = value.toBool();
settings.setValue("fCoinControlFeatures", fCoinControlFeatures);
Expand Down
4 changes: 4 additions & 0 deletions src/qt/optionsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class OptionsModel : public QAbstractListModel
ThirdPartyTxUrls, // QString
Language, // QString
FontForMoney, // FontChoice
FontForQRCodes, // FontChoice
CoinControlFeatures, // bool
SubFeeFromAmount, // bool
ThreadsScriptVerif, // int
Expand Down Expand Up @@ -104,6 +105,7 @@ class OptionsModel : public QAbstractListModel
BitcoinUnit getDisplayUnit() const { return m_display_bitcoin_unit; }
QString getThirdPartyTxUrls() const { return strThirdPartyTxUrls; }
QFont getFontForMoney() const;
FontChoice getFontChoiceForQRCodes() const { return m_font_qrcodes; }
bool getCoinControlFeatures() const { return fCoinControlFeatures; }
bool getSubFeeFromAmount() const { return m_sub_fee_from_amount; }
bool getEnablePSBTControls() const { return m_enable_psbt_controls; }
Expand Down Expand Up @@ -131,6 +133,7 @@ class OptionsModel : public QAbstractListModel
BitcoinUnit m_display_bitcoin_unit;
QString strThirdPartyTxUrls;
FontChoice m_font_money{FontChoiceAbstract::EmbeddedFont};
FontChoice m_font_qrcodes{FontChoiceAbstract::BestSystemFont};
bool fCoinControlFeatures;
bool m_sub_fee_from_amount;
bool m_enable_psbt_controls;
Expand All @@ -153,6 +156,7 @@ class OptionsModel : public QAbstractListModel
void coinControlFeaturesChanged(bool);
void showTrayIconChanged(bool);
void fontForMoneyChanged(const QFont&);
void fontForQRCodesChanged(const FontChoice&);
};

Q_DECLARE_METATYPE(OptionsModel::FontChoice)
Expand Down
26 changes: 20 additions & 6 deletions src/qt/qrimagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <qt/qrimagewidget.h>

#include <qt/guiutil.h>
#include <qt/optionsmodel.h>

#include <QApplication>
#include <QClipboard>
Expand All @@ -31,7 +32,7 @@ QRImageWidget::QRImageWidget(QWidget* parent)
contextMenu->addAction(tr("&Copy Image"), this, &QRImageWidget::copyImage);
}

bool QRImageWidget::setQR(const QString& data, const QString& text)
bool QRImageWidget::setQR(const QString& data, const QString& text, const OptionsModel::FontChoice& fontchoice)
{
#ifdef USE_QRCODE
setText("");
Expand Down Expand Up @@ -72,11 +73,19 @@ bool QRImageWidget::setQR(const QString& data, const QString& text)
QRect paddedRect = qrAddrImage.rect();
paddedRect.setHeight(QR_IMAGE_SIZE + QR_IMAGE_TEXT_MARGIN);

QFont font = GUIUtil::fixedPitchFont();
font.setStretch(QFont::SemiCondensed);
font.setLetterSpacing(QFont::AbsoluteSpacing, 1);
const qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 2 * QR_IMAGE_TEXT_MARGIN, text, font);
font.setPointSizeF(font_size);
// Determine font to use
QFont font;
if (std::holds_alternative<OptionsModel::FontChoiceAbstract>(fontchoice)) {
font = GUIUtil::fixedPitchFont(fontchoice != OptionsModel::UseBestSystemFont);
font.setWeight(QFont::Bold);
font.setStretch(QFont::SemiCondensed);
font.setLetterSpacing(QFont::AbsoluteSpacing, 1);

const qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 2 * QR_IMAGE_TEXT_MARGIN, text, font);
font.setPointSizeF(font_size);
} else {
font = std::get<QFont>(fontchoice);
}

painter.setFont(font);
painter.drawText(paddedRect, Qt::AlignBottom | Qt::AlignCenter, text);
Expand All @@ -92,6 +101,11 @@ bool QRImageWidget::setQR(const QString& data, const QString& text)
#endif
}

bool QRImageWidget::setQR(const QString& data)
{
return setQR(data, "", OptionsModel::FontChoiceAbstract::EmbeddedFont);
}

QImage QRImageWidget::exportImage()
{
return GUIUtil::GetImage(this);
Expand Down
5 changes: 4 additions & 1 deletion src/qt/qrimagewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef BITCOIN_QT_QRIMAGEWIDGET_H
#define BITCOIN_QT_QRIMAGEWIDGET_H

#include <qt/optionsmodel.h>

#include <QImage>
#include <QLabel>

Expand All @@ -29,7 +31,8 @@ class QRImageWidget : public QLabel

public:
explicit QRImageWidget(QWidget *parent = nullptr);
bool setQR(const QString& data, const QString& text = "");
bool setQR(const QString& data);
bool setQR(const QString& data, const QString& text, const OptionsModel::FontChoice& fontchoice);
QImage exportImage();

public Q_SLOTS:
Expand Down
5 changes: 4 additions & 1 deletion src/qt/receiverequestdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ void ReceiveRequestDialog::setInfo(const SendCoinsRecipient &_info)
QString uri = GUIUtil::formatBitcoinURI(info);

#ifdef USE_QRCODE
if (ui->qr_code->setQR(uri, info.address)) {
if (ui->qr_code->setQR(uri, info.address, model->getOptionsModel()->getFontChoiceForQRCodes())) {
connect(ui->btnSaveAs, &QPushButton::clicked, ui->qr_code, &QRImageWidget::saveImage);
connect(model->getOptionsModel(), &OptionsModel::fontForQRCodesChanged, this, [&](const OptionsModel::FontChoice& fontchoice){
ui->qr_code->setQR(uri, info.address, fontchoice);
});
} else {
ui->btnSaveAs->setEnabled(false);
}
Expand Down

0 comments on commit fefa5b4

Please sign in to comment.