From 175c3e0974e7980192391813b2d89239bc897fd4 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Tue, 16 Jun 2020 15:14:11 +0200 Subject: [PATCH] qt: Introduce GUIUtil::FontFamily and make font family adjustable --- src/qt/guiutil.cpp | 74 ++++++++++++++++++++++++++++++++++++---------- src/qt/guiutil.h | 16 ++++++++++ 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index a0f938cb6f..c44335abbd 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -88,7 +88,7 @@ static const QString defaultTheme = "Light"; static const QString darkThemePrefix = "Dark"; /** Font related default values. */ -static const QString defaultFontFamily = "Montserrat"; +static const FontFamily defaultFontFamily = FontFamily::SystemDefault; static const int defaultFontSize = 12; static const double fontScaleSteps = 0.01; #ifdef Q_OS_MAC @@ -102,6 +102,7 @@ static const int defaultFontScale = 0; #endif /** Font related variables. */ +FontFamily fontFamily = defaultFontFamily; // Weight for normal text QFont::Weight fontWeightNormal = defaultFontWeightNormal; // Weight for bold text @@ -1013,6 +1014,44 @@ QString loadStyleSheet() return *stylesheet.get(); } +FontFamily fontFamilyFromString(const QString& strFamily) +{ + if (strFamily == "SystemDefault") { + return FontFamily::SystemDefault; + } + if (strFamily == "Montserrat") { + return FontFamily::Montserrat; + } + throw std::invalid_argument(strprintf("Invalid font-family: %s", strFamily.toStdString())); +} + +QString fontFamilyToString(FontFamily family) +{ + switch (family) { + case FontFamily::SystemDefault: + return "SystemDefault"; + case FontFamily::Montserrat: + return "Montserrat"; + default: + assert(false); + } +} + +void setFontFamily(FontFamily family) +{ + fontFamily = family; +} + +FontFamily getFontFamilyDefault() +{ + return defaultFontFamily; +} + +FontFamily getFontFamily() +{ + return fontFamily; +} + bool weightFromArg(int nArg, QFont::Weight& weight) { const std::map mapWeight{ @@ -1108,7 +1147,7 @@ double getScaledFontSize(int nSize) bool loadFonts() { - QString family = defaultFontFamily; + QString family = fontFamilyToString(FontFamily::Montserrat); QString italic = "Italic"; std::map mapStyles{ @@ -1173,25 +1212,28 @@ void setApplicationFont() { static QFont osDefaultFont = QApplication::font(); - if (dashThemeActive()) { - QString family = defaultFontFamily; - QFont appFont = QFont(family); + std::unique_ptr font; + + if (fontFamily == FontFamily::Montserrat) { + QString family = fontFamilyToString(FontFamily::Montserrat); #ifdef Q_OS_MAC - if (getFontWeightNormal() != defaultFontWeightNormal) { - appFont = getFontNormal(); + if (getFontWeightNormal() != getFontWeightNormalDefault()) { + font = std::make_unique(getFontNormal()); } else { - appFont.setWeight(defaultFontWeightNormal); + font = std::make_unique(family); + font->setWeight(getFontWeightNormalDefault()); } - appFont.setPointSizeF(getScaledFontSize(defaultFontSize)); #else - appFont.setWeight(getFontWeightNormal()); - appFont.setPointSizeF(getScaledFontSize(defaultFontSize)); + font = std::make_unique(family); + font->setWeight(getFontWeightNormal()); #endif - qApp->setFont(appFont); } else { - qApp->setFont(osDefaultFont); + font = std::make_unique(osDefaultFont); } + font->setPointSizeF(defaultFontSize); + qApp->setFont(*font); + qDebug() << __func__ << ": " << qApp->font().toString() << " family: " << qApp->font().family() << ", style: " << qApp->font().styleName() << @@ -1292,7 +1334,7 @@ QFont getFont(FontWeight weight, bool fItalic, int nPointSize) QFont font; QFont::Weight qWeight = toQFontWeight(weight); - if (dashThemeActive()) { + if (fontFamily == FontFamily::Montserrat) { static std::map mapMontserratMapping{ {QFont::Thin, "Thin"}, @@ -1325,10 +1367,10 @@ QFont getFont(FontWeight weight, bool fItalic, int nPointSize) } } - font.setFamily(defaultFontFamily); + font.setFamily(fontFamilyToString(FontFamily::Montserrat)); font.setStyleName(styleName); #else - font.setFamily(defaultFontFamily + " " + mapMontserratMapping[qWeight]); + font.setFamily(fontFamilyToString(FontFamily::Montserrat) + " " + mapMontserratMapping[qWeight]); font.setWeight(qWeight); font.setStyle(fItalic ? QFont::StyleItalic : QFont::StyleNormal); #endif diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h index 98f1296537..eb9a70f61e 100644 --- a/src/qt/guiutil.h +++ b/src/qt/guiutil.h @@ -233,6 +233,22 @@ namespace GUIUtil /** Load global CSS theme */ QString loadStyleSheet(); + enum class FontFamily { + SystemDefault, + Montserrat, + }; + + /** Application font family */ + extern FontFamily fontFamily; + + FontFamily fontFamilyFromString(const QString& strFamily); + QString fontFamilyToString(FontFamily family); + + /** set/get font family: GUIUtil::fontFamily */ + FontFamily getFontFamilyDefault(); + FontFamily getFontFamily(); + void setFontFamily(FontFamily family); + enum class FontWeight { Normal, // Font weight for normal text Bold, // Font weight for bold text