2021-01-18 07:28:54 +00:00
|
|
|
/* Copyright 2013-2021 MultiMC Contributors
|
2013-02-25 19:24:46 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-11-04 01:53:05 +00:00
|
|
|
*
|
2013-02-25 19:24:46 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-09-07 03:00:58 +01:00
|
|
|
#pragma once
|
2013-02-25 19:24:46 +00:00
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QMap>
|
2014-01-01 14:08:40 +00:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QVariant>
|
2023-02-16 05:01:27 +00:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
2014-01-01 14:08:40 +00:00
|
|
|
#include <memory>
|
2013-02-25 19:24:46 +00:00
|
|
|
|
|
|
|
class Setting;
|
2015-05-23 15:07:47 +01:00
|
|
|
class SettingsObject;
|
|
|
|
|
|
|
|
typedef std::shared_ptr<SettingsObject> SettingsObjectPtr;
|
2022-07-06 19:56:03 +01:00
|
|
|
typedef std::weak_ptr<SettingsObject> SettingsObjectWeakPtr;
|
2013-02-25 19:24:46 +00:00
|
|
|
|
|
|
|
/*!
|
2013-11-04 01:53:05 +00:00
|
|
|
* \brief The SettingsObject handles communicating settings between the application and a
|
|
|
|
*settings file.
|
2013-02-25 19:24:46 +00:00
|
|
|
* The class keeps a list of Setting objects. Each Setting object represents one
|
2013-11-04 01:53:05 +00:00
|
|
|
* of the application's settings. These Setting objects are registered with
|
2013-02-25 19:24:46 +00:00
|
|
|
* a SettingsObject and can be managed similarly to the way a list works.
|
|
|
|
*
|
|
|
|
* \author Andrew Okin
|
|
|
|
* \date 2/22/2013
|
|
|
|
*
|
|
|
|
* \sa Setting
|
|
|
|
*/
|
2021-07-25 18:11:59 +01:00
|
|
|
class SettingsObject : public QObject
|
2013-02-25 19:24:46 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
Q_OBJECT
|
2015-05-23 15:07:47 +01:00
|
|
|
public:
|
2018-07-15 13:51:05 +01:00
|
|
|
class Lock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Lock(SettingsObjectPtr locked)
|
|
|
|
:m_locked(locked)
|
|
|
|
{
|
|
|
|
m_locked->suspendSave();
|
|
|
|
}
|
|
|
|
~Lock()
|
|
|
|
{
|
|
|
|
m_locked->resumeSave();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SettingsObjectPtr m_locked;
|
|
|
|
};
|
2013-02-25 19:24:46 +00:00
|
|
|
public:
|
2018-07-15 13:51:05 +01:00
|
|
|
explicit SettingsObject(QObject *parent = 0);
|
|
|
|
virtual ~SettingsObject();
|
|
|
|
/*!
|
|
|
|
* Registers an override setting for the given original setting in this settings object
|
|
|
|
* gate decides if the passthrough (true) or the original (false) is used for value
|
|
|
|
*
|
|
|
|
* This will fail if there is already a setting with the same ID as
|
|
|
|
* the one that is being registered.
|
|
|
|
* \return A valid Setting shared pointer if successful.
|
|
|
|
*/
|
|
|
|
std::shared_ptr<Setting> registerOverride(std::shared_ptr<Setting> original, std::shared_ptr<Setting> gate);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Registers a passthorugh setting for the given original setting in this settings object
|
|
|
|
* gate decides if the passthrough (true) or the original (false) is used for value
|
|
|
|
*
|
|
|
|
* This will fail if there is already a setting with the same ID as
|
|
|
|
* the one that is being registered.
|
|
|
|
* \return A valid Setting shared pointer if successful.
|
|
|
|
*/
|
|
|
|
std::shared_ptr<Setting> registerPassthrough(std::shared_ptr<Setting> original, std::shared_ptr<Setting> gate);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Registers the given setting with this SettingsObject and connects the necessary signals.
|
|
|
|
*
|
|
|
|
* This will fail if there is already a setting with the same ID as
|
|
|
|
* the one that is being registered.
|
|
|
|
* \return A valid Setting shared pointer if successful.
|
|
|
|
*/
|
|
|
|
std::shared_ptr<Setting> registerSetting(QStringList synonyms,
|
|
|
|
QVariant defVal = QVariant());
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Registers the given setting with this SettingsObject and connects the necessary signals.
|
|
|
|
*
|
|
|
|
* This will fail if there is already a setting with the same ID as
|
|
|
|
* the one that is being registered.
|
|
|
|
* \return A valid Setting shared pointer if successful.
|
|
|
|
*/
|
|
|
|
std::shared_ptr<Setting> registerSetting(QString id, QVariant defVal = QVariant())
|
|
|
|
{
|
|
|
|
return registerSetting(QStringList(id), defVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Gets the setting with the given ID.
|
|
|
|
* \param id The ID of the setting to get.
|
|
|
|
* \return A pointer to the setting with the given ID.
|
|
|
|
* Returns null if there is no setting with the given ID.
|
|
|
|
* \sa operator []()
|
|
|
|
*/
|
|
|
|
std::shared_ptr<Setting> getSetting(const QString &id) const;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Gets the value of the setting with the given ID.
|
|
|
|
* \param id The ID of the setting to get.
|
|
|
|
* \return The setting's value as a QVariant.
|
|
|
|
* If no setting with the given ID exists, returns an invalid QVariant.
|
|
|
|
*/
|
|
|
|
QVariant get(const QString &id) const;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Sets the value of the setting with the given ID.
|
|
|
|
* If no setting with the given ID exists, returns false
|
|
|
|
* \param id The ID of the setting to change.
|
|
|
|
* \param value The new value of the setting.
|
|
|
|
* \return True if successful, false if it failed.
|
|
|
|
*/
|
|
|
|
bool set(const QString &id, QVariant value);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Reverts the setting with the given ID to default.
|
|
|
|
* \param id The ID of the setting to reset.
|
|
|
|
*/
|
|
|
|
void reset(const QString &id) const;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Checks if this SettingsObject contains a setting with the given ID.
|
|
|
|
* \param id The ID to check for.
|
|
|
|
* \return True if the SettingsObject has a setting with the given ID.
|
|
|
|
*/
|
|
|
|
bool contains(const QString &id);
|
|
|
|
|
2023-02-16 05:01:27 +00:00
|
|
|
/*!
|
|
|
|
* \brief Sets the value of the setting with the given ID with a json list.
|
|
|
|
* If no setting with the given ID exists, returns false
|
|
|
|
* \param id The ID of the setting to change.
|
|
|
|
* \param value The new value of the setting.
|
|
|
|
*/
|
|
|
|
bool setList(const QString &id, QVariantList value);
|
|
|
|
template <typename T> bool setList(const QString &id, QList<T> val)
|
|
|
|
{
|
|
|
|
QVariantList variantList;
|
|
|
|
variantList.reserve(val.size());
|
|
|
|
for (const T& v : val)
|
|
|
|
{
|
|
|
|
variantList.append(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return setList(id, variantList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Gets the value of the setting with the given ID as if it were a json list.
|
|
|
|
* \param id The ID of the setting to change.
|
|
|
|
* \return The setting's value as a QVariantList.
|
|
|
|
* If no setting with the given ID exists, returns an empty QVariantList.
|
|
|
|
*/
|
|
|
|
QVariantList getList(const QString &id);
|
|
|
|
template <typename T> QList<T> getList(const QString &id)
|
|
|
|
{
|
|
|
|
QVariantList variantList = this->getList(id);
|
|
|
|
|
|
|
|
QList<T>TList;
|
|
|
|
TList.reserve(variantList.size());
|
|
|
|
for (const QVariant& v : variantList)
|
|
|
|
{
|
|
|
|
TList.append(v.value<T>());
|
|
|
|
}
|
|
|
|
return TList;
|
|
|
|
}
|
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
/*!
|
|
|
|
* \brief Reloads the settings and emit signals for changed settings
|
|
|
|
* \return True if reloading was successful
|
|
|
|
*/
|
|
|
|
virtual bool reload();
|
|
|
|
|
|
|
|
virtual void suspendSave() = 0;
|
|
|
|
virtual void resumeSave() = 0;
|
2013-02-25 19:24:46 +00:00
|
|
|
signals:
|
2018-07-15 13:51:05 +01:00
|
|
|
/*!
|
|
|
|
* \brief Signal emitted when one of this SettingsObject object's settings changes.
|
|
|
|
* This is usually just connected directly to each Setting object's
|
|
|
|
* SettingChanged() signals.
|
|
|
|
* \param setting A reference to the Setting object that changed.
|
|
|
|
* \param value The Setting object's new value.
|
|
|
|
*/
|
|
|
|
void SettingChanged(const Setting &setting, QVariant value);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Signal emitted when one of this SettingsObject object's settings resets.
|
|
|
|
* This is usually just connected directly to each Setting object's
|
|
|
|
* settingReset() signals.
|
|
|
|
* \param setting A reference to the Setting object that changed.
|
|
|
|
*/
|
|
|
|
void settingReset(const Setting &setting);
|
2013-11-04 01:53:05 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
slots:
|
2018-07-15 13:51:05 +01:00
|
|
|
/*!
|
|
|
|
* \brief Changes a setting.
|
|
|
|
* This slot is usually connected to each Setting object's
|
|
|
|
* SettingChanged() signal. The signal is emitted, causing this slot
|
|
|
|
* to update the setting's value in the config file.
|
|
|
|
* \param setting A reference to the Setting object that changed.
|
|
|
|
* \param value The setting's new value.
|
|
|
|
*/
|
|
|
|
virtual void changeSetting(const Setting &setting, QVariant value) = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Resets a setting.
|
|
|
|
* This slot is usually connected to each Setting object's
|
|
|
|
* settingReset() signal. The signal is emitted, causing this slot
|
|
|
|
* to update the setting's value in the config file.
|
|
|
|
* \param setting A reference to the Setting object that changed.
|
|
|
|
*/
|
|
|
|
virtual void resetSetting(const Setting &setting) = 0;
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2013-02-25 19:24:46 +00:00
|
|
|
protected:
|
2018-07-15 13:51:05 +01:00
|
|
|
/*!
|
|
|
|
* \brief Connects the necessary signals to the given Setting.
|
|
|
|
* \param setting The setting to connect.
|
|
|
|
*/
|
|
|
|
void connectSignals(const Setting &setting);
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
/*!
|
|
|
|
* \brief Function used by Setting objects to get their values from the SettingsObject.
|
|
|
|
* \param setting The
|
|
|
|
* \return
|
|
|
|
*/
|
|
|
|
virtual QVariant retrieveValue(const Setting &setting) = 0;
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
friend class Setting;
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2013-02-25 19:24:46 +00:00
|
|
|
private:
|
2018-07-15 13:51:05 +01:00
|
|
|
QMap<QString, std::shared_ptr<Setting>> m_settings;
|
2015-05-23 15:07:47 +01:00
|
|
|
protected:
|
2018-07-15 13:51:05 +01:00
|
|
|
bool m_suspendSave = false;
|
|
|
|
bool m_doSave = false;
|
2013-02-25 19:24:46 +00:00
|
|
|
};
|