Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use more robust API for storing user overriden application font
size and family
  • Loading branch information
nyalldawson committed May 17, 2023
1 parent 2ea458d commit 8793506
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 33 deletions.
90 changes: 57 additions & 33 deletions src/app/qgisappstylesheet.cpp
Expand Up @@ -42,26 +42,6 @@ QMap<QString, QVariant> QgisAppStyleSheet::defaultOptions()
// constructor to set reasonable non-Qt defaults for the app stylesheet
QgsSettings settings;

// TODO: find a better default fontsize maybe using DPI detection or so (from Marco Bernasocchi commit)
const double defaultFontSize = mAndroidOS ? 8 : mDefaultFont.pointSizeF();
const double fontSize = settings.value( QStringLiteral( "/qgis/stylesheet/fontPointSize" ), defaultFontSize ).toDouble();
QgsDebugMsgLevel( QStringLiteral( "fontPointSize: %1" ).arg( fontSize ), 2 );
opts.insert( QStringLiteral( "fontPointSize" ), fontSize );

QString fontFamily = settings.value( QStringLiteral( "/qgis/stylesheet/fontFamily" ), mDefaultFont.family() ).toString();
// make sure family exists on system
if ( fontFamily != mDefaultFont.family() )
{
const QFont tempFont( fontFamily );
if ( tempFont.family() != fontFamily )
{
// missing from system, drop back to default
fontFamily = mDefaultFont.family();
}
}
QgsDebugMsgLevel( QStringLiteral( "fontFamily: %1" ).arg( fontFamily ), 2 );
opts.insert( QStringLiteral( "fontFamily" ), QVariant( fontFamily ) );

opts.insert( QStringLiteral( "toolbarSpacing" ), settings.value( QStringLiteral( "/qgis/stylesheet/toolbarSpacing" ), QString() ) );

opts.insert( QStringLiteral( "iconSize" ), settings.value( QStringLiteral( "/qgis/toolbarIconSize" ), QGIS_ICON_SIZE ) );
Expand All @@ -76,25 +56,37 @@ void QgisAppStyleSheet::buildStyleSheet( const QMap<QString, QVariant> &opts )

// QgisApp-wide font
{
bool fontSizeOk = false;
const double fontSize = opts.value( QStringLiteral( "fontPointSize" ) ).toDouble( &fontSizeOk );
QgsDebugMsgLevel( QStringLiteral( "fontPointSize: %1" ).arg( fontSize ), 2 );
if ( !fontSizeOk )
bool overriddenFontSize = false;
double currentFontSize = fontSize();
if ( opts.contains( QStringLiteral( "fontPointSize" ) ) )
{
const double fontSizeFromOpts = opts.value( QStringLiteral( "fontPointSize" ) ).toDouble();
currentFontSize = fontSizeFromOpts;
}
QgsDebugMsgLevel( QStringLiteral( "fontPointSize: %1" ).arg( currentFontSize ), 2 );
if ( currentFontSize != defaultFont().pointSizeF() )
{
return;
overriddenFontSize = true;
}

const QString fontFamily = opts.value( QStringLiteral( "fontFamily" ) ).toString();
QgsDebugMsgLevel( QStringLiteral( "fontFamily: %1" ).arg( fontFamily ), 2 );
if ( fontFamily.isEmpty() )
bool overriddenFontFamily = false;
QString currentFontFamily = fontFamily();
if ( opts.contains( QStringLiteral( "fontFamily" ) ) )
{
currentFontFamily = opts.value( QStringLiteral( "fontFamily" ) ).toString();
}
QgsDebugMsgLevel( QStringLiteral( "fontFamily: %1" ).arg( currentFontFamily ), 2 );
if ( !currentFontFamily.isEmpty() && currentFontFamily != defaultFont().family() )
{
return;
overriddenFontFamily = true;
}

const double defaultSize = mDefaultFont.pointSizeF();
const QString defaultFamily = mDefaultFont.family();
if ( fontSize != defaultSize || fontFamily != defaultFamily )
ss += QStringLiteral( "* { font: %1pt \"%2\"} " ).arg( fontSize ).arg( fontFamily );
if ( overriddenFontFamily && overriddenFontSize )
ss += QStringLiteral( "* { font: %1pt \"%2\"} " ).arg( currentFontSize ).arg( currentFontFamily );
else if ( overriddenFontFamily )
ss += QStringLiteral( "* { font-family: \"%1\"} " ).arg( currentFontFamily );
else if ( overriddenFontSize )
ss += QStringLiteral( "* { font-size: %1pt } " ).arg( overriddenFontSize );
}

if ( mMacStyle )
Expand Down Expand Up @@ -204,6 +196,38 @@ void QgisAppStyleSheet::setActiveValues()

mDefaultFont = qApp->font(); // save before it is changed in any way

QgsSettings settings;

if ( mAndroidOS )
{
// TODO: find a better default fontsize maybe using DPI detection or so (from Marco Bernasocchi commit)
mUserFontSize = 8;
}
else
{
const double fontSize = settings.value( QStringLiteral( "/qgis/stylesheet/fontPointSize" ), mDefaultFont.pointSizeF() ).toDouble();
if ( fontSize != mDefaultFont.pointSizeF() )
{
mUserFontSize = fontSize;
}
else
{
mUserFontSize = -1;
}
}

QString fontFamily = settings.value( QStringLiteral( "/qgis/stylesheet/fontFamily" ), mDefaultFont.family() ).toString();
// make sure family exists on system
if ( fontFamily != mDefaultFont.family() )
{
const QFont tempFont( fontFamily );
if ( tempFont.family() == fontFamily )
{
// font exists on system
mUserFontFamily = fontFamily;
}
}

// platforms, specific
#ifdef Q_OS_WIN
mWinOS = true;
Expand Down
25 changes: 25 additions & 0 deletions src/app/qgisappstylesheet.h
Expand Up @@ -50,6 +50,28 @@ class APP_EXPORT QgisAppStyleSheet: public QObject
//! Gets reference font for initial qApp
QFont defaultFont() { return mDefaultFont; }

/**
* Returns the user set font size override value, or -1 if not set and the Qt default font size should be used.
*/
double userFontSize() const { return mUserFontSize; }

/**
* Returns the user set font family override value, or an empty if not set and the Qt default font family should be used.
*/
QString userFontFamily() const { return mUserFontFamily; }

/**
* Returns the application font size to use. This will match userFontSize() if the user
* has set a custom font size, or the defaultFont() font size otherwise.
*/
double fontSize() const { return mUserFontSize > 0 ? mUserFontSize : mDefaultFont.pointSizeF(); }

/**
* Returns the application font family. This will match userFontFamily() if the user
* has set a custom font, or the defaultFont() family otherwise.
*/
QString fontFamily() const { return !mUserFontFamily.isEmpty() ? mUserFontFamily : mDefaultFont.family(); }

signals:

/**
Expand All @@ -70,6 +92,9 @@ class APP_EXPORT QgisAppStyleSheet: public QObject
// default font saved for reference
QFont mDefaultFont;

double mUserFontSize = -1;
QString mUserFontFamily;

// platforms, specific
bool mWinOS = false;
bool mAndroidOS = false;
Expand Down

0 comments on commit 8793506

Please sign in to comment.