From 498225debdb22d83e591635dbd172cca12476279 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 25 Feb 2013 13:24:46 -0600 Subject: Rewrote the settings system. It may still need some work. --- libsettings/CMakeLists.txt | 10 +- libsettings/include/appsettings.h | 119 --------------------- libsettings/include/basicsettingsobject.h | 44 ++++++++ libsettings/include/setting.h | 101 ++++++++++++++++++ libsettings/include/settingsobject.h | 165 ++++++++++++++++++++++++++++++ libsettings/src/appsettings.cpp | 40 -------- libsettings/src/basicsettingsobject.cpp | 43 ++++++++ libsettings/src/setting.cpp | 42 ++++++++ libsettings/src/settingsobject.cpp | 124 ++++++++++++++++++++++ 9 files changed, 527 insertions(+), 161 deletions(-) delete mode 100644 libsettings/include/appsettings.h create mode 100644 libsettings/include/basicsettingsobject.h create mode 100644 libsettings/include/setting.h create mode 100644 libsettings/include/settingsobject.h delete mode 100644 libsettings/src/appsettings.cpp create mode 100644 libsettings/src/basicsettingsobject.cpp create mode 100644 libsettings/src/setting.cpp create mode 100644 libsettings/src/settingsobject.cpp (limited to 'libsettings') diff --git a/libsettings/CMakeLists.txt b/libsettings/CMakeLists.txt index 2cf72365..4040032d 100644 --- a/libsettings/CMakeLists.txt +++ b/libsettings/CMakeLists.txt @@ -13,11 +13,17 @@ include_directories(${CMAKE_SOURCE_DIR}/libutil/include) SET(LIBSETTINGS_HEADERS include/libsettings_config.h -include/appsettings.h +include/settingsobject.h +include/setting.h + +include/basicsettingsobject.h ) SET(LIBSETTINGS_SOURCES -src/appsettings.cpp +src/settingsobject.cpp +src/setting.cpp + +src/basicsettingsobject.cpp ) # Set the include dir path. diff --git a/libsettings/include/appsettings.h b/libsettings/include/appsettings.h deleted file mode 100644 index 79515618..00000000 --- a/libsettings/include/appsettings.h +++ /dev/null @@ -1,119 +0,0 @@ -/* Copyright 2013 MultiMC Contributors - * - * 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 - * - * 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. - */ - -#ifndef APPSETTINGS_H -#define APPSETTINGS_H - -#include -#include -//#include -#include - -#include -#include - -#include "libsettings_config.h" - -#if WINDOWS -#define JPATHKEY "JavaPathWindows" -#elif OSX -#define JPATHKEY "JavaPathOSX" -#else -#define JPATHKEY "JavaPathLinux" -#endif - -#define DEFINE_SETTING_ADVANCED(funcName, name, valType, defVal) \ - virtual valType get ## funcName() const { return getValue(name, defVal).value(); } \ - virtual void set ## funcName(valType value) { setValue(name, value); } - -#define DEFINE_SETTING(name, valType, defVal) \ - DEFINE_SETTING_ADVANCED(name, STR_VAL(name), valType, defVal) - - -class LIBMMCSETTINGS_EXPORT SettingsBase : public QObject -{ - Q_OBJECT -public: - explicit SettingsBase(QObject *parent = 0); - - // Updates - DEFINE_SETTING(UseDevBuilds, bool, false) - DEFINE_SETTING(AutoUpdate, bool, true) - - // Folders - DEFINE_SETTING(InstanceDir, QString, "instances") - DEFINE_SETTING(CentralModsDir, QString, "mods") - DEFINE_SETTING(LWJGLDir, QString, "lwjgl") - - // Console - DEFINE_SETTING(ShowConsole, bool, true) - DEFINE_SETTING(AutoCloseConsole, bool, true) - - // Toolbar settings - DEFINE_SETTING(InstanceToolbarVisible, bool, true) - DEFINE_SETTING(InstanceToolbarPosition, QPoint, QPoint()) - - // Console Colors - // Currently commented out because QColor is a part of QtGUI -// DEFINE_SETTING(SysMessageColor, QColor, QColor(Qt::blue)) -// DEFINE_SETTING(StdOutColor, QColor, QColor(Qt::black)) -// DEFINE_SETTING(StdErrColor, QColor, QColor(Qt::red)) - - // Window Size - DEFINE_SETTING(LaunchCompatMode, bool, false) - DEFINE_SETTING(LaunchMaximized, bool, false) - DEFINE_SETTING(MinecraftWinWidth, int, 854) - DEFINE_SETTING(MinecraftWinHeight, int, 480) - - // Auto login - DEFINE_SETTING(AutoLogin, bool, false) - - // Memory - DEFINE_SETTING(MinMemAlloc, int, 512) - DEFINE_SETTING(MaxMemAlloc, int, 1024) - - // Java Settings - DEFINE_SETTING_ADVANCED(JavaPath, JPATHKEY, QString, "java") - DEFINE_SETTING(JvmArgs, QString, "") - - // Custom Commands - DEFINE_SETTING(PreLaunchCommand, QString, "") - DEFINE_SETTING(PostExitCommand, QString, "") - - virtual QVariant getValue(const QString& name, QVariant defVal = QVariant()) const = 0; - virtual void setValue(const QString& name, QVariant val) = 0; -}; - -class LIBMMCSETTINGS_EXPORT AppSettings : public SettingsBase -{ - Q_OBJECT -public: - explicit AppSettings(QObject *parent = 0); - - QSettings& getConfig() { return config; } - - virtual QVariant getValue(const QString &name, QVariant defVal = QVariant()) const; - virtual void setValue(const QString& name, QVariant val); - -protected: - QSettings config; -}; - -#undef DEFINE_SETTING_ADVANCED -#undef DEFINE_SETTING - -LIBMMCSETTINGS_EXPORT extern AppSettings* settings; - -#endif // APPSETTINGS_H diff --git a/libsettings/include/basicsettingsobject.h b/libsettings/include/basicsettingsobject.h new file mode 100644 index 00000000..ec592b1e --- /dev/null +++ b/libsettings/include/basicsettingsobject.h @@ -0,0 +1,44 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 + * + * 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. + */ + +#ifndef BASICSETTINGSOBJECT_H +#define BASICSETTINGSOBJECT_H + +#include +#include + +#include "settingsobject.h" + +#include "libsettings_config.h" + +/*! + * \brief A settings object that stores its settings in a QSettings object. + */ +class LIBMMCSETTINGS_EXPORT BasicSettingsObject : public SettingsObject +{ + Q_OBJECT +public: + explicit BasicSettingsObject(QObject *parent = 0); + +protected slots: + virtual void changeSetting(const Setting &setting, QVariant value); + +protected: + virtual QVariant retrieveValue(const Setting &setting); + + QSettings config; +}; + +#endif // BASICSETTINGSOBJECT_H diff --git a/libsettings/include/setting.h b/libsettings/include/setting.h new file mode 100644 index 00000000..49185347 --- /dev/null +++ b/libsettings/include/setting.h @@ -0,0 +1,101 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 + * + * 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. + */ + +#ifndef SETTING_H +#define SETTING_H + +#include +#include + +#include "libsettings_config.h" + +class SettingsObject; + +/*! + * + */ +class LIBMMCSETTINGS_EXPORT Setting : public QObject +{ + Q_OBJECT +public: + /*! + * \brief Constructs a new Setting object with the given parent. + * \param parent The Setting's parent object. + */ + explicit Setting(QString id, QVariant defVal = QVariant(), QObject *parent = 0); + + /*! + * \brief Gets this setting's ID. + * This is used to refer to the setting within the application. + * \warning Changing the ID while the setting is registered with a SettingsObject results in undefined behavior. + * \return The ID of the setting. + */ + virtual QString id() const { return m_id; } + + /*! + * \brief Gets this setting's config file key. + * This is used to store the setting's value in the config file. It is usually + * the same as the setting's ID, but it can be different. + * \return The setting's config file key. + */ + virtual QString configKey() const { return id(); } + + /*! + * \brief Gets this setting's value as a QVariant. + * This is done by calling the SettingsObject's retrieveValue() function. + * If this Setting doesn't have a SettingsObject, this returns an invalid QVariant. + * \return QVariant containing this setting's value. + * \sa value() + */ + virtual QVariant get() const; + + /*! + * \brief Gets this setting's actual value (I.E. not as a QVariant). + * This function is just shorthand for get().value() + * \return The setting's actual value. + */ + template + inline T value() const { return get().value(); } + + + /*! + * \brief Gets this setting's default value. + * \return The default value of this setting. + */ + virtual QVariant defValue() const; + +signals: + /*! + * \brief Signal emitted when this Setting object's value changes. + * \param setting A reference to the Setting that changed. + * \param value This Setting object's new value. + */ + void settingChanged(const Setting &setting, QVariant value); + +public slots: + /*! + * \brief Changes the setting's value. + * This is done by emitting the settingChanged() signal which will then be + * handled by the SettingsObject object and cause the setting to change. + * \param value The new value. + */ + virtual void set(QVariant value); + +protected: + QString m_id; + QVariant m_defVal; +}; + +#endif // SETTING_H diff --git a/libsettings/include/settingsobject.h b/libsettings/include/settingsobject.h new file mode 100644 index 00000000..3f09b72c --- /dev/null +++ b/libsettings/include/settingsobject.h @@ -0,0 +1,165 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 + * + * 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. + */ + +#ifndef SETTINGSOBJECT_H +#define SETTINGSOBJECT_H + +#include +#include + +#include "libsettings_config.h" + +class Setting; + +/*! + * \brief The SettingsObject handles communicating settings between the application and a settings file. + * The class keeps a list of Setting objects. Each Setting object represents one + * of the application's settings. These Setting objects are registered with + * a SettingsObject and can be managed similarly to the way a list works. + * + * \author Andrew Okin + * \date 2/22/2013 + * + * \sa Setting + */ +class LIBMMCSETTINGS_EXPORT SettingsObject : public QObject +{ + Q_OBJECT +public: + explicit SettingsObject(QObject *parent = 0); + + /*! + * \brief 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. + * \note Registering a setting object causes the SettingsObject to take ownership + * of the object. This means that setting's parent will be set to the object + * it was registered with. Because the object it was registered with has taken + * ownership, it becomes responsible for managing that setting object's memory. + * \warning Do \b not delete the setting after registering it. + * \param setting A pointer to the setting that will be registered. + * \return True if successful. False if registry failed. + */ + virtual bool registerSetting(Setting *setting); + + /*! + * \brief Unregisters the given setting from this SettingsObject and disconnects its signals. + * \note This does not delete the setting. Furthermore, when the setting is + * unregistered, the SettingsObject drops ownership of the setting. This means + * that if you unregister a setting, its parent is set to null and you become + * responsible for freeing its memory. + * \param setting The setting to unregister. + */ + virtual void unregisterSetting(Setting *setting); + + + /*! + * \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 []() + */ + virtual Setting *getSetting(const QString &id) const; + + /*! + * \brief Same as getSetting() + * \param id The ID of the setting to get. + * \return A pointer to the setting with the given ID. + * \sa getSetting() + */ + inline Setting *operator [](const QString &id) { return getSetting(id); } + + + /*! + * \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. + */ + virtual 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 and logs to qDebug + * \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. + */ + virtual bool set(const QString &id, QVariant value); + + + /*! + * \brief Gets a QList with pointers to all of the registered settings. + * The order of the entries in the list is undefined. + * \return A QList with pointers to all registered settings. + */ + virtual QList getSettings(); + + /*! + * \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. + */ + virtual bool contains(const QString &id); + +signals: + /*! + * \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); + +protected slots: + /*! + * \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; + +protected: + /*! + * \brief Connects the necessary signals to the given Setting. + * \param setting The setting to connect. + */ + virtual void connectSignals(const Setting &setting); + + /*! + * \brief Disconnects signals from the given Setting. + * \param setting The setting to disconnect. + */ + virtual void disconnectSignals(const Setting &setting); + + /*! + * \brief Function used by Setting objects to get their values from the SettingsObject. + * \param setting The + * \return + */ + virtual QVariant retrieveValue(const Setting &setting) = 0; + + friend class Setting; + +private: + QMap m_settings; +}; + +#endif // SETTINGSOBJECT_H diff --git a/libsettings/src/appsettings.cpp b/libsettings/src/appsettings.cpp deleted file mode 100644 index 8fe9b8ad..00000000 --- a/libsettings/src/appsettings.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright 2013 MultiMC Contributors - * - * 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 - * - * 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. - */ - -#include "include/appsettings.h" - -AppSettings* settings; - -SettingsBase::SettingsBase(QObject *parent) : - QObject(parent) -{ - -} - -AppSettings::AppSettings(QObject *parent) : - SettingsBase(parent) -{ - -} - -QVariant AppSettings::getValue(const QString& name, QVariant defVal) const -{ - return config.value(name, defVal); -} - -void AppSettings::setValue(const QString& name, QVariant val) -{ - config.setValue(name, val); -} diff --git a/libsettings/src/basicsettingsobject.cpp b/libsettings/src/basicsettingsobject.cpp new file mode 100644 index 00000000..66a2c2c8 --- /dev/null +++ b/libsettings/src/basicsettingsobject.cpp @@ -0,0 +1,43 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 + * + * 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. + */ + +#include "include/basicsettingsobject.h" +#include "include/setting.h" + +BasicSettingsObject::BasicSettingsObject(QObject *parent) : + SettingsObject(parent) +{ + +} + +void BasicSettingsObject::changeSetting(const Setting &setting, QVariant value) +{ + if (contains(setting.id())) + { + config.setValue(setting.configKey(), value); + } +} + +QVariant BasicSettingsObject::retrieveValue(const Setting &setting) +{ + if (contains(setting.id())) + { + return config.value(setting.configKey()); + } + else + { + return QVariant(); + } +} diff --git a/libsettings/src/setting.cpp b/libsettings/src/setting.cpp new file mode 100644 index 00000000..a224ad39 --- /dev/null +++ b/libsettings/src/setting.cpp @@ -0,0 +1,42 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 + * + * 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. + */ + +#include "include/setting.h" +#include "include/settingsobject.h" + +Setting::Setting(QString id, QVariant defVal, QObject *parent) : + QObject(parent), m_id(id), m_defVal(defVal) +{ + +} + +QVariant Setting::get() const +{ + SettingsObject *sbase = qobject_cast(parent()); + if (!sbase) + return defValue(); + else + return sbase->retrieveValue(*this); +} + +QVariant Setting::defValue() const +{ + return m_defVal; +} + +void Setting::set(QVariant value) +{ + emit settingChanged(*this, value); +} diff --git a/libsettings/src/settingsobject.cpp b/libsettings/src/settingsobject.cpp new file mode 100644 index 00000000..27fbb40a --- /dev/null +++ b/libsettings/src/settingsobject.cpp @@ -0,0 +1,124 @@ +/* Copyright 2013 MultiMC Contributors + * + * 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 + * + * 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. + */ + +#include "include/settingsobject.h" +#include "include/setting.h" + +#include + +SettingsObject::SettingsObject(QObject *parent) : + QObject(parent) +{ + +} + +bool SettingsObject::registerSetting(Setting *setting) +{ + // Check if setting is null or we already have a setting with the same ID. + if (!setting) + { + qDebug(QString("Failed to register setting. Setting is null."). + arg(setting->id()).toUtf8()); + return false; // Fail + } + + if (contains(setting->id())) + { + qDebug(QString("Failed to register setting %1. ID already exists."). + arg(setting->id()).toUtf8()); + return false; // Fail + } + + m_settings.insert(setting->id(), setting); + setting->setParent(this); // Take ownership. + + // Connect signals. + connectSignals(*setting); + + qDebug(QString("Registered setting %1.").arg(setting->id()).toUtf8()); + return true; +} + +void SettingsObject::unregisterSetting(Setting *setting) +{ + if (!setting || !m_settings.contains(setting->id())) + return; // We can't unregister something that's not registered. + + m_settings.remove(setting->id()); + + // Disconnect signals. + disconnectSignals(*setting); + + setting->setParent(NULL); // Drop ownership. +} + + +Setting *SettingsObject::getSetting(const QString &id) const +{ + // Make sure there is a setting with the given ID. + if (!m_settings.contains(id)) + return NULL; + + return m_settings[id]; +} + +QVariant SettingsObject::get(const QString &id) const +{ + Setting *setting = getSetting(id); + return (setting ? setting->get() : QVariant()); +} + +bool SettingsObject::set(const QString &id, QVariant value) +{ + Setting *setting = getSetting(id); + if (!setting) + { + qDebug(QString("Error changing setting %1. Setting doesn't exist."). + arg(id).toUtf8()); + return false; + } + else + { + setting->set(value); + return true; + } +} + +QList SettingsObject::getSettings() +{ + return m_settings.values(); +} + +bool SettingsObject::contains(const QString &id) +{ + return m_settings.contains(id); +} + + +void SettingsObject::connectSignals(const Setting &setting) +{ + connect(&setting, SIGNAL(settingChanged(const Setting &, QVariant)), + SLOT(changeSetting(const Setting &, QVariant))); + connect(&setting, SIGNAL(settingChanged(const Setting &, QVariant)), + SIGNAL(settingChanged(const Setting &, QVariant))); +} + +void SettingsObject::disconnectSignals(const Setting &setting) +{ + setting.disconnect(SIGNAL(settingChanged(const Setting &, QVariant)), + this, SLOT(changeSetting(const Setting &, QVariant))); + setting.disconnect(SIGNAL(settingChanged(const Setting &, QVariant)), + this, SIGNAL(settingChanged(const Setting &, QVariant))); +} -- cgit v1.2.3