window-basic-settings-a11y.cpp 10 KB

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