DolphinQt: Add option for value mappings to ConfigSlider

This commit is contained in:
Admiral H. Curtiss 2024-12-25 04:31:03 +01:00
parent 532a8621da
commit 98ee3836e5
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
2 changed files with 58 additions and 2 deletions

View File

@ -1,6 +1,8 @@
// Copyright 2017 Dolphin Emulator Project // Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include <cassert>
#include <QFont> #include <QFont>
#include "DolphinQt/Config/ConfigControls/ConfigSlider.h" #include "DolphinQt/Config/ConfigControls/ConfigSlider.h"
@ -24,14 +26,58 @@ ConfigSlider::ConfigSlider(int minimum, int maximum, const Config::Info<int>& se
connect(this, &ConfigSlider::valueChanged, this, &ConfigSlider::Update); connect(this, &ConfigSlider::valueChanged, this, &ConfigSlider::Update);
} }
ConfigSlider::ConfigSlider(std::vector<int> tick_values, const Config::Info<int>& setting,
Config::Layer* layer)
: ConfigControl(Qt::Horizontal, setting.GetLocation(), layer), m_setting(setting),
m_tick_values(std::move(tick_values))
{
assert(!m_tick_values.empty());
setMinimum(0);
setMaximum(static_cast<int>(m_tick_values.size() - 1));
setPageStep(1);
setTickPosition(QSlider::TicksBelow);
OnConfigChanged();
connect(this, &ConfigSlider::valueChanged, this, &ConfigSlider::Update);
}
void ConfigSlider::Update(int value) void ConfigSlider::Update(int value)
{ {
SaveValue(m_setting, value); if (!m_tick_values.empty())
{
if (value >= 0 && static_cast<size_t>(value) < m_tick_values.size())
SaveValue(m_setting, m_tick_values[static_cast<size_t>(value)]);
}
else
{
SaveValue(m_setting, value);
}
} }
void ConfigSlider::OnConfigChanged() void ConfigSlider::OnConfigChanged()
{ {
setValue(ReadValue(m_setting)); if (!m_tick_values.empty())
{
// re-enable in case it was disabled
setEnabled(true);
const int config_value = ReadValue(m_setting);
for (size_t i = 0; i < m_tick_values.size(); ++i)
{
if (m_tick_values[i] == config_value)
{
setValue(static_cast<int>(i));
return;
}
}
// if we reach here than none of the options matched, disable the slider
setEnabled(false);
}
else
{
setValue(ReadValue(m_setting));
}
} }
ConfigSliderLabel::ConfigSliderLabel(const QString& text, ConfigSlider* slider) ConfigSliderLabel::ConfigSliderLabel(const QString& text, ConfigSlider* slider)

View File

@ -3,6 +3,8 @@
#pragma once #pragma once
#include <vector>
#include <QLabel> #include <QLabel>
#include <QPointer> #include <QPointer>
@ -19,6 +21,11 @@ public:
ConfigSlider(int minimum, int maximum, const Config::Info<int>& setting, Config::Layer* layer, ConfigSlider(int minimum, int maximum, const Config::Info<int>& setting, Config::Layer* layer,
int tick = 0); int tick = 0);
// Generates a slider with tick_values.size() ticks. Each tick corresponds to the integer at that
// index in the vector.
ConfigSlider(std::vector<int> tick_values, const Config::Info<int>& setting,
Config::Layer* layer);
void Update(int value); void Update(int value);
protected: protected:
@ -26,6 +33,9 @@ protected:
private: private:
const Config::Info<int> m_setting; const Config::Info<int> m_setting;
// Mappings for slider ticks to config values. Identity mapping is assumed if this is empty.
std::vector<int> m_tick_values;
}; };
class ConfigSliderLabel final : public QLabel class ConfigSliderLabel final : public QLabel