Skip to content

Commit

Permalink
Merge branch 'settings_values_editor_2' into 'master'
Browse files Browse the repository at this point in the history
Use settings values for editor (#6876)

See merge request OpenMW/openmw!3633
  • Loading branch information
Assumeru committed Dec 12, 2023
2 parents ad8392b + e1a68d8 commit 78da1eb
Show file tree
Hide file tree
Showing 28 changed files with 220 additions and 250 deletions.
16 changes: 5 additions & 11 deletions apps/opencs/model/prefs/boolsetting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
#include "state.hpp"

CSMPrefs::BoolSetting::BoolSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, bool default_)
: Setting(parent, mutex, key, label)
, mDefault(default_)
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index)
: TypedSetting(parent, mutex, key, label, index)
, mWidget(nullptr)
{
}
Expand All @@ -27,7 +26,7 @@ CSMPrefs::BoolSetting& CSMPrefs::BoolSetting::setTooltip(const std::string& tool
CSMPrefs::SettingWidgets CSMPrefs::BoolSetting::makeWidgets(QWidget* parent)
{
mWidget = new QCheckBox(getLabel(), parent);
mWidget->setCheckState(mDefault ? Qt::Checked : Qt::Unchecked);
mWidget->setCheckState(getValue() ? Qt::Checked : Qt::Unchecked);

if (!mTooltip.empty())
{
Expand All @@ -44,17 +43,12 @@ void CSMPrefs::BoolSetting::updateWidget()
{
if (mWidget)
{
mWidget->setCheckState(
Settings::Manager::getBool(getKey(), getParent()->getKey()) ? Qt::Checked : Qt::Unchecked);
mWidget->setCheckState(getValue() ? Qt::Checked : Qt::Unchecked);
}
}

void CSMPrefs::BoolSetting::valueChanged(int value)
{
{
QMutexLocker lock(getMutex());
Settings::Manager::setBool(getKey(), getParent()->getKey(), value);
}

setValue(value != Qt::Unchecked);
getParent()->getState()->update(*this);
}
6 changes: 3 additions & 3 deletions apps/opencs/model/prefs/boolsetting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ namespace CSMPrefs
{
class Category;

class BoolSetting : public Setting
class BoolSetting final : public TypedSetting<bool>
{
Q_OBJECT

std::string mTooltip;
bool mDefault;
QCheckBox* mWidget;

public:
BoolSetting(Category* parent, QMutex* mutex, const std::string& key, const QString& label, bool default_);
explicit BoolSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index);

BoolSetting& setTooltip(const std::string& tooltip);

Expand Down
18 changes: 14 additions & 4 deletions apps/opencs/model/prefs/category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "setting.hpp"
#include "state.hpp"
#include "subcategory.hpp"

CSMPrefs::Category::Category(State* parent, const std::string& key)
: mParent(parent)
Expand All @@ -23,6 +24,14 @@ CSMPrefs::State* CSMPrefs::Category::getState() const
}

void CSMPrefs::Category::addSetting(Setting* setting)
{
if (!mIndex.emplace(setting->getKey(), setting).second)
throw std::logic_error("Category " + mKey + " already has setting: " + setting->getKey());

mSettings.push_back(setting);
}

void CSMPrefs::Category::addSubcategory(Subcategory* setting)
{
mSettings.push_back(setting);
}
Expand All @@ -39,11 +48,12 @@ CSMPrefs::Category::Iterator CSMPrefs::Category::end()

CSMPrefs::Setting& CSMPrefs::Category::operator[](const std::string& key)
{
for (Iterator iter = mSettings.begin(); iter != mSettings.end(); ++iter)
if ((*iter)->getKey() == key)
return **iter;
const auto it = mIndex.find(key);

if (it != mIndex.end())
return *it->second;

throw std::logic_error("Invalid user setting: " + key);
throw std::logic_error("Invalid user setting in " + mKey + " category: " + key);
}

void CSMPrefs::Category::update()
Expand Down
5 changes: 5 additions & 0 deletions apps/opencs/model/prefs/category.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector>

namespace CSMPrefs
{
class State;
class Setting;
class Subcategory;

class Category
{
Expand All @@ -20,6 +22,7 @@ namespace CSMPrefs
State* mParent;
std::string mKey;
Container mSettings;
std::unordered_map<std::string, Setting*> mIndex;

public:
Category(State* parent, const std::string& key);
Expand All @@ -30,6 +33,8 @@ namespace CSMPrefs

void addSetting(Setting* setting);

void addSubcategory(Subcategory* setting);

Iterator begin();

Iterator end();
Expand Down
17 changes: 5 additions & 12 deletions apps/opencs/model/prefs/coloursetting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
#include "state.hpp"

CSMPrefs::ColourSetting::ColourSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, QColor default_)
: Setting(parent, mutex, key, label)
, mDefault(std::move(default_))
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index)
: TypedSetting(parent, mutex, key, label, index)
, mWidget(nullptr)
{
}
Expand All @@ -31,7 +30,7 @@ CSMPrefs::SettingWidgets CSMPrefs::ColourSetting::makeWidgets(QWidget* parent)
{
QLabel* label = new QLabel(getLabel(), parent);

mWidget = new CSVWidget::ColorEditor(mDefault, parent);
mWidget = new CSVWidget::ColorEditor(toColor(), parent);

if (!mTooltip.empty())
{
Expand All @@ -48,18 +47,12 @@ CSMPrefs::SettingWidgets CSMPrefs::ColourSetting::makeWidgets(QWidget* parent)
void CSMPrefs::ColourSetting::updateWidget()
{
if (mWidget)
{
mWidget->setColor(QString::fromStdString(Settings::Manager::getString(getKey(), getParent()->getKey())));
}
mWidget->setColor(toColor());
}

void CSMPrefs::ColourSetting::valueChanged()
{
CSVWidget::ColorEditor& widget = dynamic_cast<CSVWidget::ColorEditor&>(*sender());
{
QMutexLocker lock(getMutex());
Settings::Manager::setString(getKey(), getParent()->getKey(), widget.color().name().toUtf8().data());
}

setValue(widget.color().name().toStdString());
getParent()->getState()->update(*this);
}
7 changes: 4 additions & 3 deletions apps/opencs/model/prefs/coloursetting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ namespace CSVWidget
namespace CSMPrefs
{
class Category;
class ColourSetting : public Setting

class ColourSetting final : public TypedSetting<std::string>
{
Q_OBJECT

std::string mTooltip;
QColor mDefault;
CSVWidget::ColorEditor* mWidget;

public:
ColourSetting(Category* parent, QMutex* mutex, const std::string& key, const QString& label, QColor default_);
explicit ColourSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index);

ColourSetting& setTooltip(const std::string& tooltip);

Expand Down
17 changes: 5 additions & 12 deletions apps/opencs/model/prefs/doublesetting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
#include "state.hpp"

CSMPrefs::DoubleSetting::DoubleSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, double default_)
: Setting(parent, mutex, key, label)
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index)
: TypedSetting(parent, mutex, key, label, index)
, mPrecision(2)
, mMin(0)
, mMax(std::numeric_limits<double>::max())
, mDefault(default_)
, mWidget(nullptr)
{
}
Expand Down Expand Up @@ -63,7 +62,7 @@ CSMPrefs::SettingWidgets CSMPrefs::DoubleSetting::makeWidgets(QWidget* parent)
mWidget = new QDoubleSpinBox(parent);
mWidget->setDecimals(mPrecision);
mWidget->setRange(mMin, mMax);
mWidget->setValue(mDefault);
mWidget->setValue(getValue());

if (!mTooltip.empty())
{
Expand All @@ -80,17 +79,11 @@ CSMPrefs::SettingWidgets CSMPrefs::DoubleSetting::makeWidgets(QWidget* parent)
void CSMPrefs::DoubleSetting::updateWidget()
{
if (mWidget)
{
mWidget->setValue(Settings::Manager::getFloat(getKey(), getParent()->getKey()));
}
mWidget->setValue(getValue());
}

void CSMPrefs::DoubleSetting::valueChanged(double value)
{
{
QMutexLocker lock(getMutex());
Settings::Manager::setFloat(getKey(), getParent()->getKey(), value);
}

setValue(value);
getParent()->getState()->update(*this);
}
6 changes: 3 additions & 3 deletions apps/opencs/model/prefs/doublesetting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ namespace CSMPrefs
{
class Category;

class DoubleSetting : public Setting
class DoubleSetting final : public TypedSetting<double>
{
Q_OBJECT

int mPrecision;
double mMin;
double mMax;
std::string mTooltip;
double mDefault;
QDoubleSpinBox* mWidget;

public:
DoubleSetting(Category* parent, QMutex* mutex, const std::string& key, const QString& label, double default_);
explicit DoubleSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index);

DoubleSetting& setPrecision(int precision);

Expand Down
24 changes: 8 additions & 16 deletions apps/opencs/model/prefs/enumsetting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ CSMPrefs::EnumValues& CSMPrefs::EnumValues::add(const std::string& value, const
}

CSMPrefs::EnumSetting::EnumSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, const EnumValue& default_)
: Setting(parent, mutex, key, label)
, mDefault(default_)
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index)
: TypedSetting(parent, mutex, key, label, index)
, mWidget(nullptr)
{
}
Expand Down Expand Up @@ -83,16 +82,18 @@ CSMPrefs::SettingWidgets CSMPrefs::EnumSetting::makeWidgets(QWidget* parent)
mWidget = new QComboBox(parent);

size_t index = 0;
const std::string value = getValue();

for (size_t i = 0; i < mValues.mValues.size(); ++i)
{
if (mDefault.mValue == mValues.mValues[i].mValue)
if (value == mValues.mValues[i].mValue)
index = i;

mWidget->addItem(QString::fromUtf8(mValues.mValues[i].mValue.c_str()));

if (!mValues.mValues[i].mTooltip.empty())
mWidget->setItemData(i, QString::fromUtf8(mValues.mValues[i].mTooltip.c_str()), Qt::ToolTipRole);
mWidget->setItemData(
static_cast<int>(i), QString::fromUtf8(mValues.mValues[i].mTooltip.c_str()), Qt::ToolTipRole);
}

mWidget->setCurrentIndex(static_cast<int>(index));
Expand All @@ -111,20 +112,11 @@ CSMPrefs::SettingWidgets CSMPrefs::EnumSetting::makeWidgets(QWidget* parent)
void CSMPrefs::EnumSetting::updateWidget()
{
if (mWidget)
{
int index
= mWidget->findText(QString::fromStdString(Settings::Manager::getString(getKey(), getParent()->getKey())));

mWidget->setCurrentIndex(index);
}
mWidget->setCurrentIndex(mWidget->findText(QString::fromStdString(getValue())));
}

void CSMPrefs::EnumSetting::valueChanged(int value)
{
{
QMutexLocker lock(getMutex());
Settings::Manager::setString(getKey(), getParent()->getKey(), mValues.mValues.at(value).mValue);
}

setValue(mValues.mValues.at(value).mValue);
getParent()->getState()->update(*this);
}
7 changes: 3 additions & 4 deletions apps/opencs/model/prefs/enumsetting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ namespace CSMPrefs
EnumValues& add(const std::string& value, const std::string& tooltip);
};

class EnumSetting : public Setting
class EnumSetting final : public TypedSetting<std::string>
{
Q_OBJECT

std::string mTooltip;
EnumValue mDefault;
EnumValues mValues;
QComboBox* mWidget;

public:
EnumSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, const EnumValue& default_);
explicit EnumSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index);

EnumSetting& setTooltip(const std::string& tooltip);

Expand Down
17 changes: 5 additions & 12 deletions apps/opencs/model/prefs/intsetting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
#include "state.hpp"

CSMPrefs::IntSetting::IntSetting(
Category* parent, QMutex* mutex, const std::string& key, const QString& label, int default_)
: Setting(parent, mutex, key, label)
Category* parent, QMutex* mutex, const std::string& key, const QString& label, Settings::Index& index)
: TypedSetting(parent, mutex, key, label, index)
, mMin(0)
, mMax(std::numeric_limits<int>::max())
, mDefault(default_)
, mWidget(nullptr)
{
}
Expand Down Expand Up @@ -55,7 +54,7 @@ CSMPrefs::SettingWidgets CSMPrefs::IntSetting::makeWidgets(QWidget* parent)

mWidget = new QSpinBox(parent);
mWidget->setRange(mMin, mMax);
mWidget->setValue(mDefault);
mWidget->setValue(getValue());

if (!mTooltip.empty())
{
Expand All @@ -72,17 +71,11 @@ CSMPrefs::SettingWidgets CSMPrefs::IntSetting::makeWidgets(QWidget* parent)
void CSMPrefs::IntSetting::updateWidget()
{
if (mWidget)
{
mWidget->setValue(Settings::Manager::getInt(getKey(), getParent()->getKey()));
}
mWidget->setValue(getValue());
}

void CSMPrefs::IntSetting::valueChanged(int value)
{
{
QMutexLocker lock(getMutex());
Settings::Manager::setInt(getKey(), getParent()->getKey(), value);
}

setValue(value);
getParent()->getState()->update(*this);
}
Loading

0 comments on commit 78da1eb

Please sign in to comment.