qt: Introduce GUIUtil::FontFamily and make font family adjustable

This commit is contained in:
xdustinface 2020-06-16 15:14:11 +02:00
parent 85c353e670
commit 175c3e0974
2 changed files with 74 additions and 16 deletions

View File

@ -88,7 +88,7 @@ static const QString defaultTheme = "Light";
static const QString darkThemePrefix = "Dark"; static const QString darkThemePrefix = "Dark";
/** Font related default values. */ /** Font related default values. */
static const QString defaultFontFamily = "Montserrat"; static const FontFamily defaultFontFamily = FontFamily::SystemDefault;
static const int defaultFontSize = 12; static const int defaultFontSize = 12;
static const double fontScaleSteps = 0.01; static const double fontScaleSteps = 0.01;
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
@ -102,6 +102,7 @@ static const int defaultFontScale = 0;
#endif #endif
/** Font related variables. */ /** Font related variables. */
FontFamily fontFamily = defaultFontFamily;
// Weight for normal text // Weight for normal text
QFont::Weight fontWeightNormal = defaultFontWeightNormal; QFont::Weight fontWeightNormal = defaultFontWeightNormal;
// Weight for bold text // Weight for bold text
@ -1013,6 +1014,44 @@ QString loadStyleSheet()
return *stylesheet.get(); 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) bool weightFromArg(int nArg, QFont::Weight& weight)
{ {
const std::map<int, QFont::Weight> mapWeight{ const std::map<int, QFont::Weight> mapWeight{
@ -1108,7 +1147,7 @@ double getScaledFontSize(int nSize)
bool loadFonts() bool loadFonts()
{ {
QString family = defaultFontFamily; QString family = fontFamilyToString(FontFamily::Montserrat);
QString italic = "Italic"; QString italic = "Italic";
std::map<QString, bool> mapStyles{ std::map<QString, bool> mapStyles{
@ -1173,25 +1212,28 @@ void setApplicationFont()
{ {
static QFont osDefaultFont = QApplication::font(); static QFont osDefaultFont = QApplication::font();
if (dashThemeActive()) { std::unique_ptr<QFont> font;
QString family = defaultFontFamily;
QFont appFont = QFont(family); if (fontFamily == FontFamily::Montserrat) {
QString family = fontFamilyToString(FontFamily::Montserrat);
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
if (getFontWeightNormal() != defaultFontWeightNormal) { if (getFontWeightNormal() != getFontWeightNormalDefault()) {
appFont = getFontNormal(); font = std::make_unique<QFont>(getFontNormal());
} else { } else {
appFont.setWeight(defaultFontWeightNormal); font = std::make_unique<QFont>(family);
font->setWeight(getFontWeightNormalDefault());
} }
appFont.setPointSizeF(getScaledFontSize(defaultFontSize));
#else #else
appFont.setWeight(getFontWeightNormal()); font = std::make_unique<QFont>(family);
appFont.setPointSizeF(getScaledFontSize(defaultFontSize)); font->setWeight(getFontWeightNormal());
#endif #endif
qApp->setFont(appFont);
} else { } else {
qApp->setFont(osDefaultFont); font = std::make_unique<QFont>(osDefaultFont);
} }
font->setPointSizeF(defaultFontSize);
qApp->setFont(*font);
qDebug() << __func__ << ": " << qApp->font().toString() << qDebug() << __func__ << ": " << qApp->font().toString() <<
" family: " << qApp->font().family() << " family: " << qApp->font().family() <<
", style: " << qApp->font().styleName() << ", style: " << qApp->font().styleName() <<
@ -1292,7 +1334,7 @@ QFont getFont(FontWeight weight, bool fItalic, int nPointSize)
QFont font; QFont font;
QFont::Weight qWeight = toQFontWeight(weight); QFont::Weight qWeight = toQFontWeight(weight);
if (dashThemeActive()) { if (fontFamily == FontFamily::Montserrat) {
static std::map<QFont::Weight, QString> mapMontserratMapping{ static std::map<QFont::Weight, QString> mapMontserratMapping{
{QFont::Thin, "Thin"}, {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); font.setStyleName(styleName);
#else #else
font.setFamily(defaultFontFamily + " " + mapMontserratMapping[qWeight]); font.setFamily(fontFamilyToString(FontFamily::Montserrat) + " " + mapMontserratMapping[qWeight]);
font.setWeight(qWeight); font.setWeight(qWeight);
font.setStyle(fItalic ? QFont::StyleItalic : QFont::StyleNormal); font.setStyle(fItalic ? QFont::StyleItalic : QFont::StyleNormal);
#endif #endif

View File

@ -233,6 +233,22 @@ namespace GUIUtil
/** Load global CSS theme */ /** Load global CSS theme */
QString loadStyleSheet(); 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 { enum class FontWeight {
Normal, // Font weight for normal text Normal, // Font weight for normal text
Bold, // Font weight for bold text Bold, // Font weight for bold text