Skip to content

Commit 0292370

Browse files
committed
qt: Move font-size settings from css to c++.
This also keeps track of changed font-sizes and updates them depending on the settings if GUIUtil::updateFonts() gets called.
1 parent bb54e85 commit 0292370

20 files changed

+113
-70
lines changed

src/qt/addressbookpage.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode,
143143
connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
144144

145145
connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(accept()));
146+
147+
GUIUtil::updateFonts();
146148
}
147149

148150
AddressBookPage::~AddressBookPage()

src/qt/askpassphrasedialog.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ AskPassphraseDialog::AskPassphraseDialog(Mode _mode, QWidget *parent) :
3131

3232
GUIUtil::setFont({ui->capsLabel}, GUIUtil::getFontWeightBold());
3333

34+
GUIUtil::updateFonts();
35+
3436
GUIUtil::disableMacFocusRect(this);
3537

3638
ui->passEdit1->setMinimumSize(ui->passEdit1->sizeHint());

src/qt/bitcoingui.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ void BitcoinGUI::createActions()
396396
connect(historyAction, SIGNAL(clicked()), this, SLOT(showNormalIfMinimized()));
397397
connect(historyAction, SIGNAL(clicked()), this, SLOT(gotoHistoryPage()));
398398

399+
for (auto button : tabGroup->buttons()) {
400+
GUIUtil::setFont({button}, GUIUtil::getFontWeightNormal(), 16);
401+
}
399402
// Give the selected tab button a bolder font.
400403
connect(tabGroup, SIGNAL(buttonToggled(QAbstractButton *, bool)), this, SLOT(highlightTabButton(QAbstractButton *, bool)));
401404
#endif // ENABLE_WALLET
@@ -924,7 +927,7 @@ void BitcoinGUI::openClicked()
924927

925928
void BitcoinGUI::highlightTabButton(QAbstractButton *button, bool checked)
926929
{
927-
GUIUtil::setFont({button}, checked ? GUIUtil::getFontWeightBold() : GUIUtil::getFontWeightNormal());
930+
GUIUtil::setFont({button}, checked ? GUIUtil::getFontWeightBold() : GUIUtil::getFontWeightNormal(), 16);
928931
}
929932

930933
void BitcoinGUI::gotoOverviewPage()

src/qt/coincontroldialog.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ CoinControlDialog::CoinControlDialog(const PlatformStyle *_platformStyle, QWidge
6060
ui->labelCoinControlChangeText
6161
}, GUIUtil::getFontWeightBold());
6262

63+
GUIUtil::updateFonts();
64+
6365
GUIUtil::disableMacFocusRect(this);
6466

6567
// context menu actions

src/qt/guiutil.cpp

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ int fontScale = defaultFontScale;
129129
static std::map<QWidget*, std::pair<QFont::Weight, bool>> mapNormalFontUpdates;
130130
// Contains all widgets where a fixed pitch font has been set with GUIUtil::setFixedPitchFont
131131
static std::set<QWidget*> setFixedPitchFontUpdates;
132+
// Contains all widgets where a non-default fontsize has been seet with GUIUtil::setFont
133+
static std::map<QWidget*, int> mapFontSizeUpdates;
132134
#ifdef Q_OS_MAC
133135
// Contains all widgets where the macOS focus rect has been disabled.
134136
static std::set<QWidget*> setRectsDisabled;
@@ -1287,15 +1289,21 @@ void setApplicationFont()
12871289
" match: " << qApp->font().exactMatch();
12881290
}
12891291

1290-
void setFont(const std::vector<QWidget*> &vecWidgets, QFont::Weight weight, bool fItalic)
1292+
void setFont(const std::vector<QWidget*> &vecWidgets, QFont::Weight weight, int nPointSize, bool fItalic)
12911293
{
12921294
// TODO: Evaluate maybe wrapping this later into some "theme handler" or so..
1293-
QFont font = getFont(weight, fItalic);
1295+
QFont font = getFont(weight, fItalic, nPointSize);
12941296

12951297
for (auto it : vecWidgets) {
12961298
auto fontAttributes = std::make_pair(weight, fItalic);
12971299
auto itw = mapNormalFontUpdates.emplace(std::make_pair(it, fontAttributes));
12981300
if (!itw.second) itw.first->second = fontAttributes;
1301+
1302+
if (nPointSize != -1) {
1303+
auto its = mapFontSizeUpdates.emplace(std::make_pair(it, nPointSize));
1304+
if (!its.second) its.first->second = nPointSize;
1305+
}
1306+
12991307
it->setFont(font);
13001308
}
13011309
}
@@ -1314,6 +1322,13 @@ void updateFonts()
13141322
// TODO: Evaluate maybe wrapping this later into some "theme handler" or so..
13151323
setApplicationFont();
13161324

