fix: SettingWidget::dropdown not working well on enums with value gaps (#6293)
This commit is contained in:
@@ -50,6 +50,7 @@
|
|||||||
- Dev: Added a `run-and-kill.sh` script to help debug crash-on-exit bugs. (#6188)
|
- Dev: Added a `run-and-kill.sh` script to help debug crash-on-exit bugs. (#6188)
|
||||||
- Dev: Refactored the `TimeoutStackStyle` enum into its own file. (#6216)
|
- Dev: Refactored the `TimeoutStackStyle` enum into its own file. (#6216)
|
||||||
- Dev: Refactored `Notebook`-related enums into their own file. (#6220)
|
- Dev: Refactored `Notebook`-related enums into their own file. (#6220)
|
||||||
|
- Dev: Refactored `SettingWidget::dropdown` for string enums to the source file. (#6293)
|
||||||
- Dev: Don't try to save emote popup bounds if we're quitting. (#6292)
|
- Dev: Don't try to save emote popup bounds if we're quitting. (#6292)
|
||||||
- Dev: Implemented customizable display names for enums. (#6238)
|
- Dev: Implemented customizable display names for enums. (#6238)
|
||||||
- Dev: Refactored event API initialization away from Application and into TwitchIrcServer. (#6198)
|
- Dev: Refactored event API initialization away from Application and into TwitchIrcServer. (#6198)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "widgets/settingspages/SettingWidget.hpp"
|
#include "widgets/settingspages/SettingWidget.hpp"
|
||||||
|
|
||||||
|
#include "singletons/Settings.hpp"
|
||||||
#include "util/RapidJsonSerializeQString.hpp" // IWYU pragma: keep
|
#include "util/RapidJsonSerializeQString.hpp" // IWYU pragma: keep
|
||||||
#include "widgets/dialogs/ColorPickerDialog.hpp"
|
#include "widgets/dialogs/ColorPickerDialog.hpp"
|
||||||
#include "widgets/helper/color/ColorButton.hpp"
|
#include "widgets/helper/color/ColorButton.hpp"
|
||||||
@@ -156,6 +157,77 @@ SettingWidget *SettingWidget::intInput(const QString &label,
|
|||||||
return widget;
|
return widget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
SettingWidget *SettingWidget::dropdown(const QString &label,
|
||||||
|
EnumStringSetting<T> &setting)
|
||||||
|
{
|
||||||
|
auto *widget = new SettingWidget(label);
|
||||||
|
|
||||||
|
auto *lbl = new QLabel(label % ":");
|
||||||
|
auto *combo = new ComboBox;
|
||||||
|
combo->setFocusPolicy(Qt::StrongFocus);
|
||||||
|
|
||||||
|
for (const auto value : magic_enum::enum_values<T>())
|
||||||
|
{
|
||||||
|
combo->addItem(qmagicenum::enumDisplayNameString(value),
|
||||||
|
QVariant(static_cast<std::underlying_type_t<T>>(value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: this can probably use some other size hint/size strategy
|
||||||
|
combo->setMinimumWidth(combo->minimumSizeHint().width());
|
||||||
|
|
||||||
|
widget->actionWidget = combo;
|
||||||
|
widget->label = lbl;
|
||||||
|
|
||||||
|
widget->hLayout->addWidget(lbl);
|
||||||
|
widget->hLayout->addStretch(1);
|
||||||
|
widget->hLayout->addWidget(combo);
|
||||||
|
|
||||||
|
setting.connect(
|
||||||
|
[&setting, combo](const QString &value) {
|
||||||
|
auto enumValue =
|
||||||
|
qmagicenum::enumCast<T>(value, qmagicenum::CASE_INSENSITIVE)
|
||||||
|
.value_or(setting.defaultValue);
|
||||||
|
|
||||||
|
auto i = magic_enum::enum_index(enumValue).value_or(0);
|
||||||
|
|
||||||
|
combo->setCurrentIndex(i);
|
||||||
|
},
|
||||||
|
widget->managedConnections);
|
||||||
|
|
||||||
|
QObject::connect(
|
||||||
|
combo, &QComboBox::currentTextChanged,
|
||||||
|
[label, combo, &setting](const auto &newText) {
|
||||||
|
bool ok = true;
|
||||||
|
auto enumValue = combo->currentData().toInt(&ok);
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
qCWarning(chatterinoWidget)
|
||||||
|
<< "Combo" << label << " with value" << newText
|
||||||
|
<< "did not contain an intable UserRole data";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setting = qmagicenum::enumNameString(static_cast<T>(enumValue));
|
||||||
|
});
|
||||||
|
|
||||||
|
return widget;
|
||||||
|
}
|
||||||
|
|
||||||
|
template SettingWidget *SettingWidget::dropdown<SoundBackend>(
|
||||||
|
const QString &label, EnumStringSetting<SoundBackend> &setting);
|
||||||
|
template SettingWidget *SettingWidget::dropdown<EmoteTooltipScale>(
|
||||||
|
const QString &label, EnumStringSetting<EmoteTooltipScale> &setting);
|
||||||
|
template SettingWidget *SettingWidget::dropdown<StreamLinkPreferredQuality>(
|
||||||
|
const QString &label,
|
||||||
|
EnumStringSetting<StreamLinkPreferredQuality> &setting);
|
||||||
|
template SettingWidget *SettingWidget::dropdown<ChatSendProtocol>(
|
||||||
|
const QString &label, EnumStringSetting<ChatSendProtocol> &setting);
|
||||||
|
template SettingWidget *SettingWidget::dropdown<TabStyle>(
|
||||||
|
const QString &label, EnumStringSetting<TabStyle> &setting);
|
||||||
|
template SettingWidget *SettingWidget::dropdown<ShowModerationState>(
|
||||||
|
const QString &label, EnumStringSetting<ShowModerationState> &setting);
|
||||||
|
|
||||||
SettingWidget *SettingWidget::colorButton(const QString &label,
|
SettingWidget *SettingWidget::colorButton(const QString &label,
|
||||||
QStringSetting &setting)
|
QStringSetting &setting)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -55,61 +55,8 @@ public:
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static SettingWidget *dropdown(const QString &label,
|
static SettingWidget *dropdown(const QString &label,
|
||||||
EnumStringSetting<T> &setting)
|
EnumStringSetting<T> &setting);
|
||||||
{
|
|
||||||
auto *widget = new SettingWidget(label);
|
|
||||||
|
|
||||||
auto *lbl = new QLabel(label % ":");
|
|
||||||
auto *combo = new ComboBox;
|
|
||||||
combo->setFocusPolicy(Qt::StrongFocus);
|
|
||||||
|
|
||||||
for (const auto value : magic_enum::enum_values<T>())
|
|
||||||
{
|
|
||||||
combo->addItem(
|
|
||||||
qmagicenum::enumDisplayNameString(value),
|
|
||||||
QVariant(static_cast<std::underlying_type_t<T>>(value)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: this can probably use some other size hint/size strategy
|
|
||||||
combo->setMinimumWidth(combo->minimumSizeHint().width());
|
|
||||||
|
|
||||||
widget->actionWidget = combo;
|
|
||||||
widget->label = lbl;
|
|
||||||
|
|
||||||
widget->hLayout->addWidget(lbl);
|
|
||||||
widget->hLayout->addStretch(1);
|
|
||||||
widget->hLayout->addWidget(combo);
|
|
||||||
|
|
||||||
setting.connect(
|
|
||||||
[&setting, combo](const QString &value) {
|
|
||||||
auto enumValue =
|
|
||||||
qmagicenum::enumCast<T>(value, qmagicenum::CASE_INSENSITIVE)
|
|
||||||
.value_or(setting.defaultValue);
|
|
||||||
|
|
||||||
auto i = magic_enum::enum_integer(enumValue);
|
|
||||||
|
|
||||||
combo->setCurrentIndex(i);
|
|
||||||
},
|
|
||||||
widget->managedConnections);
|
|
||||||
|
|
||||||
QObject::connect(
|
|
||||||
combo, &QComboBox::currentTextChanged,
|
|
||||||
[label, combo, &setting](const auto &newText) {
|
|
||||||
bool ok = true;
|
|
||||||
auto enumValue = combo->currentData().toInt(&ok);
|
|
||||||
if (!ok)
|
|
||||||
{
|
|
||||||
qCWarning(chatterinoWidget)
|
|
||||||
<< "Combo" << label << " with value" << newText
|
|
||||||
<< "did not contain an intable UserRole data";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setting = qmagicenum::enumNameString(static_cast<T>(enumValue));
|
|
||||||
});
|
|
||||||
|
|
||||||
return widget;
|
|
||||||
}
|
|
||||||
static SettingWidget *colorButton(const QString &label,
|
static SettingWidget *colorButton(const QString &label,
|
||||||
QStringSetting &setting);
|
QStringSetting &setting);
|
||||||
static SettingWidget *lineEdit(const QString &label,
|
static SettingWidget *lineEdit(const QString &label,
|
||||||
|
|||||||
Reference in New Issue
Block a user