window-basic-settings-appearance.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 = savedTheme->isBaseTheme ? savedTheme->id : savedTheme->parent;
  20. for (const OBSTheme &theme : App()->GetThemes()) {
  21. if (theme.isBaseTheme && (HighContrastEnabled() || theme.isVisible || theme.id == currentBaseTheme)) {
  22. ui->theme->addItem(theme.name, theme.id);
  23. }
  24. }
  25. int idx = ui->theme->findData(currentBaseTheme);
  26. if (idx != -1)
  27. ui->theme->setCurrentIndex(idx);
  28. ui->themeVariant->setPlaceholderText(QTStr("Basic.Settings.Appearance.General.NoVariant"));
  29. }
  30. void OBSBasicSettings::LoadThemeList(bool reload)
  31. {
  32. ProfileScope("OBSBasicSettings::LoadThemeList");
  33. const OBSTheme *currentTheme = App()->GetTheme();
  34. const QString currentBaseTheme = currentTheme->isBaseTheme ? currentTheme->id : currentTheme->parent;
  35. /* Nothing to do if current and last base theme were the same */
  36. const QString baseThemeId = ui->theme->currentData().toString();
  37. if (reload && baseThemeId == currentBaseTheme)
  38. return;
  39. ui->themeVariant->blockSignals(true);
  40. ui->themeVariant->clear();
  41. auto themes = App()->GetThemes();
  42. std::sort(themes.begin(), themes.end(), [](const OBSTheme &a, const OBSTheme &b) -> bool {
  43. return QString::compare(a.name, b.name, Qt::CaseInsensitive) < 0;
  44. });
  45. QString defaultVariant;
  46. const OBSTheme *baseTheme = App()->GetTheme(baseThemeId);
  47. for (const OBSTheme &theme : themes) {
  48. /* Skip non-visible themes */
  49. if (!theme.isVisible || theme.isHighContrast)
  50. continue;
  51. /* Skip non-child themes */
  52. if (theme.isBaseTheme || theme.parent != baseThemeId)
  53. continue;
  54. ui->themeVariant->addItem(theme.name, theme.id);
  55. if (baseTheme && theme.filename == baseTheme->filename)
  56. defaultVariant = theme.id;
  57. }
  58. int idx = ui->themeVariant->findData(currentTheme->id);
  59. if (idx != -1)
  60. ui->themeVariant->setCurrentIndex(idx);
  61. ui->themeVariant->setEnabled(ui->themeVariant->count() > 0);
  62. ui->themeVariant->blockSignals(false);
  63. /* If no variant is selected but variants are available set the first one. */
  64. if (idx == -1 && ui->themeVariant->count() > 0) {
  65. idx = ui->themeVariant->findData(defaultVariant);
  66. ui->themeVariant->setCurrentIndex(idx != -1 ? idx : 0);
  67. }
  68. }
  69. void OBSBasicSettings::LoadAppearanceSettings(bool reload)
  70. {
  71. LoadThemeList(reload);
  72. if (reload) {
  73. QString themeId = ui->theme->currentData().toString();
  74. if (ui->themeVariant->currentIndex() != -1)
  75. themeId = ui->themeVariant->currentData().toString();
  76. App()->SetTheme(themeId);
  77. }
  78. }
  79. void OBSBasicSettings::SaveAppearanceSettings()
  80. {
  81. config_t *config = App()->GetUserConfig();
  82. OBSTheme *currentTheme = App()->GetTheme();
  83. if (savedTheme != currentTheme) {
  84. config_set_string(config, "Appearance", "Theme", QT_TO_UTF8(currentTheme->id));
  85. }
  86. }
  87. void OBSBasicSettings::on_theme_activated(int)
  88. {
  89. LoadAppearanceSettings(true);
  90. }
  91. void OBSBasicSettings::on_themeVariant_activated(int)
  92. {
  93. LoadAppearanceSettings(true);
  94. }