Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use more robust API to set font size/family overrides
  • Loading branch information
nyalldawson committed May 17, 2023
1 parent fc9101d commit 33ea58d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 76 deletions.
88 changes: 28 additions & 60 deletions src/app/options/qgsoptions.cpp
Expand Up @@ -140,19 +140,13 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti

// stylesheet setup
mStyleSheetBuilder = QgisApp::instance()->styleSheetBuilder();
mStyleSheetNewOpts = mStyleSheetBuilder->defaultOptions();
mStyleSheetOldOpts = QMap<QString, QVariant>( mStyleSheetNewOpts );

spinFontSize->setClearValue( mStyleSheetBuilder->defaultFont().pointSizeF() );

connect( cbxProjectDefaultNew, &QCheckBox::toggled, this, &QgsOptions::cbxProjectDefaultNew_toggled );
connect( leLayerGlobalCrs, &QgsProjectionSelectionWidget::crsChanged, this, &QgsOptions::leLayerGlobalCrs_crsChanged );
connect( lstRasterDrivers, &QTreeWidget::itemDoubleClicked, this, &QgsOptions::lstRasterDrivers_itemDoubleClicked );
connect( mProjectOnLaunchCmbBx, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsOptions::mProjectOnLaunchCmbBx_currentIndexChanged );
connect( spinFontSize, qOverload< double>( &QDoubleSpinBox::valueChanged ), this, &QgsOptions::spinFontSize_valueChanged );
connect( mFontFamilyRadioQt, &QRadioButton::released, this, &QgsOptions::mFontFamilyRadioQt_released );
connect( mFontFamilyRadioCustom, &QRadioButton::released, this, &QgsOptions::mFontFamilyRadioCustom_released );
connect( mFontFamilyComboBox, &QFontComboBox::currentFontChanged, this, &QgsOptions::mFontFamilyComboBox_currentFontChanged );
connect( mProxyTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsOptions::mProxyTypeComboBox_currentIndexChanged );
connect( mCustomVariablesChkBx, &QCheckBox::toggled, this, &QgsOptions::mCustomVariablesChkBx_toggled );
connect( mCurrentVariablesQGISChxBx, &QCheckBox::toggled, this, &QgsOptions::mCurrentVariablesQGISChxBx_toggled );
Expand Down Expand Up @@ -681,21 +675,29 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
mFontFamilyRadioCustom->blockSignals( true );
mFontFamilyComboBox->blockSignals( true );

spinFontSize->setValue( mStyleSheetOldOpts.value( QStringLiteral( "fontPointSize" ) ).toDouble() );
QString fontFamily = mStyleSheetOldOpts.value( QStringLiteral( "fontFamily" ) ).toString();
bool isQtDefault = ( fontFamily == mStyleSheetBuilder->defaultFont().family() );
mFontFamilyRadioQt->setChecked( isQtDefault );
mFontFamilyRadioCustom->setChecked( !isQtDefault );
mFontFamilyComboBox->setEnabled( !isQtDefault );
spinFontSize->setValue( mStyleSheetBuilder->fontSize() );
const QString userFontFamily = mStyleSheetBuilder->userFontFamily();
bool isQtDefault = userFontFamily.isEmpty();
if ( !isQtDefault )
{
QFont *tempFont = new QFont( fontFamily );
const QFont tempFont( userFontFamily );
// is exact family match returned from system?
if ( tempFont->family() == fontFamily )
if ( tempFont.family() != userFontFamily )
{
mFontFamilyComboBox->setCurrentFont( *tempFont );
// fallback to default
isQtDefault = true;
}
delete tempFont;
}
mFontFamilyRadioQt->setChecked( isQtDefault );
mFontFamilyRadioCustom->setChecked( !isQtDefault );
mFontFamilyComboBox->setEnabled( !isQtDefault );
if ( !isQtDefault )
{
mFontFamilyComboBox->setCurrentFont( userFontFamily );
}
else
{
mFontFamilyComboBox->setCurrentFont( mStyleSheetBuilder->defaultFont() );
}

