diff --git a/src/Gui/FreeCADGuiInit.py b/src/Gui/FreeCADGuiInit.py index 42aeed1b08..8c12f88085 100644 --- a/src/Gui/FreeCADGuiInit.py +++ b/src/Gui/FreeCADGuiInit.py @@ -50,10 +50,16 @@ class Workbench: self.__Workbench__.appendToolbar(name, cmds) def removeToolbar(self,name): self.__Workbench__.removeToolbar(name) + def listToolbars(self): + return self.__Workbench__.listToolbars() + def getToolbarItems(self): + return self.__Workbench__.getToolbarItems() def appendCommandbar(self,name,cmds): self.__Workbench__.appendCommandbar(name, cmds) def removeCommandbar(self,name): self.__Workbench__.removeCommandbar(name) + def listCommandbars(self): + return self.__Workbench__.listCommandbars() def appendMenu(self,name,cmds): self.__Workbench__.appendMenu(name, cmds) def removeMenu(self,name): diff --git a/src/Gui/PythonWorkbenchPy.xml b/src/Gui/PythonWorkbenchPy.xml index 393e4226b8..9232ff9e86 100644 --- a/src/Gui/PythonWorkbenchPy.xml +++ b/src/Gui/PythonWorkbenchPy.xml @@ -23,11 +23,6 @@ Remove a menu - - - Show a list of all menus - - Append a new context menu item @@ -48,11 +43,6 @@ Remove a toolbar - - - Show a list of all toolbars - - Append a new command bar @@ -63,66 +53,61 @@ Remove a command bar - - - Show a list of all command bars - + + + deprecated -- use appendMenu + - - - deprecated -- use appendMenu - - - - - deprecated -- use removeMenu - - - - - deprecated -- use listMenus - - - - - deprecated -- use appendContextMenu - - - - - deprecated -- use removeContextMenu - - - - - deprecated -- use appendToolbar - - - - - deprecated -- use removeToolbar - - - - - deprecated -- use listToolbars - - - - - deprecated -- use appendCommandBar - - - - - deprecated -- use removeCommandBar - - - - - deprecated -- use listCommandBars - - - + + + deprecated -- use removeMenu + + + + + deprecated -- use listMenus + + + + + deprecated -- use appendContextMenu + + + + + deprecated -- use removeContextMenu + + + + + deprecated -- use appendToolbar + + + + + deprecated -- use removeToolbar + + + + + deprecated -- use listToolbars + + + + + deprecated -- use appendCommandBar + + + + + deprecated -- use removeCommandBar + + + + + deprecated -- use listCommandBars + + + - + diff --git a/src/Gui/PythonWorkbenchPyImp.cpp b/src/Gui/PythonWorkbenchPyImp.cpp index ee7222c5af..54c5a7e7a4 100644 --- a/src/Gui/PythonWorkbenchPyImp.cpp +++ b/src/Gui/PythonWorkbenchPyImp.cpp @@ -54,7 +54,7 @@ PyObject* PythonWorkbenchPy::appendMenu(PyObject *args) PyObject* pPath; PyObject* pItems; if ( !PyArg_ParseTuple(args, "OO", &pPath, &pItems) ) - return NULL; // NULL triggers exception + return nullptr; // menu path std::list path; @@ -95,7 +95,7 @@ PyObject* PythonWorkbenchPy::appendMenu(PyObject *args) #endif } else { PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument"); - return NULL; // NULL triggers exception + return nullptr; } // menu items @@ -137,7 +137,7 @@ PyObject* PythonWorkbenchPy::appendMenu(PyObject *args) #endif } else { PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument"); - return NULL; // NULL triggers exception + return nullptr; } getPythonBaseWorkbenchPtr()->appendMenu( path, items ); @@ -151,37 +151,14 @@ PyObject* PythonWorkbenchPy::removeMenu(PyObject *args) { PY_TRY { char *psMenu; - if (!PyArg_ParseTuple(args, "s", &psMenu)) // convert args: Python->C - return NULL; // NULL triggers exception + if (!PyArg_ParseTuple(args, "s", &psMenu)) + return nullptr; getPythonBaseWorkbenchPtr()->removeMenu( psMenu ); Py_Return; } PY_CATCH; } -/** Shows a list of all menus */ -PyObject* PythonWorkbenchPy::listMenus(PyObject *args) -{ - PY_TRY { - if (!PyArg_ParseTuple(args, "")) - return NULL; - - std::list menus = getPythonBaseWorkbenchPtr()->listMenus(); - - PyObject* pyList = PyList_New(menus.size()); - int i=0; - for (std::list::iterator it = menus.begin(); it != menus.end(); ++it, ++i ) { -#if PY_MAJOR_VERSION >= 3 - PyObject* str = PyUnicode_FromString(it->c_str()); -#else - PyObject* str = PyString_FromString(it->c_str()); -#endif - PyList_SetItem(pyList, i, str); - } - return pyList; - } PY_CATCH; -} - /** Appends new context menu items */ PyObject* PythonWorkbenchPy::appendContextMenu(PyObject *args) { @@ -189,7 +166,7 @@ PyObject* PythonWorkbenchPy::appendContextMenu(PyObject *args) PyObject* pPath; PyObject* pItems; if ( !PyArg_ParseTuple(args, "OO", &pPath, &pItems) ) - return NULL; // NULL triggers exception + return nullptr; // menu path std::list path; @@ -230,7 +207,7 @@ PyObject* PythonWorkbenchPy::appendContextMenu(PyObject *args) #endif } else { PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument"); - return NULL; // NULL triggers exception + return nullptr; } // menu items @@ -272,7 +249,7 @@ PyObject* PythonWorkbenchPy::appendContextMenu(PyObject *args) #endif } else { PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument"); - return NULL; // NULL triggers exception + return nullptr; } getPythonBaseWorkbenchPtr()->appendContextMenu( path, items ); @@ -286,8 +263,8 @@ PyObject* PythonWorkbenchPy::removeContextMenu(PyObject *args) { PY_TRY { char *psMenu; - if (!PyArg_ParseTuple(args, "s", &psMenu)) // convert args: Python->C - return NULL; // NULL triggers exception + if (!PyArg_ParseTuple(args, "s", &psMenu)) + return nullptr; getPythonBaseWorkbenchPtr()->removeContextMenu( psMenu ); Py_Return; @@ -301,10 +278,10 @@ PyObject* PythonWorkbenchPy::appendToolbar(PyObject *args) PyObject* pObject; char* psToolBar; if ( !PyArg_ParseTuple(args, "sO", &psToolBar, &pObject) ) - return NULL; // NULL triggers exception + return nullptr; if (!PyList_Check(pObject)) { PyErr_SetString(PyExc_AssertionError, "Expected a list as second argument"); - return NULL; // NULL triggers exception + return nullptr; } std::list items; @@ -339,37 +316,14 @@ PyObject* PythonWorkbenchPy::removeToolbar(PyObject *args) { PY_TRY { char *psToolBar; - if (!PyArg_ParseTuple(args, "s", &psToolBar)) // convert args: Python->C - return NULL; // NULL triggers exception + if (!PyArg_ParseTuple(args, "s", &psToolBar)) + return nullptr; getPythonBaseWorkbenchPtr()->removeToolbar( psToolBar ); Py_Return; } PY_CATCH; } -/** Shows a list of all toolbars */ -PyObject* PythonWorkbenchPy::listToolbars(PyObject *args) -{ - PY_TRY { - if (!PyArg_ParseTuple(args, "")) - return NULL; - - std::list bars = getPythonBaseWorkbenchPtr()->listToolbars(); - - PyObject* pyList = PyList_New(bars.size()); - int i=0; - for (std::list::iterator it = bars.begin(); it != bars.end(); ++it, ++i ) { -#if PY_MAJOR_VERSION >= 3 - PyObject* str = PyUnicode_FromString(it->c_str()); -#else - PyObject* str = PyString_FromString(it->c_str()); -#endif - PyList_SetItem(pyList, i, str); - } - return pyList; - } PY_CATCH; -} - /** Appends a new command bar */ PyObject* PythonWorkbenchPy::appendCommandbar(PyObject *args) { @@ -377,10 +331,10 @@ PyObject* PythonWorkbenchPy::appendCommandbar(PyObject *args) PyObject* pObject; char* psToolBar; if ( !PyArg_ParseTuple(args, "sO", &psToolBar, &pObject) ) - return NULL; // NULL triggers exception + return nullptr; if (!PyList_Check(pObject)) { PyErr_SetString(PyExc_AssertionError, "Expected a list as second argument"); - return NULL; // NULL triggers exception + return nullptr; } std::list items; @@ -416,40 +370,17 @@ PyObject* PythonWorkbenchPy::removeCommandbar(PyObject *args) { PY_TRY { char *psToolBar; - if (!PyArg_ParseTuple(args, "s", &psToolBar)) // convert args: Python->C - return NULL; // NULL triggers exception + if (!PyArg_ParseTuple(args, "s", &psToolBar)) + return nullptr; getPythonBaseWorkbenchPtr()->removeCommandbar( psToolBar ); Py_Return; } PY_CATCH; } -/** Shows a list of all command bars */ -PyObject* PythonWorkbenchPy::listCommandbars(PyObject *args) +PyObject* PythonWorkbenchPy::getCustomAttributes(const char* ) const { - PY_TRY { - if (!PyArg_ParseTuple(args, "")) - return NULL; - - std::list bars = getPythonBaseWorkbenchPtr()->listCommandbars(); - - PyObject* pyList = PyList_New(bars.size()); - int i=0; - for (std::list::iterator it = bars.begin(); it != bars.end(); ++it, ++i) { -#if PY_MAJOR_VERSION >= 3 - PyObject* str = PyUnicode_FromString(it->c_str()); -#else - PyObject* str = PyString_FromString(it->c_str()); -#endif - PyList_SetItem(pyList, i, str); - } - return pyList; - } PY_CATCH; -} - -PyObject *PythonWorkbenchPy::getCustomAttributes(const char* ) const -{ - return 0; + return nullptr; } int PythonWorkbenchPy::setCustomAttributes(const char* , PyObject *) @@ -457,6 +388,8 @@ int PythonWorkbenchPy::setCustomAttributes(const char* , PyObject *) return 0; } +// deprecated methods + PyObject* PythonWorkbenchPy::AppendMenu(PyObject *args) { return appendMenu(args); diff --git a/src/Gui/Workbench.cpp b/src/Gui/Workbench.cpp index 18348d2750..d14518fd35 100644 --- a/src/Gui/Workbench.cpp +++ b/src/Gui/Workbench.cpp @@ -429,6 +429,54 @@ void Workbench::removeTaskWatcher(void) taskView->clearTaskWatcher(); } +std::list Workbench::listToolbars() const +{ + std::unique_ptr tb(setupToolBars()); + std::list bars; + QList items = tb->getItems(); + for (QList::ConstIterator item = items.begin(); item != items.end(); ++item) + bars.push_back((*item)->command()); + return bars; +} + +std::list>> Workbench::getToolbarItems() const +{ + std::unique_ptr tb(setupToolBars()); + + std::list>> itemsList; + QList items = tb->getItems(); + for (QList::ConstIterator it = items.begin(); it != items.end(); ++it) { + QList sub = (*it)->getItems(); + std::list cmds; + for (QList::ConstIterator jt = sub.begin(); jt != sub.end(); ++jt) { + cmds.push_back((*jt)->command()); + } + + itemsList.emplace_back((*it)->command(), cmds); + } + return itemsList; +} + +std::list Workbench::listMenus() const +{ + std::unique_ptr mb(setupMenuBar()); + std::list menus; + QList items = mb->getItems(); + for ( QList::ConstIterator it = items.begin(); it != items.end(); ++it ) + menus.push_back((*it)->command()); + return menus; +} + +std::list Workbench::listCommandbars() const +{ + std::unique_ptr cb(setupCommandBars()); + std::list bars; + QList items = cb->getItems(); + for (QList::ConstIterator item = items.begin(); item != items.end(); ++item) + bars.push_back((*item)->command()); + return bars; +} + // -------------------------------------------------------------------- #if 0 // needed for Qt's lupdate utility @@ -1001,15 +1049,6 @@ void PythonBaseWorkbench::removeMenu(const std::string& menu) const } } -std::list PythonBaseWorkbench::listMenus() const -{ - std::list menus; - QList items = _menuBar->getItems(); - for ( QList::ConstIterator it = items.begin(); it != items.end(); ++it ) - menus.push_back((*it)->command()); - return menus; -} - void PythonBaseWorkbench::appendContextMenu(const std::list& menu, const std::list& items) const { MenuItem* item = _contextMenu; @@ -1062,15 +1101,6 @@ void PythonBaseWorkbench::removeToolbar(const std::string& bar) const } } -std::list PythonBaseWorkbench::listToolbars() const -{ - std::list bars; - QList items = _toolBar->getItems(); - for (QList::ConstIterator item = items.begin(); item != items.end(); ++item) - bars.push_back((*item)->command()); - return bars; -} - void PythonBaseWorkbench::appendCommandbar(const std::string& bar, const std::list& items) const { ToolBarItem* item = _commandBar->findItem( bar ); @@ -1093,15 +1123,6 @@ void PythonBaseWorkbench::removeCommandbar(const std::string& bar) const } } -std::list PythonBaseWorkbench::listCommandbars() const -{ - std::list bars; - QList items = _commandBar->getItems(); - for (QList::ConstIterator item = items.begin(); item != items.end(); ++item) - bars.push_back((*item)->command()); - return bars; -} - // ----------------------------------------------------------------------- TYPESYSTEM_SOURCE(Gui::PythonBlankWorkbench, Gui::PythonBaseWorkbench) diff --git a/src/Gui/Workbench.h b/src/Gui/Workbench.h index 2badac7901..a5ee834246 100644 --- a/src/Gui/Workbench.h +++ b/src/Gui/Workbench.h @@ -97,6 +97,15 @@ public: static void createLinkMenu(MenuItem *); + //// Shows a list of all toolbars + std::list listToolbars() const; + /// Shows a list of all toolbars and their commands + std::list>> getToolbarItems() const; + //// Shows a list of all menus + std::list listMenus() const; + //// Shows a list of all command bars + std::list listCommandbars() const; + protected: /** Returns a MenuItem tree structure of menus for this workbench. */ virtual MenuItem* setupMenuBar() const=0; @@ -245,8 +254,6 @@ public: void appendMenu(const std::list& menu, const std::list& items) const; /// Removes a menu void removeMenu(const std::string& menu ) const; - //// Shows a list of all menus - std::list listMenus() const; /// Appends new context menu items void appendContextMenu(const std::list& menu, const std::list& items) const; @@ -259,15 +266,11 @@ public: void appendToolbar(const std::string& bar, const std::list& items) const; /// Removes a toolbar void removeToolbar(const std::string& bar) const; - //// Shows a list of all toolbars - std::list listToolbars() const; /// Appends a new command bar void appendCommandbar(const std::string& bar, const std::list& items) const; /// Removes a command bar void removeCommandbar(const std::string& bar) const; - //// Shows a list of all command bars - std::list listCommandbars() const; //@} protected: diff --git a/src/Gui/WorkbenchPy.xml b/src/Gui/WorkbenchPy.xml index 70917ea74f..457c1d2661 100644 --- a/src/Gui/WorkbenchPy.xml +++ b/src/Gui/WorkbenchPy.xml @@ -23,6 +23,26 @@ Activate this workbench + + + Show a list of all toolbars + + + + + Show a dict of all toolbars and their commands + + + + + Show a list of all command bars + + + + + Show a list of all menus + + - + diff --git a/src/Gui/WorkbenchPyImp.cpp b/src/Gui/WorkbenchPyImp.cpp index 1fa913e8c3..bae0d5ea0e 100644 --- a/src/Gui/WorkbenchPyImp.cpp +++ b/src/Gui/WorkbenchPyImp.cpp @@ -51,25 +51,20 @@ std::string WorkbenchPy::representation(void) const /** Retrieves the workbench name */ PyObject* WorkbenchPy::name(PyObject *args) { - if (!PyArg_ParseTuple(args, "")) // convert args: Python->C - return NULL; // NULL triggers exception + if (!PyArg_ParseTuple(args, "")) + return nullptr; PY_TRY { - std::string name = getWorkbenchPtr()->name(); -#if PY_MAJOR_VERSION >= 3 - PyObject* pyName = PyUnicode_FromString(name.c_str()); -#else - PyObject* pyName = PyString_FromString(name.c_str()); -#endif - return pyName; + Py::String name(getWorkbenchPtr()->name()); + return Py::new_reference_to(name); }PY_CATCH; } /** Activates the workbench object */ PyObject* WorkbenchPy::activate(PyObject *args) { - if (!PyArg_ParseTuple(args, "")) // convert args: Python->C - return NULL; // NULL triggers exception + if (!PyArg_ParseTuple(args, "")) + return nullptr; PY_TRY { std::string name = getWorkbenchPtr()->name(); @@ -78,14 +73,84 @@ PyObject* WorkbenchPy::activate(PyObject *args) }PY_CATCH; } -PyObject *WorkbenchPy::getCustomAttributes(const char*) const +/** Shows a list of all menus */ +PyObject* WorkbenchPy::listMenus(PyObject *args) { - return 0; + PY_TRY { + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + std::list menus = getWorkbenchPtr()->listMenus(); + + Py::List list; + for (std::list::iterator it = menus.begin(); it != menus.end(); ++it) { + list.append(Py::String(*it)); + } + return Py::new_reference_to(list); + } PY_CATCH; +} + +/** Shows a list of all toolbars */ +PyObject* WorkbenchPy::listToolbars(PyObject *args) +{ + PY_TRY { + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + std::list bars = getWorkbenchPtr()->listToolbars(); + + Py::List list; + for (std::list::iterator it = bars.begin(); it != bars.end(); ++it) { + list.append(Py::String(*it)); + } + return Py::new_reference_to(list); + } PY_CATCH; +} + +/** Shows a dict of all toolbars and their commands*/ +PyObject* WorkbenchPy::getToolbarItems(PyObject *args) +{ + PY_TRY { + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + std::list>> bars = getWorkbenchPtr()->getToolbarItems(); + + Py::Dict dict; + for (const auto it : bars) { + Py::List list; + for (const auto jt : it.second) { + list.append(Py::String(jt)); + } + dict.setItem(it.first, list); + } + return Py::new_reference_to(dict); + } PY_CATCH; +} + +/** Shows a list of all command bars */ +PyObject* WorkbenchPy::listCommandbars(PyObject *args) +{ + PY_TRY { + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + std::list bars = getWorkbenchPtr()->listCommandbars(); + + Py::List list; + for (std::list::iterator it = bars.begin(); it != bars.end(); ++it) { + list.append(Py::String(*it)); + } + return Py::new_reference_to(list); + } PY_CATCH; +} + +PyObject* WorkbenchPy::getCustomAttributes(const char*) const +{ + return nullptr; } int WorkbenchPy::setCustomAttributes(const char*, PyObject *) { return 0; } - -