1325+
auto getKey = [](QWidget* w) -> QString
1326+
{
1327+
return w->parent() ? w->parent()->objectName() + w->objectName() :
1328+
w->objectName();
1329+
};
1330+
1331+
static std::map<QString, int> mapDefaultFontSizes;
13171332
std::map<QWidget*, QFont> mapWidgetFonts;
13181333

13191334
for (QWidget* w : qApp->allWidgets()) {
@@ -1322,22 +1337,41 @@ void updateFonts()
13221337
font.setWeight(qApp->font().weight());
13231338
font.setStyleName(qApp->font().styleName());
13241339
font.setStyle(qApp->font().style());
1340+
// Set the font size based on the widgets default font size + the font scale
1341+
QString key = getKey(w);
1342+
if (!mapDefaultFontSizes.count(key)) {
1343+
mapDefaultFontSizes.emplace(std::make_pair(key, font.pointSize() > 0 ? font.pointSize() : defaultFontSize));
1344+
}
1345+
font.setPointSizeF(getScaledFontSize(mapDefaultFontSizes[key]));
13251346
mapWidgetFonts.emplace(w, font);
13261347
}
13271348

13281349
auto itn = mapNormalFontUpdates.begin();
13291350
while (itn != mapNormalFontUpdates.end()) {
13301351
if (mapWidgetFonts.count(itn->first)) {
1331-
mapWidgetFonts[itn->first] = getFont(itn->second.first, itn->second.second);
1352+
mapWidgetFonts[itn->first] = getFont(itn->second.first, itn->second.second, mapDefaultFontSizes[getKey(itn->first)]);
13321353
++itn;
13331354
} else {
13341355
itn = mapNormalFontUpdates.erase(itn);
13351356
}
13361357
}
1358+
auto its = mapFontSizeUpdates.begin();
1359+
while (its != mapFontSizeUpdates.end()) {
1360+
if (mapWidgetFonts.count(its->first)) {
1361+
QFont font = mapWidgetFonts[its->first];
1362+
font.setPointSizeF(getScaledFontSize(its->second));
1363+
mapWidgetFonts[its->first] = font;
1364+
++its;
1365+
} else {
1366+
its = mapFontSizeUpdates.erase(its);
1367+
}
1368+
}
13371369
auto itf = setFixedPitchFontUpdates.begin();
13381370
while (itf != setFixedPitchFontUpdates.end()) {
13391371
if (mapWidgetFonts.count(*itf)) {
1340-
mapWidgetFonts[*itf] = fixedPitchFont();
1372+
QFont font = fixedPitchFont();
1373+
font.setPointSizeF(getScaledFontSize(mapDefaultFontSizes[getKey(*itf)]));
1374+
mapWidgetFonts[*itf] = font;
13411375
++itf;
13421376
} else {
13431377
itf = setFixedPitchFontUpdates.erase(itf);
@@ -1346,10 +1380,13 @@ void updateFonts()
13461380

13471381
for (auto it: mapWidgetFonts) {
13481382
it.first->setFont(it.second);
1383+
if (gArgs.IsArgSet("-debug-ui")) {
1384+
qDebug() << __func__ << ": set font " << it.second << " for widget " << it.first->objectName();
1385+
}
13491386
}
13501387
}
13511388

1352-
QFont getFont(QFont::Weight weight, bool fItalic)
1389+
QFont getFont(QFont::Weight weight, bool fItalic, int nPointSize)
13531390
{
13541391
// TODO: Evaluate maybe wrapping this later into some "theme handler" or so..
13551392
QFont font;
@@ -1398,6 +1435,11 @@ QFont getFont(QFont::Weight weight, bool fItalic)
13981435
font.setWeight(weight > QFont::Normal ? QFont::Bold : QFont::Normal);
13991436
font.setStyle(fItalic ? QFont::StyleItalic : QFont::StyleNormal);
14001437
}
1438+
1439+
if (nPointSize != -1) {
1440+
font.setPointSizeF(getScaledFontSize(nPointSize));
1441+
}
1442+
14011443
if (gArgs.IsArgSet("-debug-ui")) {
14021444
qDebug() << __func__ << ": font size: " << font.pointSizeF() << " family: " << font.family() << ", style: " << font.styleName() << " match: " << font.exactMatch();
14031445
}

src/qt/masternodelist.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ MasternodeList::MasternodeList(const PlatformStyle* platformStyle, QWidget* pare
4545

4646
GUIUtil::setFont({ui->label_count_2,
4747
ui->countLabelDIP3
48-
}, GUIUtil::getFontWeightBold());
48+
}, GUIUtil::getFontWeightBold(), 14);
49+
GUIUtil::setFont({ui->label_filter_2}, GUIUtil::getFontWeightNormal(), 15);
4950

