From 087b18545f88d4e411bc681af66c743d6fde013b Mon Sep 17 00:00:00 2001 From: Seeker4 Date: Thu, 4 Jul 2019 18:14:41 -0700 Subject: [PATCH] Centralize all font validation and setting into new functions in common.cpp. Add registry key to System Info (call it App Key for cross-platform compatibility). --- oscar/SleepLib/common.cpp | 85 +++++++++++++++++++++++++++++++++++++++ oscar/SleepLib/common.h | 4 ++ oscar/main.cpp | 34 +++------------- 3 files changed, 94 insertions(+), 29 deletions(-) diff --git a/oscar/SleepLib/common.cpp b/oscar/SleepLib/common.cpp index d6ef7ddc..dd0a6785 100644 --- a/oscar/SleepLib/common.cpp +++ b/oscar/SleepLib/common.cpp @@ -16,7 +16,10 @@ #include #include #include +#include #include +#include +#include #include "SleepLib/common.h" @@ -30,6 +33,9 @@ #include "git_info.h" #include "version.h" #include "profiles.h" +#include "mainwindow.h" + +extern MainWindow * mainwin; // Used by internal settings @@ -191,6 +197,8 @@ QStringList makeBuildInfo (QString relinfo, QString forcedEngine){ branch = QObject::tr("Branch:") + " " + GIT_BRANCH + ", "; } buildInfo << branch + (QObject::tr("Revision")) + " " + GIT_REVISION; + if (GIT_BRANCH != "master") + buildInfo << (QObject::tr("App key:") + " " + getAppName()); buildInfo << QString(""); buildInfo << (QObject::tr("Operating system:") + " " + QSysInfo::prettyProductName()); buildInfo << (QObject::tr("Graphics Engine:") + " " + getOpenGLVersionString()); @@ -283,6 +291,83 @@ bool operator <(const ValueCount &a, const ValueCount &b) return a.value < b.value; } +static QStringList installedFontFamilies; + +// Validate all fonts +void validateAllFonts () { + validateFont("Application"); + validateFont("Graph"); + validateFont("Title"); + validateFont("Big"); +} + +// Validate font from preference settings, and substitute system font if font in preferences cannot be found on this system +void validateFont (QString which) { + + // Get list of installed font families, including system font + // Do this just once so we don't have to call font functions repeatedly + if (installedFontFamilies.length() <= 0) { + QFontDatabase database; + installedFontFamilies = database.families(); + qDebug() << "validateFont found" << installedFontFamilies.count() << "families"; + + // macOS default system fonts are not in QFontCombobox because they are "private": + // See https://github.com/musescore/MuseScore/commit/0eecb165664a0196c2eee12e42fb273dcfc9c637 + // The below makes sure any default system font is present in installedFontFamilies + QString systemFontFamily = QFontDatabase::systemFont(QFontDatabase::GeneralFont).family(); + if (installedFontFamilies.indexOf(systemFontFamily) < 0) { + installedFontFamilies.insert(0, systemFontFamily); + } + } + + bool forceFont = false; + QString prefPrefix = "Fonts_" + which + "_"; + + // If a font is specified, make sure it is a valid font on this platform + if (p_pref->contains(prefPrefix + "Name")) { + QString desiredFont = (*p_pref)[prefPrefix + "Name"].toString(); + if (desiredFont == "") + forceFont = true; + else { + if (installedFontFamilies.indexOf(desiredFont) == -1) { + forceFont = true; + qDebug() << "Desired font" << desiredFont << "not found in font database," << QFontDatabase::systemFont(QFontDatabase::GeneralFont) << "(system default) font used instead"; + } + } + } + + // Font not valid or not specified. Set system font in its place + if (!p_pref->contains(prefPrefix + "Name") || forceFont) { + (*p_pref)[prefPrefix + "Name"] = QFontDatabase::systemFont(QFontDatabase::GeneralFont).family(); + if (which == "Application") { + (*p_pref)[prefPrefix + "Size"] = 10; + (*p_pref)[prefPrefix + "Bold"] = false; + (*p_pref)[prefPrefix + "Italic"] = false; + } else if (which == "Graphs") { + (*p_pref)[prefPrefix + "Size"] = 10; + (*p_pref)[prefPrefix + "Bold"] = false; + (*p_pref)[prefPrefix + "Italic"] = false; + } else if (which == "Title") { + (*p_pref)[prefPrefix + "Size"] = 14; + (*p_pref)[prefPrefix + "Bold"] = true; + (*p_pref)[prefPrefix + "Italic"] = false; + } else if (which == "Big") { + (*p_pref)[prefPrefix + "Size"] = 35; + (*p_pref)[prefPrefix + "Bold"] = false; + (*p_pref)[prefPrefix + "Italic"] = false; + } + } +} + +void setApplicationFont () { + QFont font = QFont(((*p_pref)["Fonts_Application_Name"]).toString()); + font.setPointSize(((*p_pref)["Fonts_Application_Size"]).toInt()); + font.setWeight(((*p_pref)["Fonts_Application_Bold"]).toBool() ? QFont::Bold : QFont::Normal); + font.setItalic(((*p_pref)["Fonts_Application_Italic"]).toBool()); + QApplication::setFont(font); + mainwin->menuBar()->setFont(font); +} + bool removeDir(const QString &path) { bool result = true; diff --git a/oscar/SleepLib/common.h b/oscar/SleepLib/common.h index 434a6ef0..7c4a3130 100644 --- a/oscar/SleepLib/common.h +++ b/oscar/SleepLib/common.h @@ -143,6 +143,10 @@ const QString getDeveloperName(); const QString getDeveloperDomain(); const QString getModifiedAppData(); +void validateAllFonts (); +void validateFont (QString which); +void setApplicationFont (); + void initializeStrings(); diff --git a/oscar/main.cpp b/oscar/main.cpp index b17aff68..71e6459d 100644 --- a/oscar/main.cpp +++ b/oscar/main.cpp @@ -26,6 +26,7 @@ #include "mainwindow.h" #include "SleepLib/profiles.h" #include "translation.h" +#include "SleepLib/common.h" #include #include @@ -501,6 +502,10 @@ int main(int argc, char *argv[]) { QString language = settings.value(LangSetting, "").toString(); AppSetting->setLanguage(language); + // Set fonts from preferences file + validateAllFonts(); + setApplicationFont(); + // Clean up some legacy crap // QFile lf(p_pref->Get("{home}/Layout.xml")); // if (lf.exists()) { @@ -552,35 +557,6 @@ int main(int argc, char *argv[]) { AppSetting->setVersionString(VersionString); -// int id=QFontDatabase::addApplicationFont(":/fonts/FreeSans.ttf"); -// QFontDatabase fdb; -// QStringList ffam=fdb.families(); -// for (QStringList::iterator i=ffam.begin();i!=ffam.end();i++) { -// qDebug() << "Loaded Font: " << (*i); -// } - - - if (!p_pref->contains("Fonts_Application_Name")) { -#ifdef Q_OS_WIN - // Windows default Sans Serif interpretation sucks - // Segoe UI is better, but that requires OS/font detection - (*p_pref)["Fonts_Application_Name"] = "Arial"; -#else - (*p_pref)["Fonts_Application_Name"] = QFontDatabase::systemFont(QFontDatabase::GeneralFont).family(); -#endif - (*p_pref)["Fonts_Application_Size"] = 10; - (*p_pref)["Fonts_Application_Bold"] = false; - (*p_pref)["Fonts_Application_Italic"] = false; - } - - - QApplication::setFont(QFont((*p_pref)["Fonts_Application_Name"].toString(), - (*p_pref)["Fonts_Application_Size"].toInt(), - (*p_pref)["Fonts_Application_Bold"].toBool() ? QFont::Bold : QFont::Normal, - (*p_pref)["Fonts_Application_Italic"].toBool())); - - qDebug() << "Selected Font" << QApplication::font().family(); - //////////////////////////////////////////////////////////////////////////////////////////// // Register Importer Modules for autoscanner ////////////////////////////////////////////////////////////////////////////////////////////