Utils: Use new settings API

Avoid writing defaults to the settings.

Task-number: QTCREATORBUG-24430
Change-Id: I57193c0c07c02faf95753378bb1c8d86d18fd131
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Eike Ziller
2020-12-22 16:21:45 +01:00
parent b0d2c0d45d
commit fcbb4721ba
8 changed files with 65 additions and 33 deletions

View File

@@ -26,6 +26,7 @@
#include "basetreeview.h" #include "basetreeview.h"
#include "progressindicator.h" #include "progressindicator.h"
#include "qtcsettings.h"
#include "treemodel.h" #include "treemodel.h"
#include <utils/algorithm.h> #include <utils/algorithm.h>
@@ -159,7 +160,7 @@ public:
l.append(column); l.append(column);
l.append(width); l.append(width);
} }
m_settings->setValue(QLatin1String(ColumnKey), l); QtcSettings::setValueWithDefault(m_settings, ColumnKey, l);
m_settings->endGroup(); m_settings->endGroup();
} }
} }

View File

@@ -24,7 +24,9 @@
****************************************************************************/ ****************************************************************************/
#include "checkablemessagebox.h" #include "checkablemessagebox.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "qtcsettings.h"
#include <QApplication> #include <QApplication>
#include <QCheckBox> #include <QCheckBox>

View File

@@ -24,14 +24,13 @@
****************************************************************************/ ****************************************************************************/
#include "historycompleter.h" #include "historycompleter.h"
#include "fancylineedit.h" #include "fancylineedit.h"
#include "qtcassert.h"
#include "qtcsettings.h"
#include "theme/theme.h" #include "theme/theme.h"
#include "utilsicons.h" #include "utilsicons.h"
#include "qtcassert.h"
#include <QSettings>
#include <QItemDelegate> #include <QItemDelegate>
#include <QKeyEvent> #include <QKeyEvent>
#include <QListView> #include <QListView>
@@ -41,7 +40,8 @@
namespace Utils { namespace Utils {
namespace Internal { namespace Internal {
static QSettings *theSettings = nullptr; static QtcSettings *theSettings = nullptr;
const bool isLastItemEmptyDefault = false;
class HistoryCompleterPrivate : public QAbstractListModel class HistoryCompleterPrivate : public QAbstractListModel
{ {
@@ -57,7 +57,7 @@ public:
QString historyKey; QString historyKey;
QString historyKeyIsLastItemEmpty; QString historyKeyIsLastItemEmpty;
int maxLines = 6; int maxLines = 6;
bool isLastItemEmpty = false; bool isLastItemEmpty = isLastItemEmptyDefault;
}; };
class HistoryLineDelegate : public QItemDelegate class HistoryLineDelegate : public QItemDelegate
@@ -157,7 +157,7 @@ bool HistoryCompleterPrivate::removeRows(int row, int count, const QModelIndex &
beginRemoveRows(parent, row, row + count -1); beginRemoveRows(parent, row, row + count -1);
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
list.removeAt(row); list.removeAt(row);
theSettings->setValue(historyKey, list); theSettings->setValueWithDefault(historyKey, list);
endRemoveRows(); endRemoveRows();
return true; return true;
} }
@@ -175,7 +175,9 @@ void HistoryCompleterPrivate::addEntry(const QString &str)
const QString entry = str.trimmed(); const QString entry = str.trimmed();
if (entry.isEmpty()) { if (entry.isEmpty()) {
isLastItemEmpty = true; isLastItemEmpty = true;
theSettings->setValue(historyKeyIsLastItemEmpty, isLastItemEmpty); theSettings->setValueWithDefault(historyKeyIsLastItemEmpty,
isLastItemEmpty,
isLastItemEmptyDefault);
return; return;
} }
int removeIndex = list.indexOf(entry); int removeIndex = list.indexOf(entry);
@@ -185,9 +187,11 @@ void HistoryCompleterPrivate::addEntry(const QString &str)
list.prepend(entry); list.prepend(entry);
list = list.mid(0, maxLines - 1); list = list.mid(0, maxLines - 1);
endResetModel(); endResetModel();
theSettings->setValue(historyKey, list); theSettings->setValueWithDefault(historyKey, list);
isLastItemEmpty = false; isLastItemEmpty = false;
theSettings->setValue(historyKeyIsLastItemEmpty, isLastItemEmpty); theSettings->setValueWithDefault(historyKeyIsLastItemEmpty,
isLastItemEmpty,
isLastItemEmptyDefault);
} }
HistoryCompleter::HistoryCompleter(const QString &historyKey, QObject *parent) HistoryCompleter::HistoryCompleter(const QString &historyKey, QObject *parent)
@@ -201,7 +205,8 @@ HistoryCompleter::HistoryCompleter(const QString &historyKey, QObject *parent)
d->list = theSettings->value(d->historyKey).toStringList(); d->list = theSettings->value(d->historyKey).toStringList();
d->historyKeyIsLastItemEmpty = QLatin1String("CompleterHistory/") d->historyKeyIsLastItemEmpty = QLatin1String("CompleterHistory/")
+ historyKey + QLatin1String(".IsLastItemEmpty"); + historyKey + QLatin1String(".IsLastItemEmpty");
d->isLastItemEmpty = theSettings->value(d->historyKeyIsLastItemEmpty, false).toBool(); d->isLastItemEmpty = theSettings->value(d->historyKeyIsLastItemEmpty, isLastItemEmptyDefault)
.toBool();
setModel(d); setModel(d);
auto popup = new HistoryLineView(d); auto popup = new HistoryLineView(d);
@@ -260,7 +265,7 @@ void HistoryCompleter::addEntry(const QString &str)
d->addEntry(str); d->addEntry(str);
} }
void HistoryCompleter::setSettings(QSettings *settings) void HistoryCompleter::setSettings(QtcSettings *settings)
{ {
Internal::theSettings = settings; Internal::theSettings = settings;
} }

