Add API for saving settings with default value

We should never actually write default values into the settings, because

- if the default value changes in a later Qt Creator version, the new
  default should automatically take effect if the user didn't change the
  value
- it senselessly grows the settings file

Add a QtcSettings class that extends QSettings by a
"setValueWithDefault" method, which does not write default values to the
settings, and actually removes the settingskey if the user switches back
to the default.

Use it at the places where we already do this manually.

Task-number: QTCREATORBUG-24762
Change-Id: Ia76414cb21e8521f3aeed1e37b43ae4fb3393ea3
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Eike Ziller
2020-11-19 15:34:59 +01:00
parent 8ad9c0111c
commit 41b73594ad
15 changed files with 170 additions and 70 deletions

View File

@@ -110,16 +110,6 @@ static QString defaultFallbackFontStyleName(const QString &fontFamily)
return styles.first();
}
template <typename T>
static void setOrRemoveSetting(const char *key, const T &value, const T &defaultValue)
{
QSettings *settings = Core::ICore::settings();
if (value == defaultValue)
settings->remove(QLatin1String(key));
else
settings->setValue(QLatin1String(key), value);
}
LocalHelpManager::LocalHelpManager(QObject *parent)
: QObject(parent)
{
@@ -179,9 +169,15 @@ QFont LocalHelpManager::fallbackFont()
void LocalHelpManager::setFallbackFont(const QFont &font)
{
setOrRemoveSetting(kFontFamilyKey, font.family(), defaultFallbackFontFamily());
setOrRemoveSetting(kFontStyleNameKey, font.styleName(), defaultFallbackFontStyleName(font.family()));
setOrRemoveSetting(kFontSizeKey, font.pointSize(), kDefaultFallbackFontSize);
Core::ICore::settings()->setValueWithDefault(kFontFamilyKey,
font.family(),
defaultFallbackFontFamily());
Core::ICore::settings()->setValueWithDefault(kFontStyleNameKey,
font.styleName(),
defaultFallbackFontStyleName(font.family()));
Core::ICore::settings()->setValueWithDefault(kFontSizeKey,
font.pointSize(),
kDefaultFallbackFontSize);
emit m_instance->fallbackFontChanged(font);
}
@@ -369,10 +365,7 @@ HelpViewerFactory LocalHelpManager::viewerBackend()
void LocalHelpManager::setViewerBackendId(const QByteArray &id)
{
if (id.isEmpty())
Core::ICore::settings()->remove(kViewerBackend);
else
Core::ICore::settings()->setValue(kViewerBackend, id);
Core::ICore::settings()->setValueWithDefault(kViewerBackend, id, {});
}
QByteArray LocalHelpManager::viewerBackendId()