diff --git a/src/app/ui/key.h b/src/app/ui/key.h index 7ff1f41ec..2daec3491 100644 --- a/src/app/ui/key.h +++ b/src/app/ui/key.h @@ -164,7 +164,6 @@ namespace app { typedef std::shared_ptr KeyPtr; typedef std::vector Keys; - std::string convertKeyContextToString(KeyContext keyContext); std::string convertKeyContextToUserFriendlyString(KeyContext keyContext); } // namespace app diff --git a/src/app/ui/keyboard_shortcuts.cpp b/src/app/ui/keyboard_shortcuts.cpp index a889a94bb..5cb612d5f 100644 --- a/src/app/ui/keyboard_shortcuts.cpp +++ b/src/app/ui/keyboard_shortcuts.cpp @@ -62,6 +62,22 @@ namespace { { NULL , NULL , app::KeyAction::None } }; + static struct { + const char* name; + app::KeyContext context; + } contexts[] = { + { "" , app::KeyContext::Any }, + { "Normal" , app::KeyContext::Normal }, + { "Selection" , app::KeyContext::SelectionTool }, + { "TranslatingSelection" , app::KeyContext::TranslatingSelection }, + { "ScalingSelection" , app::KeyContext::ScalingSelection }, + { "RotatingSelection" , app::KeyContext::RotatingSelection }, + { "MoveTool" , app::KeyContext::MoveTool }, + { "FreehandTool" , app::KeyContext::FreehandTool }, + { "ShapeTool" , app::KeyContext::ShapeTool }, + { NULL , app::KeyContext::Any } + }; + static struct { const char* name; const char* userfriendly; @@ -162,6 +178,22 @@ namespace base { return ""; } + template<> app::KeyContext convert_to(const std::string& from) { + for (int c=0; contexts[c].name; ++c) { + if (from == contexts[c].name) + return contexts[c].context; + } + return app::KeyContext::Any; + } + + template<> std::string convert_to(const app::KeyContext& from) { + for (int c=0; contexts[c].name; ++c) { + if (from == contexts[c].context) + return contexts[c].name; + } + return std::string(); + } + } // namespace base namespace app { @@ -426,12 +458,8 @@ void KeyboardShortcuts::importFile(TiXmlElement* rootElement, KeySource source) // Read context KeyContext keycontext = KeyContext::Any; const char* keycontextstr = xmlKey->Attribute("context"); - if (keycontextstr) { - if (strcmp(keycontextstr, "Selection") == 0) - keycontext = KeyContext::SelectionTool; - else if (strcmp(keycontextstr, "Normal") == 0) - keycontext = KeyContext::Normal; - } + if (keycontextstr) + keycontext = base::convert_to(std::string(keycontextstr)); // Read params Params params; @@ -650,9 +678,10 @@ void KeyboardShortcuts::exportAccel(TiXmlElement& parent, const Key* key, const case KeyType::Command: { elem.SetAttribute("command", key->command()->id().c_str()); - if (key->keycontext() != KeyContext::Any) - elem.SetAttribute( - "context", convertKeyContextToString(key->keycontext()).c_str()); + if (key->keycontext() != KeyContext::Any) { + elem.SetAttribute("context", + base::convert_to(key->keycontext()).c_str()); + } for (const auto& param : key->params()) { if (param.second.empty()) @@ -673,12 +702,12 @@ void KeyboardShortcuts::exportAccel(TiXmlElement& parent, const Key* key, const case KeyType::Action: elem.SetAttribute("action", - base::convert_to(key->action()).c_str()); + base::convert_to(key->action()).c_str()); break; case KeyType::WheelAction: elem.SetAttribute("action", - base::convert_to(key->wheelAction()).c_str()); + base::convert_to(key->wheelAction()).c_str()); break; } @@ -1009,31 +1038,6 @@ std::string key_tooltip(const char* str, const app::Key* key) return res; } -std::string convertKeyContextToString(KeyContext keyContext) -{ - switch (keyContext) { - case KeyContext::Any: - return std::string(); - case KeyContext::Normal: - return "Normal"; - case KeyContext::SelectionTool: - return "Selection"; - case KeyContext::TranslatingSelection: - return "TranslatingSelection"; - case KeyContext::ScalingSelection: - return "ScalingSelection"; - case KeyContext::RotatingSelection: - return "RotatingSelection"; - case KeyContext::MoveTool: - return "MoveTool"; - case KeyContext::FreehandTool: - return "FreehandTool"; - case KeyContext::ShapeTool: - return "ShapeTool"; - } - return std::string(); -} - std::string convertKeyContextToUserFriendlyString(KeyContext keyContext) { switch (keyContext) {