1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 08:23:52 +00:00
openmw/apps/opencs/model/prefs/enumsetting.cpp

127 lines
3.1 KiB
C++
Raw Normal View History

2015-12-10 16:33:14 +00:00
#include "enumsetting.hpp"
#include <QComboBox>
2022-09-22 18:26:05 +00:00
#include <QLabel>
2015-12-15 11:19:48 +00:00
#include <QMutexLocker>
#include <QString>
2015-12-10 16:33:14 +00:00
#include <components/settings/settings.hpp>
#include "category.hpp"
#include "state.hpp"
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumValue::EnumValue(const std::string& value, const std::string& tooltip)
: mValue(value)
, mTooltip(tooltip)
{
}
2015-12-10 16:33:14 +00:00
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumValue::EnumValue(const char* value)
: mValue(value)
{
}
2015-12-10 16:33:14 +00:00
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumValues& CSMPrefs::EnumValues::add(const EnumValues& values)
2015-12-10 16:33:14 +00:00
{
2022-09-22 18:26:05 +00:00
mValues.insert(mValues.end(), values.mValues.begin(), values.mValues.end());
2015-12-10 16:33:14 +00:00
return *this;
}
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumValues& CSMPrefs::EnumValues::add(const EnumValue& value)
2015-12-10 16:33:14 +00:00
{
2022-09-22 18:26:05 +00:00
mValues.push_back(value);
2015-12-10 16:33:14 +00:00
return *this;
}
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumValues& CSMPrefs::EnumValues::add(const std::string& value, const std::string& tooltip)
2015-12-10 16:33:14 +00:00
{
2020-10-17 08:26:35 +00:00
mValues.emplace_back(value, tooltip);
2015-12-10 16:33:14 +00:00
return *this;
}
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumSetting::EnumSetting(
Category* parent, QMutex* mutex, const std::string& key, const std::string& label, const EnumValue& default_)
: Setting(parent, mutex, key, label)
, mDefault(default_)
, mWidget(nullptr)
{
}
2015-12-10 16:33:14 +00:00
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumSetting& CSMPrefs::EnumSetting::setTooltip(const std::string& tooltip)
2015-12-10 16:33:14 +00:00
{
mTooltip = tooltip;
return *this;
}
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumSetting& CSMPrefs::EnumSetting::addValues(const EnumValues& values)
2015-12-10 16:33:14 +00:00
{
2022-09-22 18:26:05 +00:00
mValues.add(values);
2015-12-10 16:33:14 +00:00
return *this;
}
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumSetting& CSMPrefs::EnumSetting::addValue(const EnumValue& value)
2015-12-10 16:33:14 +00:00
{
2022-09-22 18:26:05 +00:00
mValues.add(value);
2015-12-10 16:33:14 +00:00
return *this;
}
2022-09-22 18:26:05 +00:00
CSMPrefs::EnumSetting& CSMPrefs::EnumSetting::addValue(const std::string& value, const std::string& tooltip)
2015-12-10 16:33:14 +00:00
{
2022-09-22 18:26:05 +00:00
mValues.add(value, tooltip);
2015-12-10 16:33:14 +00:00
return *this;
}
2022-09-22 18:26:05 +00:00
std::pair<QWidget*, QWidget*> CSMPrefs::EnumSetting::makeWidgets(QWidget* parent)
2015-12-10 16:33:14 +00:00
{
2022-09-22 18:26:05 +00:00
QLabel* label = new QLabel(QString::fromUtf8(getLabel().c_str()), parent);
2015-12-10 16:33:14 +00:00
2022-09-22 18:26:05 +00:00
mWidget = new QComboBox(parent);
2015-12-10 16:33:14 +00:00
int index = 0;
2022-09-22 18:26:05 +00:00
for (int i = 0; i < static_cast<int>(mValues.mValues.size()); ++i)
2015-12-10 16:33:14 +00:00
{
2022-09-22 18:26:05 +00:00
if (mDefault.mValue == mValues.mValues[i].mValue)
2015-12-10 16:33:14 +00:00
index = i;
2022-09-22 18:26:05 +00:00
mWidget->addItem(QString::fromUtf8(mValues.mValues[i].mValue.c_str()));
2015-12-10 16:33:14 +00:00
if (!mValues.mValues[i].mTooltip.empty())
2022-09-22 18:26:05 +00:00
mWidget->setItemData(i, QString::fromUtf8(mValues.mValues[i].mTooltip.c_str()), Qt::ToolTipRole);
2015-12-10 16:33:14 +00:00
}
2022-09-22 18:26:05 +00:00
mWidget->setCurrentIndex(index);
2015-12-10 16:33:14 +00:00
if (!mTooltip.empty())
{
2022-09-22 18:26:05 +00:00
QString tooltip = QString::fromUtf8(mTooltip.c_str());
label->setToolTip(tooltip);
2015-12-10 16:33:14 +00:00
}
2022-09-22 18:26:05 +00:00
connect(mWidget, qOverload<int>(&QComboBox::currentIndexChanged), this, &EnumSetting::valueChanged);
2015-12-10 16:33:14 +00:00
2022-09-22 18:26:05 +00:00
return std::make_pair(label, mWidget);
}
void CSMPrefs::EnumSetting::updateWidget()
{
if (mWidget)
{
2022-09-22 18:26:05 +00:00
int index
= mWidget->findText(QString::fromStdString(Settings::Manager::getString(getKey(), getParent()->getKey())));
mWidget->setCurrentIndex(index);
}
2015-12-10 16:33:14 +00:00
}
2022-09-22 18:26:05 +00:00
void CSMPrefs::EnumSetting::valueChanged(int value)
2015-12-10 16:33:14 +00:00
{
2015-12-15 11:19:48 +00:00
{
2022-09-22 18:26:05 +00:00
QMutexLocker lock(getMutex());
Settings::Manager::setString(getKey(), getParent()->getKey(), mValues.mValues.at(value).mValue);
2015-12-15 11:19:48 +00:00
}
2022-09-22 18:26:05 +00:00
getParent()->getState()->update(*this);
2015-12-10 16:33:14 +00:00
}