Skip to content

Commit

Permalink
[Gui Commands] new command: listCommandsByShortcut(string) -- returns…
Browse files Browse the repository at this point in the history
… a python list of all commands that are using the shortcut. Search is case-insensitive and ignores spaces
  • Loading branch information
mwganson authored and wwmayer committed Aug 2, 2020
1 parent 9b529bc commit 0cf8279
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Gui/CommandPy.xml
Expand Up @@ -34,6 +34,15 @@ Update active status of all commands.
<UserDocu>listAll() -> list of strings

Returns the name of all commands.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="listByShortcut" Static='true'>
<Documentation>
<UserDocu>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.
</UserDocu>
</Documentation>
</Methode>
Expand Down
49 changes: 49 additions & 0 deletions src/Gui/CommandPyImp.cpp
Expand Up @@ -21,6 +21,9 @@
***************************************************************************/

#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
#endif

#include "Command.h"
#include "Action.h"
Expand Down Expand Up @@ -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 <Command*> cmds = Application::Instance->commandManager().getAllCommands();
std::vector <std::string> 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;
Expand Down

0 comments on commit 0cf8279

Please sign in to comment.