summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt/Config/ConfigControls/ConfigChoice.cpp
blob: c2652563d12b143e15e2a6fd69b5a64fe378555c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DolphinQt/Config/ConfigControls/ConfigChoice.h"

#include <QSignalBlocker>

ConfigChoice::ConfigChoice(const QStringList& options, const Config::Info<int>& setting,
                           Config::Layer* layer)
    : ConfigControl(setting.GetLocation(), layer), m_setting(setting)
{
  addItems(options);
  setCurrentIndex(ReadValue(setting));

  connect(this, &QComboBox::currentIndexChanged, this, &ConfigChoice::Update);
}

void ConfigChoice::Update(int choice)
{
  SaveValue(m_setting, choice);
}

void ConfigChoice::OnConfigChanged()
{
  setCurrentIndex(ReadValue(m_setting));
}

ConfigStringChoice::ConfigStringChoice(const std::vector<std::string>& options,
                                       const Config::Info<std::string>& setting,
                                       Config::Layer* layer)
    : ConfigControl(setting.GetLocation(), layer), m_setting(setting), m_text_is_data(true)
{
  for (const auto& op : options)
    addItem(QString::fromStdString(op));

  Load();
  connect(this, &QComboBox::currentIndexChanged, this, &ConfigStringChoice::Update);
}

ConfigStringChoice::ConfigStringChoice(const std::vector<std::pair<QString, QString>>& options,
                                       const Config::Info<std::string>& setting,
                                       Config::Layer* layer)
    : ConfigControl(setting.GetLocation(), layer), m_setting(setting), m_text_is_data(false)
{
  for (const auto& [option_text, option_data] : options)
    addItem(option_text, option_data);

  connect(this, &QComboBox::currentIndexChanged, this, &ConfigStringChoice::Update);
  Load();
}

void ConfigStringChoice::Update(int index)
{
  if (m_text_is_data)
    SaveValue(m_setting, itemText(index).toStdString());
  else
    SaveValue(m_setting, itemData(index).toString().toStdString());
}

void ConfigStringChoice::Load()
{
  const QString setting_value = QString::fromStdString(ReadValue(m_setting));
  const int index = m_text_is_data ? findText(setting_value) : findData(setting_value);

  // This can be called publicly.
  const QSignalBlocker block(this);
  setCurrentIndex(index);
}

void ConfigStringChoice::OnConfigChanged()
{
  Load();
}