From 1cf8d0390da2c5dcfd8603ce0bf49dd5ea28d28c Mon Sep 17 00:00:00 2001 From: YuSanka Date: Tue, 7 May 2024 19:55:39 +0200 Subject: [PATCH] UIManager: Added selection of the loaded archive (UI only) + Fixed state of selections till changes weren't saved --- src/slic3r/GUI/PresetArchiveDatabase.cpp | 8 +-- src/slic3r/GUI/PresetArchiveDatabase.hpp | 2 +- src/slic3r/GUI/UpdatesUIManager.cpp | 67 +++++++++--------------- src/slic3r/GUI/UpdatesUIManager.hpp | 5 +- 4 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/slic3r/GUI/PresetArchiveDatabase.cpp b/src/slic3r/GUI/PresetArchiveDatabase.cpp index ff947ef747..63b13cb064 100644 --- a/src/slic3r/GUI/PresetArchiveDatabase.cpp +++ b/src/slic3r/GUI/PresetArchiveDatabase.cpp @@ -360,7 +360,7 @@ bool PresetArchiveDatabase::set_selected_repositories(const std::vector& ptr) { return ptr->get_manifest().source_path == path; @@ -368,21 +368,21 @@ bool PresetArchiveDatabase::add_local_archive(const boost::filesystem::path path { msg = GUI::format(_L("Failed to add local archive %1%. Path already used."), path); BOOST_LOG_TRIVIAL(error) << msg; - return false; + return std::string(); } std::string uuid = get_next_uuid(); ArchiveRepository::RepositoryManifest header_data; if (!extract_local_archive_repository(uuid, path, m_unq_tmp_path, header_data)) { msg = GUI::format(_L("Failed to extract local archive %1%."), path); BOOST_LOG_TRIVIAL(error) << msg; - return false; + return std::string(); } // Solve if it can be set true first. m_selected_repositories_uuid[uuid] = false; m_archive_repositories.emplace_back(std::make_unique(uuid, std::move(header_data))); save_app_manifest_json(); - return true; + return uuid; } void PresetArchiveDatabase::remove_local_archive(const std::string& uuid) { diff --git a/src/slic3r/GUI/PresetArchiveDatabase.hpp b/src/slic3r/GUI/PresetArchiveDatabase.hpp index e50f0ca804..964c9d09b5 100644 --- a/src/slic3r/GUI/PresetArchiveDatabase.hpp +++ b/src/slic3r/GUI/PresetArchiveDatabase.hpp @@ -119,7 +119,7 @@ public: void read_server_manifest(const std::string& json_body); const std::map& get_selected_repositories_uuid() const { assert(m_selected_repositories_uuid.size() == m_archive_repositories.size()); return m_selected_repositories_uuid; } bool set_selected_repositories(const std::vector& used_uuids, std::string& msg); - bool add_local_archive(const boost::filesystem::path path, std::string& msg); + std::string add_local_archive(const boost::filesystem::path path, std::string& msg); void remove_local_archive(const std::string& uuid); private: void load_app_manifest_json(); diff --git a/src/slic3r/GUI/UpdatesUIManager.cpp b/src/slic3r/GUI/UpdatesUIManager.cpp index 79bb1114ab..26f89c24f5 100644 --- a/src/slic3r/GUI/UpdatesUIManager.cpp +++ b/src/slic3r/GUI/UpdatesUIManager.cpp @@ -52,52 +52,37 @@ UIManager::UIManager(wxWindow* parent, PresetArchiveDatabase* pad, int em) : m_main_sizer->Add(m_offline_sizer, 0, wxALL, 2 * em); - fill_entries(); + fill_entries(true); fill_grids(); } -void UIManager::fill_entries() +void UIManager::fill_entries(bool init_selection/* = false*/) { m_online_entries.clear(); m_offline_entries.clear(); - m_online_selections.clear(); - m_offline_selections.clear(); - const ArchiveRepositoryVector& archs = m_pad->get_archive_repositories(); const std::map& selected_repos = m_pad->get_selected_repositories_uuid(); for (const auto& archive : archs) { - const auto& data = archive->get_manifest(); + const std::string& uuid = archive->get_uuid(); + auto sel_it = selected_repos.find(uuid); + assert(sel_it != selected_repos.end()); + if (init_selection && sel_it->second) + m_selected_uuids.emplace(uuid); + + const bool is_selected = m_selected_uuids.find(uuid) != m_selected_uuids.end(); + const auto& data = archive->get_manifest(); + if (data.source_path.empty()) { // online repo - auto selected_it = selected_repos.find(archive->get_uuid()); - assert(selected_it != selected_repos.end()); - bool is_selected = selected_it->second; - m_online_entries.push_back({is_selected, archive->get_uuid(), data.name, data.description, data.visibility }); - if (is_selected) - m_online_selections.emplace(archive->get_uuid()); - } + m_online_entries.push_back({ is_selected, uuid, data.name, data.description, data.visibility }); + } else { // offline repo - auto selected_it = selected_repos.find(archive->get_uuid()); - assert(selected_it != selected_repos.end()); - bool is_selected = selected_it->second; - m_offline_entries.push_back({is_selected, archive->get_uuid(), data.name, data.description, data.source_path.filename().string(), fs::exists(data.source_path)}); - if (is_selected) - m_offline_selections.emplace(archive->get_uuid()); + m_offline_entries.push_back({ is_selected, uuid, data.name, data.description, data.source_path.filename().string(), fs::exists(data.source_path) }); } } - -#if 0 // ysFIXME_delete - // Next code is just for testing - - if (m_offline_entries.empty()) - m_offline_entries = { - {true, "333", "Prusa AFS" , "Prusa FDM Prusa FDM Prusa FDM" , "/path/field/file1.zip", false}, - {false, "444", "Prusa Trilab" , "Prusa sla Prusa sla Prusa sla" , "/path/field/file2.zip", true}, - }; -#endif } @@ -129,9 +114,9 @@ void UIManager::fill_grids() CheckBox::SetValue(chb, entry.use); chb->Bind(wxEVT_CHECKBOX, [this, chb, &entry](wxCommandEvent e) { if (CheckBox::GetValue(chb)) - m_online_selections.emplace(entry.id); + m_selected_uuids.emplace(entry.id); else - m_online_selections.erase(entry.id); + m_selected_uuids.erase(entry.id); }); add(chb); @@ -169,9 +154,9 @@ void UIManager::fill_grids() CheckBox::SetValue(chb, entry.use); chb->Bind(wxEVT_CHECKBOX, [this, chb, &entry](wxCommandEvent e) { if (CheckBox::GetValue(chb)) - m_offline_selections.emplace(entry.id); + m_selected_uuids.emplace(entry.id); else - m_offline_selections.erase(entry.id); + m_selected_uuids.erase(entry.id); }); add(chb); @@ -229,6 +214,7 @@ void UIManager::update() void UIManager::remove_offline_repos(const std::string& id) { m_pad->remove_local_archive(id); + m_selected_uuids.erase(id); if (wxDialog* dlg = dynamic_cast(m_parent)) { // Invalidate min_size for correct next Layout() @@ -257,26 +243,25 @@ void UIManager::load_offline_repos() try { fs::path input_path = fs::path(input_file); std::string msg; - if (!m_pad->add_local_archive(input_path, msg)) - { + std::string uuid = m_pad->add_local_archive(input_path, msg); + if (uuid.empty()) { ErrorDialog(m_parent, msg, false).ShowModal(); } + else { + m_selected_uuids.emplace(uuid); + update(); + } } catch (fs::filesystem_error const& e) { std::cerr << e.what() << '\n'; } } - - update(); } bool UIManager::set_selected_repositories() { std::vector used_ids; - for (const std::string& id : m_online_selections) - used_ids.push_back(id); - for (const std::string& id : m_offline_selections) - used_ids.push_back(id); + std::copy(m_selected_uuids.begin(), m_selected_uuids.end(), std::back_inserter(used_ids)); std::string msg; if (m_pad->set_selected_repositories(used_ids, msg)) diff --git a/src/slic3r/GUI/UpdatesUIManager.hpp b/src/slic3r/GUI/UpdatesUIManager.hpp index cb95d4159d..4f02c0a085 100644 --- a/src/slic3r/GUI/UpdatesUIManager.hpp +++ b/src/slic3r/GUI/UpdatesUIManager.hpp @@ -50,10 +50,9 @@ class UIManager std::vector m_online_entries; std::vector m_offline_entries; - std::set m_online_selections; - std::set m_offline_selections; + std::set m_selected_uuids; - void fill_entries(); + void fill_entries(bool init_selection = false); void fill_grids(); void update();