2018-02-11 05:10:01 +05:30
|
|
|
/* Copyright 2013-2018 MultiMC Contributors
|
2013-02-26 00:54:46 +05:30
|
|
|
*
|
|
|
|
* 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 07:23:05 +05:30
|
|
|
*
|
2013-02-26 00:54:46 +05:30
|
|
|
* 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 07:30:58 +05:30
|
|
|
#pragma once
|
2013-02-26 00:54:46 +05:30
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QMap>
|
2014-01-01 19:38:40 +05:30
|
|
|
#include <QStringList>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <memory>
|
2013-02-26 00:54:46 +05:30
|
|
|
|
2015-09-05 22:16:57 +05:30
|
|
|
#include "multimc_logic_export.h"
|
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
class Setting;
|
2015-05-23 19:37:47 +05:30
|
|
|
class SettingsObject;
|
|
|
|
|
|
|
|
typedef std::shared_ptr<SettingsObject> SettingsObjectPtr;
|
2013-02-26 00:54:46 +05:30
|
|
|
|
|
|
|
/*!
|
2013-11-04 07:23:05 +05:30
|
|
|
* \brief The SettingsObject handles communicating settings between the application and a
|
|
|
|
*settings file.
|
2013-02-26 00:54:46 +05:30
|
|
|
* The class keeps a list of Setting objects. Each Setting object represents one
|
2013-11-04 07:23:05 +05:30
|
|
|
* of the application's settings. These Setting objects are registered with
|
2013-02-26 00:54:46 +05:30
|
|
|
* a SettingsObject and can be managed similarly to the way a list works.
|
|
|
|
*
|
|
|
|
* \author Andrew Okin
|
|
|
|
* \date 2/22/2013
|
|
|
|
*
|
|
|
|
* \sa Setting
|
|
|
|
*/
|
2015-09-05 22:16:57 +05:30
|
|
|
class MULTIMC_LOGIC_EXPORT SettingsObject : public QObject
|
2013-02-26 00:54:46 +05:30
|
|
|
{
|
|
|
|
Q_OBJECT
|
2015-05-23 19:37:47 +05:30
|
|
|
public:
|
|
|
|
class Lock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Lock(SettingsObjectPtr locked)
|
|
|
|
:m_locked(locked)
|
|
|
|
{
|
|
|
|
m_locked->suspendSave();
|
|
|
|
}
|
|
|
|
~Lock()
|
|
|
|
{
|
|
|
|
m_locked->resumeSave();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SettingsObjectPtr m_locked;
|
|
|
|
};
|
2013-02-26 00:54:46 +05:30
|
|
|
public:
|
|
|
|
explicit SettingsObject(QObject *parent = 0);
|
2014-01-01 19:38:40 +05:30
|
|
|
virtual ~SettingsObject();
|
|
|
|
/*!
|
|
|
|
* Registers an override setting for the given original setting in this settings object
|
2015-09-04 05:40:29 +05:30
|
|
|
* gate decides if the passthrough (true) or the original (false) is used for value
|
2014-01-01 19:38:40 +05:30
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-09-04 05:40:29 +05:30
|
|
|
std::shared_ptr<Setting> registerOverride(std::shared_ptr<Setting> original, std::shared_ptr<Setting> gate);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2015-05-04 04:50:48 +05:30
|
|
|
/*!
|
|
|
|
* 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);
|
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
/*!
|
2014-01-01 19:38:40 +05:30
|
|
|
* Registers the given setting with this SettingsObject and connects the necessary signals.
|
|
|
|
*
|
2013-02-26 00:54:46 +05:30
|
|
|
* This will fail if there is already a setting with the same ID as
|
|
|
|
* the one that is being registered.
|
2014-01-01 19:38:40 +05:30
|
|
|
* \return A valid Setting shared pointer if successful.
|
2013-02-26 00:54:46 +05:30
|
|
|
*/
|
2014-01-01 19:38:40 +05:30
|
|
|
std::shared_ptr<Setting> registerSetting(QStringList synonyms,
|
|
|
|
QVariant defVal = QVariant());
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
/*!
|
2014-01-01 19:38:40 +05:30
|
|
|
* 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.
|
2013-02-26 00:54:46 +05:30
|
|
|
*/
|
2014-01-01 19:38:40 +05:30
|
|
|
std::shared_ptr<Setting> registerSetting(QString id, QVariant defVal = QVariant())
|
|
|
|
{
|
|
|
|
return registerSetting(QStringList(id), defVal);
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
/*!
|
|
|
|
* \brief Gets the setting with the given ID.
|
|
|
|
* \param id The ID of the setting to get.
|
2013-11-04 07:23:05 +05:30
|
|
|
* \return A pointer to the setting with the given ID.
|
2013-02-26 00:54:46 +05:30
|
|
|
* Returns null if there is no setting with the given ID.
|
|
|
|
* \sa operator []()
|
|
|
|
*/
|
2014-01-01 19:38:40 +05:30
|
|
|
std::shared_ptr<Setting> getSetting(const QString &id) const;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
/*!
|
|
|
|
* \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.
|
|
|
|
*/
|
2014-01-01 19:38:40 +05:30
|
|
|
QVariant get(const QString &id) const;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
/*!
|
|
|
|
* \brief Sets the value of the setting with the given ID.
|
2014-07-06 14:45:15 +05:30
|
|
|
* If no setting with the given ID exists, returns false
|
2013-02-26 00:54:46 +05:30
|
|
|
* \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.
|
|
|
|
*/
|
2014-01-01 19:38:40 +05:30
|
|
|
bool set(const QString &id, QVariant value);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-07-16 04:00:32 +05:30
|
|
|
/*!
|
|
|
|
* \brief Reverts the setting with the given ID to default.
|
|
|
|
* \param id The ID of the setting to reset.
|
|
|
|
*/
|
2014-01-01 19:38:40 +05:30
|
|
|
void reset(const QString &id) const;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
/*!
|
|
|
|
* \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.
|
|
|
|
*/
|
2014-01-01 19:38:40 +05:30
|
|
|
bool contains(const QString &id);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2014-03-09 13:13:08 +05:30
|
|
|
/*!
|
|
|
|
* \brief Reloads the settings and emit signals for changed settings
|
|
|
|
* \return True if reloading was successful
|
|
|
|
*/
|
|
|
|
virtual bool reload();
|
|
|
|
|
2015-05-23 19:37:47 +05:30
|
|
|
virtual void suspendSave() = 0;
|
|
|
|
virtual void resumeSave() = 0;
|
2013-02-26 00:54:46 +05:30
|
|
|
signals:
|
|
|
|
/*!
|
|
|
|
* \brief Signal emitted when one of this SettingsObject object's settings changes.
|
2013-11-04 07:23:05 +05:30
|
|
|
* This is usually just connected directly to each Setting object's
|
2014-07-01 05:18:09 +05:30
|
|
|
* SettingChanged() signals.
|
2013-02-26 00:54:46 +05:30
|
|
|
* \param setting A reference to the Setting object that changed.
|
|
|
|
* \param value The Setting object's new value.
|
|
|
|
*/
|
2014-07-01 05:18:09 +05:30
|
|
|
void SettingChanged(const Setting &setting, QVariant value);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-07-16 04:00:32 +05:30
|
|
|
/*!
|
|
|
|
* \brief Signal emitted when one of this SettingsObject object's settings resets.
|
2013-11-04 07:23:05 +05:30
|
|
|
* This is usually just connected directly to each Setting object's
|
2013-07-16 04:00:32 +05:30
|
|
|
* settingReset() signals.
|
|
|
|
* \param setting A reference to the Setting object that changed.
|
|
|
|
*/
|
|
|
|
void settingReset(const Setting &setting);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
protected
|
|
|
|
slots:
|
2013-02-26 00:54:46 +05:30
|
|
|
/*!
|
|
|
|
* \brief Changes a setting.
|
2013-11-04 07:23:05 +05:30
|
|
|
* This slot is usually connected to each Setting object's
|
2014-07-01 05:18:09 +05:30
|
|
|
* SettingChanged() signal. The signal is emitted, causing this slot
|
2013-02-26 00:54:46 +05:30
|
|
|
* 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;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-07-16 04:00:32 +05:30
|
|
|
/*!
|
|
|
|
* \brief Resets a setting.
|
2013-11-04 07:23:05 +05:30
|
|
|
* This slot is usually connected to each Setting object's
|
2013-07-16 04:00:32 +05:30
|
|
|
* 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 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
protected:
|
|
|
|
/*!
|
|
|
|
* \brief Connects the necessary signals to the given Setting.
|
|
|
|
* \param setting The setting to connect.
|
|
|
|
*/
|
2014-01-01 19:38:40 +05:30
|
|
|
void connectSignals(const Setting &setting);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
/*!
|
|
|
|
* \brief Function used by Setting objects to get their values from the SettingsObject.
|
2013-11-04 07:23:05 +05:30
|
|
|
* \param setting The
|
|
|
|
* \return
|
2013-02-26 00:54:46 +05:30
|
|
|
*/
|
|
|
|
virtual QVariant retrieveValue(const Setting &setting) = 0;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
friend class Setting;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 00:54:46 +05:30
|
|
|
private:
|
2014-01-01 19:38:40 +05:30
|
|
|
QMap<QString, std::shared_ptr<Setting>> m_settings;
|
2015-05-23 19:37:47 +05:30
|
|
|
protected:
|
|
|
|
bool m_suspendSave = false;
|
|
|
|
bool m_doSave = false;
|
2013-02-26 00:54:46 +05:30
|
|
|
};
|