PluginManagerWindow.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /******************************************************************************
  2. Copyright (C) 2025 by FiniteSingularity <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "PluginManagerWindow.hpp"
  15. #include <OBSApp.hpp>
  16. #include <QCheckBox>
  17. #include <QDialogButtonBox>
  18. #include <QHBoxLayout>
  19. #include <QLabel>
  20. #include <QScrollArea>
  21. #include <QString>
  22. #include <QVBoxLayout>
  23. #include "moc_PluginManagerWindow.cpp"
  24. namespace OBS {
  25. PluginManagerWindow::PluginManagerWindow(std::vector<ModuleInfo> const &modules, QWidget *parent)
  26. : QDialog(parent),
  27. modules_(modules),
  28. ui(new Ui::PluginManagerWindow)
  29. {
  30. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  31. ui->setupUi(this);
  32. ui->modulesListContainer->viewport()->setAutoFillBackground(false);
  33. ui->modulesListContents->setAutoFillBackground(false);
  34. // Set up sidebar entries
  35. ui->sectionList->clear();
  36. ui->sectionList->setSelectionMode(QAbstractItemView::SingleSelection);
  37. connect(ui->sectionList, &QListWidget::itemSelectionChanged, this,
  38. &PluginManagerWindow::sectionSelectionChanged);
  39. QListWidgetItem *browse = new QListWidgetItem(QTStr("PluginManager.Section.Discover"));
  40. browse->setFlags(browse->flags() & ~Qt::ItemIsEnabled);
  41. browse->setFlags(browse->flags() & ~Qt::ItemIsSelectable);
  42. browse->setToolTip(QTStr("ComingSoon"));
  43. ui->sectionList->addItem(browse);
  44. QListWidgetItem *installed = new QListWidgetItem(QTStr("PluginManager.Section.Manage"));
  45. ui->sectionList->addItem(installed);
  46. QListWidgetItem *updates = new QListWidgetItem(QTStr("PluginManager.Section.Updates"));
  47. updates->setFlags(updates->flags() & ~Qt::ItemIsEnabled);
  48. updates->setFlags(updates->flags() & ~Qt::ItemIsSelectable);
  49. updates->setToolTip(QTStr("ComingSoon"));
  50. ui->sectionList->addItem(updates);
  51. setSection(ui->sectionList->indexFromItem(installed));
  52. std::sort(modules_.begin(), modules_.end(), [](const ModuleInfo &a, const ModuleInfo &b) {
  53. std::string aName = !a.display_name.empty() ? a.display_name : a.module_name;
  54. std::string bName = !b.display_name.empty() ? b.display_name : b.module_name;
  55. return aName < bName;
  56. });
  57. int row = 0;
  58. for (auto &metadata : modules_) {
  59. std::string id = metadata.module_name;
  60. // Check if the module is missing:
  61. bool missing = !obs_get_module(id.c_str()) && !obs_get_disabled_module(id.c_str());
  62. QString name = !metadata.display_name.empty() ? metadata.display_name.c_str()
  63. : metadata.module_name.c_str();
  64. if (missing) {
  65. name += " " + QTStr("PluginManager.MissingPlugin");
  66. }
  67. auto item = new QCheckBox(name);
  68. item->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
  69. item->setChecked(metadata.enabled);
  70. if (!metadata.enabledAtLaunch) {
  71. item->setProperty("class", "text-muted");
  72. }
  73. if (missing) {
  74. item->setEnabled(false);
  75. }
  76. ui->modulesList->layout()->addWidget(item);
  77. connect(item, &QCheckBox::toggled, this, [this, row](bool checked) {
  78. modules_[row].enabled = checked;
  79. ui->manageRestartLabel->setVisible(isEnabledPluginsChanged());
  80. });
  81. row++;
  82. }
  83. ui->modulesList->adjustSize();
  84. ui->modulesListContents->adjustSize();
  85. ui->manageRestartLabel->setVisible(isEnabledPluginsChanged());
  86. connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
  87. connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
  88. }
  89. void PluginManagerWindow::sectionSelectionChanged()
  90. {
  91. auto selected = ui->sectionList->selectedItems();
  92. if (selected.count() != 1) {
  93. setSection(activeSectionIndex);
  94. } else {
  95. auto selectionIndex = ui->sectionList->indexFromItem(selected.first());
  96. setSection(selectionIndex);
  97. }
  98. }
  99. void PluginManagerWindow::setSection(QPersistentModelIndex index)
  100. {
  101. if (ui->sectionList->itemFromIndex(index)) {
  102. activeSectionIndex = index;
  103. ui->sectionList->setCurrentIndex(index);
  104. }
  105. }
  106. bool PluginManagerWindow::isEnabledPluginsChanged()
  107. {
  108. bool result = false;
  109. for (auto &metadata : modules_) {
  110. if (metadata.enabledAtLaunch != metadata.enabled) {
  111. result = true;
  112. break;
  113. }
  114. }
  115. return result;
  116. }
  117. }; // namespace OBS