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";
/** 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<int, QFont::Weight> mapWeight{
@ -1108,7 +1147,7 @@ double getScaledFontSize(int nSize)
bool loadFonts()
{
QString family = defaultFontFamily;
QString family = fontFamilyToString(FontFamily::Montserrat);
QString italic = "Italic";
std::map<QString, bool> mapStyles{
@ -1173,25 +1212,28 @@ void setApplicationFont()
{
static QFont osDefaultFont = QApplication::font();
if (dashThemeActive()) {
QString family = defaultFontFamily;
QFont appFont = QFont(family);
std::unique_ptr<QFont> 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<QFont>(getFontNormal());
} else {
appFont.setWeight(defaultFontWeightNormal);
font = std::make_unique<QFont>(family);
font->setWeight(getFontWeightNormalDefault());
}
appFont.setPointSizeF(getScaledFontSize(defaultFontSize));
#else
appFont.setWeight(getFontWeightNormal());
appFont.setPointSizeF(getScaledFontSize(defaultFontSize));
font = std::make_unique<QFont>(family);
font->setWeight(getFontWeightNormal());
#endif
qApp->setFont(appFont);
} else {
qApp->setFont(osDefaultFont);
font = std::make_unique<QFont>(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<QFont::Weight, QString> 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

View File

@ -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