Gui: move functions to list menus and toolbars from sub-class to base Workbench class

implement new function to list all toolbars and their used commands
This commit is contained in:
wmayer
2020-04-04 11:46:45 +02:00
parent 475f518510
commit 8663e724f5
7 changed files with 242 additions and 209 deletions

View File

@@ -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):

View File

@@ -23,11 +23,6 @@
<UserDocu>Remove a menu</UserDocu>
</Documentation>
</Methode>
<Methode Name="listMenus">
<Documentation>
<UserDocu>Show a list of all menus</UserDocu>
</Documentation>
</Methode>
<Methode Name="appendContextMenu">
<Documentation>
<UserDocu>Append a new context menu item</UserDocu>
@@ -48,11 +43,6 @@
<UserDocu>Remove a toolbar</UserDocu>
</Documentation>
</Methode>
<Methode Name="listToolbars">
<Documentation>
<UserDocu>Show a list of all toolbars</UserDocu>
</Documentation>
</Methode>
<Methode Name="appendCommandbar">
<Documentation>
<UserDocu>Append a new command bar</UserDocu>
@@ -63,66 +53,61 @@
<UserDocu>Remove a command bar</UserDocu>
</Documentation>
</Methode>
<Methode Name="listCommandbars">
<Documentation>
<UserDocu>Show a list of all command bars</UserDocu>
</Documentation>
<Methode Name="AppendMenu">
<Documentation>
<UserDocu>deprecated -- use appendMenu</UserDocu>
</Documentation>
</Methode>
<Methode Name="AppendMenu">
<Documentation>
<UserDocu>deprecated -- use appendMenu</UserDocu>
</Documentation>
</Methode>
<Methode Name="RemoveMenu">
<Documentation>
<UserDocu>deprecated -- use removeMenu</UserDocu>
</Documentation>
</Methode>
<Methode Name="ListMenus">
<Documentation>
<UserDocu>deprecated -- use listMenus</UserDocu>
</Documentation>
</Methode>
<Methode Name="AppendContextMenu">
<Documentation>
<UserDocu>deprecated -- use appendContextMenu</UserDocu>
</Documentation>
</Methode>
<Methode Name="RemoveContextMenu">
<Documentation>
<UserDocu>deprecated -- use removeContextMenu</UserDocu>
</Documentation>
</Methode>
<Methode Name="AppendToolbar">
<Documentation>
<UserDocu>deprecated -- use appendToolbar</UserDocu>
</Documentation>
</Methode>
<Methode Name="RemoveToolbar">
<Documentation>
<UserDocu>deprecated -- use removeToolbar</UserDocu>
</Documentation>
</Methode>
<Methode Name="ListToolbars">
<Documentation>
<UserDocu>deprecated -- use listToolbars</UserDocu>
</Documentation>
</Methode>
<Methode Name="AppendCommandbar">
<Documentation>
<UserDocu>deprecated -- use appendCommandBar</UserDocu>
</Documentation>
</Methode>
<Methode Name="RemoveCommandbar">
<Documentation>
<UserDocu>deprecated -- use removeCommandBar</UserDocu>
</Documentation>
</Methode>
<Methode Name="ListCommandbars">
<Documentation>
<UserDocu>deprecated -- use listCommandBars</UserDocu>
</Documentation>
</Methode>
<CustomAttributes />
<Methode Name="RemoveMenu">
<Documentation>
<UserDocu>deprecated -- use removeMenu</UserDocu>
</Documentation>
</Methode>
<Methode Name="ListMenus">
<Documentation>
<UserDocu>deprecated -- use listMenus</UserDocu>
</Documentation>
</Methode>
<Methode Name="AppendContextMenu">
<Documentation>
<UserDocu>deprecated -- use appendContextMenu</UserDocu>
</Documentation>
</Methode>
<Methode Name="RemoveContextMenu">
<Documentation>
<UserDocu>deprecated -- use removeContextMenu</UserDocu>
</Documentation>
</Methode>
<Methode Name="AppendToolbar">
<Documentation>
<UserDocu>deprecated -- use appendToolbar</UserDocu>
</Documentation>
</Methode>
<Methode Name="RemoveToolbar">
<Documentation>
<UserDocu>deprecated -- use removeToolbar</UserDocu>
</Documentation>
</Methode>
<Methode Name="ListToolbars">
<Documentation>
<UserDocu>deprecated -- use listToolbars</UserDocu>
</Documentation>
</Methode>
<Methode Name="AppendCommandbar">
<Documentation>
<UserDocu>deprecated -- use appendCommandBar</UserDocu>
</Documentation>
</Methode>
<Methode Name="RemoveCommandbar">
<Documentation>
<UserDocu>deprecated -- use removeCommandBar</UserDocu>
</Documentation>
</Methode>
<Methode Name="ListCommandbars">
<Documentation>
<UserDocu>deprecated -- use listCommandBars</UserDocu>
</Documentation>
</Methode>
<CustomAttributes />
</PythonExport>
</GenerateModel>
</GenerateModel>

View File

