Utils: Use a bit more Key and Store

Also, separate the Key into a separate header to minimiz impact
on #include load.

Change-Id: I4f719cad4d23a71a9c228d0b7f5c2409fd6e24d5
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
hjk
2023-08-23 17:51:05 +02:00
parent 6e307be365
commit 2bf886ed73
42 changed files with 152 additions and 130 deletions

View File

@@ -162,6 +162,7 @@ add_qtc_library(Utils
span.h span.h
statuslabel.cpp statuslabel.h statuslabel.cpp statuslabel.h
store.cpp store.h store.cpp store.h
storekey.h
stringtable.cpp stringtable.h stringtable.cpp stringtable.h
stringutils.cpp stringutils.h stringutils.cpp stringutils.h
styleanimator.cpp styleanimator.h styleanimator.cpp styleanimator.h

View File

@@ -63,7 +63,7 @@ public:
std::function<QVariant(const QVariant &)> m_fromSettings; std::function<QVariant(const QVariant &)> m_fromSettings;
QString m_displayName; QString m_displayName;
QString m_settingsKey; // Name of data in settings. Key m_settingsKey; // Name of data in settings.
QString m_tooltip; QString m_tooltip;
QString m_labelText; QString m_labelText;
QPixmap m_labelPixmap; QPixmap m_labelPixmap;
@@ -390,7 +390,7 @@ void BaseAspect::setConfigWidgetCreator(const ConfigWidgetCreator &configWidgetC
\sa setSettingsKey() \sa setSettingsKey()
*/ */
QString BaseAspect::settingsKey() const Key BaseAspect::settingsKey() const
{ {
return d->m_settingsKey; return d->m_settingsKey;
} }
@@ -400,7 +400,7 @@ QString BaseAspect::settingsKey() const
\sa settingsKey() \sa settingsKey()
*/ */
void BaseAspect::setSettingsKey(const QString &key) void BaseAspect::setSettingsKey(const Key &key)
{ {
d->m_settingsKey = key; d->m_settingsKey = key;
} }
@@ -410,7 +410,7 @@ void BaseAspect::setSettingsKey(const QString &key)
\sa settingsKey() \sa settingsKey()
*/ */
void BaseAspect::setSettingsKey(const QString &group, const QString &key) void BaseAspect::setSettingsKey(const Key &group, const Key &key)
{ {
d->m_settingsKey = group + "/" + key; d->m_settingsKey = group + "/" + key;
} }
@@ -569,8 +569,8 @@ void BaseAspect::registerSubWidget(QWidget *widget)
widget->setVisible(d->m_visible); widget->setVisible(d->m_visible);
} }
void BaseAspect::saveToMap(QVariantMap &data, const QVariant &value, void BaseAspect::saveToMap(Store &data, const QVariant &value,
const QVariant &defaultValue, const QString &key) const QVariant &defaultValue, const Key &key)
{ {
if (key.isEmpty()) if (key.isEmpty())
return; return;
@@ -581,7 +581,7 @@ void BaseAspect::saveToMap(QVariantMap &data, const QVariant &value,
} }
/*! /*!
Retrieves the internal value of this BaseAspect from the QVariantMap \a map. Retrieves the internal value of this BaseAspect from the Store \a map.
*/ */
void BaseAspect::fromMap(const Store &map) void BaseAspect::fromMap(const Store &map)
{ {
@@ -592,7 +592,7 @@ void BaseAspect::fromMap(const Store &map)
} }
/*! /*!
Stores the internal value of this BaseAspect into the QVariantMap \a map. Stores the internal value of this BaseAspect into the Store \a map.
*/ */
void BaseAspect::toMap(Store &map) const void BaseAspect::toMap(Store &map) const
{ {
@@ -732,7 +732,7 @@ public:
} }
void makeCheckable(CheckBoxPlacement checkBoxPlacement, const QString &checkerLabel, void makeCheckable(CheckBoxPlacement checkBoxPlacement, const QString &checkerLabel,
const QString &checkerKey, BaseAspect *aspect) const Key &checkerKey, BaseAspect *aspect)
{ {
QTC_ASSERT(!m_checked, return); QTC_ASSERT(!m_checked, return);
m_checkBoxPlacement = checkBoxPlacement; m_checkBoxPlacement = checkBoxPlacement;
@@ -788,7 +788,7 @@ public:
Qt::TextElideMode m_elideMode = Qt::ElideNone; Qt::TextElideMode m_elideMode = Qt::ElideNone;
QString m_placeHolderText; QString m_placeHolderText;
QString m_historyCompleterKey; Key m_historyCompleterKey;
QPointer<ElidingLabel> m_labelDisplay; QPointer<ElidingLabel> m_labelDisplay;
QPointer<FancyLineEdit> m_lineEditDisplay; QPointer<FancyLineEdit> m_lineEditDisplay;
QPointer<ShowPasswordButton> m_showPasswordButton; QPointer<ShowPasswordButton> m_showPasswordButton;
@@ -996,7 +996,7 @@ void StringAspect::setElideMode(Qt::TextElideMode elideMode)
\sa Utils::PathChooser::setExpectedKind() \sa Utils::PathChooser::setExpectedKind()
*/ */
void StringAspect::setHistoryCompleter(const QString &historyCompleterKey) void StringAspect::setHistoryCompleter(const Key &historyCompleterKey)
{ {
d->m_historyCompleterKey = historyCompleterKey; d->m_historyCompleterKey = historyCompleterKey;
if (d->m_lineEditDisplay) if (d->m_lineEditDisplay)
@@ -1202,7 +1202,7 @@ void StringAspect::bufferToGui()
\a checkerKey. \a checkerKey.
*/ */
void StringAspect::makeCheckable(CheckBoxPlacement checkBoxPlacement, void StringAspect::makeCheckable(CheckBoxPlacement checkBoxPlacement,
const QString &checkerLabel, const QString &checkerKey) const QString &checkerLabel, const Key &checkerKey)
{ {
d->m_checkerImpl.makeCheckable(checkBoxPlacement, checkerLabel, checkerKey, this); d->m_checkerImpl.makeCheckable(checkBoxPlacement, checkerLabel, checkerKey, this);
} }
@@ -1242,7 +1242,7 @@ public:
QString m_prompDialogFilter; QString m_prompDialogFilter;
QString m_prompDialogTitle; QString m_prompDialogTitle;
QStringList m_commandVersionArguments; QStringList m_commandVersionArguments;
QString m_historyCompleterKey; Key m_historyCompleterKey;
PathChooser::Kind m_expectedKind = PathChooser::File; PathChooser::Kind m_expectedKind = PathChooser::File;
Environment m_environment; Environment m_environment;
QPointer<PathChooser> m_pathChooserDisplay; QPointer<PathChooser> m_pathChooserDisplay;
@@ -1325,7 +1325,8 @@ void FilePathAspect::setDefaultValue(const QString &filePath)
\a checkerKey. \a checkerKey.
*/ */
void FilePathAspect::makeCheckable(CheckBoxPlacement checkBoxPlacement, void FilePathAspect::makeCheckable(CheckBoxPlacement checkBoxPlacement,
const QString &checkerLabel, const QString &checkerKey) const QString &checkerLabel,
const Key &checkerKey)
{ {
d->m_checkerImpl.makeCheckable(checkBoxPlacement, checkerLabel, checkerKey, this); d->m_checkerImpl.makeCheckable(checkBoxPlacement, checkerLabel, checkerKey, this);
} }
@@ -1545,7 +1546,7 @@ void FilePathAspect::setDisplayFilter(const std::function<QString (const QString
d->m_displayFilter = displayFilter; d->m_displayFilter = displayFilter;
} }
void FilePathAspect::setHistoryCompleter(const QString &historyCompleterKey) void FilePathAspect::setHistoryCompleter(const Key &historyCompleterKey)
{ {
d->m_historyCompleterKey = historyCompleterKey; d->m_historyCompleterKey = historyCompleterKey;
if (d->m_pathChooserDisplay) if (d->m_pathChooserDisplay)
@@ -2608,7 +2609,7 @@ bool AspectContainer::isDirty()
bool AspectContainer::equals(const AspectContainer &other) const bool AspectContainer::equals(const AspectContainer &other) const
{ {
// FIXME: Expensive, but should not really be needed in a fully aspectified world. // FIXME: Expensive, but should not really be needed in a fully aspectified world.
QVariantMap thisMap, thatMap; Store thisMap, thatMap;
toMap(thisMap); toMap(thisMap);
other.toMap(thatMap); other.toMap(thatMap);
return thisMap == thatMap; return thisMap == thatMap;
@@ -2616,7 +2617,7 @@ bool AspectContainer::equals(const AspectContainer &other) const
void AspectContainer::copyFrom(const AspectContainer &other) void AspectContainer::copyFrom(const AspectContainer &other)
{ {
QVariantMap map; Store map;
other.toMap(map); other.toMap(map);
fromMap(map); fromMap(map);
} }

View File

@@ -63,9 +63,9 @@ public:
virtual void setDefaultVariantValue(const QVariant &value); virtual void setDefaultVariantValue(const QVariant &value);
virtual bool isDefaultValue() const; virtual bool isDefaultValue() const;
QString settingsKey() const; Key settingsKey() const;
void setSettingsKey(const QString &settingsKey); void setSettingsKey(const Key &settingsKey);
void setSettingsKey(const QString &group, const QString &key); void setSettingsKey(const Key &group, const Key &key);
QString displayName() const; QString displayName() const;
void setDisplayName(const QString &displayName); void setDisplayName(const QString &displayName);
@@ -230,8 +230,8 @@ protected:
} }
void registerSubWidget(QWidget *widget); void registerSubWidget(QWidget *widget);
static void saveToMap(QVariantMap &data, const QVariant &value, static void saveToMap(Store &data, const QVariant &value,
const QVariant &defaultValue, const QString &key); const QVariant &defaultValue, const Key &key);
protected: protected:
template <class Value> template <class Value>
@@ -534,7 +534,7 @@ public:
void setShowToolTipOnLabel(bool show); void setShowToolTipOnLabel(bool show);
void setDisplayFilter(const std::function<QString (const QString &)> &displayFilter); void setDisplayFilter(const std::function<QString (const QString &)> &displayFilter);
void setPlaceHolderText(const QString &placeHolderText); void setPlaceHolderText(const QString &placeHolderText);
void setHistoryCompleter(const QString &historyCompleterKey); void setHistoryCompleter(const Key &historyCompleterKey);
void setUndoRedoEnabled(bool readOnly); void setUndoRedoEnabled(bool readOnly);
void setAcceptRichText(bool acceptRichText); void setAcceptRichText(bool acceptRichText);
void setMacroExpanderProvider(const MacroExpanderProvider &expanderProvider); void setMacroExpanderProvider(const MacroExpanderProvider &expanderProvider);
@@ -544,7 +544,7 @@ public:
void setAutoApplyOnEditingFinished(bool applyOnEditingFinished); void setAutoApplyOnEditingFinished(bool applyOnEditingFinished);
void setElideMode(Qt::TextElideMode elideMode); void setElideMode(Qt::TextElideMode elideMode);
void makeCheckable(CheckBoxPlacement checkBoxPlacement, const QString &optionalLabel, const QString &optionalBaseKey); void makeCheckable(CheckBoxPlacement checkBoxPlacement, const QString &optionalLabel, const Key &optionalBaseKey);
bool isChecked() const; bool isChecked() const;
void setChecked(bool checked); void setChecked(bool checked);
@@ -608,14 +608,14 @@ public:
void setPlaceHolderText(const QString &placeHolderText); void setPlaceHolderText(const QString &placeHolderText);
void setValidationFunction(const FancyLineEdit::ValidationFunction &validator); void setValidationFunction(const FancyLineEdit::ValidationFunction &validator);
void setDisplayFilter(const std::function<QString (const QString &)> &displayFilter); void setDisplayFilter(const std::function<QString (const QString &)> &displayFilter);
void setHistoryCompleter(const QString &historyCompleterKey); void setHistoryCompleter(const Key &historyCompleterKey);
void setMacroExpanderProvider(const MacroExpanderProvider &expanderProvider); void setMacroExpanderProvider(const MacroExpanderProvider &expanderProvider);
void setShowToolTipOnLabel(bool show); void setShowToolTipOnLabel(bool show);
void setAutoApplyOnEditingFinished(bool applyOnEditingFinished); void setAutoApplyOnEditingFinished(bool applyOnEditingFinished);
void validateInput(); void validateInput();
void makeCheckable(CheckBoxPlacement checkBoxPlacement, const QString &optionalLabel, const QString &optionalBaseKey); void makeCheckable(CheckBoxPlacement checkBoxPlacement, const QString &optionalLabel, const Key &optionalBaseKey);
bool isChecked() const; bool isChecked() const;
void setChecked(bool checked); void setChecked(bool checked);

View File

@@ -151,7 +151,7 @@ void PathChooserDelegate::updateEditorGeometry(QWidget *editor, const QStyleOpti
editor->setGeometry(option.rect); editor->setGeometry(option.rect);
} }
void PathChooserDelegate::setHistoryCompleter(const QString &key) void PathChooserDelegate::setHistoryCompleter(const Key &key)
{ {
m_historyKey = key; m_historyKey = key;
} }

View File

@@ -52,12 +52,12 @@ public:
void updateEditorGeometry(QWidget *editor, void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const override; const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setHistoryCompleter(const QString &key); void setHistoryCompleter(const Key &key);
private: private:
PathChooser::Kind m_kind = PathChooser::ExistingDirectory; PathChooser::Kind m_kind = PathChooser::ExistingDirectory;
QString m_filter; QString m_filter;
QString m_historyKey; Key m_historyKey;
}; };
class QTCREATOR_UTILS_EXPORT CompleterDelegate : public QStyledItemDelegate class QTCREATOR_UTILS_EXPORT CompleterDelegate : public QStyledItemDelegate

View File

@@ -35,13 +35,13 @@ bool DisplayName::usesDefaultValue() const
return m_value.isEmpty(); return m_value.isEmpty();
} }
void DisplayName::toMap(Store &map, const QString &key) const void DisplayName::toMap(Store &map, const Key &key) const
{ {
if (m_forceSerialization || !usesDefaultValue()) if (m_forceSerialization || !usesDefaultValue())
map.insert(key, m_value); map.insert(key, m_value);
} }
void DisplayName::fromMap(const Store &map, const QString &key) void DisplayName::fromMap(const Store &map, const Key &key)
{ {
m_value = map.value(key).toString(); m_value = map.value(key).toString();
} }

View File

@@ -346,7 +346,7 @@ bool FancyLineEdit::hasAutoHideButton(Side side) const
return d->m_iconbutton[side]->hasAutoHide(); return d->m_iconbutton[side]->hasAutoHide();
} }
void FancyLineEdit::setHistoryCompleter(const QString &historyKey, bool restoreLastItemFromHistory) void FancyLineEdit::setHistoryCompleter(const Key &historyKey, bool restoreLastItemFromHistory)
{ {
QTC_ASSERT(!d->m_historyCompleter, return); QTC_ASSERT(!d->m_historyCompleter, return);
d->m_historyCompleter = new HistoryCompleter(historyKey, this); d->m_historyCompleter = new HistoryCompleter(historyKey, this);

View File

@@ -7,6 +7,7 @@
#include "completinglineedit.h" #include "completinglineedit.h"
#include "expected.h" #include "expected.h"
#include "storekey.h"
#include <QAbstractButton> #include <QAbstractButton>
#include <QFuture> #include <QFuture>
@@ -86,7 +87,7 @@ public:
// Completion // Completion
// Enable a history completer with a history of entries. // Enable a history completer with a history of entries.
void setHistoryCompleter(const QString &historyKey, bool restoreLastItemFromHistory = false); void setHistoryCompleter(const Utils::Key &historyKey, bool restoreLastItemFromHistory = false);
// Sets a completer that is not a history completer. // Sets a completer that is not a history completer.
void setSpecialCompleter(QCompleter *completer); void setSpecialCompleter(QCompleter *completer);

View File

@@ -30,8 +30,8 @@ public:
void addEntry(const QString &str); void addEntry(const QString &str);
QStringList list; QStringList list;
QString historyKey; Key historyKey;
QString historyKeyIsLastItemEmpty; Key historyKeyIsLastItemEmpty;
int maxLines = 6; int maxLines = 6;
bool isLastItemEmpty = isLastItemEmptyDefault; bool isLastItemEmpty = isLastItemEmptyDefault;
}; };
@@ -174,17 +174,16 @@ void HistoryCompleterPrivate::addEntry(const QString &str)
isLastItemEmptyDefault); isLastItemEmptyDefault);
} }
HistoryCompleter::HistoryCompleter(const QString &historyKey, QObject *parent) HistoryCompleter::HistoryCompleter(const Key &historyKey, QObject *parent)
: QCompleter(parent), : QCompleter(parent),
d(new HistoryCompleterPrivate) d(new HistoryCompleterPrivate)
{ {
QTC_ASSERT(!historyKey.isEmpty(), return); QTC_ASSERT(!historyKey.isEmpty(), return);
QTC_ASSERT(theSettings, return); QTC_ASSERT(theSettings, return);
d->historyKey = QLatin1String("CompleterHistory/") + historyKey; d->historyKey = "CompleterHistory/" + historyKey;
d->list = theSettings->value(d->historyKey).toStringList(); d->list = theSettings->value(d->historyKey).toStringList();
d->historyKeyIsLastItemEmpty = QLatin1String("CompleterHistory/") d->historyKeyIsLastItemEmpty = "CompleterHistory/" + historyKey + ".IsLastItemEmpty";
+ historyKey + QLatin1String(".IsLastItemEmpty");
d->isLastItemEmpty = theSettings->value(d->historyKeyIsLastItemEmpty, isLastItemEmptyDefault) d->isLastItemEmpty = theSettings->value(d->historyKeyIsLastItemEmpty, isLastItemEmptyDefault)
.toBool(); .toBool();

View File

@@ -5,6 +5,8 @@
#include "utils_global.h" #include "utils_global.h"
#include "storekey.h"
#include <QCompleter> #include <QCompleter>
namespace Utils { namespace Utils {
@@ -17,7 +19,7 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
{ {
public: public:
static void setSettings(QtcSettings *settings); static void setSettings(QtcSettings *settings);
HistoryCompleter(const QString &historyKey, QObject *parent = nullptr); HistoryCompleter(const Key &historyKey, QObject *parent = nullptr);
bool removeHistoryItem(int index); bool removeHistoryItem(int index);
QString historyItem() const; QString historyItem() const;
bool hasHistory() const { return historySize() > 0; } bool hasHistory() const { return historySize() > 0; }

View File

@@ -33,7 +33,7 @@ void MinimizableInfoBars::setPossibleInfoBarEntries(const QList<Utils::InfoBarEn
createActions(); createActions();
} }
void MinimizableInfoBars::setSettingsGroup(const QString &settingsGroup) void MinimizableInfoBars::setSettingsGroup(const Key &settingsGroup)
{ {
m_settingsGroup = settingsGroup; m_settingsGroup = settingsGroup;
} }
@@ -55,10 +55,10 @@ void MinimizableInfoBars::createActions()
} }
} }
QString MinimizableInfoBars::settingsKey(const Id &id) const Key MinimizableInfoBars::settingsKey(const Id &id) const
{ {
QTC_CHECK(!m_settingsGroup.isEmpty()); QTC_CHECK(!m_settingsGroup.isEmpty());
return m_settingsGroup + '/' + SETTINGS_PREFIX + id.toString(); return m_settingsGroup + '/' + SETTINGS_PREFIX + id.name();
} }
void MinimizableInfoBars::createShowInfoBarActions(const ActionCreator &actionCreator) const void MinimizableInfoBars::createShowInfoBarActions(const ActionCreator &actionCreator) const

View File

@@ -7,6 +7,7 @@
#include "id.h" #include "id.h"
#include "infobar.h" #include "infobar.h"
#include "store.h"
#include <QHash> #include <QHash>
#include <QObject> #include <QObject>
@@ -27,7 +28,7 @@ public:
public: public:
explicit MinimizableInfoBars(InfoBar &infoBar); explicit MinimizableInfoBars(InfoBar &infoBar);
void setSettingsGroup(const QString &settingsGroup); void setSettingsGroup(const Key &settingsGroup);
void setPossibleInfoBarEntries(const QList<InfoBarEntry> &entries); void setPossibleInfoBarEntries(const QList<InfoBarEntry> &entries);
void createShowInfoBarActions(const ActionCreator &actionCreator) const; void createShowInfoBarActions(const ActionCreator &actionCreator) const;
@@ -37,7 +38,7 @@ public:
private: private:
void createActions(); void createActions();
QString settingsKey(const Id &id) const; Key settingsKey(const Id &id) const;
bool showInInfoBar(const Id &id) const; bool showInInfoBar(const Id &id) const;
void setShowInInfoBar(const Id &id, bool show); void setShowInInfoBar(const Id &id, bool show);
@@ -46,7 +47,7 @@ private:
void showInfoBar(const Id &id); void showInfoBar(const Id &id);
InfoBar &m_infoBar; InfoBar &m_infoBar;
QString m_settingsGroup; Key m_settingsGroup;
QHash<Id, QAction *> m_actions; QHash<Id, QAction *> m_actions;
QHash<Id, bool> m_isInfoVisible; QHash<Id, bool> m_isInfoVisible;
QHash<Id, InfoBarEntry> m_infoEntries; QHash<Id, InfoBarEntry> m_infoEntries;

View File

@@ -735,7 +735,7 @@ void PathChooser::installLineEditVersionToolTip(QLineEdit *le, const QStringList
ef->setArguments(arguments); ef->setArguments(arguments);
} }
void PathChooser::setHistoryCompleter(const QString &historyKey, bool restoreLastItemFromHistory) void PathChooser::setHistoryCompleter(const Key &historyKey, bool restoreLastItemFromHistory)
{ {
d->m_lineEdit->setHistoryCompleter(historyKey, restoreLastItemFromHistory); d->m_lineEdit->setHistoryCompleter(historyKey, restoreLastItemFromHistory);
} }

