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

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