spinFontSize->blockSignals( false );
Expand Down Expand Up @@ -1858,12 +1860,17 @@ void QgsOptions::saveOptions()
//save variables
QgsExpressionContextUtils::setGlobalVariables( mVariableEditor->variablesInActiveScope() );

// save app stylesheet last (in case reset becomes necessary)
if ( mStyleSheetNewOpts != mStyleSheetOldOpts )
// only update stylesheet if changed
const double newFontSize = spinFontSize->value();
const QString newUserFontFamily = mFontFamilyRadioQt->isChecked() ? QString() : mFontFamilyComboBox->currentFont().family();
if ( ( newUserFontFamily.isEmpty() && !mStyleSheetBuilder->userFontFamily().isEmpty() )
|| ( !newUserFontFamily.isEmpty() && newUserFontFamily != mStyleSheetBuilder->fontFamily() )
|| ( newFontSize != mStyleSheetBuilder->fontSize() ) )
{
mStyleSheetBuilder->saveToSettings( mStyleSheetNewOpts );
// trigger an extra style sheet build to propagate saved settings
mStyleSheetBuilder->buildStyleSheet( mStyleSheetNewOpts );
mStyleSheetBuilder->setUserFontSize( newFontSize );
mStyleSheetBuilder->setUserFontFamily( newUserFontFamily );
// trigger a style sheet build to propagate saved settings
mStyleSheetBuilder->updateStyleSheet();
}

mDefaultDatumTransformTableWidget->transformContext().writeSettings();
Expand All @@ -1880,45 +1887,6 @@ void QgsOptions::saveOptions()

void QgsOptions::rejectOptions()
{
// don't reset stylesheet if we don't have to
if ( mStyleSheetNewOpts != mStyleSheetOldOpts )
{
mStyleSheetBuilder->buildStyleSheet( mStyleSheetOldOpts );
}
}

void QgsOptions::spinFontSize_valueChanged( double fontSize )
{
mStyleSheetNewOpts.insert( QStringLiteral( "fontPointSize" ), QVariant( fontSize ) );
mStyleSheetBuilder->buildStyleSheet( mStyleSheetNewOpts );
}

void QgsOptions::mFontFamilyRadioQt_released()
{
if ( mStyleSheetNewOpts.value( QStringLiteral( "fontFamily" ) ).toString() != mStyleSheetBuilder->defaultFont().family() )
{
mStyleSheetNewOpts.insert( QStringLiteral( "fontFamily" ), QVariant( mStyleSheetBuilder->defaultFont().family() ) );
mStyleSheetBuilder->buildStyleSheet( mStyleSheetNewOpts );
}
}

void QgsOptions::mFontFamilyRadioCustom_released()
{
if ( mFontFamilyComboBox->currentFont().family() != mStyleSheetBuilder->defaultFont().family() )
{
mStyleSheetNewOpts.insert( QStringLiteral( "fontFamily" ), QVariant( mFontFamilyComboBox->currentFont().family() ) );
mStyleSheetBuilder->buildStyleSheet( mStyleSheetNewOpts );
}
}

void QgsOptions::mFontFamilyComboBox_currentFontChanged( const QFont &font )
{
if ( mFontFamilyRadioCustom->isChecked()
&& mStyleSheetNewOpts.value( QStringLiteral( "fontFamily" ) ).toString() != font.family() )
{
mStyleSheetNewOpts.insert( QStringLiteral( "fontFamily" ), QVariant( font.family() ) );
mStyleSheetBuilder->buildStyleSheet( mStyleSheetNewOpts );
}
}

void QgsOptions::leLayerGlobalCrs_crsChanged( const QgsCoordinateReferenceSystem &crs )
Expand Down
14 changes: 0 additions & 14 deletions src/app/options/qgsoptions.h
Expand Up @@ -112,18 +112,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
//! Slot to choose path to project to open after launch
void selectProjectOnLaunch();

//! Slot to select the default font point size for app
void spinFontSize_valueChanged(double fontSize );

//! Slot to set font family for app to Qt default
void mFontFamilyRadioQt_released();

