window-basic-settings-a11y.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "window-basic-settings.hpp"
  2. #include "window-basic-main.hpp"
  3. #include "obs-frontend-api.h"
  4. #include "obs-app.hpp"
  5. #include <qt-wrappers.hpp>
  6. #include <QColorDialog>
  7. enum ColorPreset {
  8. COLOR_PRESET_DEFAULT,
  9. COLOR_PRESET_COLOR_BLIND_1,
  10. COLOR_PRESET_CUSTOM = 99,
  11. };
  12. static inline QColor color_from_int(long long val)
  13. {
  14. return QColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
  15. }
  16. static inline long long color_to_int(QColor color)
  17. {
  18. auto shift = [&](unsigned val, int shift) {
  19. return ((val & 0xff) << shift);
  20. };
  21. return shift(color.red(), 0) | shift(color.green(), 8) | shift(color.blue(), 16) | shift(color.alpha(), 24);
  22. }
  23. QColor OBSBasicSettings::GetColor(uint32_t colorVal, QString label)
  24. {
  25. QColorDialog::ColorDialogOptions options;
  26. #ifdef __linux__
  27. // TODO: Revisit hang on Ubuntu with native dialog
  28. options |= QColorDialog::DontUseNativeDialog;
  29. #endif
  30. QColor color = color_from_int(colorVal);
  31. return QColorDialog::getColor(color, this, label, options);
  32. }
  33. void OBSBasicSettings::LoadA11ySettings(bool presetChange)
  34. {
  35. config_t *config = App()->GetUserConfig();
  36. loading = true;
  37. if (!presetChange) {
  38. preset = config_get_int(config, "Accessibility", "ColorPreset");
  39. bool block = ui->colorPreset->blockSignals(true);
  40. ui->colorPreset->setCurrentIndex(std::min(preset, (uint32_t)ui->colorPreset->count() - 1));
  41. ui->colorPreset->blockSignals(block);
  42. bool checked = config_get_bool(config, "Accessibility", "OverrideColors");
  43. ui->colorsGroupBox->setChecked(checked);
  44. }
  45. if (preset == COLOR_PRESET_DEFAULT) {
  46. ResetDefaultColors();
  47. SetDefaultColors();
  48. } else if (preset == COLOR_PRESET_COLOR_BLIND_1) {
  49. ResetDefaultColors();
  50. mixerGreenActive = 0x742E94;
  51. mixerGreen = 0x4A1A60;
  52. mixerYellowActive = 0x3349F9;
  53. mixerYellow = 0x1F2C97;
  54. mixerRedActive = 0xBEAC63;
  55. mixerRed = 0x675B28;
  56. selectRed = 0x3349F9;
  57. selectGreen = 0xFF56C9;
  58. selectBlue = 0xB09B44;
  59. SetDefaultColors();
  60. } else if (preset == COLOR_PRESET_CUSTOM) {
  61. SetDefaultColors();
  62. selectRed = config_get_int(config, "Accessibility", "SelectRed");
  63. selectGreen = config_get_int(config, "Accessibility", "SelectGreen");
  64. selectBlue = config_get_int(config, "Accessibility", "SelectBlue");
  65. mixerGreen = config_get_int(config, "Accessibility", "MixerGreen");
  66. mixerYellow = config_get_int(config, "Accessibility", "MixerYellow");
  67. mixerRed = config_get_int(config, "Accessibility", "MixerRed");
  68. mixerGreenActive = config_get_int(config, "Accessibility", "MixerGreenActive");
  69. mixerYellowActive = config_get_int(config, "Accessibility", "MixerYellowActive");
  70. mixerRedActive = config_get_int(config, "Accessibility", "MixerRedActive");
  71. }
  72. UpdateA11yColors();
  73. loading = false;
  74. }
  75. void OBSBasicSettings::SaveA11ySettings()
  76. {
  77. config_t *config = App()->GetUserConfig();
  78. config_set_bool(config, "Accessibility", "OverrideColors", ui->colorsGroupBox->isChecked());
  79. config_set_int(config, "Accessibility", "ColorPreset", preset);
  80. config_set_int(config, "Accessibility", "SelectRed", selectRed);
  81. config_set_int(config, "Accessibility", "SelectGreen", selectGreen);
  82. config_set_int(config, "Accessibility", "SelectBlue", selectBlue);
  83. config_set_int(config, "Accessibility", "MixerGreen", mixerGreen);
  84. config_set_int(config, "Accessibility", "MixerYellow", mixerYellow);
  85. config_set_int(config, "Accessibility", "MixerRed", mixerRed);
  86. config_set_int(config, "Accessibility", "MixerGreenActive", mixerGreenActive);
  87. config_set_int(config, "Accessibility", "MixerYellowActive", mixerYellowActive);
  88. config_set_int(config, "Accessibility", "MixerRedActive", mixerRedActive);
  89. main->RefreshVolumeColors();
  90. }
  91. static void SetStyle(QLabel *label, uint32_t colorVal)
  92. {
  93. QColor color = color_from_int(colorVal);
  94. color.setAlpha(255);
  95. QPalette palette = QPalette(color);
  96. label->setFrameStyle(QFrame::Sunken | QFrame::Panel);
  97. label->setText(color.name(QColor::HexRgb));
  98. label->setPalette(palette);
  99. label->setStyleSheet(QString("background-color: %1; color: %2;")
  100. .arg(palette.color(QPalette::Window).name(QColor::HexRgb))
  101. .arg(palette.color(QPalette::WindowText).name(QColor::HexRgb)));
  102. label->setAutoFillBackground(true);
  103. label->setAlignment(Qt::AlignCenter);
  104. }
  105. void OBSBasicSettings::UpdateA11yColors()
  106. {
  107. SetStyle(ui->color1, selectRed);
  108. SetStyle(ui->color2, selectGreen);
  109. SetStyle(ui->color3, selectBlue);
  110. SetStyle(ui->color4, mixerGreen);
  111. SetStyle(ui->color5, mixerYellow);
  112. SetStyle(ui->color6, mixerRed);
  113. SetStyle(ui->color7, mixerGreenActive);
  114. SetStyle(ui->color8, mixerYellowActive);
  115. SetStyle(ui->color9, mixerRedActive);
  116. }
  117. void OBSBasicSettings::SetDefaultColors()
  118. {
  119. config_t *config = App()->GetUserConfig();
  120. config_set_default_int(config, "Accessibility", "SelectRed", selectRed);
  121. config_set_default_int(config, "Accessibility", "SelectGreen", selectGreen);
  122. config_set_default_int(config, "Accessibility", "SelectBlue", selectBlue);
  123. config_set_default_int(config, "Accessibility", "MixerGreen", mixerGreen);
  124. config_set_default_int(config, "Accessibility", "MixerYellow", mixerYellow);
  125. config_set_default_int(config, "Accessibility", "MixerRed", mixerRed);
  126. config_set_default_int(config, "Accessibility", "MixerGreenActive", mixerGreenActive);
  127. config_set_default_int(config, "Accessibility", "MixerYellowActive", mixerYellowActive);
  128. config_set_default_int(config, "Accessibility", "MixerRedActive", mixerRedActive);
  129. }
  130. void OBSBasicSettings::ResetDefaultColors()
  131. {
  132. selectRed = 0x0000FF;
  133. selectGreen = 0x00FF00;
  134. selectBlue = 0xFF7F00;
  135. mixerGreen = 0x267f26;
  136. mixerYellow = 0x267f7f;
  137. mixerRed = 0x26267f;
  138. mixerGreenActive = 0x4cff4c;
  139. mixerYellowActive = 0x4cffff;
  140. mixerRedActive = 0x4c4cff;
  141. }
  142. void OBSBasicSettings::on_colorPreset_currentIndexChanged(int idx)
  143. {
  144. preset = idx == ui->colorPreset->count() - 1 ? COLOR_PRESET_CUSTOM : idx;
  145. LoadA11ySettings(true);
  146. }
  147. void OBSBasicSettings::on_choose1_clicked()
  148. {
  149. QColor color = GetColor(selectRed, QTStr("Basic.Settings.Accessibility.ColorOverrides.SelectRed"));
  150. if (!color.isValid())
  151. return;
  152. selectRed = color_to_int(color);
  153. preset = COLOR_PRESET_CUSTOM;
  154. bool block = ui->colorPreset->blockSignals(true);
  155. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  156. ui->colorPreset->blockSignals(block);
  157. A11yChanged();
  158. UpdateA11yColors();
  159. }
  160. void OBSBasicSettings::on_choose2_clicked()
  161. {
  162. QColor color = GetColor(selectGreen, QTStr("Basic.Settings.Accessibility.ColorOverrides.SelectGreen"));
  163. if (!color.isValid())
  164. return;
  165. selectGreen = color_to_int(color);
  166. preset = COLOR_PRESET_CUSTOM;
  167. bool block = ui->colorPreset->blockSignals(true);
  168. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  169. ui->colorPreset->blockSignals(block);
  170. A11yChanged();
  171. UpdateA11yColors();
  172. }
  173. void OBSBasicSettings::on_choose3_clicked()
  174. {
  175. QColor color = GetColor(selectBlue, QTStr("Basic.Settings.Accessibility.ColorOverrides.SelectBlue"));
  176. if (!color.isValid())
  177. return;
  178. selectBlue = color_to_int(color);
  179. preset = COLOR_PRESET_CUSTOM;
  180. bool block = ui->colorPreset->blockSignals(true);
  181. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  182. ui->colorPreset->blockSignals(block);
  183. A11yChanged();
  184. UpdateA11yColors();
  185. }
  186. void OBSBasicSettings::on_choose4_clicked()
  187. {
  188. QColor color = GetColor(mixerGreen, QTStr("Basic.Settings.Accessibility.ColorOverrides.MixerGreen"));
  189. if (!color.isValid())
  190. return;
  191. mixerGreen = color_to_int(color);
  192. preset = COLOR_PRESET_CUSTOM;
  193. bool block = ui->colorPreset->blockSignals(true);
  194. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  195. ui->colorPreset->blockSignals(block);
  196. A11yChanged();
  197. UpdateA11yColors();
  198. }
  199. void OBSBasicSettings::on_choose5_clicked()
  200. {
  201. QColor color = GetColor(mixerYellow, QTStr("Basic.Settings.Accessibility.ColorOverrides.MixerYellow"));
  202. if (!color.isValid())
  203. return;
  204. mixerYellow = color_to_int(color);
  205. preset = COLOR_PRESET_CUSTOM;
  206. bool block = ui->colorPreset->blockSignals(true);
  207. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  208. ui->colorPreset->blockSignals(block);
  209. A11yChanged();
  210. UpdateA11yColors();
  211. }
  212. void OBSBasicSettings::on_choose6_clicked()
  213. {
  214. QColor color = GetColor(mixerRed, QTStr("Basic.Settings.Accessibility.ColorOverrides.MixerRed"));
  215. if (!color.isValid())
  216. return;
  217. mixerRed = color_to_int(color);
  218. preset = COLOR_PRESET_CUSTOM;
  219. bool block = ui->colorPreset->blockSignals(true);
  220. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  221. ui->colorPreset->blockSignals(block);
  222. A11yChanged();
  223. UpdateA11yColors();
  224. }
  225. void OBSBasicSettings::on_choose7_clicked()
  226. {
  227. QColor color =
  228. GetColor(mixerGreenActive, QTStr("Basic.Settings.Accessibility.ColorOverrides.MixerGreenActive"));
  229. if (!color.isValid())
  230. return;
  231. mixerGreenActive = color_to_int(color);
  232. preset = COLOR_PRESET_CUSTOM;
  233. bool block = ui->colorPreset->blockSignals(true);
  234. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  235. ui->colorPreset->blockSignals(block);
  236. A11yChanged();
  237. UpdateA11yColors();
  238. }
  239. void OBSBasicSettings::on_choose8_clicked()
  240. {
  241. QColor color =
  242. GetColor(mixerYellowActive, QTStr("Basic.Settings.Accessibility.ColorOverrides.MixerYellowActive"));
  243. if (!color.isValid())
  244. return;
  245. mixerYellowActive = color_to_int(color);
  246. preset = COLOR_PRESET_CUSTOM;
  247. bool block = ui->colorPreset->blockSignals(true);
  248. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  249. ui->colorPreset->blockSignals(block);
  250. A11yChanged();
  251. UpdateA11yColors();
  252. }
  253. void OBSBasicSettings::on_choose9_clicked()
  254. {
  255. QColor color = GetColor(mixerRedActive, QTStr("Basic.Settings.Accessibility.ColorOverrides.MixerRedActive"));
  256. if (!color.isValid())
  257. return;
  258. mixerRedActive = color_to_int(color);
  259. preset = COLOR_PRESET_CUSTOM;
  260. bool block = ui->colorPreset->blockSignals(true);
  261. ui->colorPreset->setCurrentIndex(ui->colorPreset->count() - 1);
  262. ui->colorPreset->blockSignals(block);
  263. A11yChanged();
  264. UpdateA11yColors();
  265. }