@@ -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<std::string> 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<std::string> menus = getPythonBaseWorkbenchPtr()->listMenus();
PyObject* pyList = PyList_New(menus.size());
int i=0;
for (std::list<std::string>::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<std::string> 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<std::string> 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<std::string> bars = getPythonBaseWorkbenchPtr()->listToolbars();
PyObject* pyList = PyList_New(bars.size());
int i=0;
for (std::list<std::string>::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<std::string> 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<std::string> bars = getPythonBaseWorkbenchPtr()->listCommandbars();
PyObject* pyList = PyList_New(bars.size());
int i=0;
for (std::list<std::string>::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);

View File

@@ -429,6 +429,54 @@ void Workbench::removeTaskWatcher(void)
taskView->clearTaskWatcher();
}
std::list<std::string> Workbench::listToolbars() const
{
std::unique_ptr<ToolBarItem> tb(setupToolBars());
std::list<std::string> bars;
QList<ToolBarItem*> items = tb->getItems();
for (QList<ToolBarItem*>::ConstIterator item = items.begin(); item != items.end(); ++item)
bars.push_back((*item)->command());
return bars;
}
std::list<std::pair<std::string, std::list<std::string>>> Workbench::getToolbarItems() const
{
std::unique_ptr<ToolBarItem> tb(setupToolBars());
std::list<std::pair<std::string, std::list<std::string>>> itemsList;
QList<ToolBarItem*> items = tb->getItems();
for (QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); ++it) {
QList<ToolBarItem*> sub = (*it)->getItems();
std::list<std::string> cmds;
for (QList<ToolBarItem*>::ConstIterator jt = sub.begin(); jt != sub.end(); ++jt) {
cmds.push_back((*jt)->command());
}
itemsList.emplace_back((*it)->command(), cmds);
}
return itemsList;
}
std::list<std::string> Workbench::listMenus() const
{
std::unique_ptr<MenuItem> mb(setupMenuBar());
std::list<std::string> menus;
QList<MenuItem*> items = mb->getItems();
for ( QList<MenuItem*>::ConstIterator it = items.begin(); it != items.end(); ++it )
menus.push_back((*it)->command());
return menus;
}
std::list<std::string> Workbench::listCommandbars() const
{
std::unique_ptr<ToolBarItem> cb(setupCommandBars());
std::list<std::string> bars;
QList<ToolBarItem*> items = cb->getItems();
for (QList<ToolBarItem*>::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<std::string> PythonBaseWorkbench::listMenus() const
{
std::list<std::string> menus;
QList<MenuItem*> items = _menuBar->getItems();
for ( QList<MenuItem*>::ConstIterator it = items.begin(); it != items.end(); ++it )
menus.push_back((*it)->command());
return menus;
}
void PythonBaseWorkbench::appendContextMenu(const std::list<std::string>& menu, const std::list<std::string>& items) const
{
MenuItem* item = _contextMenu;
@@ -1062,15 +1101,6 @@ void PythonBaseWorkbench::removeToolbar(const std::string& bar) const
}
}
std::list<std::string> PythonBaseWorkbench::listToolbars() const
{
std::list<std::string> bars;
QList<ToolBarItem*> items = _toolBar->getItems();
for (QList<ToolBarItem*>::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<std::string>& items) const
{
ToolBarItem* item = _commandBar->findItem( bar );
@@ -1093,15 +1123,6 @@ void PythonBaseWorkbench::removeCommandbar(const std::string& bar) const
}
}
std::list<std::string> PythonBaseWorkbench::listCommandbars() const
{
std::list<std::string> bars;
QList<ToolBarItem*> items = _commandBar->getItems();
for (QList<ToolBarItem*>::ConstIterator item = items.begin(); item != items.end(); ++item)
bars.push_back((*item)->command());
return bars;
}
// -----------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PythonBlankWorkbench, Gui::PythonBaseWorkbench)

View File

@@ -97,6 +97,15 @@ public:
static void createLinkMenu(MenuItem *);
//// Shows a list of all toolbars
std::list<std::string> listToolbars() const;
/// Shows a list of all toolbars and their commands
std::list<std::pair<std::string, std::list<std::string>>> getToolbarItems() const;
//// Shows a list of all menus
std::list<std::string> listMenus() const;
//// Shows a list of all command bars
std::list<std::string> 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<std::string>& menu, const std::list<std::string>& items) const;
/// Removes a menu
void removeMenu(const std::string& menu ) const;
//// Shows a list of all menus
std::list<std::string> listMenus() const;
/// Appends new context menu items
void appendContextMenu(const std::list<std::string>& menu, const std::list<std::string>& items) const;
@@ -259,15 +266,11 @@ public:
void appendToolbar(const std::string& bar, const std::list<std::string>& items) const;
/// Removes a toolbar
void removeToolbar(const std::string& bar) const;
//// Shows a list of all toolbars
std::list<std::string> listToolbars() const;
/// Appends a new command bar
void appendCommandbar(const std::string& bar, const std::list<std::string>& items) const;
/// Removes a command bar
void removeCommandbar(const std::string& bar) const;
//// Shows a list of all command bars
std::list<std::string> listCommandbars() const;
//@}
protected:

View File

@@ -23,6 +23,26 @@
<UserDocu>Activate this workbench</UserDocu>
</Documentation>
</Methode>
<Methode Name="listToolbars">
<Documentation>
<UserDocu>Show a list of all toolbars</UserDocu>
</Documentation>
</Methode>
<Methode Name="getToolbarItems">
<Documentation>
<UserDocu>Show a dict of all toolbars and their commands</UserDocu>
</Documentation>
</Methode>
<Methode Name="listCommandbars">
<Documentation>
<UserDocu>Show a list of all command bars</UserDocu>
</Documentation>
</Methode>
<Methode Name="listMenus">
<Documentation>
<UserDocu>Show a list of all menus</UserDocu>
</Documentation>
</Methode>
<CustomAttributes />
</PythonExport>
</GenerateModel>
</GenerateModel>

View File

@@ -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<std::string> menus = getWorkbenchPtr()->listMenus();
Py::List list;
for (std::list<std::string>::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<std::string> bars = getWorkbenchPtr()->listToolbars();
Py::List list;
for (std::list<std::string>::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<std::pair<std::string, std::list<std::string>>> 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<std::string> bars = getWorkbenchPtr()->listCommandbars();
Py::List list;
for (std::list<std::string>::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;
}