From 79318da0a988cd8a203a3b173461d674d416b36b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Garramu=C3=B1o?= Date: Thu, 12 Oct 2023 05:26:21 -0300 Subject: [PATCH] Refactored session functions to session namespace. --- mrv2/lib/mrvApp/App.cpp | 2 +- mrv2/lib/mrvFl/mrvCallbacks.cpp | 8 +- mrv2/lib/mrvFl/mrvSession.cpp | 927 +++++++++++++------------- mrv2/lib/mrvFl/mrvSession.h | 28 +- mrv2/lib/mrvPy/Cmds.cpp | 18 +- mrv2/lib/mrvWidgets/mrvMainWindow.cpp | 2 +- 6 files changed, 500 insertions(+), 485 deletions(-) diff --git a/mrv2/lib/mrvApp/App.cpp b/mrv2/lib/mrvApp/App.cpp index e7e1f87f1..df5832dfa 100644 --- a/mrv2/lib/mrvApp/App.cpp +++ b/mrv2/lib/mrvApp/App.cpp @@ -956,7 +956,7 @@ namespace mrv fileName.substr(fileName.size() - 6, fileName.size()) == ".mrv2s") { p.session = true; - load_session(fileName); + session::load(fileName); return; } diff --git a/mrv2/lib/mrvFl/mrvCallbacks.cpp b/mrv2/lib/mrvFl/mrvCallbacks.cpp index e232aba55..89408c3d9 100644 --- a/mrv2/lib/mrvFl/mrvCallbacks.cpp +++ b/mrv2/lib/mrvFl/mrvCallbacks.cpp @@ -1661,7 +1661,7 @@ namespace mrv return; } - if (save_session(file)) + if (session::save(file)) { auto settingsObject = ui->app->settingsObject(); settingsObject->addRecentFile(file); @@ -1678,12 +1678,12 @@ namespace mrv save_session_impl(file, ui); - set_current_session(file); + session::setCurrent(file); } void save_session_cb(Fl_Menu_* m, ViewerUI* ui) { - const std::string file = current_session(); + const std::string file = session::current(); if (file.empty()) return save_session_as_cb(m, ui); @@ -1696,7 +1696,7 @@ namespace mrv if (file.empty()) return; - if (load_session(file)) + if (session::load(file)) { auto settingsObject = ui->app->settingsObject(); settingsObject->addRecentFile(file); diff --git a/mrv2/lib/mrvFl/mrvSession.cpp b/mrv2/lib/mrvFl/mrvSession.cpp index 3b142a8ef..5e48c1a9a 100644 --- a/mrv2/lib/mrvFl/mrvSession.cpp +++ b/mrv2/lib/mrvFl/mrvSession.cpp @@ -37,575 +37,586 @@ namespace const int kSessionVersion = 7; } // namespace -namespace mrv +namespace { - - static void toggle_widget(Fl_Widget* w, const bool visible) + void toggle_widget(Fl_Widget* w, const bool visible) { if (visible) w->show(); else w->hide(); } +} // namespace - std::string sessionMetadata; - std::string currentSession; - - std::string current_session() - { - return currentSession; - } - - void set_current_session(const std::string& file) - { - currentSession = file; - ViewerUI* ui = App::ui; - ui->uiMain->update_title_bar(); - } +namespace mrv +{ - bool save_session(const std::string& fileName) + namespace session { - ViewerUI* ui = App::ui; - App* app = ui->app; - auto model = app->filesModel(); - auto files_ptrs = model->observeFiles()->get(); - enable_cypher(false); + std::string sessionMetadata; + std::string currentSession; - Message session; - session["version"] = kSessionVersion; + std::string current() + { + return currentSession; + } - std::vector< FilesModelItem > files; - for (const auto file : files_ptrs) + void setCurrent(const std::string& file) { - const std::string fileName = file->path.get(); - if (isTemporaryEDL(fileName)) - continue; - files.push_back(*file.get()); + currentSession = file; + ViewerUI* ui = App::ui; + ui->uiMain->update_title_bar(); } - session["files"] = files; - session["Aindex"] = model->observeAIndex()->get(); - session["Bindexes"] = model->observeBIndexes()->get(); - auto player = ui->uiView->getTimelinePlayer(); + bool save(const std::string& fileName) + { + ViewerUI* ui = App::ui; + App* app = ui->app; + auto model = app->filesModel(); + auto files_ptrs = model->observeFiles()->get(); + + enable_cypher(false); - Message timeline; - Message annotation; - Message time; - Message playback; + Message session; + session["version"] = kSessionVersion; - if (player) - { - auto annotations = player->getAllAnnotations(); - std::vector< draw::Annotation > jAnnotations; - for (const auto& ann : annotations) + std::vector< FilesModelItem > files; + for (const auto file : files_ptrs) { - jAnnotations.push_back(*(ann.get())); + const std::string fileName = file->path.get(); + if (isTemporaryEDL(fileName)) + continue; + files.push_back(*file.get()); } - annotation = jAnnotations; - time = player->currentTime(); - playback = player->playback(); - } + session["files"] = files; + session["Aindex"] = model->observeAIndex()->get(); + session["Bindexes"] = model->observeBIndexes()->get(); + + auto player = ui->uiView->getTimelinePlayer(); + + Message timeline; + Message annotation; + Message time; + Message playback; - timeline["annotations"] = annotation; - timeline["time"] = time; - timeline["playback"] = playback; - - Message bars = { - {"menu_bar", (bool)ui->uiMenuGroup->visible()}, - {"top_bar", (bool)ui->uiTopBar->visible()}, - {"pixel_bar", (bool)ui->uiPixelBar->visible()}, - {"bottom_bar", (bool)ui->uiBottomBar->visible()}, - {"status_bar", (bool)ui->uiStatusGroup->visible()}, - {"action_bar", (bool)ui->uiToolsGroup->visible()}, - {"secondary_window", (ui->uiSecondary != nullptr)}, - }; - - Message panels = { - {"Files", (filesPanel != nullptr)}, - {"Color", (colorPanel != nullptr)}, - {"Color Area", (colorAreaPanel != nullptr)}, - {"Compare", (comparePanel != nullptr)}, - {"Playlist", (playlistPanel != nullptr)}, - {"Media Information", (imageInfoPanel != nullptr)}, - {"Annotations", (annotationsPanel != nullptr)}, - {"Devices", (devicesPanel != nullptr)}, - {"Environment Map", (environmentMapPanel != nullptr)}, - {"Settings", (settingsPanel != nullptr)}, + if (player) + { + auto annotations = player->getAllAnnotations(); + std::vector< draw::Annotation > jAnnotations; + for (const auto& ann : annotations) + { + jAnnotations.push_back(*(ann.get())); + } + annotation = jAnnotations; + time = player->currentTime(); + playback = player->playback(); + } + + timeline["annotations"] = annotation; + timeline["time"] = time; + timeline["playback"] = playback; + + Message bars = { + {"menu_bar", (bool)ui->uiMenuGroup->visible()}, + {"top_bar", (bool)ui->uiTopBar->visible()}, + {"pixel_bar", (bool)ui->uiPixelBar->visible()}, + {"bottom_bar", (bool)ui->uiBottomBar->visible()}, + {"status_bar", (bool)ui->uiStatusGroup->visible()}, + {"action_bar", (bool)ui->uiToolsGroup->visible()}, + {"secondary_window", (ui->uiSecondary != nullptr)}, + }; + + Message panels = { + {"Files", (filesPanel != nullptr)}, + {"Color", (colorPanel != nullptr)}, + {"Color Area", (colorAreaPanel != nullptr)}, + {"Compare", (comparePanel != nullptr)}, + {"Playlist", (playlistPanel != nullptr)}, + {"Media Information", (imageInfoPanel != nullptr)}, + {"Annotations", (annotationsPanel != nullptr)}, + {"Devices", (devicesPanel != nullptr)}, + {"Environment Map", (environmentMapPanel != nullptr)}, + {"Settings", (settingsPanel != nullptr)}, #ifdef MRV2_PYBIND11 - {"Python", (pythonPanel != nullptr)}, + {"Python", (pythonPanel != nullptr)}, #endif #ifdef MRV2_NETWORK - {"Network", (networkPanel != nullptr)}, + {"Network", (networkPanel != nullptr)}, #endif - {"Histogram", (histogramPanel != nullptr)}, - {"Vectorscope", (vectorscopePanel != nullptr)}, - {"Stereo 3D", (stereo3DPanel != nullptr)}, + {"Histogram", (histogramPanel != nullptr)}, + {"Vectorscope", (vectorscopePanel != nullptr)}, + {"Stereo 3D", (stereo3DPanel != nullptr)}, #ifdef TLRENDER_USD - {"USD", (usdPanel != nullptr)}, + {"USD", (usdPanel != nullptr)}, #endif - {"Logs", (logsPanel != nullptr)}, - }; - - if (filesPanel) - filesPanel->save(); - if (colorPanel) - colorPanel->save(); - if (colorAreaPanel) - colorAreaPanel->save(); - if (comparePanel) - comparePanel->save(); - if (playlistPanel) - playlistPanel->save(); - if (imageInfoPanel) - imageInfoPanel->save(); - if (annotationsPanel) - annotationsPanel->save(); - if (environmentMapPanel) - environmentMapPanel->save(); - if (settingsPanel) - settingsPanel->save(); + {"Logs", (logsPanel != nullptr)}, + }; + + if (filesPanel) + filesPanel->save(); + if (colorPanel) + colorPanel->save(); + if (colorAreaPanel) + colorAreaPanel->save(); + if (comparePanel) + comparePanel->save(); + if (playlistPanel) + playlistPanel->save(); + if (imageInfoPanel) + imageInfoPanel->save(); + if (annotationsPanel) + annotationsPanel->save(); + if (environmentMapPanel) + environmentMapPanel->save(); + if (settingsPanel) + settingsPanel->save(); #ifdef MRV2_PYBIND11 - if (pythonPanel) - pythonPanel->save(); + if (pythonPanel) + pythonPanel->save(); #endif #ifdef MRV2_NETWORK - if (networkPanel) - networkPanel->save(); + if (networkPanel) + networkPanel->save(); #endif #ifdef TLRENDER_USD - if (usdPanel) - usdPanel->save(); + if (usdPanel) + usdPanel->save(); #endif - if (histogramPanel) - histogramPanel->save(); - if (vectorscopePanel) - vectorscopePanel->save(); - if (logsPanel) - logsPanel->save(); - - std::string config = ui->uiPrefs->uiPrefsOCIOConfig->value(); - int ics = ui->uiICS->value(); - int view = ui->OCIOView->value(); - int layer = ui->uiColorChannel->value(); - - Message ocio = { - {"config", config}, - {"ics", ics}, - {"view", view}, - }; - - Message settings; - - const auto settingsObject = app->settingsObject(); - const auto keys = settingsObject->keys(); - for (const auto& key : keys) - { - std_any value = settingsObject->value(key); - // // nlohmann::json cannot distinguish floats from doubles - // try - // { - // double tmpD = std_any_cast< double >(value); - // settings[key] = tmpD; - // continue; - // } - // catch (const std::bad_cast& e) - // { - // } - try - { - float tmpF = std::any_cast< float >(value); - settings[key] = tmpF; - continue; - } - catch (const std::bad_cast& e) - { - } - try - { - int tmp = std::any_cast< int >(value); - settings[key] = tmp; - continue; - } - catch (const std::bad_cast& e) + if (histogramPanel) + histogramPanel->save(); + if (vectorscopePanel) + vectorscopePanel->save(); + if (logsPanel) + logsPanel->save(); + + std::string config = ui->uiPrefs->uiPrefsOCIOConfig->value(); + int ics = ui->uiICS->value(); + int view = ui->OCIOView->value(); + int layer = ui->uiColorChannel->value(); + + Message ocio = { + {"config", config}, + {"ics", ics}, + {"view", view}, + }; + + Message settings; + + const auto settingsObject = app->settingsObject(); + const auto keys = settingsObject->keys(); + for (const auto& key : keys) { + std_any value = settingsObject->value(key); + // // nlohmann::json cannot distinguish floats from doubles + // try + // { + // double tmpD = std_any_cast< double >(value); + // settings[key] = tmpD; + // continue; + // } + // catch (const std::bad_cast& e) + // { + // } + try + { + float tmpF = std::any_cast< float >(value); + settings[key] = tmpF; + continue; + } + catch (const std::bad_cast& e) + { + } + try + { + int tmp = std::any_cast< int >(value); + settings[key] = tmp; + continue; + } + catch (const std::bad_cast& e) + { + } + try + { + bool tmp = std::any_cast< bool >(value); + settings[key] = tmp; + continue; + } + catch (const std::bad_cast& e) + { + } + try + { + const std::string& tmpS = + std::any_cast< std::string >(value); + settings[key] = tmpS; + continue; + } + catch (const std::bad_cast& e) + { + } + try + { + const std::string tmpS = std::any_cast< char* >(value); + settings[key] = tmpS; + continue; + } + catch (const std::bad_cast& e) + { + } + try + { + // If we don't know the type, don't store anything + continue; + } + catch (const std::bad_cast& e) + { + LOG_ERROR( + "Could not save sesssion ror " << key << " type " + << value.type().name()); + } } - try - { - bool tmp = std::any_cast< bool >(value); - settings[key] = tmp; - continue; - } - catch (const std::bad_cast& e) - { - } - try - { - const std::string& tmpS = std::any_cast< std::string >(value); - settings[key] = tmpS; - continue; - } - catch (const std::bad_cast& e) - { - } - try - { - const std::string tmpS = std::any_cast< char* >(value); - settings[key] = tmpS; - continue; - } - catch (const std::bad_cast& e) + + Message display = app->displayOptions(); + Message compare = model->observeCompareOptions()->get(); + Message stereo = model->observeStereo3DOptions()->get(); + Message environmentMap = ui->uiView->getEnvironmentMapOptions(); + int stereoIndex = model->observeStereoIndex()->get(); + + session["ui"] = bars; + session["panels"] = panels; + session["timeline"] = timeline; + session["ocio"] = ocio; + session["layer"] = layer; + session["settings"] = settings; + session["compareOptions"] = compare; + session["stereo3DOptions"] = stereo; + session["stereoIndex"] = stereoIndex; + session["environmentMapOptions"] = environmentMap; + session["displayOptions"] = display; + session["editMode"] = editMode; + session["metadata"] = sessionMetadata; + + std::ofstream ofs(fileName); + if (!ofs.is_open()) { + LOG_ERROR(_("Failed to open the file for writing.")); + return false; } - try + + ofs << std::setw(4) << session << std::endl; + + if (ofs.fail()) { - // If we don't know the type, don't store anything - continue; + LOG_ERROR(_("Failed to write to the file.")); + return false; } - catch (const std::bad_cast& e) + if (ofs.bad()) { - LOG_ERROR( - "Could not save sesssion ror " << key << " type " - << value.type().name()); + LOG_ERROR(_("The stream is in an unrecoverable error state.")); + return false; } - } + ofs.close(); - Message display = app->displayOptions(); - Message compare = model->observeCompareOptions()->get(); - Message stereo = model->observeStereo3DOptions()->get(); - Message environmentMap = ui->uiView->getEnvironmentMapOptions(); - int stereoIndex = model->observeStereoIndex()->get(); - - session["ui"] = bars; - session["panels"] = panels; - session["timeline"] = timeline; - session["ocio"] = ocio; - session["layer"] = layer; - session["settings"] = settings; - session["compareOptions"] = compare; - session["stereo3DOptions"] = stereo; - session["stereoIndex"] = stereoIndex; - session["environmentMapOptions"] = environmentMap; - session["displayOptions"] = display; - session["editMode"] = editMode; - session["metadata"] = sessionMetadata; - - std::ofstream ofs(fileName); - if (!ofs.is_open()) - { - LOG_ERROR(_("Failed to open the file for writing.")); - return false; - } + enable_cypher(true); - ofs << std::setw(4) << session << std::endl; + std::string msg = + string::Format(_("Session saved to \"{0}\".")).arg(fileName); + LOG_INFO(msg); - if (ofs.fail()) - { - LOG_ERROR(_("Failed to write to the file.")); - return false; + return true; } - if (ofs.bad()) - { - LOG_ERROR(_("The stream is in an unrecoverable error state.")); - return false; - } - ofs.close(); - - enable_cypher(true); - - std::string msg = - string::Format(_("Session saved to \"{0}\".")).arg(fileName); - LOG_INFO(msg); - - return true; - } - - bool load_session(const std::string& fileName) - { - ViewerUI* ui = App::ui; - App* app = ui->app; - auto view = ui->uiView; - auto model = app->filesModel(); - std::ifstream ifs(fileName); - if (!ifs.is_open()) + bool load(const std::string& fileName) { - LOG_ERROR(_("Failed to open the file for reading.")); - return false; - } + ViewerUI* ui = App::ui; + App* app = ui->app; + auto view = ui->uiView; + auto model = app->filesModel(); - Message session; - enable_cypher(false); + std::ifstream ifs(fileName); + if (!ifs.is_open()) + { + LOG_ERROR(_("Failed to open the file for reading.")); + return false; + } - ifs >> session; + Message session; + enable_cypher(false); - if (ifs.fail()) - { - LOG_ERROR(_("Failed to write to the file.")); - return false; - } - if (ifs.bad()) - { - LOG_ERROR(_("The stream is in an unrecoverable error state.")); - return false; - } - ifs.close(); + ifs >> session; - // Get session version - int version = session["version"]; + if (ifs.fail()) + { + LOG_ERROR(_("Failed to write to the file.")); + return false; + } + if (ifs.bad()) + { + LOG_ERROR(_("The stream is in an unrecoverable error state.")); + return false; + } + ifs.close(); - // Decode session file - close_all_cb(nullptr, ui); + // Get session version + int version = session["version"]; - for (const auto& j : session["files"]) - { - FilesModelItem item; - j.get_to(item); + // Decode session file + close_all_cb(nullptr, ui); - std::string path = item.path.get(); - std::string audioPath = item.audioPath.get(); + for (const auto& j : session["files"]) + { + FilesModelItem item; + j.get_to(item); - replace_path(path); - if (!audioPath.empty()) - replace_path(audioPath); + std::string path = item.path.get(); + std::string audioPath = item.audioPath.get(); - app->open(path, audioPath); + replace_path(path); + if (!audioPath.empty()) + replace_path(audioPath); - // Copy annotations to both item and player - auto Aitem = model->observeA()->get(); - Aitem->annotations = item.annotations; - Aitem->videoLayer = item.videoLayer; - Aitem->currentTime = item.currentTime; + app->open(path, audioPath); - ui->uiColorChannel->value(item.videoLayer); - ui->uiColorChannel->do_callback(); + // Copy annotations to both item and player + auto Aitem = model->observeA()->get(); + Aitem->annotations = item.annotations; + Aitem->videoLayer = item.videoLayer; + Aitem->currentTime = item.currentTime; - auto player = view->getTimelinePlayer(); - if (player) - { - player->setAllAnnotations(item.annotations); - player->seek(Aitem->currentTime); + ui->uiColorChannel->value(item.videoLayer); + ui->uiColorChannel->do_callback(); + + auto player = view->getTimelinePlayer(); + if (player) + { + player->setAllAnnotations(item.annotations); + player->seek(Aitem->currentTime); + } } - } - if (version >= 2) - { - int Aindex = session["Aindex"]; - model->setA(Aindex); + if (version >= 2) + { + int Aindex = session["Aindex"]; + model->setA(Aindex); - std::vector Bindexes = session["Bindexes"]; - model->clearB(); - for (auto i : Bindexes) - model->setB(i, true); + std::vector Bindexes = session["Bindexes"]; + model->clearB(); + for (auto i : Bindexes) + model->setB(i, true); - Message j = session["timeline"]; + Message j = session["timeline"]; - auto tmp = j["annotations"]; + auto tmp = j["annotations"]; - std::vector< std::shared_ptr > annotations; - for (const auto& value : tmp) - { - auto annotation = draw::messageToAnnotation(value); - annotations.push_back(annotation); - } - - auto player = view->getTimelinePlayer(); - if (player) - { - player->setAllAnnotations(annotations); + std::vector< std::shared_ptr > annotations; + for (const auto& value : tmp) + { + auto annotation = draw::messageToAnnotation(value); + annotations.push_back(annotation); + } - if (version >= 3) + auto player = view->getTimelinePlayer(); + if (player) { - timeline::Playback playback; - if (j["playback"].type() == nlohmann::json::value_t::string) - { - j.at("playback").get_to(playback); - } - else + player->setAllAnnotations(annotations); + + if (version >= 3) { - int v; - j.at("playback").get_to(v); - playback = static_cast(v); + timeline::Playback playback; + if (j["playback"].type() == + nlohmann::json::value_t::string) + { + j.at("playback").get_to(playback); + } + else + { + int v; + j.at("playback").get_to(v); + playback = static_cast(v); + } + player->setPlayback(playback); } - player->setPlayback(playback); + + otime::RationalTime time; + j["time"].get_to(time); + player->seek(time); } - otime::RationalTime time; - j["time"].get_to(time); - player->seek(time); + ui->uiTimeline->redraw(); + ui->uiMain->fill_menu(ui->uiMenuBar); } - ui->uiTimeline->redraw(); - ui->uiMain->fill_menu(ui->uiMenuBar); - } - - Message j = session["ui"]; + Message j = session["ui"]; - // Decode bars - toggle_widget(ui->uiMenuGroup, j["menu_bar"]); - toggle_widget(ui->uiTopBar, j["top_bar"]); - toggle_widget(ui->uiPixelBar, j["pixel_bar"]); - toggle_widget(ui->uiBottomBar, j["bottom_bar"]); - toggle_widget(ui->uiStatusBar, j["status_bar"]); - toggle_widget(ui->uiToolsGroup, j["action_bar"]); + // Decode bars + toggle_widget(ui->uiMenuGroup, j["menu_bar"]); + toggle_widget(ui->uiTopBar, j["top_bar"]); + toggle_widget(ui->uiPixelBar, j["pixel_bar"]); + toggle_widget(ui->uiBottomBar, j["bottom_bar"]); + toggle_widget(ui->uiStatusBar, j["status_bar"]); + toggle_widget(ui->uiToolsGroup, j["action_bar"]); - if ((j["secondary_window"] && !ui->uiSecondary) || - (!j["secondary_window"] && ui->uiSecondary)) - { - toggle_secondary_cb(nullptr, ui); - } + if ((j["secondary_window"] && !ui->uiSecondary) || + (!j["secondary_window"] && ui->uiSecondary)) + { + toggle_secondary_cb(nullptr, ui); + } - // Decode ICS - if (version >= 2) - { - // - // Handle color channel layer - // - int layer = session["layer"]; - ui->uiColorChannel->value(layer); - ui->uiColorChannel->do_callback(); + // Decode ICS + if (version >= 2) + { + // + // Handle color channel layer + // + int layer = session["layer"]; + ui->uiColorChannel->value(layer); + ui->uiColorChannel->do_callback(); - // - // Handle OCIO - // - j = session["ocio"]; - std::string config = j["config"]; + // + // Handle OCIO + // + j = session["ocio"]; + std::string config = j["config"]; - replace_path(config); + replace_path(config); - ui->uiPrefs->uiPrefsOCIOConfig->value(config.c_str()); + ui->uiPrefs->uiPrefsOCIOConfig->value(config.c_str()); - Preferences::OCIO(ui); + Preferences::OCIO(ui); - int value = j["ics"]; - ui->uiICS->value(value); - value = j["view"]; - ui->OCIOView->value(value); - ui->uiView->updateColorConfigOptions(); + int value = j["ics"]; + ui->uiICS->value(value); + value = j["view"]; + ui->OCIOView->value(value); + ui->uiView->updateColorConfigOptions(); - // Hide Panels and Windows - removePanels(ui); - removeWindows(ui); + // Hide Panels and Windows + removePanels(ui); + removeWindows(ui); - j = session["settings"]; + j = session["settings"]; - auto settingsObject = app->settingsObject(); + auto settingsObject = app->settingsObject(); - for (const auto& item : j.items()) - { - const std::string& key = item.key(); - Message value = item.value(); - Message::value_t type = value.type(); - switch (type) - { - case Message::value_t::boolean: - settingsObject->setValue(key, value.get()); - continue; - case Message::value_t::number_unsigned: + for (const auto& item : j.items()) { - int v = value.get(); - settingsObject->setValue(key, v); - continue; - } - case Message::value_t::number_integer: - settingsObject->setValue(key, value.get()); - continue; - case Message::value_t::number_float: - { - std_any val = settingsObject->value(key); - if (!std_any_empty(val)) + const std::string& key = item.key(); + Message value = item.value(); + Message::value_t type = value.type(); + switch (type) + { + case Message::value_t::boolean: + settingsObject->setValue(key, value.get()); + continue; + case Message::value_t::number_unsigned: { - try + int v = value.get(); + settingsObject->setValue(key, v); + continue; + } + case Message::value_t::number_integer: + settingsObject->setValue(key, value.get()); + continue; + case Message::value_t::number_float: + { + std_any val = settingsObject->value(key); + if (!std_any_empty(val)) { - double v = std::any_cast(val); - settingsObject->setValue(key, value.get()); + try + { + double v = std::any_cast(val); + settingsObject->setValue( + key, value.get()); + } + catch (const std::bad_cast& e) + { + settingsObject->setValue( + key, value.get()); + } } - catch (const std::bad_cast& e) + else { settingsObject->setValue(key, value.get()); } + continue; } - else - { - settingsObject->setValue(key, value.get()); + case Message::value_t::string: + settingsObject->setValue(key, value.get()); + continue; + default: + LOG_ERROR("Unknown Message::value_t for key " << key); } - continue; } - case Message::value_t::string: - settingsObject->setValue(key, value.get()); - continue; - default: - LOG_ERROR("Unknown Message::value_t for key " << key); + } + + // Decode panels + j = session["panels"]; + const WindowCallback* wc = kWindowCallbacks; + for (; wc->name; ++wc) + { + Message value = j[wc->name]; + bool shown = false; + if (!value.is_null()) + shown = value; + if (shown) + { + show_window_cb(_(wc->name), ui); } } - } - // Decode panels - j = session["panels"]; - const WindowCallback* wc = kWindowCallbacks; - for (; wc->name; ++wc) - { - Message value = j[wc->name]; - bool shown = false; - if (!value.is_null()) - shown = value; - if (shown) + if (version >= 3) + { + EnvironmentMapOptions env = session["environmentMapOptions"]; + view->setEnvironmentMapOptions(env); + + timeline::CompareOptions compare = session["compareOptions"]; + model->setCompareOptions(compare); + + int stereoIndex = session["stereoIndex"]; + model->setStereo(stereoIndex); + + Stereo3DOptions stereo = session["stereo3DOptions"]; + model->setStereo3DOptions(stereo); + } + + if (version >= 4) { - show_window_cb(_(wc->name), ui); + timeline::DisplayOptions display = session["displayOptions"]; + app->setDisplayOptions(display); } - } - if (version >= 3) - { - EnvironmentMapOptions env = session["environmentMapOptions"]; - view->setEnvironmentMapOptions(env); + if (version >= 6) + { + EditMode editMode = session["editMode"]; + set_edit_mode_cb(editMode, ui); + } - timeline::CompareOptions compare = session["compareOptions"]; - model->setCompareOptions(compare); + if (version >= 7) + { + session["metadata"].get_to(sessionMetadata); + } - int stereoIndex = session["stereoIndex"]; - model->setStereo(stereoIndex); + enable_cypher(true); + ui->uiMain->fill_menu(ui->uiMenuBar); - Stereo3DOptions stereo = session["stereo3DOptions"]; - model->setStereo3DOptions(stereo); - } + // Change current session filename. + setCurrent(fileName); - if (version >= 4) - { - timeline::DisplayOptions display = session["displayOptions"]; - app->setDisplayOptions(display); + return true; } - if (version >= 6) + //! Returns user provided metadata in the last saved session. + const std::string& metadata() { - EditMode editMode = session["editMode"]; - set_edit_mode_cb(editMode, ui); + return sessionMetadata; } - if (version >= 7) + //! Stores some user provided metadata. + void setMetadata(const std::string& metadata) { - session["metadata"].get_to(sessionMetadata); + sessionMetadata = metadata; } - enable_cypher(true); - ui->uiMain->fill_menu(ui->uiMenuBar); - - // Change current session filename. - set_current_session(fileName); - - return true; - } - - //! Returns user provided metadata in the last saved session. - const std::string& session_metadata() - { - return sessionMetadata; - } - - //! Stores some user provided metadata. - void set_session_metadata(const std::string& metadata) - { - sessionMetadata = metadata; - } - + } // namespace session } // namespace mrv diff --git a/mrv2/lib/mrvFl/mrvSession.h b/mrv2/lib/mrvFl/mrvSession.h index 718a17079..0924b08c5 100644 --- a/mrv2/lib/mrvFl/mrvSession.h +++ b/mrv2/lib/mrvFl/mrvSession.h @@ -8,21 +8,25 @@ namespace mrv { - //! Returns user provided metadata in the last saved session. - const std::string& session_metadata(); + namespace session + { + //! Returns user provided metadata in the last saved session. + const std::string& metadata(); - //! Stores some user provided metadata. - void set_session_metadata(const std::string& metadata); + //! Stores some user provided metadata. + void setMetadata(const std::string& metadata); - //! Returns the current session file name. - std::string current_session(); + //! Returns the current session file name. + std::string current(); - //! Sets the current session to file. - void set_current_session(const std::string& file); + //! Sets the current session to file. + void setCurrent(const std::string& file); - //! Returns true on success, false on failure - bool save_session(const std::string& file); + //! Returns true on success, false on failure + bool save(const std::string& file); + + //! Returns true on success, false on failure + bool load(const std::string& file); + } // namespace session - //! Returns true on success, false on failure - bool load_session(const std::string& file); } // namespace mrv diff --git a/mrv2/lib/mrvPy/Cmds.cpp b/mrv2/lib/mrvPy/Cmds.cpp index 12cbabb91..8efbdda57 100644 --- a/mrv2/lib/mrvPy/Cmds.cpp +++ b/mrv2/lib/mrvPy/Cmds.cpp @@ -368,7 +368,7 @@ namespace mrv2 */ std::string currentSession() { - return current_session(); + return session::current(); } /** @@ -378,7 +378,7 @@ namespace mrv2 */ bool openSession(std::string file) { - return load_session(file); + return session::load(file); } /** @@ -388,14 +388,14 @@ namespace mrv2 */ bool saveSession() { - const std::string& file = current_session(); + const std::string& file = session::current(); if (file.empty()) { throw std::runtime_error( _("No session name established, cannot save.")); return false; } - return save_session(file); + return session::save(file); } /** @@ -409,7 +409,7 @@ namespace mrv2 { file += ".mrv2s"; } - return save_session(file); + return session::save(file); } } // namespace cmd @@ -544,19 +544,19 @@ Used to run main commands and get and set the display, image, compare, LUT optio // Session commands // cmds.def( - "sessionMetadata", &mrv::session_metadata, + "sessionMetadata", &mrv::session::metadata, _("Returns the current session metadata.")); cmds.def( - "setSessionMetadata", &mrv::set_session_metadata, + "setSessionMetadata", &mrv::session::setMetadata, _("Sets the current session metadata.")); cmds.def( - "currentSession", &mrv::current_session, + "currentSession", &mrv::session::current, _("Returns current session file.")); cmds.def( - "setCurrentSession", &mrv::set_current_session, + "setCurrentSession", &mrv::session::setCurrent, _("Sets the current session file."), py::arg("file")); cmds.def( diff --git a/mrv2/lib/mrvWidgets/mrvMainWindow.cpp b/mrv2/lib/mrvWidgets/mrvMainWindow.cpp index 3f7020561..f088210a0 100644 --- a/mrv2/lib/mrvWidgets/mrvMainWindow.cpp +++ b/mrv2/lib/mrvWidgets/mrvMainWindow.cpp @@ -195,7 +195,7 @@ namespace mrv size_t numFiles = model->observeFiles()->getSize(); char buf[256]; - std::string session = current_session(); + std::string session = session::current(); if (!session.empty()) { file::Path path(session);