configuration_shared.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-FileCopyrightText: 2016 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <memory>
  4. #include <QCheckBox>
  5. #include <QObject>
  6. #include <QString>
  7. #include <QWidget>
  8. #include "common/settings.h"
  9. #include "yuzu/configuration/configuration_shared.h"
  10. #include "yuzu/configuration/configure_per_game.h"
  11. namespace ConfigurationShared {
  12. Tab::Tab(std::shared_ptr<std::forward_list<Tab*>> group_, QWidget* parent)
  13. : QWidget(parent), group{group_} {
  14. if (group != nullptr) {
  15. group->push_front(this);
  16. }
  17. }
  18. Tab::~Tab() = default;
  19. } // namespace ConfigurationShared
  20. void ConfigurationShared::ApplyPerGameSetting(Settings::SwitchableSetting<bool>* setting,
  21. const QCheckBox* checkbox,
  22. const CheckState& tracker) {
  23. if (Settings::IsConfiguringGlobal() && setting->UsingGlobal()) {
  24. setting->SetValue(checkbox->checkState());
  25. } else if (!Settings::IsConfiguringGlobal()) {
  26. if (tracker == CheckState::Global) {
  27. setting->SetGlobal(true);
  28. } else {
  29. setting->SetGlobal(false);
  30. setting->SetValue(checkbox->checkState());
  31. }
  32. }
  33. }
  34. void ConfigurationShared::SetPerGameSetting(QCheckBox* checkbox,
  35. const Settings::SwitchableSetting<bool>* setting) {
  36. if (setting->UsingGlobal()) {
  37. checkbox->setCheckState(Qt::PartiallyChecked);
  38. } else {
  39. checkbox->setCheckState(setting->GetValue() ? Qt::Checked : Qt::Unchecked);
  40. }
  41. }
  42. void ConfigurationShared::SetHighlight(QWidget* widget, bool highlighted) {
  43. if (highlighted) {
  44. widget->setStyleSheet(QStringLiteral("QWidget#%1 { background-color:rgba(0,203,255,0.5) }")
  45. .arg(widget->objectName()));
  46. } else {
  47. widget->setStyleSheet(QStringLiteral("QWidget#%1 { background-color:rgba(0,0,0,0) }")
  48. .arg(widget->objectName()));
  49. }
  50. widget->show();
  51. }
  52. void ConfigurationShared::SetColoredTristate(QCheckBox* checkbox, bool global, bool state,
  53. bool global_state, CheckState& tracker) {
  54. if (global) {
  55. tracker = CheckState::Global;
  56. } else {
  57. tracker = (state == global_state) ? CheckState::On : CheckState::Off;
  58. }
  59. SetHighlight(checkbox, tracker != CheckState::Global);
  60. QObject::connect(checkbox, &QCheckBox::clicked, checkbox, [checkbox, global_state, &tracker] {
  61. tracker = static_cast<CheckState>((static_cast<int>(tracker) + 1) %
  62. static_cast<int>(CheckState::Count));
  63. if (tracker == CheckState::Global) {
  64. checkbox->setChecked(global_state);
  65. }
  66. SetHighlight(checkbox, tracker != CheckState::Global);
  67. });
  68. }
  69. void ConfigurationShared::SetColoredComboBox(QComboBox* combobox, QWidget* target, int global) {
  70. InsertGlobalItem(combobox, global);
  71. QObject::connect(combobox, qOverload<int>(&QComboBox::activated), target,
  72. [target](int index) { SetHighlight(target, index != 0); });
  73. }
  74. void ConfigurationShared::InsertGlobalItem(QComboBox* combobox, int global_index) {
  75. const QString use_global_text =
  76. ConfigurePerGame::tr("Use global configuration (%1)").arg(combobox->itemText(global_index));
  77. combobox->insertItem(ConfigurationShared::USE_GLOBAL_INDEX, use_global_text);
  78. combobox->insertSeparator(ConfigurationShared::USE_GLOBAL_SEPARATOR_INDEX);
  79. }
  80. int ConfigurationShared::GetComboboxIndex(int global_setting_index, const QComboBox* combobox) {
  81. if (Settings::IsConfiguringGlobal()) {
  82. return combobox->currentIndex();
  83. }
  84. if (combobox->currentIndex() == ConfigurationShared::USE_GLOBAL_INDEX) {
  85. return global_setting_index;
  86. }
  87. return combobox->currentIndex() - ConfigurationShared::USE_GLOBAL_OFFSET;
  88. }