View File

@@ -105,7 +105,7 @@ public:
static void installLineEditVersionToolTip(QLineEdit *le, const QStringList &arguments); static void installLineEditVersionToolTip(QLineEdit *le, const QStringList &arguments);
// Enable a history completer with a history of entries. // Enable a history completer with a history of entries.
void setHistoryCompleter(const QString &historyKey, bool restoreLastItemFromHistory = false); void setHistoryCompleter(const Key &historyKey, bool restoreLastItemFromHistory = false);
// Sets a macro expander that is used when producing path and fileName. // Sets a macro expander that is used when producing path and fileName.
// By default, the global expander is used. // By default, the global expander is used.

View File

@@ -5,6 +5,8 @@
#include "utils_global.h" #include "utils_global.h"
#include "store.h"
#include <QSettings> #include <QSettings>
namespace Utils { namespace Utils {
@@ -15,34 +17,34 @@ public:
using QSettings::QSettings; using QSettings::QSettings;
template<typename T> template<typename T>
void setValueWithDefault(const QString &key, const T &val, const T &defaultValue); void setValueWithDefault(const Key &key, const T &val, const T &defaultValue);
template<typename T> template<typename T>
void setValueWithDefault(const QString &key, const T &val); void setValueWithDefault(const Key &key, const T &val);
template<typename T> template<typename T>
static void setValueWithDefault(QSettings *settings, static void setValueWithDefault(QSettings *settings,
const QString &key, const Key &key,
const T &val, const T &val,
const T &defaultValue); const T &defaultValue);
template<typename T> template<typename T>
static void setValueWithDefault(QSettings *settings, const QString &key, const T &val); static void setValueWithDefault(QSettings *settings, const Key &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 Key &key, const T &val, const T &defaultValue)
{ {
setValueWithDefault(this, key, val, defaultValue); setValueWithDefault(this, key, val, defaultValue);
} }
template<typename T> template<typename T>
void QtcSettings::setValueWithDefault(const QString &key, const T &val) void QtcSettings::setValueWithDefault(const Key &key, const T &val)
{ {
setValueWithDefault(this, key, val); setValueWithDefault(this, key, val);
} }
template<typename T> template<typename T>
void QtcSettings::setValueWithDefault(QSettings *settings, void QtcSettings::setValueWithDefault(QSettings *settings,
const QString &key, const Key &key,
const T &val, const T &val,
const T &defaultValue) const T &defaultValue)
{ {
@@ -53,7 +55,7 @@ void QtcSettings::setValueWithDefault(QSettings *settings,
} }
template<typename T> template<typename T>
void QtcSettings::setValueWithDefault(QSettings *settings, const QString &key, const T &val) void QtcSettings::setValueWithDefault(QSettings *settings, const Key &key, const T &val)
{ {
if (val == T()) if (val == T())
settings->remove(key); settings->remove(key);

View File

@@ -3,38 +3,39 @@
#pragma once #pragma once
#include "store.h"
#include <QSettings> #include <QSettings>
#include <QString>
#include <QStringList> #include <QStringList>
#include <QVariant> #include <QVariant>
namespace Utils { namespace Utils {
template <class SettingsClassT> template <class SettingsClassT>
void fromSettings(const QString &postFix, void fromSettings(const Key &postFix,
const QString &category, const Key &category,
QSettings *s, QSettings *s,
SettingsClassT *obj) SettingsClassT *obj)
{ {
QVariantMap map; Store map;
s->beginGroup(category + postFix); s->beginGroup(category + postFix);
const QStringList keys = s->allKeys(); const KeyList keys = s->allKeys();
for (const QString &key : keys) for (const Key &key : keys)
map.insert(key, s->value(key)); map.insert(key, s->value(key));
s->endGroup(); s->endGroup();
obj->fromMap(map); obj->fromMap(map);
} }
template <class SettingsClassT> template <class SettingsClassT>
void toSettings(const QString &postFix, void toSettings(const Key &postFix,
const QString &category, const Key &category,
QSettings *s, QSettings *s,
const SettingsClassT *obj) const SettingsClassT *obj)
{ {
QString group = postFix; Key group = postFix;
if (!category.isEmpty()) if (!category.isEmpty())
group.insert(0, category); group.insert(0, category);
const QVariantMap map = obj->toMap(); const Store map = obj->toMap();
s->beginGroup(group); s->beginGroup(group);
for (auto it = map.constBegin(), end = map.constEnd(); it != end; ++it) for (auto it = map.constBegin(), end = map.constEnd(); it != end; ++it)

View File

@@ -3,12 +3,14 @@
#pragma once #pragma once
#include <QVariantMap> #include "storekey.h"
#include <QMap>
#include <QVariant>
namespace Utils { namespace Utils {
using Key = QString; using KeyList = QList<Key>;
using Data = QVariant; using Store = QMap<Key, QVariant>;
using Store = QMap<Key, Data>;
} // Utils } // Utils

12
src/libs/utils/storekey.h Normal file
View File

@@ -0,0 +1,12 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <QString>
namespace Utils {
using Key = QString;
} // Utils

View File

@@ -295,6 +295,7 @@ Project {
"statuslabel.h", "statuslabel.h",
"store.cpp", "store.cpp",
"store.h", "store.h",
"storekey.h",
"stringtable.cpp", "stringtable.cpp",
"stringtable.h", "stringtable.h",
"stringutils.cpp", "stringutils.cpp",

View File

@@ -769,7 +769,7 @@ bool SettingsDialog::execDialog()
if (!m_running) { if (!m_running) {
m_running = true; m_running = true;
m_finished = false; m_finished = false;
static const QLatin1String kPreferenceDialogSize("Core/PreferenceDialogSize"); static const char kPreferenceDialogSize[] = "Core/PreferenceDialogSize";
const QSize initialSize(kInitialWidth, kInitialHeight); const QSize initialSize(kInitialWidth, kInitialHeight);
resize(ICore::settings()->value(kPreferenceDialogSize, initialSize).toSize()); resize(ICore::settings()->value(kPreferenceDialogSize, initialSize).toSize());
exec(); exec();

View File

@@ -841,7 +841,7 @@ void FolderNavigationWidgetFactory::saveSettings(Utils::QtcSettings *settings,
{ {
auto fnw = qobject_cast<FolderNavigationWidget *>(widget); auto fnw = qobject_cast<FolderNavigationWidget *>(widget);
QTC_ASSERT(fnw, return); QTC_ASSERT(fnw, return);
const QString base = kSettingsBase + QString::number(position); const Key base = kSettingsBase + Key::number(position);
settings->setValueWithDefault(base + kHiddenFilesKey, settings->setValueWithDefault(base + kHiddenFilesKey,
fnw->hiddenFilesFilter(), fnw->hiddenFilesFilter(),
kHiddenFilesDefault); kHiddenFilesDefault);

View File

@@ -269,7 +269,7 @@ void GeneralSettingsWidget::setLanguage(const QString &locale)
dialog.exec(); dialog.exec();
} }
settings->setValueWithDefault(QLatin1String("General/OverrideLanguage"), locale, {}); settings->setValueWithDefault("General/OverrideLanguage", locale, {});
} }
void GeneralSettingsWidget::fillCodecBox() const void GeneralSettingsWidget::fillCodecBox() const

View File

@@ -81,7 +81,7 @@ signals:
void fontChanged(const QFont &font); void fontChanged(const QFont &font);
protected: protected:
void setupFilterUi(const QString &historyKey); void setupFilterUi(const Utils::Key &historyKey);
QString filterText() const; QString filterText() const;
bool filterUsesRegexp() const { return m_filterRegexp; } bool filterUsesRegexp() const { return m_filterRegexp; }
bool filterIsInverted() const { return m_invertFilter; } bool filterIsInverted() const { return m_invertFilter; }

View File

@@ -249,11 +249,9 @@ void NavigationWidget::setFactories(const QList<INavigationWidgetFactory *> &fac
updateToggleText(); updateToggleText();
} }
QString NavigationWidget::settingsGroup() const Key NavigationWidget::settingsGroup() const
{ {
const QString side(d->m_side == Side::Left ? QStringLiteral("Left") return d->m_side == Side::Left ? Key("NavigationLeft") : Key("NavigationRight");
: QStringLiteral("Right"));
return QStringLiteral("Navigation%1").arg(side);
} }
int NavigationWidget::storedWidth() int NavigationWidget::storedWidth()
@@ -400,12 +398,12 @@ void NavigationWidget::saveSettings(QtcSettings *settings)
settings->setValue(settingsKey("VerticalPosition"), saveState()); settings->setValue(settingsKey("VerticalPosition"), saveState());
settings->setValue(settingsKey("Width"), d->m_width); settings->setValue(settingsKey("Width"), d->m_width);
const QString activationKey = QStringLiteral("ActivationPosition."); const Key activationKey = "ActivationPosition.";
const auto keys = NavigationWidgetPrivate::s_activationsMap.keys(); const auto keys = NavigationWidgetPrivate::s_activationsMap.keys();
for (const auto &factoryId : keys) { for (const auto &factoryId : keys) {
const auto &info = NavigationWidgetPrivate::s_activationsMap[factoryId]; const auto &info = NavigationWidgetPrivate::s_activationsMap[factoryId];
if (info.side == d->m_side) if (info.side == d->m_side)
settings->setValue(settingsKey(activationKey + factoryId.toString()), info.position); settings->setValue(settingsKey(activationKey + factoryId.name()), info.position);
} }
} }
@@ -524,9 +522,9 @@ int NavigationWidget::factoryIndex(Id id)
return -1; return -1;
} }
QString NavigationWidget::settingsKey(const QString &key) const Key NavigationWidget::settingsKey(const Key &key) const
{ {
return QStringLiteral("%1/%2").arg(settingsGroup(), key); return settingsGroup() + '/' + key;
} }
QHash<Id, Command *> NavigationWidget::commandMap() const QHash<Id, Command *> NavigationWidget::commandMap() const

View File

@@ -6,6 +6,7 @@
#include <coreplugin/minisplitter.h> #include <coreplugin/minisplitter.h>
#include <utils/id.h> #include <utils/id.h>
#include <utils/store.h>
#include <QHash> #include <QHash>
@@ -69,7 +70,7 @@ public:
void setFactories(const QList<INavigationWidgetFactory*> &factories); void setFactories(const QList<INavigationWidgetFactory*> &factories);
QString settingsGroup() const; Utils::Key settingsGroup() const;
void saveSettings(Utils::QtcSettings *settings); void saveSettings(Utils::QtcSettings *settings);
void restoreSettings(QSettings *settings); void restoreSettings(QSettings *settings);
@@ -98,7 +99,7 @@ private:
void updateToggleText(); void updateToggleText();
Internal::NavigationSubWidget *insertSubItem(int position, int factoryIndex); Internal::NavigationSubWidget *insertSubItem(int position, int factoryIndex);
int factoryIndex(Utils::Id id); int factoryIndex(Utils::Id id);
QString settingsKey(const QString &key) const; Utils::Key settingsKey(const Utils::Key &key) const;
NavigationWidgetPrivate *d; NavigationWidgetPrivate *d;
}; };

View File

@@ -117,7 +117,7 @@ void IOutputPane::setWheelZoomEnabled(bool enabled)
emit wheelZoomEnabledChanged(enabled); emit wheelZoomEnabledChanged(enabled);
} }
void IOutputPane::setupFilterUi(const QString &historyKey) void IOutputPane::setupFilterUi(const Key &historyKey)
{ {
m_filterOutputLineEdit = new FancyLineEdit; m_filterOutputLineEdit = new FancyLineEdit;
m_filterActionRegexp = new QAction(this); m_filterActionRegexp = new QAction(this);

View File

@@ -50,7 +50,7 @@ public:
{ {
} }
QString settingsKey; Key settingsKey;
OutputFormatter formatter; OutputFormatter formatter;
QList<QPair<QString, OutputFormat>> queuedOutput; QList<QPair<QString, OutputFormat>> queuedOutput;
QTimer queueTimer; QTimer queueTimer;
@@ -77,7 +77,7 @@ public:
/*******************/ /*******************/
OutputWindow::OutputWindow(Context context, const QString &settingsKey, QWidget *parent) OutputWindow::OutputWindow(Context context, const Key &settingsKey, QWidget *parent)
: QPlainTextEdit(parent) : QPlainTextEdit(parent)
, d(new Internal::OutputWindowPrivate(document())) , d(new Internal::OutputWindowPrivate(document()))
{ {

View File

@@ -6,6 +6,7 @@
#include "core_global.h" #include "core_global.h"
#include "icontext.h" #include "icontext.h"
#include <utils/storekey.h>
#include <utils/outputformat.h> #include <utils/outputformat.h>
#include <QPlainTextEdit> #include <QPlainTextEdit>
@@ -32,7 +33,7 @@ public:
}; };
Q_DECLARE_FLAGS(FilterModeFlags, FilterModeFlag) Q_DECLARE_FLAGS(FilterModeFlags, FilterModeFlag)
OutputWindow(Context context, const QString &settingsKey, QWidget *parent = nullptr); OutputWindow(Context context, const Utils::Key &settingsKey, QWidget *parent = nullptr);
~OutputWindow() override; ~OutputWindow() override;
void setLineParsers(const QList<Utils::OutputLineParser *> &parsers); void setLineParsers(const QList<Utils::OutputLineParser *> &parsers);

View File

@@ -33,18 +33,18 @@ BehaviorSettings::BehaviorSettings() :
{ {
} }
void BehaviorSettings::toSettings(const QString &category) const void BehaviorSettings::toSettings(const Key &category) const
{ {
Utils::toSettings(QLatin1String(groupPostfix), category, Core::ICore::settings(), this); Utils::toSettings(groupPostfix, category, Core::ICore::settings(), this);
} }
void BehaviorSettings::fromSettings(const QString &category) void BehaviorSettings::fromSettings(const Key &category)
{ {
*this = BehaviorSettings(); *this = BehaviorSettings();
Utils::fromSettings(QLatin1String(groupPostfix), category, Core::ICore::settings(), this); Utils::fromSettings(groupPostfix, category, Core::ICore::settings(), this);
} }
QVariantMap BehaviorSettings::toMap() const Store BehaviorSettings::toMap() const
{ {
return { return {
{mouseHidingKey, m_mouseHiding}, {mouseHidingKey, m_mouseHiding},

View File

@@ -18,8 +18,8 @@ class TEXTEDITOR_EXPORT BehaviorSettings
public: public:
BehaviorSettings(); BehaviorSettings();
void toSettings(const QString &category) const; void toSettings(const Utils::Key &category) const;
void fromSettings(const QString &category); void fromSettings(const Utils::Key &category);
Utils::Store toMap() const; Utils::Store toMap() const;
void fromMap(const Utils::Store &map); void fromMap(const Utils::Store &map);

View File

@@ -33,6 +33,8 @@
#include <QPointer> #include <QPointer>
#include <QSpacerItem> #include <QSpacerItem>
using namespace Utils;
namespace TextEditor { namespace TextEditor {
class BehaviorSettingsPagePrivate : public QObject class BehaviorSettingsPagePrivate : public QObject
@@ -40,7 +42,7 @@ class BehaviorSettingsPagePrivate : public QObject
public: public:
BehaviorSettingsPagePrivate(); BehaviorSettingsPagePrivate();
const QString m_settingsPrefix{"text"}; const Key m_settingsPrefix{"text"};
TextEditor::BehaviorSettingsWidget *m_behaviorWidget = nullptr; TextEditor::BehaviorSettingsWidget *m_behaviorWidget = nullptr;
CodeStylePool *m_defaultCodeStylePool = nullptr; CodeStylePool *m_defaultCodeStylePool = nullptr;

View File

@@ -23,19 +23,19 @@ ExtraEncodingSettings::ExtraEncodingSettings() : m_utf8BomSetting(OnlyKeep)
ExtraEncodingSettings::~ExtraEncodingSettings() = default; ExtraEncodingSettings::~ExtraEncodingSettings() = default;
void ExtraEncodingSettings::toSettings(const QString &category) const void ExtraEncodingSettings::toSettings(const Key &category) const
{ {
Q_UNUSED(category) Q_UNUSED(category)
Utils::toSettings(QLatin1String(kGroupPostfix), QString(), Core::ICore::settings(), this); Utils::toSettings(kGroupPostfix, Key(), Core::ICore::settings(), this);
} }
void ExtraEncodingSettings::fromSettings(const QString &category) void ExtraEncodingSettings::fromSettings(const Key &category)
{ {
Q_UNUSED(category) Q_UNUSED(category)
*this = ExtraEncodingSettings(); *this = ExtraEncodingSettings();
Utils::fromSettings(QLatin1String(kGroupPostfix), QString(), Core::ICore::settings(), this); Utils::fromSettings(kGroupPostfix, Key(), Core::ICore::settings(), this);
} }
QVariantMap ExtraEncodingSettings::toMap() const QVariantMap ExtraEncodingSettings::toMap() const

View File

@@ -16,7 +16,7 @@ public:
~ExtraEncodingSettings(); ~ExtraEncodingSettings();
void toSettings(const QString &category) const; void toSettings(const QString &category) const;
void fromSettings(const QString &category); void fromSettings(const Utils::Key &category);
Utils::Store toMap() const; Utils::Store toMap() const;
void fromMap(const Utils::Store &map); void fromMap(const Utils::Store &map);

View File

@@ -142,8 +142,7 @@ QWidget *FindInFiles::createConfigWidget()
connect(m_directory.data(), &PathChooser::textChanged, this, connect(m_directory.data(), &PathChooser::textChanged, this,
[this] { setSearchDir(m_directory->filePath()); }); [this] { setSearchDir(m_directory->filePath()); });
connect(this, &BaseFileFind::searchDirChanged, m_directory, &PathChooser::setFilePath); connect(this, &BaseFileFind::searchDirChanged, m_directory, &PathChooser::setFilePath);
m_directory->setHistoryCompleter(QLatin1String(HistoryKey), m_directory->setHistoryCompleter(HistoryKey, /*restoreLastItemFromHistory=*/ true);
/*restoreLastItemFromHistory=*/ true);
if (!HistoryCompleter::historyExistsFor(QLatin1String(HistoryKey))) { if (!HistoryCompleter::historyExistsFor(QLatin1String(HistoryKey))) {
auto completer = static_cast<HistoryCompleter *>(m_directory->lineEdit()->completer()); auto completer = static_cast<HistoryCompleter *>(m_directory->lineEdit()->completer());
const QStringList legacyHistory = ICore::settings()->value( const QStringList legacyHistory = ICore::settings()->value(

View File

@@ -27,7 +27,7 @@ public:
bool m_readOnly = false; bool m_readOnly = false;
bool m_temporarilyReadOnly = false; bool m_temporarilyReadOnly = false;
bool m_isAdditionalTabDisabled = false; bool m_isAdditionalTabDisabled = false;
QString m_settingsSuffix; Key m_settingsSuffix;
}; };
} }
@@ -207,17 +207,17 @@ void ICodeStylePreferences::setCurrentDelegate(const QByteArray &id)
setCurrentDelegate(d->m_pool->codeStyle(id)); setCurrentDelegate(d->m_pool->codeStyle(id));
} }
void ICodeStylePreferences::setSettingsSuffix(const QString &suffix) void ICodeStylePreferences::setSettingsSuffix(const Key &suffix)
{ {
d->m_settingsSuffix = suffix; d->m_settingsSuffix = suffix;
} }
void ICodeStylePreferences::toSettings(const QString &category) const void ICodeStylePreferences::toSettings(const Key &category) const
{ {
Utils::toSettings(d->m_settingsSuffix, category, Core::ICore::settings(), this); Utils::toSettings(d->m_settingsSuffix, category, Core::ICore::settings(), this);
} }
void ICodeStylePreferences::fromSettings(const QString &category) void ICodeStylePreferences::fromSettings(const Key &category)
{ {
Utils::fromSettings(d->m_settingsSuffix, category, Core::ICore::settings(), this); Utils::fromSettings(d->m_settingsSuffix, category, Core::ICore::settings(), this);
} }

View File

@@ -64,9 +64,9 @@ public:
QByteArray currentDelegateId() const; QByteArray currentDelegateId() const;
void setCurrentDelegate(const QByteArray &id); void setCurrentDelegate(const QByteArray &id);
void setSettingsSuffix(const QString &suffix); void setSettingsSuffix(const Utils::Key &suffix);
void toSettings(const QString &category) const; void toSettings(const QString &category) const;
void fromSettings(const QString &category); void fromSettings(const Utils::Key &category);
// make below 2 protected? // make below 2 protected?
virtual Utils::Store toMap() const; virtual Utils::Store toMap() const;

View File

@@ -9,7 +9,6 @@
#include <utils/settingsutils.h> #include <utils/settingsutils.h>
#include <QRegularExpression> #include <QRegularExpression>
#include <QString>
using namespace Utils; using namespace Utils;
@@ -34,15 +33,15 @@ StorageSettings::StorageSettings()
{ {
} }
void StorageSettings::toSettings(const QString &category) const void StorageSettings::toSettings(const Key &category) const
{ {
Utils::toSettings(QLatin1String(groupPostfix), category, Core::ICore::settings(), this); Utils::toSettings(groupPostfix, category, Core::ICore::settings(), this);
} }
void StorageSettings::fromSettings(const QString &category) void StorageSettings::fromSettings(const Key &category)
{ {
*this = StorageSettings(); *this = StorageSettings();
Utils::fromSettings(QLatin1String(groupPostfix), category, Core::ICore::settings(), this); Utils::fromSettings(groupPostfix, category, Core::ICore::settings(), this);
} }
Store StorageSettings::toMap() const Store StorageSettings::toMap() const

View File

@@ -14,8 +14,8 @@ class TEXTEDITOR_EXPORT StorageSettings
public: public:
StorageSettings(); StorageSettings();
void toSettings(const QString &category) const; void toSettings(const Utils::Key &category) const;
void fromSettings(const QString &category); void fromSettings(const Utils::Key &category);
Utils::Store toMap() const; Utils::Store toMap() const;
void fromMap(const Utils::Store &map); void fromMap(const Utils::Store &map);

View File

@@ -7,7 +7,6 @@
#include <QDebug> #include <QDebug>
#include <QSettings> #include <QSettings>
#include <QString>
#include <QTextCursor> #include <QTextCursor>
#include <QTextDocument> #include <QTextDocument>
@@ -34,18 +33,18 @@ TabSettings::TabSettings(TabSettings::TabPolicy tabPolicy,
} }
void TabSettings::toSettings(const QString &category, QSettings *s) const void TabSettings::toSettings(const Key &category, QSettings *s) const
{ {
Utils::toSettings(QLatin1String(groupPostfix), category, s, this); Utils::toSettings(groupPostfix, category, s, this);
} }
void TabSettings::fromSettings(const QString &category, QSettings *s) void TabSettings::fromSettings(const Key &category, QSettings *s)
{ {
*this = TabSettings(); // Assign defaults *this = TabSettings(); // Assign defaults
Utils::fromSettings(QLatin1String(groupPostfix), category, s, this); Utils::fromSettings(groupPostfix, category, s, this);
} }
QVariantMap TabSettings::toMap() const Store TabSettings::toMap() const
{ {
return { return {
{spacesForTabsKey, m_tabPolicy != TabsOnlyTabPolicy}, {spacesForTabsKey, m_tabPolicy != TabsOnlyTabPolicy},

View File

@@ -38,8 +38,8 @@ public:
TabSettings(TabPolicy tabPolicy, int tabSize, TabSettings(TabPolicy tabPolicy, int tabSize,
int indentSize, ContinuationAlignBehavior continuationAlignBehavior); int indentSize, ContinuationAlignBehavior continuationAlignBehavior);
void toSettings(const QString &category, QSettings *s) const; void toSettings(const Utils::Key &category, QSettings *s) const;
void fromSettings(const QString &category, QSettings *s); void fromSettings(const Utils::Key &category, QSettings *s);
Utils::Store toMap() const; Utils::Store toMap() const;
void fromMap(const Utils::Store &map); void fromMap(const Utils::Store &map);

View File

@@ -28,15 +28,15 @@ TypingSettings::TypingSettings():
{ {
} }
void TypingSettings::toSettings(const QString &category) const void TypingSettings::toSettings(const Key &category) const
{ {
Utils::toSettings(QLatin1String(groupPostfix), category, Core::ICore::settings(), this); Utils::toSettings(groupPostfix, category, Core::ICore::settings(), this);
} }
void TypingSettings::fromSettings(const QString &category) void TypingSettings::fromSettings(const Key &category)
{ {
*this = TypingSettings(); // Assign defaults *this = TypingSettings(); // Assign defaults
Utils::fromSettings(QLatin1String(groupPostfix), category, Core::ICore::settings(), this); Utils::fromSettings(groupPostfix, category, Core::ICore::settings(), this);
} }
Store TypingSettings::toMap() const Store TypingSettings::toMap() const

View File

@@ -35,8 +35,8 @@ public:
bool tabShouldIndent(const QTextDocument *document, const QTextCursor &cursor, int *suggestedPosition) const; bool tabShouldIndent(const QTextDocument *document, const QTextCursor &cursor, int *suggestedPosition) const;
void toSettings(const QString &category) const; void toSettings(const Utils::Key &category) const;
void fromSettings(const QString &category); void fromSettings(const Utils::Key &category);
Utils::Store toMap() const; Utils::Store toMap() const;
void fromMap(const Utils::Store &map); void fromMap(const Utils::Store &map);