From e4208dadc724bdf675fb3801223999b28c9fc153 Mon Sep 17 00:00:00 2001 From: mwganson Date: Sat, 1 Aug 2020 15:43:50 -0500 Subject: [PATCH] [Gui Commands] new command: listCommandsByShortcut(string) -- returns a python list of all commands that are using the shortcut. Search is case-insensitive and ignores spaces --- src/Gui/CommandPy.xml | 9 ++++++++ src/Gui/CommandPyImp.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/Gui/CommandPy.xml b/src/Gui/CommandPy.xml index 44758591ca..93619d5ac1 100644 --- a/src/Gui/CommandPy.xml +++ b/src/Gui/CommandPy.xml @@ -34,6 +34,15 @@ Update active status of all commands. listAll() -> list of strings Returns the name of all commands. + + + + + + listByShortcut(string, bool bUseRegExp=False) -> list of strings + +Returns a list of all commands, filtered by shortcut. +Shortcuts are converted to uppercase and spaces removed prior to comparison. diff --git a/src/Gui/CommandPyImp.cpp b/src/Gui/CommandPyImp.cpp index 2643aa1f73..9b3ce09efd 100644 --- a/src/Gui/CommandPyImp.cpp +++ b/src/Gui/CommandPyImp.cpp @@ -21,6 +21,9 @@ ***************************************************************************/ #include "PreCompiled.h" +#ifndef _PreComp_ +# include +#endif #include "Command.h" #include "Action.h" @@ -83,6 +86,52 @@ PyObject* CommandPy::listAll(PyObject *args) return pyList; } +PyObject* CommandPy::listByShortcut(PyObject *args) +{ + char* shortcut_to_find; + bool bIsRegularExp = false; + if (!PyArg_ParseTuple(args, "s|b", &shortcut_to_find, &bIsRegularExp)) + return nullptr; + + std::vector cmds = Application::Instance->commandManager().getAllCommands(); + std::vector matches; + for (Command* c : cmds){ + Action* action = c->getAction(); + if (action){ + QString spc = QString::fromLatin1(" "); + if(bIsRegularExp){ + QRegExp re = QRegExp(QString::fromLatin1(shortcut_to_find)); + re.setCaseSensitivity(Qt::CaseInsensitive); + if (!re.isValid()){ + std::stringstream str; + str << "Invalid regular expression: " << shortcut_to_find; + throw Py::RuntimeError(str.str()); + } + + if (re.indexIn(action->shortcut().toString().remove(spc).toUpper()) != -1){ + matches.push_back(c->getName()); + } + } + else if (action->shortcut().toString().remove(spc).toUpper() == + QString::fromLatin1(shortcut_to_find).remove(spc).toUpper()) { + matches.push_back(c->getName()); + } + } + } + + PyObject* pyList = PyList_New(matches.size()); + int i=0; + for (std::string match : matches) { +#if PY_MAJOR_VERSION >= 3 + PyObject* str = PyUnicode_FromString(match.c_str()); +#else + PyObject* str = PyString_FromString(match.c_str()); +#endif + PyList_SetItem(pyList, i++, str); + } + return pyList; +} + PyObject* CommandPy::run(PyObject *args) { int item = 0;