Debian Codegraph @

Source/Core/DolphinQt/Config/ConfigControls/ConfigChoice.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Config/ConfigControls/ConfigChoice.h"
5
6 #include <QSignalBlocker>
7
8 ConfigChoice::ConfigChoice(const QStringList& options, const Config::Info<int>& setting,
9 Config::Layer* layer)
10 : ConfigControl(setting.GetLocation(), layer), m_setting(setting)
11 {
12 addItems(options);
13 setCurrentIndex(ReadValue(setting));
14
15 connect(this, &QComboBox::currentIndexChanged, this, &ConfigChoice::Update);
16 }
17
18 void ConfigChoice::Update(int choice)
19 {
20 SaveValue(m_setting, choice);
21 }
22
23 void ConfigChoice::OnConfigChanged()
24 {
25 setCurrentIndex(ReadValue(m_setting));
26 }
27
28 ConfigChoiceU32::ConfigChoiceU32(const QStringList& options, const Config::Info<u32>& setting,
29 Config::Layer* layer)
30 : ConfigControl(setting.GetLocation(), layer), m_setting(setting)
31 {
32 addItems(options);
33 setCurrentIndex(ReadValue(setting));
34
35 connect(this, &QComboBox::currentIndexChanged, this, &ConfigChoiceU32::Update);
36 }
37
38 void ConfigChoiceU32::Update(int choice)
39 {
40 SaveValue(m_setting, (u32)choice);
41 }
42
43 void ConfigChoiceU32::OnConfigChanged()
44 {
45 setCurrentIndex(ReadValue(m_setting));
46 }
47
48 ConfigStringChoice::ConfigStringChoice(std::span<const std::string> options,
49 const Config::Info<std::string>& setting,
50 Config::Layer* layer)
51 : ConfigControl(setting.GetLocation(), layer), m_setting(setting), m_text_is_data(true)
52 {
53 for (const auto& op : options)
54 addItem(QString::fromStdString(op));
55
56 Load();
57 connect(this, &QComboBox::currentIndexChanged, this, &ConfigStringChoice::Update);
58 }
59
60 ConfigStringChoice::ConfigStringChoice(std::span<const std::pair<QString, QString>> options,
61 const Config::Info<std::string>& setting,
62 Config::Layer* layer)
63 : ConfigControl(setting.GetLocation(), layer), m_setting(setting), m_text_is_data(false)
64 {
65 for (const auto& [option_text, option_data] : options)
66 addItem(option_text, option_data);
67
68 Load();
69 connect(this, &QComboBox::currentIndexChanged, this, &ConfigStringChoice::Update);
70 }
71
72 void ConfigStringChoice::Update(int index)
73 {
74 if (m_text_is_data)
75 SaveValue(m_setting, itemText(index).toStdString());
76 else
77 SaveValue(m_setting, itemData(index).toString().toStdString());
78 }
79
80 void ConfigStringChoice::Load()
81 {
82 const QString setting_value = QString::fromStdString(ReadValue(m_setting));
83 const int index = m_text_is_data ? findText(setting_value) : findData(setting_value);
84
85 // This can be called publicly.
86 setCurrentIndex(index);
87 }
88
89 void ConfigStringChoice::OnConfigChanged()
90 {
91 Load();
92 }
93
94 ConfigComplexChoice::ConfigComplexChoice(const InfoVariant& setting1, const InfoVariant& setting2,
95 Config::Layer* layer)
96 : m_layer(layer), m_setting1(setting1), m_setting2(setting2)
97 {
98 connect(&Settings::Instance(), &Settings::ConfigChanged, this, &ConfigComplexChoice::Refresh);
99 connect(this, &QComboBox::currentIndexChanged, this, &ConfigComplexChoice::SaveValue);
100 }
101
102 void ConfigComplexChoice::Refresh()
103 {
104 auto& location = GetLocation();
105
106 QFont bf = font();
107 if (m_layer != nullptr)
108 {
109 bf.setBold(m_layer->Exists(location.first) || m_layer->Exists(location.second));
110 }
111 else
112 {
113 bf.setBold(Config::GetActiveLayerForConfig(location.first) != Config::LayerType::Base ||
114 Config::GetActiveLayerForConfig(location.second) != Config::LayerType::Base);
115 }
116
117 setFont(bf);
118 UpdateComboIndex();
119 }
120
121 void ConfigComplexChoice::Add(const QString& name, const OptionVariant option1,
122 const OptionVariant option2)
123 {
124 const QSignalBlocker blocker(this);
125 addItem(name);
126 m_options.push_back(std::make_pair(option1, option2));
127 }
128
129 void ConfigComplexChoice::Reset()
130 {
131 clear();
132 m_options.clear();
133 }
134
135 void ConfigComplexChoice::SaveValue(int choice)
136 {
137 auto Set = [this](auto& setting, auto& value) {
138 if (m_layer != nullptr)
139 {
140 m_layer->Set(setting.GetLocation(), value);
141 Config::OnConfigChanged();
142 return;
143 }
144
145 Config::SetBaseOrCurrent(setting, value);
146 };
147
148 std::visit(Set, m_setting1, m_options[choice].first);
149 std::visit(Set, m_setting2, m_options[choice].second);
150 }
151
152 void ConfigComplexChoice::UpdateComboIndex()
153 {
154 auto get_layer_value = [this](auto& setting) {
155 if (m_layer != nullptr)
156 return static_cast<OptionVariant>(m_layer->Get(setting));
157
158 return static_cast<OptionVariant>(Config::Get(setting));
159 };
160
161 auto get_default_value = [](auto& setting) { return OptionVariant(setting.GetDefaultValue()); };
162
163 auto is_current_value = [&](const InfoVariant& info, const OptionVariant& option) {
164 return std::visit(get_layer_value, info) ==
165 (std::holds_alternative<Config::DefaultState>(option) ?
166 std::visit(get_default_value, info) :
167 option);
168 };
169
170 auto is_correct_option = [&](const std::pair<OptionVariant, OptionVariant>& option) {
171 return is_current_value(m_setting1, option.first) &&
172 is_current_value(m_setting2, option.second);
173 };
174
175 auto it = std::find_if(m_options.begin(), m_options.end(), is_correct_option);
176 int index;
177
178 if (it == m_options.end())
179 index = m_default_index;
180 else
181 index = static_cast<int>(std::distance(m_options.begin(), it));
182
183 // Will crash if not blocked
184 const QSignalBlocker blocker(this);
185 setCurrentIndex(index);
186 }
187
188 void ConfigComplexChoice::SetDefault(int index)
189 {
190 m_default_index = index;
191 }
192
193 const std::pair<Config::Location, Config::Location> ConfigComplexChoice::GetLocation() const
194 {
195 auto visit = [](auto& v) { return v.GetLocation(); };
196
197 return {std::visit(visit, m_setting1), std::visit(visit, m_setting2)};
198 }
199
200 void ConfigComplexChoice::mousePressEvent(QMouseEvent* event)
201 {
202 if (event->button() == Qt::RightButton && m_layer != nullptr)
203 {
204 auto& location = GetLocation();
205 m_layer->DeleteKey(location.first);
206 m_layer->DeleteKey(location.second);
207 Config::OnConfigChanged();
208 }
209 else
210 {
211 QComboBox::mousePressEvent(event);
212 }
213 }