1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-25 15:35:23 +00:00
OpenMW/apps/opencs/model/prefs/setting.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
2.9 KiB
C++
Raw Normal View History

2015-12-08 17:21:58 +01:00
#ifndef CSM_PREFS_SETTING_H
#define CSM_PREFS_SETTING_H
#include <string>
#include <utility>
2023-11-12 00:52:09 +01:00
#include <QMutexLocker>
2015-12-08 17:21:58 +01:00
#include <QObject>
2023-11-12 00:52:09 +01:00
#include <components/settings/settingvalue.hpp>
#include "category.hpp"
2015-12-08 17:21:58 +01:00
class QWidget;
2015-12-11 11:50:06 +01:00
class QColor;
2015-12-15 12:19:48 +01:00
class QMutex;
class QGridLayout;
class QLabel;
2015-12-08 17:21:58 +01:00
namespace CSMPrefs
{
struct SettingWidgets
{
QLabel* mLabel;
QWidget* mInput;
QGridLayout* mLayout;
};
2015-12-08 17:21:58 +01:00
class Setting : public QObject
{
Q_OBJECT
Category* mParent;
2015-12-15 12:19:48 +01:00
QMutex* mMutex;
2015-12-08 17:21:58 +01:00
std::string mKey;
QString mLabel;
2023-11-12 00:52:09 +01:00
Settings::Index& mIndex;
2015-12-08 17:21:58 +01:00
protected:
2015-12-15 12:19:48 +01:00
QMutex* getMutex();
2015-12-08 17:21:58 +01:00
2023-11-12 00:52:09 +01:00
template <class T>
void resetValueImpl()
{
QMutexLocker lock(mMutex);
return mIndex.get<T>(mParent->getKey(), mKey).reset();
}
template <class T>
T getValueImpl() const
{
QMutexLocker lock(mMutex);
return mIndex.get<T>(mParent->getKey(), mKey).get();
}
template <class T>
void setValueImpl(const T& value)
{
QMutexLocker lock(mMutex);
return mIndex.get<T>(mParent->getKey(), mKey).set(value);
}
2022-09-22 21:26:05 +03:00
public:
2023-11-12 00:52:09 +01:00
explicit Setting(
Category* parent, QMutex* mutex, std::string_view key, const QString& label, Settings::Index& index);
2015-12-15 12:19:48 +01:00
~Setting() override = default;
2015-12-08 17:21:58 +01:00
virtual SettingWidgets makeWidgets(QWidget* parent) = 0;
2015-12-08 17:21:58 +01:00
/// Updates the widget returned by makeWidgets() to the current setting.
2022-09-22 21:26:05 +03:00
///
/// \note If make_widgets() has not been called yet then nothing happens.
virtual void updateWidget() = 0;
2015-12-08 17:21:58 +01:00
2023-11-12 00:52:09 +01:00
virtual void reset() = 0;
2015-12-08 17:21:58 +01:00
const Category* getParent() const;
const std::string& getKey() const;
const QString& getLabel() const { return mLabel; }
2015-12-08 17:21:58 +01:00
2023-11-12 00:52:09 +01:00
int toInt() const { return getValueImpl<int>(); }
2015-12-08 17:21:58 +01:00
2023-11-12 00:52:09 +01:00
double toDouble() const { return getValueImpl<double>(); }
2015-12-11 11:50:06 +01:00
2023-11-12 00:52:09 +01:00
std::string toString() const { return getValueImpl<std::string>(); }
2015-12-11 11:50:06 +01:00
2023-11-12 00:52:09 +01:00
bool isTrue() const { return getValueImpl<bool>(); }
2015-12-11 11:50:06 +01:00
QColor toColor() const;
2015-12-08 17:21:58 +01:00
};
2015-12-11 11:50:06 +01:00
2023-11-12 00:52:09 +01:00
template <class T>
class TypedSetting : public Setting
{
public:
using Setting::Setting;
void reset() final
{
resetValueImpl<T>();
updateWidget();
}
T getValue() const { return getValueImpl<T>(); }
void setValue(const T& value) { return setValueImpl(value); }
};
2015-12-11 11:50:06 +01:00
// note: fullKeys have the format categoryKey/settingKey
bool operator==(const Setting& setting, const std::string& fullKey);
bool operator==(const std::string& fullKey, const Setting& setting);
bool operator!=(const Setting& setting, const std::string& fullKey);
bool operator!=(const std::string& fullKey, const Setting& setting);
2015-12-08 17:21:58 +01:00
}
#endif