| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <cassert> |
| 5 | |
| 6 | #include <QFont> |
| 7 | |
| 8 | #include "DolphinQt/Config/ConfigControls/ConfigSlider.h" |
| 9 | #include "DolphinQt/Settings.h" |
| 10 | |
| 11 | ConfigSlider::ConfigSlider(int minimum, int maximum, const Config::Info<int>& setting, int tick) |
| 12 | : ConfigSlider(minimum, maximum, setting, nullptr, tick) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | ConfigSlider::ConfigSlider(int minimum, int maximum, const Config::Info<int>& setting, |
| 17 | Config::Layer* layer, int tick) |
| 18 | : ConfigControl(Qt::Horizontal, setting.GetLocation(), layer), m_setting(setting) |
| 19 | |
| 20 | { |
| 21 | setMinimum(minimum); |
| 22 | setMaximum(maximum); |
| 23 | setTickInterval(tick); |
| 24 | setValue(ReadValue(setting)); |
| 25 | |
| 26 | connect(this, &ConfigSlider::valueChanged, this, &ConfigSlider::Update); |
| 27 | } |
| 28 | |
| 29 | ConfigSlider::ConfigSlider(std::vector<int> tick_values, const Config::Info<int>& setting, |
| 30 | Config::Layer* layer) |
| 31 | : ConfigControl(Qt::Horizontal, setting.GetLocation(), layer), m_setting(setting), |
| 32 | m_tick_values(std::move(tick_values)) |
| 33 | { |
| 34 | assert(!m_tick_values.empty()); |
| 35 | setMinimum(0); |
| 36 | setMaximum(static_cast<int>(m_tick_values.size() - 1)); |
| 37 | setPageStep(1); |
| 38 | setTickPosition(QSlider::TicksBelow); |
| 39 | OnConfigChanged(); |
| 40 | |
| 41 | connect(this, &ConfigSlider::valueChanged, this, &ConfigSlider::Update); |
| 42 | } |
| 43 | |
| 44 | void ConfigSlider::Update(int value) |
| 45 | { |
| 46 | if (!m_tick_values.empty()) |
| 47 | { |
| 48 | if (value >= 0 && static_cast<size_t>(value) < m_tick_values.size()) |
| 49 | SaveValue(m_setting, m_tick_values[static_cast<size_t>(value)]); |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | SaveValue(m_setting, value); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void ConfigSlider::OnConfigChanged() |
| 58 | { |
| 59 | if (!m_tick_values.empty()) |
| 60 | { |
| 61 | // re-enable in case it was disabled |
| 62 | setEnabled(true); |
| 63 | |
| 64 | const int config_value = ReadValue(m_setting); |
| 65 | for (size_t i = 0; i < m_tick_values.size(); ++i) |
| 66 | { |
| 67 | if (m_tick_values[i] == config_value) |
| 68 | { |
| 69 | setValue(static_cast<int>(i)); |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // if we reach here than none of the options matched, disable the slider |
| 75 | setEnabled(false); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | setValue(ReadValue(m_setting)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | ConfigSliderU32::ConfigSliderU32(u32 minimum, u32 maximum, const Config::Info<u32>& setting, |
| 84 | u32 scale) |
| 85 | : ConfigSliderU32(minimum, maximum, setting, nullptr, scale) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | ConfigSliderU32::ConfigSliderU32(u32 minimum, u32 maximum, const Config::Info<u32>& setting, |
| 90 | Config::Layer* layer, u32 scale) |
| 91 | : ConfigControl(Qt::Horizontal, setting.GetLocation(), layer), m_setting(setting), |
| 92 | m_scale(scale) |
| 93 | |
| 94 | { |
| 95 | setMinimum(minimum); |
| 96 | setMaximum(maximum); |
| 97 | setValue(ReadValue(setting)); |
| 98 | OnConfigChanged(); |
| 99 | |
| 100 | connect(this, &ConfigSliderU32::valueChanged, this, &ConfigSliderU32::Update); |
| 101 | } |
| 102 | |
| 103 | void ConfigSliderU32::Update(u32 value) |
| 104 | { |
| 105 | SaveValue(m_setting, value * m_scale); |
| 106 | } |
| 107 | |
| 108 | void ConfigSliderU32::OnConfigChanged() |
| 109 | { |
| 110 | setValue(ReadValue(m_setting) / m_scale); |
| 111 | } |
| 112 | |
| 113 | ConfigSliderLabel::ConfigSliderLabel(const QString& text, ConfigSlider* slider) |
| 114 | : QLabel(text), m_slider(QPointer<ConfigSlider>(slider)) |
| 115 | { |
| 116 | connect(&Settings::Instance(), &Settings::ConfigChanged, this, [this] { |
| 117 | // Label shares font changes with slider to mark game ini settings. |
| 118 | if (m_slider) |
| 119 | setFont(m_slider->font()); |
| 120 | }); |
| 121 | } |