diff --git a/src/lay/lay/layMainConfigPages.cc b/src/lay/lay/layMainConfigPages.cc index 281812294..67a85bacd 100644 --- a/src/lay/lay/layMainConfigPages.cc +++ b/src/lay/lay/layMainConfigPages.cc @@ -326,76 +326,6 @@ MainConfigPage5::commit (lay::PluginRoot *root) // ------------------------------------------------------------ // The "key bindings" config page -std::vector > -unpack_key_binding (const std::string &packed) -{ - tl::Extractor ex (packed.c_str ()); - - std::vector > key_bindings; - - while (! ex.at_end ()) { - ex.test(";"); - key_bindings.push_back (std::make_pair (std::string (), std::string ())); - ex.read_word_or_quoted (key_bindings.back ().first); - ex.test(":"); - ex.read_word_or_quoted (key_bindings.back ().second); - } - - return key_bindings; -} - -std::string -pack_key_binding (const std::vector > &unpacked) -{ - std::string packed; - - for (std::vector >::const_iterator p = unpacked.begin (); p != unpacked.end (); ++p) { - if (! packed.empty ()) { - packed += ";"; - } - packed += tl::to_word_or_quoted_string (p->first); - packed += ":"; - packed += tl::to_word_or_quoted_string (p->second); - } - - return packed; -} - -std::vector > -unpack_menu_items_hidden (const std::string &packed) -{ - tl::Extractor ex (packed.c_str ()); - - std::vector > hidden; - - while (! ex.at_end ()) { - ex.test(";"); - hidden.push_back (std::make_pair (std::string (), false)); - ex.read_word_or_quoted (hidden.back ().first); - ex.test(":"); - ex.read (hidden.back ().second); - } - - return hidden; -} - -std::string -pack_menu_items_hidden (const std::vector > &unpacked) -{ - std::string packed; - - for (std::vector >::const_iterator p = unpacked.begin (); p != unpacked.end (); ++p) { - if (! packed.empty ()) { - packed += ";"; - } - packed += tl::to_word_or_quoted_string (p->first); - packed += ":"; - packed += tl::to_string (p->second); - } - - return packed; -} - CustomizeMenuConfigPage::CustomizeMenuConfigPage (QWidget *parent) : lay::ConfigPage (parent), m_enable_event (true) { diff --git a/src/lay/lay/layMainConfigPages.h b/src/lay/lay/layMainConfigPages.h index 1cc660e6d..4c93ad8ce 100644 --- a/src/lay/lay/layMainConfigPages.h +++ b/src/lay/lay/layMainConfigPages.h @@ -48,27 +48,6 @@ class QAction; namespace lay { -/** - * @brief A utility function to convert the packed key binding in the cfg_key_bindings string to a vector - */ -std::vector > unpack_key_binding (const std::string &packed); - -/** - * @brief A utility function to convert the key binding (as path/shortcut pair vector) to a packed string for cfg_key_bindings - */ -std::string pack_key_binding (const std::vector > &unpacked); - -/** - * @brief A utility function to convert the packed hidden flags in the cfg_menu_items_hidden string to a vector - */ -std::vector > unpack_menu_items_hidden (const std::string &packed); - -/** - * @brief A utility function to convert the hidde flags (as path/bool pair vector) to a packed string for cfg_menu_items_hidden - */ -std::string pack_menu_items_hidden (const std::vector > &unpacked); - - class ColorButton; class MainConfigPage diff --git a/src/laybasic/laybasic/gsiDeclLayMenu.cc b/src/laybasic/laybasic/gsiDeclLayMenu.cc index 3af817039..dfd043d82 100644 --- a/src/laybasic/laybasic/gsiDeclLayMenu.cc +++ b/src/laybasic/laybasic/gsiDeclLayMenu.cc @@ -28,7 +28,65 @@ namespace gsi { +static std::string pack_key_binding (const std::map &kb) +{ + std::vector > v; + v.insert (v.end (), kb.begin (), kb.end ()); + return lay::pack_key_binding (v); +} + +static std::map unpack_key_binding (const std::string &s) +{ + std::vector > v = lay::unpack_key_binding (s); + std::map kb; + kb.insert (v.begin (), v.end ()); + return kb; +} + +static std::string pack_menu_items_hidden (const std::map &kb) +{ + std::vector > v; + v.insert (v.end (), kb.begin (), kb.end ()); + return lay::pack_menu_items_hidden (v); +} + +static std::map unpack_menu_items_hidden (const std::string &s) +{ + std::vector > v = lay::unpack_menu_items_hidden (s); + std::map kb; + kb.insert (v.begin (), v.end ()); + return kb; +} + Class decl_AbstractMenu ("lay", "AbstractMenu", + method ("pack_key_binding", &pack_key_binding, gsi::arg ("path_to_keys"), + "@brief Serializes a key binding definition into a single string\n" + "The serialized format is used by the 'key-bindings' config key. " + "This method will take an array of path/key definitions (including the \\Action#NoKeyBound option) " + "and convert it to a single string suitable for assigning to the config key.\n" + "\n" + "This method has been introduced in version 0.26." + ) + + method ("unpack_key_binding", &unpack_key_binding, gsi::arg ("s"), + "@brief Deserializes a key binding definition\n" + "This method is the reverse of \\pack_key_binding.\n" + "\n" + "This method has been introduced in version 0.26." + ) + + method ("pack_menu_items_hidden", &pack_menu_items_hidden, gsi::arg ("path_to_visibility"), + "@brief Serializes a menu item visibility definition into a single string\n" + "The serialized format is used by the 'menu-items-hidden' config key. " + "This method will take an array of path/visibility flag definitions " + "and convert it to a single string suitable for assigning to the config key.\n" + "\n" + "This method has been introduced in version 0.26." + ) + + method ("unpack_menu_items_hidden", &unpack_menu_items_hidden, gsi::arg ("s"), + "@brief Deserializes a menu item visibility definition\n" + "This method is the reverse of \\pack_menu_items_hidden.\n" + "\n" + "This method has been introduced in version 0.26." + ) + method ("action", &lay::AbstractMenu::action, "@brief Get the reference to a Action object associated with the given path\n" "@args path\n" @@ -158,13 +216,13 @@ Class decl_ActionBase ("lay", "ActionBase", "@brief Sets the keyboard shortcut\n" "@args shortcut\n" "If the shortcut string is empty, the default shortcut will be used. If the string " - "is equal to \\Action#NoShortCut, no keyboard shortcut will be assigned.\n" + "is equal to \\Action#NoKeyBound, no keyboard shortcut will be assigned.\n" "\n" "@param shortcut The keyboard shortcut in Qt notation (i.e. \"Ctrl+C\")\n" "\n" - "The NoShortCut option has been added in version 0.26." + "The NoKeyBound option has been added in version 0.26." ) + - constant ("NoShortCut", &lay::Action::no_shortcut, + constant ("NoKeyBound", &lay::Action::no_shortcut, "@brief Gets a shortcut value indicating that no shortcut shall be assigned\n" "This method has been introduced in version 0.26." ) + diff --git a/src/laybasic/laybasic/layAbstractMenu.cc b/src/laybasic/laybasic/layAbstractMenu.cc index 6acbd9bbd..bd6acbb20 100644 --- a/src/laybasic/laybasic/layAbstractMenu.cc +++ b/src/laybasic/laybasic/layAbstractMenu.cc @@ -65,6 +65,79 @@ static const bool s_can_move_menu = true; #endif +// --------------------------------------------------------------- +// Serialization of key bindings and hidden menu state + +std::vector > +unpack_key_binding (const std::string &packed) +{ + tl::Extractor ex (packed.c_str ()); + + std::vector > key_bindings; + + while (! ex.at_end ()) { + ex.test(";"); + key_bindings.push_back (std::make_pair (std::string (), std::string ())); + ex.read_word_or_quoted (key_bindings.back ().first); + ex.test(":"); + ex.read_word_or_quoted (key_bindings.back ().second); + } + + return key_bindings; +} + +std::string +pack_key_binding (const std::vector > &unpacked) +{ + std::string packed; + + for (std::vector >::const_iterator p = unpacked.begin (); p != unpacked.end (); ++p) { + if (! packed.empty ()) { + packed += ";"; + } + packed += tl::to_word_or_quoted_string (p->first); + packed += ":"; + packed += tl::to_word_or_quoted_string (p->second); + } + + return packed; +} + +std::vector > +unpack_menu_items_hidden (const std::string &packed) +{ + tl::Extractor ex (packed.c_str ()); + + std::vector > hidden; + + while (! ex.at_end ()) { + ex.test(";"); + hidden.push_back (std::make_pair (std::string (), false)); + ex.read_word_or_quoted (hidden.back ().first); + ex.test(":"); + ex.read (hidden.back ().second); + } + + return hidden; +} + +std::string +pack_menu_items_hidden (const std::vector > &unpacked) +{ + std::string packed; + + for (std::vector >::const_iterator p = unpacked.begin (); p != unpacked.end (); ++p) { + if (! packed.empty ()) { + packed += ";"; + } + packed += tl::to_word_or_quoted_string (p->first); + packed += ":"; + packed += tl::to_string (p->second); + } + + return packed; +} + // --------------------------------------------------------------- // Helper function to parse a title with potential shortcut and // icon specification diff --git a/src/laybasic/laybasic/layAbstractMenu.h b/src/laybasic/laybasic/layAbstractMenu.h index 4fa94916e..5aad5f41a 100644 --- a/src/laybasic/laybasic/layAbstractMenu.h +++ b/src/laybasic/laybasic/layAbstractMenu.h @@ -55,6 +55,26 @@ class Action; class AbstractMenu; class PluginRoot; +/** + * @brief A utility function to convert the packed key binding in the cfg_key_bindings string to a vector + */ +LAYBASIC_PUBLIC std::vector > unpack_key_binding (const std::string &packed); + +/** + * @brief A utility function to convert the key binding (as path/shortcut pair vector) to a packed string for cfg_key_bindings + */ +LAYBASIC_PUBLIC std::string pack_key_binding (const std::vector > &unpacked); + +/** + * @brief A utility function to convert the packed hidden flags in the cfg_menu_items_hidden string to a vector + */ +LAYBASIC_PUBLIC std::vector > unpack_menu_items_hidden (const std::string &packed); + +/** + * @brief A utility function to convert the hidde flags (as path/bool pair vector) to a packed string for cfg_menu_items_hidden + */ +LAYBASIC_PUBLIC std::string pack_menu_items_hidden (const std::vector > &unpacked); + /** * @brief A helper class that does reference counting for the QAction object */ diff --git a/testdata/ruby/layMenuTest.rb b/testdata/ruby/layMenuTest.rb index a59356a0f..22a0be329 100644 --- a/testdata/ruby/layMenuTest.rb +++ b/testdata/ruby/layMenuTest.rb @@ -59,6 +59,12 @@ class LAYMenuTest_TestClass < TestBase assert_equal(b.shortcut, "") assert_equal(b.effective_shortcut, "X") + a.shortcut = RBA::Action::NoKeyBound + assert_equal(a.default_shortcut, "X") + assert_equal(a.shortcut, "none") + assert_equal(a.effective_shortcut, "") + a.shortcut = "" + assert_equal(a.is_visible?, true) a.hidden = false @@ -319,6 +325,26 @@ RESULT end + def test_3 + + map = RBA::AbstractMenu::unpack_key_binding("'path.a':X;'path.b':''") + assert_equal(map["path.a"], "X") + assert_equal(map["path.b"], "") + assert_equal(map["path.c"], nil) + + map2 = RBA::AbstractMenu::unpack_key_binding(RBA::AbstractMenu::pack_key_binding(map)) + assert_equal(map == map2, true) + + map = RBA::AbstractMenu::unpack_menu_items_hidden("'path.a':true;'path.b':false") + assert_equal(map["path.a"], true) + assert_equal(map["path.b"], false) + assert_equal(map["path.c"], nil) + + map2 = RBA::AbstractMenu::unpack_menu_items_hidden(RBA::AbstractMenu::pack_menu_items_hidden(map)) + assert_equal(map == map2, true) + + end + end load("test_epilogue.rb")