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

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