5051
int columnAddressWidth = 200;
5152
int columnStatusWidth = 80;

src/qt/openuridialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ OpenURIDialog::OpenURIDialog(QWidget *parent) :
1616
ui(new Ui::OpenURIDialog)
1717
{
1818
ui->setupUi(this);
19+
GUIUtil::updateFonts();
1920
GUIUtil::disableMacFocusRect(this);
2021
ui->uriEdit->setPlaceholderText("dash:");
2122
}

src/qt/optionsdialog.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
3939

4040
previousTheme = GUIUtil::getActiveTheme();
4141

42-
GUIUtil::setFont({ui->statusLabel}, GUIUtil::getFontWeightBold());
42+
GUIUtil::setFont({ui->statusLabel}, GUIUtil::getFontWeightBold(), 16);
43+
44+
GUIUtil::updateFonts();
4345

4446
GUIUtil::disableMacFocusRect(this);
4547

@@ -252,8 +254,8 @@ void OptionsDialog::showPage(int index)
252254
}
253255
}
254256

255-
GUIUtil::setFont({btnActive}, GUIUtil::getFontWeightBold());
256-
GUIUtil::setFont(vecNormal, GUIUtil::getFontWeightNormal());
257+
GUIUtil::setFont({btnActive}, GUIUtil::getFontWeightBold(), 16);
258+
GUIUtil::setFont(vecNormal, GUIUtil::getFontWeightNormal(), 16);
257259

258260
ui->stackedWidgetOptions->setCurrentIndex(index);
259261
btnActive->setChecked(true);

src/qt/overviewpage.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,19 @@ OverviewPage::OverviewPage(const PlatformStyle *platformStyle, QWidget *parent)
137137

138138
GUIUtil::setFont({ui->label_4,
139139
ui->label_5,
140-
ui->labelWatchPending,
141-
ui->labelUnconfirmed,
142-
ui->labelWatchImmature,
143-
ui->labelImmature,
144-
ui->labelTotal,
140+
ui->labelPrivateSendHeader
141+
}, GUIUtil::getFontWeightBold(), 16);
142+
143+
GUIUtil::setFont({ui->labelTotalText,
145144
ui->labelWatchTotal,
146-
ui->labelWatchAvailable,
147-
ui->labelBalance,
148-
ui->labelPrivateSendHeader,
149-
ui->labelAnonymized
145+
ui->labelTotal
146+
}, GUIUtil::getFontWeightBold(), 14);
147+
148+
GUIUtil::setFont({ui->labelBalanceText,
149+
ui->labelPendingText,
150+
ui->labelImmatureText,
151+
ui->labelWatchonly,
152+
ui->labelSpendable
150153
}, GUIUtil::getFontWeightBold());
151154

152155
// Recent transactions

src/qt/qrdialog.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ QRDialog::QRDialog(QWidget *parent) :
9797
{
9898
ui->setupUi(this);
9999

100-
GUIUtil::setFont({ui->labelQRCodeTitle}, GUIUtil::getFontWeightBold());
100+
GUIUtil::setFont({ui->labelQRCodeTitle}, GUIUtil::getFontWeightBold(), 16);
101+
102+
GUIUtil::updateFonts();
101103

102104
#ifndef USE_QRCODE
103105
ui->button_saveImage->setVisible(false);

0 commit comments

Comments
 (0)