window-basic-settings-a11y.cpp 10 KB

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