View File

@@ -29,12 +29,10 @@
#include <QCompleter> #include <QCompleter>
QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE
namespace Utils { namespace Utils {
class QtcSettings;
namespace Internal { class HistoryCompleterPrivate; } namespace Internal { class HistoryCompleterPrivate; }
class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
@@ -42,7 +40,7 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
Q_OBJECT Q_OBJECT
public: public:
static void setSettings(QSettings *settings); static void setSettings(QtcSettings *settings);
HistoryCompleter(const QString &historyKey, QObject *parent = nullptr); HistoryCompleter(const QString &historyKey, QObject *parent = nullptr);
bool removeHistoryItem(int index); bool removeHistoryItem(int index);
QString historyItem() const; QString historyItem() const;

View File

@@ -27,6 +27,7 @@
#include "algorithm.h" #include "algorithm.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "qtcsettings.h"
#include "theme/theme.h" #include "theme/theme.h"
#include "utilsicons.h" #include "utilsicons.h"
@@ -192,7 +193,7 @@ void InfoBar::clearGloballySuppressed()
{ {
globallySuppressed.clear(); globallySuppressed.clear();
if (m_settings) if (m_settings)
m_settings->setValue(QLatin1String(C_SUPPRESSED_WARNINGS), QStringList()); m_settings->remove(C_SUPPRESSED_WARNINGS);
} }
bool InfoBar::anyGloballySuppressed() bool InfoBar::anyGloballySuppressed()
@@ -205,7 +206,7 @@ void InfoBar::writeGloballySuppressedToSettings()
if (!m_settings) if (!m_settings)
return; return;
const QStringList list = Utils::transform<QList>(globallySuppressed, &Id::toString); const QStringList list = Utils::transform<QList>(globallySuppressed, &Id::toString);
m_settings->setValue(QLatin1String(C_SUPPRESSED_WARNINGS), list); QtcSettings::setValueWithDefault(m_settings, C_SUPPRESSED_WARNINGS, list);
} }

View File

@@ -40,24 +40,46 @@ public:
void setValueWithDefault(const QString &key, const T &val, const T &defaultValue); void setValueWithDefault(const QString &key, const T &val, const T &defaultValue);
template<typename T> template<typename T>
void setValueWithDefault(const QString &key, const T &val); void setValueWithDefault(const QString &key, const T &val);
template<typename T>
static void setValueWithDefault(QSettings *settings,
const QString &key,
const T &val,
const T &defaultValue);
template<typename T>
static void setValueWithDefault(QSettings *settings, const QString &key, const T &val);
}; };
template<typename T> template<typename T>
void QtcSettings::setValueWithDefault(const QString &key, const T &val, const T &defaultValue) void QtcSettings::setValueWithDefault(const QString &key, const T &val, const T &defaultValue)
{ {
if (val == defaultValue) setValueWithDefault(this, key, val, defaultValue);
remove(key);
else
setValue(key, QVariant::fromValue(val));
} }
template<typename T> template<typename T>
void QtcSettings::setValueWithDefault(const QString &key, const T &val) void QtcSettings::setValueWithDefault(const QString &key, const T &val)
{ {
if (val == T()) setValueWithDefault(this, key, val);
remove(key);
else
setValue(key, QVariant::fromValue(val));
} }
template<typename T>
void QtcSettings::setValueWithDefault(QSettings *settings,
const QString &key,
const T &val,
const T &defaultValue)
{
if (val == defaultValue)
settings->remove(key);
else
settings->setValue(key, QVariant::fromValue(val));
}
template<typename T>
void QtcSettings::setValueWithDefault(QSettings *settings, const QString &key, const T &val)
{
if (val == T())
settings->remove(key);
else
settings->setValue(key, QVariant::fromValue(val));
}
} // namespace Utils } // namespace Utils

View File

@@ -25,9 +25,10 @@
#include <utils/savedaction.h> #include <utils/savedaction.h>
#include <utils/qtcassert.h> #include "pathchooser.h"
#include <utils/pathchooser.h> #include "pathlisteditor.h"
#include <utils/pathlisteditor.h> #include "qtcassert.h"
#include "qtcsettings.h"
#include <QActionGroup> #include <QActionGroup>
#include <QCheckBox> #include <QCheckBox>
@@ -147,7 +148,7 @@ void SavedAction::writeSettings(QSettings *settings)
{ {
if (settingsKey().isEmpty()) if (settingsKey().isEmpty())
return; return;
settings->setValue(settingsKey(), m_value); QtcSettings::setValueWithDefault(settings, settingsKey(), m_value, m_defaultValue);
} }
/* /*

View File

@@ -24,7 +24,9 @@
****************************************************************************/ ****************************************************************************/
#include "unixutils.h" #include "unixutils.h"
#include "fileutils.h" #include "fileutils.h"
#include "qtcsettings.h"
#include <QSettings> #include <QSettings>
#include <QFileInfo> #include <QFileInfo>
@@ -47,7 +49,7 @@ QString UnixUtils::fileBrowser(const QSettings *settings)
void UnixUtils::setFileBrowser(QSettings *settings, const QString &term) void UnixUtils::setFileBrowser(QSettings *settings, const QString &term)
{ {
settings->setValue(QLatin1String("General/FileBrowser"), term); QtcSettings::setValueWithDefault(settings, "General/FileBrowser", term, defaultFileBrowser());
} }