From d65712caa21da3b1aac467fb949458d2864370e5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 18 Nov 2026 19:38:10 -0800 Subject: [PATCH 1/3] PanelCatalog: extract widget-free catalog and load_panel_session() (PR 1) --- .../CommonFramework/Panels/PanelList.cpp | 20 +- .../Source/CommonFramework/Panels/PanelList.h | 12 +- .../Panels/UI/PanelListWidget.cpp | 18 +- .../ComputerPrograms_UnitTestRunner.cpp | 2 + SerialPrograms/Source/PanelCatalog.cpp | 91 ++++++ SerialPrograms/Source/PanelCatalog.h | 57 ++++ SerialPrograms/Source/PanelCatalog_Tests.cpp | 288 ++++++++++++++++++ SerialPrograms/Source/PanelCatalog_Tests.h | 18 ++ SerialPrograms/Source/PanelLists.cpp | 91 ++---- SerialPrograms/Source/PanelLists.h | 7 +- SerialPrograms/cmake/SourceFiles.cmake | 4 + 11 files changed, 526 insertions(+), 82 deletions(-) create mode 100644 SerialPrograms/Source/PanelCatalog.cpp create mode 100644 SerialPrograms/Source/PanelCatalog.h create mode 100644 SerialPrograms/Source/PanelCatalog_Tests.cpp create mode 100644 SerialPrograms/Source/PanelCatalog_Tests.h diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp b/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp index ae16768c86..77c8628e9d 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp @@ -6,6 +6,7 @@ #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Logging/Logger.h" +#include "CommonFramework/Panels/PanelSession.h" #include "PanelList.h" //#include @@ -33,9 +34,24 @@ PanelListDescriptor::PanelListDescriptor( } } +bool load_panel_session( + PanelHolder& holder, + const std::shared_ptr& descriptor, + const JsonValue& panel_json +){ + if (descriptor == nullptr){ + return false; + } + if (!holder.report_new_panel_intent(*descriptor)){ + return false; + } + std::unique_ptr panel = descriptor->make_panel(); + panel->load_json(panel_json); + holder.load_panel(descriptor, std::move(panel)); + return true; +} - -} +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h index b6da3ab2d5..50bb2714f9 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_PanelList_H #define PokemonAutomation_PanelList_H +#include #include #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/Panels/PanelTools.h" @@ -15,6 +16,7 @@ class QWidget; namespace PokemonAutomation{ +class JsonValue; class PanelListWidget; @@ -48,6 +50,14 @@ class PanelListDescriptor{ }; +// Instantiate `descriptor`, load `panel_json`, and install it in `holder`. +// Returns false if `descriptor` is null or `holder.report_new_panel_intent()` declined. +bool load_panel_session( + PanelHolder& holder, + const std::shared_ptr& descriptor, + const JsonValue& panel_json +); -} + +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp index 387ca75331..dd8106907b 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp @@ -93,18 +93,16 @@ void PanelListWidget::handle_panel_clicked(const std::string& text){ PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL] = ""; return; } - std::shared_ptr& descriptor = iter->second; + const std::shared_ptr& descriptor = iter->second; // cout << descriptor->display_name() << endl; - if (!m_panel_holder.report_new_panel_intent(*descriptor)){ - return; - } try{ - std::unique_ptr panel = descriptor->make_panel(); -// try{ - panel->load_json(PERSISTENT_SETTINGS().panels[descriptor->identifier()]); -// }catch (ParseException&){} - m_panel_holder.load_panel(descriptor, std::move(panel)); - + if (!load_panel_session( + m_panel_holder, + descriptor, + PERSISTENT_SETTINGS().panels[descriptor->identifier()] + )){ + return; + } PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL] = iter->first; }catch (const Exception& error){ QMessageBox box; diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 0cda95e530..058586d5bd 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -23,6 +23,7 @@ #include "CommonFramework/Tools/GlobalThreadPools.h" #include "CommonFramework/Windows/LabChannel_Tests.h" #include "CommonFramework/Startup/StartupArguments_Tests.h" +#include "PanelCatalog_Tests.h" #include "CommonFramework/Startup/LabStartupPolicy_Tests.h" #include "CommonFramework/Devices/LabDeviceAccessQt_Tests.h" #include "CommonTools/VisualDetectors/BlackBorderDetector.h" @@ -86,6 +87,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_AudioFileLoader(ret); add_tests_LabChannel(ret); add_tests_StartupArguments(ret); + add_tests_PanelCatalog(ret); add_tests_LabStartupPolicy(ret); add_tests_LabDeviceAccess(ret); OCR::add_tests(ret); diff --git a/SerialPrograms/Source/PanelCatalog.cpp b/SerialPrograms/Source/PanelCatalog.cpp new file mode 100644 index 0000000000..36e3e54509 --- /dev/null +++ b/SerialPrograms/Source/PanelCatalog.cpp @@ -0,0 +1,91 @@ +/* Panel Catalog + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/StaticGlobals.h" +#include "ML/ML_Panels.h" +#include "NintendoSwitch/NintendoSwitch_Panels.h" +#include "PokemonBDSP/PokemonBDSP_Panels.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Panels.h" +#include "VideoGames/PokemonHome/PokemonHome_Panels.h" +#include "VideoGames/PokemonLA/PokemonLA_Panels.h" +#include "VideoGames/PokemonLGPE/PokemonLGPE_Panels.h" +#include "VideoGames/PokemonLZA/PokemonLZA_Panels.h" +#include "VideoGames/PokemonPokopia/PokemonPokopia_Panels.h" +#include "VideoGames/PokemonRSE/PokemonRSE_Panels.h" +#include "VideoGames/PokemonSV/PokemonSV_Panels.h" +#include "VideoGames/PokemonSwSh/PokemonSwSh_Panels.h" +#include "VideoGames/ZeldaTotK/ZeldaTotK_Panels.h" +#include "PanelCatalog.h" + +namespace PokemonAutomation{ + + +PanelCatalog::PanelCatalog(){ + add(std::make_unique()); + add(std::make_unique()); + add(std::make_unique()); + add(std::make_unique()); + add(std::make_unique()); + add(std::make_unique()); + add(std::make_unique()); + add(std::make_unique()); + add(std::make_unique()); + add(std::make_unique()); + if (STATIC_GLOBALS.DEVELOPER_MODE){ + add(std::make_unique()); + } + + add(std::make_unique()); + + if (STATIC_GLOBALS.DEVELOPER_MODE){ + add(std::make_unique()); + } +} + +PanelCatalog::PanelCatalog(std::vector> lists){ + for (std::unique_ptr& list : lists){ + add(std::move(list)); + } +} + +void PanelCatalog::add(std::unique_ptr list){ + const size_t index = m_lists.size(); + m_lists.emplace_back(std::move(list)); + const PanelListDescriptor& back = *m_lists.back(); + if (!m_tab_map.emplace(back.name(), index).second){ + const std::string name = back.name(); + m_lists.pop_back(); + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Duplicate Category Name: " + name); + } +} + +std::optional PanelCatalog::find_category(std::string_view name) const{ + auto iter = m_tab_map.find(name); + if (iter == m_tab_map.end()){ + return std::nullopt; + } + return iter->second; +} + +std::optional PanelCatalog::find_panel(std::string_view identifier) const{ + for (size_t c = 0; c < m_lists.size(); c++){ + for (PanelEntry& entry : m_lists[c]->panels()){ + if (entry.descriptor == nullptr || entry.descriptor->identifier() != identifier){ + continue; + } + return PanelCatalogMatch{ + .category_index = c, + .display_name = std::move(entry.display_name), + .descriptor = std::shared_ptr(std::move(entry.descriptor)), + }; + } + } + return std::nullopt; +} + + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PanelCatalog.h b/SerialPrograms/Source/PanelCatalog.h new file mode 100644 index 0000000000..6e24e657f6 --- /dev/null +++ b/SerialPrograms/Source/PanelCatalog.h @@ -0,0 +1,57 @@ +/* Panel Catalog + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PanelCatalog_H +#define PokemonAutomation_PanelCatalog_H + +#include +#include +#include +#include +#include +#include +#include +#include "CommonFramework/Panels/PanelList.h" + +namespace PokemonAutomation{ + + +// Match returned by PanelCatalog::find_panel(). +struct PanelCatalogMatch{ + size_t category_index; + std::string display_name; + std::shared_ptr descriptor; +}; + + +// Widget-free catalog of all registered program categories and panels. +class PanelCatalog{ +public: + PanelCatalog(); + explicit PanelCatalog(std::vector> lists); + + const std::vector>& lists() const{ + return m_lists; + } + + // Find category index by category display name. + std::optional find_category(std::string_view name) const; + + // Scan each category's panels() for `identifier` and convert the matching + // entry's unique_ptr into an owning shared_ptr. + std::optional find_panel(std::string_view identifier) const; + +private: + void add(std::unique_ptr list); + +private: + std::vector> m_lists; + std::map> m_tab_map; +}; + + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PanelCatalog_Tests.cpp b/SerialPrograms/Source/PanelCatalog_Tests.cpp new file mode 100644 index 0000000000..e4fb7e6287 --- /dev/null +++ b/SerialPrograms/Source/PanelCatalog_Tests.cpp @@ -0,0 +1,288 @@ +/* Panel Catalog Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "CommonFramework/Panels/PanelDescriptor.h" +#include "CommonFramework/Panels/PanelList.h" +#include "CommonFramework/Panels/PanelSession.h" +#include "PanelCatalog.h" +#include "PanelCatalog_Tests.h" + +namespace PokemonAutomation{ +namespace{ + +class FakePanelSession final : public PanelSession{ +public: + explicit FakePanelSession(const PanelDescriptor& descriptor) + : PanelSession(descriptor) + {} + + virtual void load_json(const JsonValue& json) override{ + const JsonObject* obj = json.to_object(); + if (obj != nullptr){ + if (const std::string* val = obj->get_string("key")){ + loaded_value = *val; + } + } + } + + std::string loaded_value; +}; + +class FakePanelDescriptor final : public PanelDescriptor{ +public: + FakePanelDescriptor(std::string identifier, std::string display_name) + : PanelDescriptor( + std::move(identifier), + "FakeCategory", + std::move(display_name), + "", + "", + PanelQualityStanding::DEVELOPER, + false + ) + {} + + virtual std::unique_ptr make_panel() const override{ + return std::make_unique(*this); + } +}; + +class FakePanelListDescriptor final : public PanelListDescriptor{ +public: + FakePanelListDescriptor( + std::string name, + std::vector> entries + ) + : PanelListDescriptor(std::move(name)) + , m_entries(std::move(entries)) + {} + +protected: + virtual std::vector make_panels() const override{ + std::vector ret; + for (const auto& [display_name, id] : m_entries){ + if (id.empty()){ + ret.emplace_back(display_name); + }else{ + ret.emplace_back(std::make_unique(id, display_name)); + } + } + return ret; + } + +private: + std::vector> m_entries; +}; + +class FakePanelHolder final : public PanelHolder{ +public: + explicit FakePanelHolder(bool accept_intent) + : m_accept_intent(accept_intent) + {} + + virtual bool report_new_panel_intent(const PanelDescriptor&) override{ + intent_called = true; + return m_accept_intent; + } + + virtual void load_panel( + std::shared_ptr descriptor, + std::unique_ptr panel + ) override{ + loaded_descriptor = std::move(descriptor); + loaded_panel = std::move(panel); + } + + virtual void on_busy() override{} + virtual void on_idle() override{} + + bool intent_called = false; + std::shared_ptr loaded_descriptor; + std::unique_ptr loaded_panel; + +private: + bool m_accept_intent; +}; + +PanelCatalog make_test_catalog(){ + std::vector> lists; + lists.push_back(std::make_unique( + "CatA", + std::vector>{ + {"Divider", ""}, + {"Alpha", "CatA:Alpha"}, + } + )); + lists.push_back(std::make_unique( + "CatB", + std::vector>{ + {"Beta", "CatB:Beta"}, + } + )); + return PanelCatalog(std::move(lists)); +} + +struct CatalogLookupInput{ + std::string_view name; + std::string_view identifier; +}; + +struct CatalogLookupExpectation{ + bool found; + size_t category_index = 0; + std::string_view display_name = ""; +}; + +struct CatalogLookupCase{ + CatalogLookupInput input; + CatalogLookupExpectation expectation; +}; + +const CatalogLookupCase CATALOG_LOOKUP_CASES[] = { + {.input = {.name = "first category after divider", .identifier = "CatA:Alpha"}, + .expectation = {.found = true, .category_index = 0, .display_name = "Alpha"}}, + {.input = {.name = "second category", .identifier = "CatB:Beta"}, + .expectation = {.found = true, .category_index = 1, .display_name = "Beta"}}, + {.input = {.name = "missing identifier", .identifier = "CatB:Missing"}, + .expectation = {.found = false}}, +}; + +UnitTestResult check_catalog_lookup(const CatalogLookupCase& test){ + const PanelCatalog catalog = make_test_catalog(); + const std::optional match = catalog.find_panel(test.input.identifier); + if (match.has_value() != test.expectation.found){ + return "Unexpected find_panel() presence."; + } + if (!match){ + return true; + } + if (match->category_index != test.expectation.category_index || + match->display_name != test.expectation.display_name || + match->descriptor == nullptr || + match->descriptor->identifier() != test.input.identifier) + { + return "Catalog match fields did not match expected values."; + } + return true; +} + +struct LoadSessionInput{ + std::string_view name; + bool has_descriptor; + bool accept_intent; + std::string_view json_value; +}; + +struct LoadSessionExpectation{ + bool result; + bool loaded; + std::string_view loaded_value = ""; +}; + +struct LoadSessionCase{ + LoadSessionInput input; + LoadSessionExpectation expectation; +}; + +const LoadSessionCase LOAD_SESSION_CASES[] = { + {.input = {.name = "loads panel and json", .has_descriptor = true, .accept_intent = true, .json_value = "abc"}, + .expectation = {.result = true, .loaded = true, .loaded_value = "abc"}}, + {.input = {.name = "declined intent skips load", .has_descriptor = true, .accept_intent = false, .json_value = "abc"}, + .expectation = {.result = false, .loaded = false}}, + {.input = {.name = "null descriptor returns false", .has_descriptor = false, .accept_intent = true, .json_value = "abc"}, + .expectation = {.result = false, .loaded = false}}, +}; + +UnitTestResult check_load_session(const LoadSessionCase& test){ + FakePanelHolder holder(test.input.accept_intent); + std::shared_ptr descriptor; + if (test.input.has_descriptor){ + descriptor = std::make_shared("Fake:Id", "Fake"); + } + JsonObject obj; + obj["key"] = std::string(test.input.json_value); + const bool ok = load_panel_session(holder, descriptor, obj); + if (ok != test.expectation.result){ + return "Unexpected load_panel_session() return value."; + } + const bool is_loaded = holder.loaded_panel != nullptr && holder.loaded_descriptor != nullptr; + if (is_loaded != test.expectation.loaded){ + return "Unexpected holder load state."; + } + if (is_loaded){ + const auto* fake = dynamic_cast(holder.loaded_panel.get()); + if (fake == nullptr || fake->loaded_value != test.expectation.loaded_value){ + return "Loaded JSON value did not match."; + } + } + return true; +} + +class CatalogLookupTest final : public UnitTest{ +public: + explicit CatalogLookupTest(const CatalogLookupCase& test) + : UnitTest("PanelCatalog/Lookup/" + std::string(test.input.name)) + , m_test(test) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_catalog_lookup(m_test); + } + +private: + const CatalogLookupCase& m_test; +}; + +class LoadSessionTest final : public UnitTest{ +public: + explicit LoadSessionTest(const LoadSessionCase& test) + : UnitTest("PanelCatalog/LoadSession/" + std::string(test.input.name)) + , m_test(test) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_load_session(m_test); + } + +private: + const LoadSessionCase& m_test; +}; + +class DuplicateCategoryTest final : public UnitTest{ +public: + DuplicateCategoryTest() + : UnitTest("PanelCatalog/DuplicateCategoryThrows") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector> lists; + lists.push_back(std::make_unique("Dup", std::vector>{})); + lists.push_back(std::make_unique("Dup", std::vector>{})); + try{ + PanelCatalog catalog(std::move(lists)); + }catch (const InternalProgramError&){ + return true; + } + return "Expected InternalProgramError on duplicate category."; + } +}; + +} // namespace + +void add_tests_PanelCatalog(UnitTestDatabase& database){ + for (const CatalogLookupCase& test : CATALOG_LOOKUP_CASES){ + database.add(test); + } + for (const LoadSessionCase& test : LOAD_SESSION_CASES){ + database.add(test); + } + database.add(); +} + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PanelCatalog_Tests.h b/SerialPrograms/Source/PanelCatalog_Tests.h new file mode 100644 index 0000000000..c2883043c9 --- /dev/null +++ b/SerialPrograms/Source/PanelCatalog_Tests.h @@ -0,0 +1,18 @@ +/* Panel Catalog Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PanelCatalog_Tests_H +#define PokemonAutomation_PanelCatalog_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + +// Register PanelCatalog and load_panel_session() unit tests. +void add_tests_PanelCatalog(UnitTestDatabase& database); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PanelLists.cpp b/SerialPrograms/Source/PanelLists.cpp index cf857b7c9c..e86a357628 100644 --- a/SerialPrograms/Source/PanelLists.cpp +++ b/SerialPrograms/Source/PanelLists.cpp @@ -7,24 +7,10 @@ #include #include #include "Common/Qt/NoWheelComboBox.h" -#include "CommonFramework/StaticGlobals.h" #include "CommonFramework/PersistentSettings.h" #include "CommonFramework/ImageTypes/ImageRGB32_Qt.h" #include "CommonFramework/Windows/DpiScaler.h" #include "CommonFramework/Panels/UI/PanelListWidget.h" -#include "ML/ML_Panels.h" -#include "NintendoSwitch/NintendoSwitch_Panels.h" -#include "VideoGames/PokemonSwSh/PokemonSwSh_Panels.h" -#include "VideoGames/PokemonHome/PokemonHome_Panels.h" -#include "PokemonBDSP/PokemonBDSP_Panels.h" -#include "VideoGames/PokemonFRLG/PokemonFRLG_Panels.h" -#include "VideoGames/PokemonLA/PokemonLA_Panels.h" -#include "VideoGames/PokemonLGPE/PokemonLGPE_Panels.h" -#include "VideoGames/PokemonRSE/PokemonRSE_Panels.h" -#include "VideoGames/PokemonSV/PokemonSV_Panels.h" -#include "VideoGames/PokemonLZA/PokemonLZA_Panels.h" -#include "VideoGames/PokemonPokopia/PokemonPokopia_Panels.h" -#include "VideoGames/ZeldaTotK/ZeldaTotK_Panels.h" #include "PanelLists.h" //#include @@ -59,31 +45,15 @@ ProgramSelect::ProgramSelect(QWidget& parent, PanelHolder& holder) layout->addWidget(m_dropdown); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - if (STATIC_GLOBALS.DEVELOPER_MODE){ - add(std::make_unique()); + const auto& lists = m_catalog.lists(); + for (size_t c = 0; c < lists.size(); c++){ + add_dropdown_entry(*lists[c], static_cast(c)); } - add(std::make_unique()); - - if (STATIC_GLOBALS.DEVELOPER_MODE){ - add(std::make_unique()); - } - - // Load the 1st list by default. m_dropdown->setCurrentIndex(0); m_active_index = 0; - m_active_list = m_lists[0]->make_QWidget(*this, m_holder); + m_active_list = lists[0]->make_QWidget(*this, m_holder); layout->addWidget(m_active_list); connect( @@ -103,25 +73,18 @@ void ProgramSelect::unlock(){ m_active_list->setEnabled(true); } -void ProgramSelect::add(std::unique_ptr list){ - int index = m_dropdown->count(); - const ImageViewRGB32& icon = list->icon(); +void ProgramSelect::add_dropdown_entry(const PanelListDescriptor& list, int index){ + const ImageViewRGB32& icon = list.icon(); if (icon){ m_dropdown->addItem( QIcon(QPixmap::fromImage(to_QImage_ref(icon))), - QString::fromStdString(list->name()) + QString::fromStdString(list.name()) ); }else{ - m_dropdown->addItem(QString::fromStdString(list->name())); + m_dropdown->addItem(QString::fromStdString(list.name())); } - m_lists.emplace_back(std::move(list)); - const PanelListDescriptor& back = *m_lists.back(); - if (!m_tab_map.emplace(back.name(), index).second){ - m_lists.pop_back(); - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Duplicate Category Name: " + back.name()); - } - if (!back.enabled()){ + if (!list.enabled()){ auto* model = qobject_cast(m_dropdown->model()); if (model != nullptr){ QStandardItem* line_handle = model->item(index); @@ -139,12 +102,13 @@ void ProgramSelect::load_persistent_panel(){ if (str == nullptr){ return; } - auto iter = m_tab_map.find(*str); - if (iter == m_tab_map.end()){ + const std::optional category = m_catalog.find_category(*str); + if (!category){ return; } - m_dropdown->setCurrentIndex(iter->second); - change_list(iter->second); + const int index = static_cast(*category); + m_dropdown->setCurrentIndex(index); + change_list(index); str = PERSISTENT_SETTINGS().panels.get_string(PanelListWidget::JSON_PROGRAM_PANEL); if (str != nullptr){ m_active_list->set_panel(*str); @@ -152,31 +116,28 @@ void ProgramSelect::load_persistent_panel(){ } std::string ProgramSelect::select_program(const std::string& identifier){ - for (size_t c = 0; c < m_lists.size(); c++){ - for (const PanelEntry& entry : m_lists[c]->panels()){ - if (entry.descriptor == nullptr || entry.descriptor->identifier() != identifier){ - continue; - } - if (m_locked){ - return "A program is running."; - } - change_list((int)c); - m_active_list->set_panel(entry.display_name); - return ""; - } + std::optional match = m_catalog.find_panel(identifier); + if (!match){ + return "No program with identifier \"" + identifier + "\"."; + } + if (m_locked){ + return "A program is running."; } - return "No program with identifier \"" + identifier + "\"."; + change_list(static_cast(match->category_index)); + m_active_list->set_panel(match->display_name); + return ""; } void ProgramSelect::change_list(int index){ if (m_active_index == index && m_active_list != nullptr){ return; } + const auto& lists = m_catalog.lists(); m_dropdown->setCurrentIndex(index); m_active_index = index; - PERSISTENT_SETTINGS().panels["ProgramCategory"] = m_lists[index]->name(); + PERSISTENT_SETTINGS().panels["ProgramCategory"] = lists[index]->name(); delete m_active_list; - m_active_list = m_lists[index]->make_QWidget(*this, m_holder); + m_active_list = lists[index]->make_QWidget(*this, m_holder); m_active_list->setEnabled(!m_locked); layout()->addWidget(m_active_list); } diff --git a/SerialPrograms/Source/PanelLists.h b/SerialPrograms/Source/PanelLists.h index b11edcc25c..83bf454a1e 100644 --- a/SerialPrograms/Source/PanelLists.h +++ b/SerialPrograms/Source/PanelLists.h @@ -8,7 +8,7 @@ #define PokemonAutomation_ProgramTabs_H #include -#include "CommonFramework/Panels/PanelList.h" +#include "PanelCatalog.h" class QComboBox; @@ -37,14 +37,13 @@ class ProgramSelect : public QWidget{ virtual QSize sizeHint() const override; private: - void add(std::unique_ptr list); + void add_dropdown_entry(const PanelListDescriptor& list, int index); void change_list(int index); private: PanelHolder& m_holder; - std::vector> m_lists; - std::map m_tab_map; + PanelCatalog m_catalog; QComboBox* m_dropdown; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5e5ced94bf..41df561e6d 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1456,6 +1456,10 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_TypewriterTextMapping.cpp Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_TypewriterTextMapping.h + Source/PanelCatalog.cpp + Source/PanelCatalog.h + Source/PanelCatalog_Tests.cpp + Source/PanelCatalog_Tests.h Source/PanelLists.cpp Source/PanelLists.h Source/Pokemon/Inference/Pokemon_AbilityReader.cpp From a76e8ffe49c61416c9f637093716fd53dbdc29ef Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 18 Nov 2026 19:54:52 -0800 Subject: [PATCH 2/3] PanelCatalog: free functions instead of a catalog class (review) make_all_panel_lists() and find_panel(lists, id) replace PanelCatalog; ProgramSelect keeps its lists, category map and add(). Drop the unreachable null branch in load_panel_session() (holder now last) and the JsonValue forward declaration. Tests: lookup past a divider and declined intent skips load. --- .../CommonFramework/Panels/PanelList.cpp | 10 +- .../Source/CommonFramework/Panels/PanelList.h | 13 +- .../Panels/UI/PanelListWidget.cpp | 4 +- SerialPrograms/Source/PanelCatalog.cpp | 79 ++--- SerialPrograms/Source/PanelCatalog.h | 43 +-- SerialPrograms/Source/PanelCatalog_Tests.cpp | 269 ++++-------------- SerialPrograms/Source/PanelLists.cpp | 47 +-- SerialPrograms/Source/PanelLists.h | 7 +- 8 files changed, 136 insertions(+), 336 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp b/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp index 77c8628e9d..0ebce8d7e7 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp @@ -35,13 +35,10 @@ PanelListDescriptor::PanelListDescriptor( } bool load_panel_session( - PanelHolder& holder, const std::shared_ptr& descriptor, - const JsonValue& panel_json + const JsonValue& panel_json, + PanelHolder& holder ){ - if (descriptor == nullptr){ - return false; - } if (!holder.report_new_panel_intent(*descriptor)){ return false; } @@ -52,6 +49,7 @@ bool load_panel_session( } -} // namespace PokemonAutomation + +} diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h index 50bb2714f9..0ab5f36dca 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h @@ -16,7 +16,6 @@ class QWidget; namespace PokemonAutomation{ -class JsonValue; class PanelListWidget; @@ -50,14 +49,16 @@ class PanelListDescriptor{ }; -// Instantiate `descriptor`, load `panel_json`, and install it in `holder`. -// Returns false if `descriptor` is null or `holder.report_new_panel_intent()` declined. +// Instantiate `descriptor` (non-null), load `panel_json` into it, and install +// it in `holder`. Returns false, loading nothing, if +// `holder.report_new_panel_intent()` declines. bool load_panel_session( - PanelHolder& holder, const std::shared_ptr& descriptor, - const JsonValue& panel_json + const JsonValue& panel_json, + PanelHolder& holder ); -} // namespace PokemonAutomation + +} #endif diff --git a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp index dd8106907b..277bdfebba 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp @@ -97,9 +97,9 @@ void PanelListWidget::handle_panel_clicked(const std::string& text){ // cout << descriptor->display_name() << endl; try{ if (!load_panel_session( - m_panel_holder, descriptor, - PERSISTENT_SETTINGS().panels[descriptor->identifier()] + PERSISTENT_SETTINGS().panels[descriptor->identifier()], + m_panel_holder )){ return; } diff --git a/SerialPrograms/Source/PanelCatalog.cpp b/SerialPrograms/Source/PanelCatalog.cpp index 36e3e54509..545a036e92 100644 --- a/SerialPrograms/Source/PanelCatalog.cpp +++ b/SerialPrograms/Source/PanelCatalog.cpp @@ -4,84 +4,59 @@ * */ -#include "Common/Cpp/Exceptions.h" #include "CommonFramework/StaticGlobals.h" #include "ML/ML_Panels.h" #include "NintendoSwitch/NintendoSwitch_Panels.h" +#include "VideoGames/PokemonSwSh/PokemonSwSh_Panels.h" +#include "VideoGames/PokemonHome/PokemonHome_Panels.h" #include "PokemonBDSP/PokemonBDSP_Panels.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Panels.h" -#include "VideoGames/PokemonHome/PokemonHome_Panels.h" #include "VideoGames/PokemonLA/PokemonLA_Panels.h" #include "VideoGames/PokemonLGPE/PokemonLGPE_Panels.h" -#include "VideoGames/PokemonLZA/PokemonLZA_Panels.h" -#include "VideoGames/PokemonPokopia/PokemonPokopia_Panels.h" #include "VideoGames/PokemonRSE/PokemonRSE_Panels.h" #include "VideoGames/PokemonSV/PokemonSV_Panels.h" -#include "VideoGames/PokemonSwSh/PokemonSwSh_Panels.h" +#include "VideoGames/PokemonLZA/PokemonLZA_Panels.h" +#include "VideoGames/PokemonPokopia/PokemonPokopia_Panels.h" #include "VideoGames/ZeldaTotK/ZeldaTotK_Panels.h" #include "PanelCatalog.h" namespace PokemonAutomation{ -PanelCatalog::PanelCatalog(){ - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); - add(std::make_unique()); +std::vector> make_all_panel_lists(){ + std::vector> ret; + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); if (STATIC_GLOBALS.DEVELOPER_MODE){ - add(std::make_unique()); + ret.emplace_back(std::make_unique()); } - add(std::make_unique()); + ret.emplace_back(std::make_unique()); if (STATIC_GLOBALS.DEVELOPER_MODE){ - add(std::make_unique()); - } -} - -PanelCatalog::PanelCatalog(std::vector> lists){ - for (std::unique_ptr& list : lists){ - add(std::move(list)); - } -} - -void PanelCatalog::add(std::unique_ptr list){ - const size_t index = m_lists.size(); - m_lists.emplace_back(std::move(list)); - const PanelListDescriptor& back = *m_lists.back(); - if (!m_tab_map.emplace(back.name(), index).second){ - const std::string name = back.name(); - m_lists.pop_back(); - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Duplicate Category Name: " + name); - } -} - -std::optional PanelCatalog::find_category(std::string_view name) const{ - auto iter = m_tab_map.find(name); - if (iter == m_tab_map.end()){ - return std::nullopt; + ret.emplace_back(std::make_unique()); } - return iter->second; + return ret; } -std::optional PanelCatalog::find_panel(std::string_view identifier) const{ - for (size_t c = 0; c < m_lists.size(); c++){ - for (PanelEntry& entry : m_lists[c]->panels()){ +std::optional find_panel( + const std::vector>& lists, + std::string_view identifier +){ + for (size_t c = 0; c < lists.size(); c++){ + for (PanelEntry& entry : lists[c]->panels()){ if (entry.descriptor == nullptr || entry.descriptor->identifier() != identifier){ continue; } - return PanelCatalogMatch{ - .category_index = c, - .display_name = std::move(entry.display_name), - .descriptor = std::shared_ptr(std::move(entry.descriptor)), - }; + return PanelMatch{.category_index = c, .entry = std::move(entry)}; } } return std::nullopt; diff --git a/SerialPrograms/Source/PanelCatalog.h b/SerialPrograms/Source/PanelCatalog.h index 6e24e657f6..6d2eb49ad2 100644 --- a/SerialPrograms/Source/PanelCatalog.h +++ b/SerialPrograms/Source/PanelCatalog.h @@ -8,10 +8,8 @@ #define PokemonAutomation_PanelCatalog_H #include -#include #include #include -#include #include #include #include "CommonFramework/Panels/PanelList.h" @@ -19,39 +17,22 @@ namespace PokemonAutomation{ -// Match returned by PanelCatalog::find_panel(). -struct PanelCatalogMatch{ - size_t category_index; - std::string display_name; - std::shared_ptr descriptor; -}; - - -// Widget-free catalog of all registered program categories and panels. -class PanelCatalog{ -public: - PanelCatalog(); - explicit PanelCatalog(std::vector> lists); - - const std::vector>& lists() const{ - return m_lists; - } +// Every program category, in dropdown order. +std::vector> make_all_panel_lists(); - // Find category index by category display name. - std::optional find_category(std::string_view name) const; - // Scan each category's panels() for `identifier` and convert the matching - // entry's unique_ptr into an owning shared_ptr. - std::optional find_panel(std::string_view identifier) const; - -private: - void add(std::unique_ptr list); - -private: - std::vector> m_lists; - std::map> m_tab_map; +// A panel found by find_panel(), with the index of its list in `lists`. +struct PanelMatch{ + size_t category_index; + PanelEntry entry; }; +// Scans each list's panels() for the panel with `identifier`. +std::optional find_panel( + const std::vector>& lists, + std::string_view identifier +); + } // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/PanelCatalog_Tests.cpp b/SerialPrograms/Source/PanelCatalog_Tests.cpp index e4fb7e6287..dccf8ba173 100644 --- a/SerialPrograms/Source/PanelCatalog_Tests.cpp +++ b/SerialPrograms/Source/PanelCatalog_Tests.cpp @@ -4,8 +4,6 @@ * */ -#include "Common/Cpp/Exceptions.h" -#include "Common/Cpp/Json/JsonObject.h" #include "Common/Cpp/Json/JsonValue.h" #include "CommonFramework/Panels/PanelDescriptor.h" #include "CommonFramework/Panels/PanelList.h" @@ -16,273 +14,112 @@ namespace PokemonAutomation{ namespace{ -class FakePanelSession final : public PanelSession{ -public: - explicit FakePanelSession(const PanelDescriptor& descriptor) - : PanelSession(descriptor) - {} - - virtual void load_json(const JsonValue& json) override{ - const JsonObject* obj = json.to_object(); - if (obj != nullptr){ - if (const std::string* val = obj->get_string("key")){ - loaded_value = *val; - } - } - } - - std::string loaded_value; -}; class FakePanelDescriptor final : public PanelDescriptor{ public: - FakePanelDescriptor(std::string identifier, std::string display_name) + explicit FakePanelDescriptor(std::string identifier) : PanelDescriptor( - std::move(identifier), - "FakeCategory", - std::move(display_name), - "", - "", + identifier, + /*category=*/"FakeCategory", + /*display_name=*/identifier, + /*doc_link=*/"", + /*description=*/"", PanelQualityStanding::DEVELOPER, - false + /*restore_defaults_button=*/false ) {} virtual std::unique_ptr make_panel() const override{ - return std::make_unique(*this); + return std::make_unique(*this); } }; -class FakePanelListDescriptor final : public PanelListDescriptor{ +// One category: a divider, then the panel "Fake:Alpha". +class FakePanelList final : public PanelListDescriptor{ public: - FakePanelListDescriptor( - std::string name, - std::vector> entries - ) - : PanelListDescriptor(std::move(name)) - , m_entries(std::move(entries)) + FakePanelList() + : PanelListDescriptor("FakeList") {} protected: virtual std::vector make_panels() const override{ std::vector ret; - for (const auto& [display_name, id] : m_entries){ - if (id.empty()){ - ret.emplace_back(display_name); - }else{ - ret.emplace_back(std::make_unique(id, display_name)); - } - } + ret.emplace_back("Divider"); + ret.emplace_back(std::make_unique("Fake:Alpha")); return ret; } - -private: - std::vector> m_entries; }; -class FakePanelHolder final : public PanelHolder{ +class DecliningPanelHolder final : public PanelHolder{ public: - explicit FakePanelHolder(bool accept_intent) - : m_accept_intent(accept_intent) - {} - virtual bool report_new_panel_intent(const PanelDescriptor&) override{ - intent_called = true; - return m_accept_intent; + return false; } - virtual void load_panel( - std::shared_ptr descriptor, - std::unique_ptr panel + std::shared_ptr, + std::unique_ptr ) override{ - loaded_descriptor = std::move(descriptor); - loaded_panel = std::move(panel); + loaded = true; } - virtual void on_busy() override{} virtual void on_idle() override{} - bool intent_called = false; - std::shared_ptr loaded_descriptor; - std::unique_ptr loaded_panel; - -private: - bool m_accept_intent; -}; - -PanelCatalog make_test_catalog(){ - std::vector> lists; - lists.push_back(std::make_unique( - "CatA", - std::vector>{ - {"Divider", ""}, - {"Alpha", "CatA:Alpha"}, - } - )); - lists.push_back(std::make_unique( - "CatB", - std::vector>{ - {"Beta", "CatB:Beta"}, - } - )); - return PanelCatalog(std::move(lists)); -} - -struct CatalogLookupInput{ - std::string_view name; - std::string_view identifier; + bool loaded = false; }; -struct CatalogLookupExpectation{ - bool found; - size_t category_index = 0; - std::string_view display_name = ""; -}; -struct CatalogLookupCase{ - CatalogLookupInput input; - CatalogLookupExpectation expectation; -}; - -const CatalogLookupCase CATALOG_LOOKUP_CASES[] = { - {.input = {.name = "first category after divider", .identifier = "CatA:Alpha"}, - .expectation = {.found = true, .category_index = 0, .display_name = "Alpha"}}, - {.input = {.name = "second category", .identifier = "CatB:Beta"}, - .expectation = {.found = true, .category_index = 1, .display_name = "Beta"}}, - {.input = {.name = "missing identifier", .identifier = "CatB:Missing"}, - .expectation = {.found = false}}, -}; - -UnitTestResult check_catalog_lookup(const CatalogLookupCase& test){ - const PanelCatalog catalog = make_test_catalog(); - const std::optional match = catalog.find_panel(test.input.identifier); - if (match.has_value() != test.expectation.found){ - return "Unexpected find_panel() presence."; - } - if (!match){ - return true; - } - if (match->category_index != test.expectation.category_index || - match->display_name != test.expectation.display_name || - match->descriptor == nullptr || - match->descriptor->identifier() != test.input.identifier) - { - return "Catalog match fields did not match expected values."; - } - return true; -} - -struct LoadSessionInput{ - std::string_view name; - bool has_descriptor; - bool accept_intent; - std::string_view json_value; -}; - -struct LoadSessionExpectation{ - bool result; - bool loaded; - std::string_view loaded_value = ""; -}; - -struct LoadSessionCase{ - LoadSessionInput input; - LoadSessionExpectation expectation; -}; - -const LoadSessionCase LOAD_SESSION_CASES[] = { - {.input = {.name = "loads panel and json", .has_descriptor = true, .accept_intent = true, .json_value = "abc"}, - .expectation = {.result = true, .loaded = true, .loaded_value = "abc"}}, - {.input = {.name = "declined intent skips load", .has_descriptor = true, .accept_intent = false, .json_value = "abc"}, - .expectation = {.result = false, .loaded = false}}, - {.input = {.name = "null descriptor returns false", .has_descriptor = false, .accept_intent = true, .json_value = "abc"}, - .expectation = {.result = false, .loaded = false}}, -}; - -UnitTestResult check_load_session(const LoadSessionCase& test){ - FakePanelHolder holder(test.input.accept_intent); - std::shared_ptr descriptor; - if (test.input.has_descriptor){ - descriptor = std::make_shared("Fake:Id", "Fake"); - } - JsonObject obj; - obj["key"] = std::string(test.input.json_value); - const bool ok = load_panel_session(holder, descriptor, obj); - if (ok != test.expectation.result){ - return "Unexpected load_panel_session() return value."; - } - const bool is_loaded = holder.loaded_panel != nullptr && holder.loaded_descriptor != nullptr; - if (is_loaded != test.expectation.loaded){ - return "Unexpected holder load state."; - } - if (is_loaded){ - const auto* fake = dynamic_cast(holder.loaded_panel.get()); - if (fake == nullptr || fake->loaded_value != test.expectation.loaded_value){ - return "Loaded JSON value did not match."; - } - } - return true; -} - -class CatalogLookupTest final : public UnitTest{ +class FindPanelPastDividerTest final : public UnitTest{ public: - explicit CatalogLookupTest(const CatalogLookupCase& test) - : UnitTest("PanelCatalog/Lookup/" + std::string(test.input.name)) - , m_test(test) + FindPanelPastDividerTest() + : UnitTest("PanelCatalog/FindPanel/past a divider") {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - return check_catalog_lookup(m_test); - } - -private: - const CatalogLookupCase& m_test; -}; - -class LoadSessionTest final : public UnitTest{ -public: - explicit LoadSessionTest(const LoadSessionCase& test) - : UnitTest("PanelCatalog/LoadSession/" + std::string(test.input.name)) - , m_test(test) - {} + std::vector> lists; + lists.emplace_back(std::make_unique()); - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - return check_load_session(m_test); + const std::optional match = find_panel(lists, "Fake:Alpha"); + if (!match){ + return "find_panel() did not find Fake:Alpha."; + } + if (match->category_index != 0){ + return "find_panel() returned the wrong category index."; + } + if (match->entry.descriptor == nullptr || match->entry.descriptor->identifier() != "Fake:Alpha"){ + return "find_panel() returned the wrong panel."; + } + return true; } - -private: - const LoadSessionCase& m_test; }; -class DuplicateCategoryTest final : public UnitTest{ +class LoadPanelSessionDeclinedTest final : public UnitTest{ public: - DuplicateCategoryTest() - : UnitTest("PanelCatalog/DuplicateCategoryThrows") + LoadPanelSessionDeclinedTest() + : UnitTest("PanelCatalog/LoadPanelSession/declined intent skips load") {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - std::vector> lists; - lists.push_back(std::make_unique("Dup", std::vector>{})); - lists.push_back(std::make_unique("Dup", std::vector>{})); - try{ - PanelCatalog catalog(std::move(lists)); - }catch (const InternalProgramError&){ - return true; + DecliningPanelHolder holder; + const std::shared_ptr descriptor = + std::make_shared("Fake:Alpha"); + + if (load_panel_session(descriptor, JsonValue(), holder)){ + return "load_panel_session() returned true after the holder declined."; } - return "Expected InternalProgramError on duplicate category."; + if (holder.loaded){ + return "load_panel_session() loaded a panel after the holder declined."; + } + return true; } }; + } // namespace void add_tests_PanelCatalog(UnitTestDatabase& database){ - for (const CatalogLookupCase& test : CATALOG_LOOKUP_CASES){ - database.add(test); - } - for (const LoadSessionCase& test : LOAD_SESSION_CASES){ - database.add(test); - } - database.add(); + database.add(); + database.add(); } + } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PanelLists.cpp b/SerialPrograms/Source/PanelLists.cpp index e86a357628..054dd61006 100644 --- a/SerialPrograms/Source/PanelLists.cpp +++ b/SerialPrograms/Source/PanelLists.cpp @@ -11,6 +11,7 @@ #include "CommonFramework/ImageTypes/ImageRGB32_Qt.h" #include "CommonFramework/Windows/DpiScaler.h" #include "CommonFramework/Panels/UI/PanelListWidget.h" +#include "PanelCatalog.h" #include "PanelLists.h" //#include @@ -45,15 +46,15 @@ ProgramSelect::ProgramSelect(QWidget& parent, PanelHolder& holder) layout->addWidget(m_dropdown); - const auto& lists = m_catalog.lists(); - for (size_t c = 0; c < lists.size(); c++){ - add_dropdown_entry(*lists[c], static_cast(c)); + for (std::unique_ptr& list : make_all_panel_lists()){ + add(std::move(list)); } + // Load the 1st list by default. m_dropdown->setCurrentIndex(0); m_active_index = 0; - m_active_list = lists[0]->make_QWidget(*this, m_holder); + m_active_list = m_lists[0]->make_QWidget(*this, m_holder); layout->addWidget(m_active_list); connect( @@ -73,18 +74,26 @@ void ProgramSelect::unlock(){ m_active_list->setEnabled(true); } -void ProgramSelect::add_dropdown_entry(const PanelListDescriptor& list, int index){ - const ImageViewRGB32& icon = list.icon(); +void ProgramSelect::add(std::unique_ptr list){ + int index = m_dropdown->count(); + const ImageViewRGB32& icon = list->icon(); if (icon){ m_dropdown->addItem( QIcon(QPixmap::fromImage(to_QImage_ref(icon))), - QString::fromStdString(list.name()) + QString::fromStdString(list->name()) ); }else{ - m_dropdown->addItem(QString::fromStdString(list.name())); + m_dropdown->addItem(QString::fromStdString(list->name())); } - if (!list.enabled()){ + m_lists.emplace_back(std::move(list)); + const PanelListDescriptor& back = *m_lists.back(); + if (!m_tab_map.emplace(back.name(), index).second){ + const std::string name = back.name(); + m_lists.pop_back(); + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Duplicate Category Name: " + name); + } + if (!back.enabled()){ auto* model = qobject_cast(m_dropdown->model()); if (model != nullptr){ QStandardItem* line_handle = model->item(index); @@ -102,13 +111,12 @@ void ProgramSelect::load_persistent_panel(){ if (str == nullptr){ return; } - const std::optional category = m_catalog.find_category(*str); - if (!category){ + auto iter = m_tab_map.find(*str); + if (iter == m_tab_map.end()){ return; } - const int index = static_cast(*category); - m_dropdown->setCurrentIndex(index); - change_list(index); + m_dropdown->setCurrentIndex(iter->second); + change_list(iter->second); str = PERSISTENT_SETTINGS().panels.get_string(PanelListWidget::JSON_PROGRAM_PANEL); if (str != nullptr){ m_active_list->set_panel(*str); @@ -116,15 +124,15 @@ void ProgramSelect::load_persistent_panel(){ } std::string ProgramSelect::select_program(const std::string& identifier){ - std::optional match = m_catalog.find_panel(identifier); + const std::optional match = find_panel(m_lists, identifier); if (!match){ return "No program with identifier \"" + identifier + "\"."; } if (m_locked){ return "A program is running."; } - change_list(static_cast(match->category_index)); - m_active_list->set_panel(match->display_name); + change_list((int)match->category_index); + m_active_list->set_panel(match->entry.display_name); return ""; } @@ -132,12 +140,11 @@ void ProgramSelect::change_list(int index){ if (m_active_index == index && m_active_list != nullptr){ return; } - const auto& lists = m_catalog.lists(); m_dropdown->setCurrentIndex(index); m_active_index = index; - PERSISTENT_SETTINGS().panels["ProgramCategory"] = lists[index]->name(); + PERSISTENT_SETTINGS().panels["ProgramCategory"] = m_lists[index]->name(); delete m_active_list; - m_active_list = lists[index]->make_QWidget(*this, m_holder); + m_active_list = m_lists[index]->make_QWidget(*this, m_holder); m_active_list->setEnabled(!m_locked); layout()->addWidget(m_active_list); } diff --git a/SerialPrograms/Source/PanelLists.h b/SerialPrograms/Source/PanelLists.h index 83bf454a1e..b11edcc25c 100644 --- a/SerialPrograms/Source/PanelLists.h +++ b/SerialPrograms/Source/PanelLists.h @@ -8,7 +8,7 @@ #define PokemonAutomation_ProgramTabs_H #include -#include "PanelCatalog.h" +#include "CommonFramework/Panels/PanelList.h" class QComboBox; @@ -37,13 +37,14 @@ class ProgramSelect : public QWidget{ virtual QSize sizeHint() const override; private: - void add_dropdown_entry(const PanelListDescriptor& list, int index); + void add(std::unique_ptr list); void change_list(int index); private: PanelHolder& m_holder; - PanelCatalog m_catalog; + std::vector> m_lists; + std::map m_tab_map; QComboBox* m_dropdown; From ce1636d649644bd6a9d5ee2c5cfc97be7fe37518 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 18 Nov 2026 19:55:13 -0800 Subject: [PATCH 3/3] docs: Headless-CC names the PanelCatalog free functions --- docs/design/Headless-CC.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/design/Headless-CC.md b/docs/design/Headless-CC.md index b89d132086..2d85ebb1fe 100644 --- a/docs/design/Headless-CC.md +++ b/docs/design/Headless-CC.md @@ -40,15 +40,15 @@ Extend `StartupMode` (`HEADLESS`) and `StartupPlan` on `lab/main`: Currently `ProgramSelect` (`SerialPrograms/Source/PanelLists.h`, `PanelLists.cpp`) owns `std::vector> m_lists` and `select_program(identifier)`, while `PanelListWidget::handle_panel_clicked()` (`CommonFramework/Panels/UI/PanelListWidget.cpp:90`) stores `std::shared_ptr`, calls `make_panel()`, loads JSON, calls `load_panel()`, and writes `PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL]`. -Extract a widget-free `PanelCatalog` (in PR 1, no behavior change): -- **Ownership (no caching in PR 1):** `PanelListDescriptor::panels()` creates a fresh `std::vector` per call (`PanelList.h`). `PanelCatalog::find_panel(identifier)` scans `panels()` per lookup (matching `ProgramSelect::select_program()` today) and converts the matching `PanelEntry`'s `std::unique_ptr` into an owning `std::shared_ptr`. `HeadlessHost` keeps that `shared_ptr` alongside `m_current_panel` so it outlives the `PanelSession`. +Extract widget-free free functions `make_all_panel_lists()` and `find_panel(lists, identifier)` into `Source/PanelCatalog.{h,cpp}` (in PR 1, no behavior change): +- **Ownership (no caching in PR 1):** `PanelListDescriptor::panels()` creates a fresh `std::vector` per call (`PanelList.h`). `find_panel(lists, identifier)` scans `panels()` per lookup (matching `ProgramSelect::select_program()` today) and returns the matching `PanelEntry` with its category index; the caller converts its `std::unique_ptr` into an owning `std::shared_ptr`. `HeadlessHost` keeps that `shared_ptr` alongside `m_current_panel` so it outlives the `PanelSession`. - **Shared load helper:** Extract the `report_new_panel_intent()` -> `make_panel()` -> `load_json()` -> `load_panel()` sequence out of `PanelListWidget::handle_panel_clicked()` into a shared helper with a `bool persist_selection` flag (`true` in GUI, `false` in headless) that reports errors via return/exception instead of opening `QMessageBox` directly. ### 3.3 `HeadlessHost` (Widgetless `PanelHolder`) Add `HeadlessHost : public PanelHolder`: - **Owns:** - - `PanelCatalog&` + - the panel lists from `make_all_panel_lists()` - `std::shared_ptr m_current_panel_descriptor` - `std::unique_ptr m_current_panel` - (`ProgramSession::run_program()` already holds a `SleepSuppressScope` at `ProgramSession.cpp:323`, so `HeadlessHost` needs no `m_sleep_scope` and does not rely on `on_busy()`/`on_idle()`.) @@ -69,7 +69,7 @@ Add `HeadlessHost : public PanelHolder`: ### 3.5 One-Shot Execution, Readiness, Exit Codes, and Concurrency -1. **Select & verify runnable:** `HeadlessHost` looks up `` via `PanelCatalog::find_panel()`, instantiates `PanelSession`, applies `PERSISTENT_SETTINGS().panels[id]`, validates/applies `--options` (rejecting `ConsoleSetup` or parse errors with exit `4`), and casts with `dynamic_cast` (`LabChannel.cpp:58`). If null (e.g. a settings panel), exit `4`. +1. **Select & verify runnable:** `HeadlessHost` looks up `` via `find_panel()`, instantiates `PanelSession`, applies `PERSISTENT_SETTINGS().panels[id]`, validates/applies `--options` (rejecting `ConsoleSetup` or parse errors with exit `4`), and casts with `dynamic_cast` (`LabChannel.cpp:58`). If null (e.g. a settings panel), exit `4`. 2. **Per-device leases & readiness:** - Per-device leases (`acquire_lab_serial`, `acquire_lab_capture` from `lab-devices.json`) throw `UserSetupError` on the connect path; in headless mode, log the error and fail fast with exit `5` (no wait, no dialog). - Otherwise, if the panel has a console (`UnitTestRunner` has no console and skips this wait), wait up to a bounded timeout (e.g. 10 s) for the controller status (`"ready"` / `"none"` / `""`, factored from `LabChannel.h:88`, `LabRequest.h:70` without requiring `LabChannel`) to become `"ready"`, exiting `5` on timeout. @@ -94,7 +94,7 @@ Add `HeadlessHost : public PanelHolder`: ## 4. Rollout Plan 1. **PR 1 — `PanelCatalog` extraction (no behavior change):** - - Extract `PanelCatalog` from `ProgramSelect` (`PanelLists.{h,cpp}`) and share the panel-load helper with `PanelListWidget::handle_panel_clicked()`. + - Move the panel-list factories and the lookup out of `ProgramSelect` (`PanelLists.{h,cpp}`) into `make_all_panel_lists()` / `find_panel()` and share the panel-load helper with `PanelListWidget::handle_panel_clicked()`. - Acceptance: Existing GUI behavior and `StartupArguments_Tests` / `LabStartupPolicy_Tests` unchanged. 2. **PR 2 — `--headless --run` + `HeadlessHost` (macOS-only):** - Add `StartupMode::HEADLESS`, `--headless`, `--run`, `--options` to `StartupArguments` + `LabStartupPolicy` (macOS-only, requires `--profile`) with unit tests.