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

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