window-basic-main-scene-collections.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[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 <filesystem>
  15. #include <string>
  16. #include <obs.hpp>
  17. #include <util/util.hpp>
  18. #include <QMessageBox>
  19. #include <QVariant>
  20. #include <QFileDialog>
  21. #include <QStandardPaths>
  22. #include <qt-wrappers.hpp>
  23. #include "item-widget-helpers.hpp"
  24. #include "window-basic-main.hpp"
  25. #include "window-importer.hpp"
  26. #include "window-namedialog.hpp"
  27. // MARK: Constant Expressions
  28. constexpr std::string_view OBSSceneCollectionPath = "/obs-studio/basic/scenes/";
  29. // MARK: - Main Scene Collection Management Functions
  30. void OBSBasic::SetupNewSceneCollection(const std::string &collectionName)
  31. {
  32. const OBSSceneCollection &newCollection = CreateSceneCollection(collectionName);
  33. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  34. ActivateSceneCollection(newCollection);
  35. blog(LOG_INFO, "Created scene collection '%s' (clean, %s)", newCollection.name.c_str(),
  36. newCollection.fileName.c_str());
  37. blog(LOG_INFO, "------------------------------------------------");
  38. }
  39. void OBSBasic::SetupDuplicateSceneCollection(const std::string &collectionName)
  40. {
  41. const OBSSceneCollection &newCollection = CreateSceneCollection(collectionName);
  42. const OBSSceneCollection &currentCollection = GetCurrentSceneCollection();
  43. SaveProjectNow();
  44. const auto copyOptions = std::filesystem::copy_options::overwrite_existing;
  45. try {
  46. std::filesystem::copy(currentCollection.collectionFile, newCollection.collectionFile, copyOptions);
  47. } catch (const std::filesystem::filesystem_error &error) {
  48. blog(LOG_DEBUG, "%s", error.what());
  49. throw std::logic_error("Failed to copy file for cloned scene collection: " + newCollection.name);
  50. }
  51. OBSDataAutoRelease collection = obs_data_create_from_json_file(newCollection.collectionFile.u8string().c_str());
  52. obs_data_set_string(collection, "name", newCollection.name.c_str());
  53. OBSDataArrayAutoRelease sources = obs_data_get_array(collection, "sources");
  54. if (sources) {
  55. obs_data_erase(collection, "sources");
  56. obs_data_array_enum(
  57. sources,
  58. [](obs_data_t *data, void *) -> void {
  59. const char *uuid = os_generate_uuid();
  60. obs_data_set_string(data, "uuid", uuid);
  61. bfree((void *)uuid);
  62. },
  63. nullptr);
  64. obs_data_set_array(collection, "sources", sources);
  65. }
  66. obs_data_save_json_safe(collection, newCollection.collectionFile.u8string().c_str(), "tmp", nullptr);
  67. ActivateSceneCollection(newCollection);
  68. blog(LOG_INFO, "Created scene collection '%s' (duplicate, %s)", newCollection.name.c_str(),
  69. newCollection.fileName.c_str());
  70. blog(LOG_INFO, "------------------------------------------------");
  71. }
  72. void OBSBasic::SetupRenameSceneCollection(const std::string &collectionName)
  73. {
  74. const OBSSceneCollection &newCollection = CreateSceneCollection(collectionName);
  75. const OBSSceneCollection currentCollection = GetCurrentSceneCollection();
  76. SaveProjectNow();
  77. const auto copyOptions = std::filesystem::copy_options::overwrite_existing;
  78. try {
  79. std::filesystem::copy(currentCollection.collectionFile, newCollection.collectionFile, copyOptions);
  80. } catch (const std::filesystem::filesystem_error &error) {
  81. blog(LOG_DEBUG, "%s", error.what());
  82. throw std::logic_error("Failed to copy file for scene collection: " + currentCollection.name);
  83. }
  84. collections.erase(currentCollection.name);
  85. OBSDataAutoRelease collection = obs_data_create_from_json_file(newCollection.collectionFile.u8string().c_str());
  86. obs_data_set_string(collection, "name", newCollection.name.c_str());
  87. obs_data_save_json_safe(collection, newCollection.collectionFile.u8string().c_str(), "tmp", nullptr);
  88. ActivateSceneCollection(newCollection);
  89. RemoveSceneCollection(currentCollection);
  90. blog(LOG_INFO, "Renamed scene collection '%s' to '%s' (%s)", currentCollection.name.c_str(),
  91. newCollection.name.c_str(), newCollection.fileName.c_str());
  92. blog(LOG_INFO, "------------------------------------------------");
  93. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_RENAMED);
  94. }
  95. // MARK: - Scene Collection File Management Functions
  96. const OBSSceneCollection &OBSBasic::CreateSceneCollection(const std::string &collectionName)
  97. {
  98. if (const auto &foundCollection = GetSceneCollectionByName(collectionName)) {
  99. throw std::invalid_argument("Scene collection already exists: " + collectionName);
  100. }
  101. std::string fileName;
  102. if (!GetFileSafeName(collectionName.c_str(), fileName)) {
  103. throw std::invalid_argument("Failed to create safe directory for new scene collection: " +
  104. collectionName);
  105. }
  106. std::string collectionFile;
  107. collectionFile.reserve(App()->userScenesLocation.u8string().size() + OBSSceneCollectionPath.size() +
  108. fileName.size());
  109. collectionFile.append(App()->userScenesLocation.u8string()).append(OBSSceneCollectionPath).append(fileName);
  110. if (!GetClosestUnusedFileName(collectionFile, "json")) {
  111. throw std::invalid_argument("Failed to get closest file name for new scene collection: " + fileName);
  112. }
  113. const std::filesystem::path collectionFilePath = std::filesystem::u8path(collectionFile);
  114. auto [iterator, success] = collections.try_emplace(
  115. collectionName,
  116. OBSSceneCollection{collectionName, collectionFilePath.filename().u8string(), collectionFilePath});
  117. return iterator->second;
  118. }
  119. void OBSBasic::RemoveSceneCollection(OBSSceneCollection collection)
  120. {
  121. std::filesystem::path collectionBackupFile{collection.collectionFile};
  122. collectionBackupFile.replace_extension("json.bak");
  123. try {
  124. std::filesystem::remove(collection.collectionFile);
  125. std::filesystem::remove(collectionBackupFile);
  126. } catch (const std::filesystem::filesystem_error &error) {
  127. blog(LOG_DEBUG, "%s", error.what());
  128. throw std::logic_error("Failed to remove scene collection file: " + collection.fileName);
  129. }
  130. blog(LOG_INFO, "Removed scene collection '%s' (%s)", collection.name.c_str(), collection.fileName.c_str());
  131. blog(LOG_INFO, "------------------------------------------------");
  132. }
  133. // MARK: - Scene Collection UI Handling Functions
  134. bool OBSBasic::CreateNewSceneCollection(const QString &name)
  135. {
  136. try {
  137. SetupNewSceneCollection(name.toStdString());
  138. return true;
  139. } catch (const std::invalid_argument &error) {
  140. blog(LOG_ERROR, "%s", error.what());
  141. return false;
  142. } catch (const std::logic_error &error) {
  143. blog(LOG_ERROR, "%s", error.what());
  144. return false;
  145. }
  146. }
  147. bool OBSBasic::CreateDuplicateSceneCollection(const QString &name)
  148. {
  149. try {
  150. SetupDuplicateSceneCollection(name.toStdString());
  151. return true;
  152. } catch (const std::invalid_argument &error) {
  153. blog(LOG_ERROR, "%s", error.what());
  154. return false;
  155. } catch (const std::logic_error &error) {
  156. blog(LOG_ERROR, "%s", error.what());
  157. return false;
  158. }
  159. }
  160. void OBSBasic::DeleteSceneCollection(const QString &name)
  161. {
  162. const std::string_view currentCollectionName{
  163. config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  164. if (currentCollectionName == name.toStdString()) {
  165. on_actionRemoveSceneCollection_triggered();
  166. return;
  167. }
  168. OBSSceneCollection currentCollection = GetCurrentSceneCollection();
  169. RemoveSceneCollection(currentCollection);
  170. collections.erase(name.toStdString());
  171. RefreshSceneCollections();
  172. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  173. }
  174. void OBSBasic::ChangeSceneCollection()
  175. {
  176. QAction *action = reinterpret_cast<QAction *>(sender());
  177. if (!action) {
  178. return;
  179. }
  180. const std::string_view currentCollectionName{
  181. config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  182. const std::string selectedCollectionName{action->text().toStdString()};
  183. if (currentCollectionName == selectedCollectionName) {
  184. action->setChecked(true);
  185. return;
  186. }
  187. const std::optional<OBSSceneCollection> foundCollection = GetSceneCollectionByName(selectedCollectionName);
  188. if (!foundCollection) {
  189. const std::string errorMessage{"Selected scene collection not found: "};
  190. throw std::invalid_argument(errorMessage + currentCollectionName.data());
  191. }
  192. const OBSSceneCollection &selectedCollection = foundCollection.value();
  193. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  194. ActivateSceneCollection(selectedCollection);
  195. blog(LOG_INFO, "Switched to scene collection '%s' (%s)", selectedCollection.name.c_str(),
  196. selectedCollection.fileName.c_str());
  197. blog(LOG_INFO, "------------------------------------------------");
  198. }
  199. void OBSBasic::RefreshSceneCollections(bool refreshCache)
  200. {
  201. std::string_view currentCollectionName{config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  202. QList<QAction *> menuActions = ui->sceneCollectionMenu->actions();
  203. for (auto &action : menuActions) {
  204. QVariant variant = action->property("file_name");
  205. if (variant.typeName() != nullptr) {
  206. delete action;
  207. }
  208. }
  209. if (refreshCache) {
  210. RefreshSceneCollectionCache();
  211. }
  212. size_t numAddedCollections = 0;
  213. for (auto &[collectionName, collection] : collections) {
  214. QAction *action = new QAction(QString().fromStdString(collectionName), this);
  215. action->setProperty("file_name", QString().fromStdString(collection.fileName));
  216. connect(action, &QAction::triggered, this, &OBSBasic::ChangeSceneCollection);
  217. action->setCheckable(true);
  218. action->setChecked(collectionName == currentCollectionName);
  219. ui->sceneCollectionMenu->addAction(action);
  220. numAddedCollections += 1;
  221. }
  222. ui->actionRemoveSceneCollection->setEnabled(numAddedCollections > 1);
  223. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  224. main->ui->actionPasteFilters->setEnabled(false);
  225. main->ui->actionPasteRef->setEnabled(false);
  226. main->ui->actionPasteDup->setEnabled(false);
  227. }
  228. // MARK: - Scene Collection Cache Functions
  229. void OBSBasic::RefreshSceneCollectionCache()
  230. {
  231. OBSSceneCollectionCache foundCollections{};
  232. const std::filesystem::path collectionsPath =
  233. App()->userScenesLocation / std::filesystem::u8path(OBSSceneCollectionPath.substr(1));
  234. if (!std::filesystem::exists(collectionsPath)) {
  235. blog(LOG_WARNING, "Failed to get scene collections config path");
  236. return;
  237. }
  238. for (const auto &entry : std::filesystem::directory_iterator(collectionsPath)) {
  239. if (entry.is_directory()) {
  240. continue;
  241. }
  242. if (entry.path().extension().u8string() != ".json") {
  243. continue;
  244. }
  245. OBSDataAutoRelease collectionData =
  246. obs_data_create_from_json_file_safe(entry.path().u8string().c_str(), "bak");
  247. std::string candidateName;
  248. const char *collectionName = obs_data_get_string(collectionData, "name");
  249. if (!collectionName) {
  250. candidateName = entry.path().filename().u8string();
  251. } else {
  252. candidateName = collectionName;
  253. }
  254. foundCollections.try_emplace(candidateName,
  255. OBSSceneCollection{candidateName, entry.path().filename().u8string(),
  256. entry.path()});
  257. }
  258. collections.swap(foundCollections);
  259. }
  260. const OBSSceneCollection &OBSBasic::GetCurrentSceneCollection() const
  261. {
  262. std::string currentCollectionName{config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  263. if (currentCollectionName.empty()) {
  264. throw std::invalid_argument("No valid scene collection name in configuration Basic->SceneCollection");
  265. }
  266. const auto &foundCollection = collections.find(currentCollectionName);
  267. if (foundCollection != collections.end()) {
  268. return foundCollection->second;
  269. } else {
  270. throw std::invalid_argument("Scene collection not found in collection list: " + currentCollectionName);
  271. }
  272. }
  273. std::optional<OBSSceneCollection> OBSBasic::GetSceneCollectionByName(const std::string &collectionName) const
  274. {
  275. auto foundCollection = collections.find(collectionName);
  276. if (foundCollection == collections.end()) {
  277. return {};
  278. } else {
  279. return foundCollection->second;
  280. }
  281. }
  282. std::optional<OBSSceneCollection> OBSBasic::GetSceneCollectionByFileName(const std::string &fileName) const
  283. {
  284. for (auto &[iterator, collection] : collections) {
  285. if (collection.fileName == fileName) {
  286. return collection;
  287. }
  288. }
  289. return {};
  290. }
  291. // MARK: - Qt Slot Functions
  292. void OBSBasic::on_actionNewSceneCollection_triggered()
  293. {
  294. const OBSPromptCallback sceneCollectionCallback = [this](const OBSPromptResult &result) {
  295. if (GetSceneCollectionByName(result.promptValue)) {
  296. return false;
  297. }
  298. return true;
  299. };
  300. const OBSPromptRequest request{Str("Basic.Main.AddSceneCollection.Title"),
  301. Str("Basic.Main.AddSceneCollection.Text")};
  302. OBSPromptResult result = PromptForName(request, sceneCollectionCallback);
  303. if (!result.success) {
  304. return;
  305. }
  306. try {
  307. SetupNewSceneCollection(result.promptValue);
  308. } catch (const std::invalid_argument &error) {
  309. blog(LOG_ERROR, "%s", error.what());
  310. } catch (const std::logic_error &error) {
  311. blog(LOG_ERROR, "%s", error.what());
  312. }
  313. }
  314. void OBSBasic::on_actionDupSceneCollection_triggered()
  315. {
  316. const OBSPromptCallback sceneCollectionCallback = [this](const OBSPromptResult &result) {
  317. if (GetSceneCollectionByName(result.promptValue)) {
  318. return false;
  319. }
  320. return true;
  321. };
  322. const OBSPromptRequest request{Str("Basic.Main.AddSceneCollection.Title"),
  323. Str("Basic.Main.AddSceneCollection.Text")};
  324. OBSPromptResult result = PromptForName(request, sceneCollectionCallback);
  325. if (!result.success) {
  326. return;
  327. }
  328. try {
  329. SetupDuplicateSceneCollection(result.promptValue);
  330. } catch (const std::invalid_argument &error) {
  331. blog(LOG_ERROR, "%s", error.what());
  332. } catch (const std::logic_error &error) {
  333. blog(LOG_ERROR, "%s", error.what());
  334. }
  335. }
  336. void OBSBasic::on_actionRenameSceneCollection_triggered()
  337. {
  338. const OBSSceneCollection &currentCollection = GetCurrentSceneCollection();
  339. const OBSPromptCallback sceneCollectionCallback = [this](const OBSPromptResult &result) {
  340. if (GetSceneCollectionByName(result.promptValue)) {
  341. return false;
  342. }
  343. return true;
  344. };
  345. const OBSPromptRequest request{Str("Basic.Main.RenameSceneCollection.Title"),
  346. Str("Basic.Main.AddSceneCollection.Text"), currentCollection.name};
  347. OBSPromptResult result = PromptForName(request, sceneCollectionCallback);
  348. if (!result.success) {
  349. return;
  350. }
  351. try {
  352. SetupRenameSceneCollection(result.promptValue);
  353. } catch (const std::invalid_argument &error) {
  354. blog(LOG_ERROR, "%s", error.what());
  355. } catch (const std::logic_error &error) {
  356. blog(LOG_ERROR, "%s", error.what());
  357. }
  358. }
  359. void OBSBasic::on_actionRemoveSceneCollection_triggered(bool skipConfirmation)
  360. {
  361. if (collections.size() < 2) {
  362. return;
  363. }
  364. OBSSceneCollection currentCollection;
  365. try {
  366. currentCollection = GetCurrentSceneCollection();
  367. if (!skipConfirmation) {
  368. const QString confirmationText =
  369. QTStr("ConfirmRemove.Text").arg(QString::fromStdString(currentCollection.name));
  370. const QMessageBox::StandardButton button =
  371. OBSMessageBox::question(this, QTStr("ConfirmRemove.Title"), confirmationText);
  372. if (button == QMessageBox::No) {
  373. return;
  374. }
  375. }
  376. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  377. collections.erase(currentCollection.name);
  378. } catch (const std::invalid_argument &error) {
  379. blog(LOG_ERROR, "%s", error.what());
  380. } catch (const std::logic_error &error) {
  381. blog(LOG_ERROR, "%s", error.what());
  382. }
  383. const OBSSceneCollection &newCollection = collections.begin()->second;
  384. ActivateSceneCollection(newCollection);
  385. RemoveSceneCollection(currentCollection);
  386. blog(LOG_INFO, "Switched to scene collection '%s' (%s)", newCollection.name.c_str(),
  387. newCollection.fileName.c_str());
  388. blog(LOG_INFO, "------------------------------------------------");
  389. }
  390. void OBSBasic::on_actionImportSceneCollection_triggered()
  391. {
  392. OBSImporter imp(this);
  393. imp.exec();
  394. RefreshSceneCollections(true);
  395. }
  396. void OBSBasic::on_actionExportSceneCollection_triggered()
  397. {
  398. SaveProjectNow();
  399. const OBSSceneCollection &currentCollection = GetCurrentSceneCollection();
  400. const QString home = QDir::homePath();
  401. const QString destinationFileName = SaveFile(this, QTStr("Basic.MainMenu.SceneCollection.Export"),
  402. home + "/" + currentCollection.fileName.c_str(),
  403. "JSON Files (*.json)");
  404. if (!destinationFileName.isEmpty() && !destinationFileName.isNull()) {
  405. const std::filesystem::path sourceFile = currentCollection.collectionFile;
  406. const std::filesystem::path destinationFile =
  407. std::filesystem::u8path(destinationFileName.toStdString());
  408. OBSDataAutoRelease collection = obs_data_create_from_json_file(sourceFile.u8string().c_str());
  409. OBSDataArrayAutoRelease sources = obs_data_get_array(collection, "sources");
  410. if (!sources) {
  411. blog(LOG_WARNING, "No sources in exported scene collection");
  412. return;
  413. }
  414. obs_data_erase(collection, "sources");
  415. using OBSDataVector = std::vector<OBSData>;
  416. OBSDataVector sourceItems;
  417. obs_data_array_enum(
  418. sources,
  419. [](obs_data_t *data, void *vector) -> void {
  420. OBSDataVector &sourceItems{*static_cast<OBSDataVector *>(vector)};
  421. sourceItems.push_back(data);
  422. },
  423. &sourceItems);
  424. std::sort(sourceItems.begin(), sourceItems.end(), [](const OBSData &a, const OBSData &b) {
  425. return astrcmpi(obs_data_get_string(a, "name"), obs_data_get_string(b, "name")) < 0;
  426. });
  427. OBSDataArrayAutoRelease newSources = obs_data_array_create();
  428. for (auto &item : sourceItems) {
  429. obs_data_array_push_back(newSources, item);
  430. }
  431. obs_data_set_array(collection, "sources", newSources);
  432. obs_data_save_json_pretty_safe(collection, destinationFile.u8string().c_str(), "tmp", "bak");
  433. }
  434. }
  435. void OBSBasic::on_actionRemigrateSceneCollection_triggered()
  436. {
  437. if (Active()) {
  438. OBSMessageBox::warning(this, QTStr("Basic.Main.RemigrateSceneCollection.Title"),
  439. QTStr("Basic.Main.RemigrateSceneCollection.CannotMigrate.Active"));
  440. return;
  441. }
  442. OBSDataAutoRelease priv = obs_get_private_data();
  443. if (!usingAbsoluteCoordinates && !migrationBaseResolution) {
  444. OBSMessageBox::warning(
  445. this, QTStr("Basic.Main.RemigrateSceneCollection.Title"),
  446. QTStr("Basic.Main.RemigrateSceneCollection.CannotMigrate.UnknownBaseResolution"));
  447. return;
  448. }
  449. obs_video_info ovi;
  450. obs_get_video_info(&ovi);
  451. if (!usingAbsoluteCoordinates && migrationBaseResolution->first == ovi.base_width &&
  452. migrationBaseResolution->second == ovi.base_height) {
  453. OBSMessageBox::warning(
  454. this, QTStr("Basic.Main.RemigrateSceneCollection.Title"),
  455. QTStr("Basic.Main.RemigrateSceneCollection.CannotMigrate.BaseResolutionMatches"));
  456. return;
  457. }
  458. const OBSSceneCollection &currentCollection = GetCurrentSceneCollection();
  459. QString name = QString::fromStdString(currentCollection.name);
  460. QString message =
  461. QTStr("Basic.Main.RemigrateSceneCollection.Text").arg(name).arg(ovi.base_width).arg(ovi.base_height);
  462. auto answer = OBSMessageBox::question(this, QTStr("Basic.Main.RemigrateSceneCollection.Title"), message);
  463. if (answer == QMessageBox::No)
  464. return;
  465. lastOutputResolution = {ovi.base_width, ovi.base_height};
  466. if (!usingAbsoluteCoordinates) {
  467. /* Temporarily change resolution to migration resolution */
  468. ovi.base_width = migrationBaseResolution->first;
  469. ovi.base_height = migrationBaseResolution->second;
  470. if (obs_reset_video(&ovi) != OBS_VIDEO_SUCCESS) {
  471. OBSMessageBox::critical(
  472. this, QTStr("Basic.Main.RemigrateSceneCollection.Title"),
  473. QTStr("Basic.Main.RemigrateSceneCollection.CannotMigrate.FailedVideoReset"));
  474. return;
  475. }
  476. }
  477. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  478. /* Save and immediately reload to (re-)run migrations. */
  479. SaveProjectNow();
  480. /* Reset video if we potentially changed to a temporary resolution */
  481. if (!usingAbsoluteCoordinates) {
  482. ResetVideo();
  483. }
  484. ActivateSceneCollection(currentCollection);
  485. }
  486. // MARK: - Scene Collection Management Helper Functions
  487. void OBSBasic::ActivateSceneCollection(const OBSSceneCollection &collection)
  488. {
  489. const std::string currentCollectionName{config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  490. if (auto foundCollection = GetSceneCollectionByName(currentCollectionName)) {
  491. if (collection.name != foundCollection.value().name) {
  492. SaveProjectNow();
  493. }
  494. }
  495. config_set_string(App()->GetUserConfig(), "Basic", "SceneCollection", collection.name.c_str());
  496. config_set_string(App()->GetUserConfig(), "Basic", "SceneCollectionFile", collection.fileName.c_str());
  497. Load(collection.collectionFile.u8string().c_str());
  498. RefreshSceneCollections();
  499. UpdateTitleBar();
  500. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  501. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  502. }