configuration_shared.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QCheckBox>
  5. #include <QComboBox>
  6. #include <QObject>
  7. #include <QString>
  8. #include "common/settings.h"
  9. #include "yuzu/configuration/configuration_shared.h"
  10. #include "yuzu/configuration/configure_per_game.h"
  11. void ConfigurationShared::ApplyPerGameSetting(Settings::Setting<bool>* setting,
  12. const QCheckBox* checkbox,
  13. const CheckState& tracker) {
  14. if (Settings::IsConfiguringGlobal() && setting->UsingGlobal()) {
  15. setting->SetValue(checkbox->checkState());
  16. } else if (!Settings::IsConfiguringGlobal()) {
  17. if (tracker == CheckState::Global) {
  18. setting->SetGlobal(true);
  19. } else {
  20. setting->SetGlobal(false);
  21. setting->SetValue(checkbox->checkState());
  22. }
  23. }
  24. }
  25. void ConfigurationShared::ApplyPerGameSetting(Settings::Setting<int>* setting,
  26. const QComboBox* combobox) {
  27. if (Settings::IsConfiguringGlobal() && setting->UsingGlobal()) {
  28. setting->SetValue(combobox->currentIndex());
  29. } else if (!Settings::IsConfiguringGlobal()) {
  30. if (combobox->currentIndex() == ConfigurationShared::USE_GLOBAL_INDEX) {
  31. setting->SetGlobal(true);
  32. } else {
  33. setting->SetGlobal(false);
  34. setting->SetValue(combobox->currentIndex() - ConfigurationShared::USE_GLOBAL_OFFSET);
  35. }
  36. }
  37. }
  38. void ConfigurationShared::SetPerGameSetting(QCheckBox* checkbox,
  39. const Settings::Setting<bool>* setting) {
  40. if (setting->UsingGlobal()) {
  41. checkbox->setCheckState(Qt::PartiallyChecked);
  42. } else {
  43. checkbox->setCheckState(setting->GetValue() ? Qt::Checked : Qt::Unchecked);
  44. }
  45. }
  46. void ConfigurationShared::SetHighlight(QWidget* widget, bool highlighted) {
  47. if (highlighted) {
  48. widget->setStyleSheet(QStringLiteral("QWidget#%1 { background-color:rgba(0,203,255,0.5) }")
  49. .arg(widget->objectName()));
  50. } else {
  51. widget->setStyleSheet(QStringLiteral("QWidget#%1 { background-color:rgba(0,0,0,0) }")
  52. .arg(widget->objectName()));
  53. }
  54. widget->show();
  55. }
  56. void ConfigurationShared::SetColoredTristate(QCheckBox* checkbox,
  57. const Settings::Setting<bool>& setting,
  58. CheckState& tracker) {
  59. if (setting.UsingGlobal()) {
  60. tracker = CheckState::Global;
  61. } else {
  62. tracker = (setting.GetValue() == setting.GetValue(true)) ? CheckState::On : CheckState::Off;
  63. }
  64. SetHighlight(checkbox, tracker != CheckState::Global);
  65. QObject::connect(checkbox, &QCheckBox::clicked, checkbox, [checkbox, setting, &tracker] {
  66. tracker = static_cast<CheckState>((static_cast<int>(tracker) + 1) %
  67. static_cast<int>(CheckState::Count));
  68. if (tracker == CheckState::Global) {
  69. checkbox->setChecked(setting.GetValue(true));
  70. }
  71. SetHighlight(checkbox, tracker != CheckState::Global);
  72. });
  73. }
  74. void ConfigurationShared::SetColoredTristate(QCheckBox* checkbox, bool global, bool state,
  75. bool global_state, CheckState& tracker) {
  76. if (global) {
  77. tracker = CheckState::Global;
  78. } else {
  79. tracker = (state == global_state) ? CheckState::On : CheckState::Off;
  80. }
  81. SetHighlight(checkbox, tracker != CheckState::Global);
  82. QObject::connect(checkbox, &QCheckBox::clicked, checkbox, [checkbox, global_state, &tracker] {
  83. tracker = static_cast<CheckState>((static_cast<int>(tracker) + 1) %
  84. static_cast<int>(CheckState::Count));
  85. if (tracker == CheckState::Global) {
  86. checkbox->setChecked(global_state);
  87. }
  88. SetHighlight(checkbox, tracker != CheckState::Global);
  89. });
  90. }
  91. void ConfigurationShared::SetColoredComboBox(QComboBox* combobox, QWidget* target, int global) {
  92. InsertGlobalItem(combobox, global);
  93. QObject::connect(combobox, qOverload<int>(&QComboBox::activated), target,
  94. [target](int index) { SetHighlight(target, index != 0); });
  95. }
  96. void ConfigurationShared::InsertGlobalItem(QComboBox* combobox, int global_index) {
  97. const QString use_global_text =
  98. ConfigurePerGame::tr("Use global configuration (%1)").arg(combobox->itemText(global_index));
  99. combobox->insertItem(ConfigurationShared::USE_GLOBAL_INDEX, use_global_text);
  100. combobox->insertSeparator(ConfigurationShared::USE_GLOBAL_SEPARATOR_INDEX);
  101. }