window-basic-settings-appearance.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "window-basic-settings.hpp"
  2. #include "window-basic-main.hpp"
  3. #include "obs-frontend-api.h"
  4. #include "qt-wrappers.hpp"
  5. #include "platform.hpp"
  6. #include "obs-app.hpp"
  7. #include <QColorDialog>
  8. #include <QDirIterator>
  9. #include <QFile>
  10. #include <QMetaEnum>
  11. #include <QObject>
  12. #include <QRandomGenerator>
  13. #include <QPainter>
  14. #include "util/profiler.hpp"
  15. using namespace std;
  16. void OBSBasicSettings::InitAppearancePage()
  17. {
  18. savedTheme = App()->GetTheme();
  19. const QString currentBaseTheme =
  20. savedTheme->isBaseTheme ? savedTheme->id : savedTheme->parent;
  21. for (const OBSTheme &theme : App()->GetThemes()) {
  22. if (theme.isBaseTheme &&
  23. (HighContrastEnabled() || theme.isVisible ||
  24. theme.id == currentBaseTheme)) {
  25. ui->theme->addItem(theme.name, theme.id);
  26. }
  27. }
  28. int idx = ui->theme->findData(currentBaseTheme);
  29. if (idx != -1)
  30. ui->theme->setCurrentIndex(idx);
  31. ui->themeVariant->setPlaceholderText(
  32. QTStr("Basic.Settings.Appearance.General.NoVariant"));
  33. }
  34. void OBSBasicSettings::LoadThemeList(bool reload)
  35. {
  36. ProfileScope("OBSBasicSettings::LoadThemeList");
  37. const OBSTheme *currentTheme = App()->GetTheme();
  38. const QString currentBaseTheme = currentTheme->isBaseTheme
  39. ? currentTheme->id
  40. : currentTheme->parent;
  41. /* Nothing to do if current and last base theme were the same */
  42. const QString baseThemeId = ui->theme->currentData().toString();
  43. if (reload && baseThemeId == currentBaseTheme)
  44. return;
  45. ui->themeVariant->blockSignals(true);
  46. ui->themeVariant->clear();
  47. auto themes = App()->GetThemes();
  48. std::sort(themes.begin(), themes.end(),
  49. [](const OBSTheme &a, const OBSTheme &b) -> bool {
  50. return QString::compare(a.name, b.name,
  51. Qt::CaseInsensitive) < 0;
  52. });
  53. QString defaultVariant;
  54. const OBSTheme *baseTheme = App()->GetTheme(baseThemeId);
  55. for (const OBSTheme &theme : themes) {
  56. /* Skip non-visible themes */
  57. if (!theme.isVisible || theme.isHighContrast)
  58. continue;
  59. /* Skip non-child themes */
  60. if (theme.isBaseTheme || theme.parent != baseThemeId)
  61. continue;
  62. ui->themeVariant->addItem(theme.name, theme.id);
  63. if (baseTheme && theme.filename == baseTheme->filename)
  64. defaultVariant = theme.id;
  65. }
  66. int idx = ui->themeVariant->findData(currentTheme->id);
  67. if (idx != -1)
  68. ui->themeVariant->setCurrentIndex(idx);
  69. ui->themeVariant->setEnabled(ui->themeVariant->count() > 0);
  70. ui->themeVariant->blockSignals(false);
  71. /* If no variant is selected but variants are available set the first one. */
  72. if (idx == -1 && ui->themeVariant->count() > 0) {
  73. idx = ui->themeVariant->findData(defaultVariant);
  74. ui->themeVariant->setCurrentIndex(idx != -1 ? idx : 0);
  75. }
  76. }
  77. void OBSBasicSettings::LoadAppearanceSettings(bool reload)
  78. {
  79. LoadThemeList(reload);
  80. if (reload) {
  81. QString themeId = ui->theme->currentData().toString();
  82. if (ui->themeVariant->currentIndex() != -1)
  83. themeId = ui->themeVariant->currentData().toString();
  84. App()->SetTheme(themeId);
  85. }
  86. }
  87. void OBSBasicSettings::SaveAppearanceSettings()
  88. {
  89. config_t *config = App()->GetUserConfig();
  90. OBSTheme *currentTheme = App()->GetTheme();
  91. if (savedTheme != currentTheme) {
  92. config_set_string(config, "Appearance", "Theme",
  93. QT_TO_UTF8(currentTheme->id));
  94. }
  95. }
  96. void OBSBasicSettings::on_theme_activated(int)
  97. {
  98. LoadAppearanceSettings(true);
  99. }
  100. void OBSBasicSettings::on_themeVariant_activated(int)
  101. {
  102. LoadAppearanceSettings(true);
  103. }