//! Slot to set font family for app to custom choice
void mFontFamilyRadioCustom_released();

//! Slot to select custom font family choice for app
void mFontFamilyComboBox_currentFontChanged( const QFont &font );

void mProxyTypeComboBox_currentIndexChanged( int idx );

//! Add a new URL to no proxy URL list
Expand Down Expand Up @@ -292,8 +280,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption

protected:
QgisAppStyleSheet *mStyleSheetBuilder = nullptr;
QMap<QString, QVariant> mStyleSheetNewOpts;
QMap<QString, QVariant> mStyleSheetOldOpts;

static const int PALETTE_COLOR_ROLE = Qt::UserRole + 1;
static const int PALETTE_LABEL_ROLE = Qt::UserRole + 2;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -4104,7 +4104,7 @@ void QgisApp::setTheme( const QString &themeName )

QString theme = themeName;

mStyleSheetBuilder->applyStyleSheet( mStyleSheetBuilder->defaultOptions() );
mStyleSheetBuilder->updateStyleSheet();
QgsApplication::setUITheme( theme );

mActionNewProject->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileNew.svg" ) ) );
Expand Down
37 changes: 36 additions & 1 deletion src/app/qgisappstylesheet.cpp
Expand Up @@ -86,7 +86,7 @@ void QgisAppStyleSheet::applyStyleSheet( const QMap<QString, QVariant> &opts )
else if ( overriddenFontFamily )
ss += QStringLiteral( "* { font-family: \"%1\"} " ).arg( currentFontFamily );
else if ( overriddenFontSize )
ss += QStringLiteral( "* { font-size: %1pt } " ).arg( overriddenFontSize );
ss += QStringLiteral( "* { font-size: %1pt } " ).arg( currentFontSize );
}

if ( mMacStyle )
Expand Down Expand Up @@ -171,6 +171,41 @@ void QgisAppStyleSheet::applyStyleSheet( const QMap<QString, QVariant> &opts )
emit appStyleSheetChanged( ss );
}

void QgisAppStyleSheet::updateStyleSheet()
{
applyStyleSheet( defaultOptions() );
}

void QgisAppStyleSheet::setUserFontSize( double size )
{
QgsSettings settings;
if ( size == mDefaultFont.pointSizeF() || size < 0 )
{
settings.remove( QStringLiteral( "/qgis/stylesheet/fontPointSize" ) );
mUserFontSize = -1;
}
else
{
mUserFontSize = size;
settings.setValue( QStringLiteral( "/qgis/stylesheet/fontPointSize" ), mUserFontSize );
}
}

void QgisAppStyleSheet::setUserFontFamily( const QString &family )
{
QgsSettings settings;
if ( family == mDefaultFont.family() || family.isEmpty() )
{
settings.remove( QStringLiteral( "/qgis/stylesheet/fontFamily" ) );
mUserFontFamily.clear();
}
else
{
mUserFontFamily = family;
settings.setValue( QStringLiteral( "/qgis/stylesheet/fontFamily" ), mUserFontFamily );
}
}

void QgisAppStyleSheet::saveToSettings( const QMap<QString, QVariant> &opts )
{
QgsSettings settings;
Expand Down
15 changes: 15 additions & 0 deletions src/app/qgisappstylesheet.h
Expand Up @@ -45,6 +45,21 @@ class APP_EXPORT QgisAppStyleSheet: public QObject
*/
void applyStyleSheet( const QMap<QString, QVariant> &opts );

/**
* Applies an updated stylesheet using current user settings.
*/
void updateStyleSheet();

/**
* Sets a new user font \a size. Set to -1 to force the default Qt font size to be used.
*/
void setUserFontSize( double size );

/**
* Sets a new user font \a family. Set to an empty string to force the default Qt font family to be used.
*/
void setUserFontFamily( const QString &family );

//! Save changed default option keys/values to user settings
void saveToSettings( const QMap<QString, QVariant> &opts );

Expand Down

0 comments on commit 33ea58d

Please sign in to comment.