From 6a77635bdbb0fea476ab268234bea8d0cbe9506c Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 26 Feb 2012 17:56:18 +0100 Subject: [PATCH] Implement empty workbench to access from python --- src/Gui/Application.cpp | 4 +- src/Gui/PythonWorkbenchPy.xml | 4 +- src/Gui/PythonWorkbenchPyImp.cpp | 22 +++--- src/Gui/Workbench.cpp | 121 ++++++++++++++++++++++++------- src/Gui/Workbench.h | 46 ++++++++++-- src/Mod/Sketcher/Gui/Makefile.am | 1 + 6 files changed, 152 insertions(+), 46 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index f4e108c6c..232d0e23f 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -928,7 +928,7 @@ bool Application::activateWorkbench(const char* name) Py::Tuple args; Py::String result(method.apply(args)); type = result.as_std_string(); - if (type == "Gui::PythonWorkbench") { + if (Base::Type::fromName(type.c_str()).isDerivedFrom(Gui::PythonBaseWorkbench::getClassTypeId())) { Workbench* wb = WorkbenchManager::instance()->createWorkbench(name, type); handler.setAttr(std::string("__Workbench__"), Py::Object(wb->getPyObject(), true)); } @@ -1411,6 +1411,8 @@ void Application::initTypes(void) Gui::BlankWorkbench ::init(); Gui::NoneWorkbench ::init(); Gui::TestWorkbench ::init(); + Gui::PythonBaseWorkbench ::init(); + Gui::PythonBlankWorkbench ::init(); Gui::PythonWorkbench ::init(); } diff --git a/src/Gui/PythonWorkbenchPy.xml b/src/Gui/PythonWorkbenchPy.xml index deb76c211..393e4226b 100644 --- a/src/Gui/PythonWorkbenchPy.xml +++ b/src/Gui/PythonWorkbenchPy.xml @@ -3,8 +3,8 @@ appendMenu( path, items ); + getPythonBaseWorkbenchPtr()->appendMenu( path, items ); Py_Return; } PY_CATCH; @@ -110,7 +110,7 @@ PyObject* PythonWorkbenchPy::removeMenu(PyObject *args) if (!PyArg_ParseTuple(args, "s", &psMenu)) // convert args: Python->C return NULL; // NULL triggers exception - getPythonWorkbenchPtr()->removeMenu( psMenu ); + getPythonBaseWorkbenchPtr()->removeMenu( psMenu ); Py_Return; } PY_CATCH; } @@ -119,7 +119,7 @@ PyObject* PythonWorkbenchPy::removeMenu(PyObject *args) PyObject* PythonWorkbenchPy::listMenus(PyObject *args) { PY_TRY { - std::list menus = getPythonWorkbenchPtr()->listMenus(); + std::list menus = getPythonBaseWorkbenchPtr()->listMenus(); PyObject* pyList = PyList_New(menus.size()); int i=0; @@ -180,7 +180,7 @@ PyObject* PythonWorkbenchPy::appendContextMenu(PyObject *args) return NULL; // NULL triggers exception } - getPythonWorkbenchPtr()->appendContextMenu( path, items ); + getPythonBaseWorkbenchPtr()->appendContextMenu( path, items ); Py_Return; } PY_CATCH; @@ -194,7 +194,7 @@ PyObject* PythonWorkbenchPy::removeContextMenu(PyObject *args) if (!PyArg_ParseTuple(args, "s", &psMenu)) // convert args: Python->C return NULL; // NULL triggers exception - getPythonWorkbenchPtr()->removeContextMenu( psMenu ); + getPythonBaseWorkbenchPtr()->removeContextMenu( psMenu ); Py_Return; } PY_CATCH; } @@ -222,7 +222,7 @@ PyObject* PythonWorkbenchPy::appendToolbar(PyObject *args) items.push_back(pItem); } - getPythonWorkbenchPtr()->appendToolbar( psToolBar, items ); + getPythonBaseWorkbenchPtr()->appendToolbar( psToolBar, items ); Py_Return; } PY_CATCH; @@ -236,7 +236,7 @@ PyObject* PythonWorkbenchPy::removeToolbar(PyObject *args) if (!PyArg_ParseTuple(args, "s", &psToolBar)) // convert args: Python->C return NULL; // NULL triggers exception - getPythonWorkbenchPtr()->removeToolbar( psToolBar ); + getPythonBaseWorkbenchPtr()->removeToolbar( psToolBar ); Py_Return; } PY_CATCH; } @@ -245,7 +245,7 @@ PyObject* PythonWorkbenchPy::removeToolbar(PyObject *args) PyObject* PythonWorkbenchPy::listToolbars(PyObject *args) { PY_TRY { - std::list bars = getPythonWorkbenchPtr()->listToolbars(); + std::list bars = getPythonBaseWorkbenchPtr()->listToolbars(); PyObject* pyList = PyList_New(bars.size()); int i=0; @@ -280,7 +280,7 @@ PyObject* PythonWorkbenchPy::appendCommandbar(PyObject *args) items.push_back(pItem); } - getPythonWorkbenchPtr()->appendCommandbar( psToolBar, items ); + getPythonBaseWorkbenchPtr()->appendCommandbar( psToolBar, items ); Py_Return; } PY_CATCH; @@ -294,7 +294,7 @@ PyObject* PythonWorkbenchPy::removeCommandbar(PyObject *args) if (!PyArg_ParseTuple(args, "s", &psToolBar)) // convert args: Python->C return NULL; // NULL triggers exception - getPythonWorkbenchPtr()->removeCommandbar( psToolBar ); + getPythonBaseWorkbenchPtr()->removeCommandbar( psToolBar ); Py_Return; } PY_CATCH; } @@ -303,7 +303,7 @@ PyObject* PythonWorkbenchPy::removeCommandbar(PyObject *args) PyObject* PythonWorkbenchPy::listCommandbars(PyObject *args) { PY_TRY { - std::list bars = getPythonWorkbenchPtr()->listCommandbars(); + std::list bars = getPythonBaseWorkbenchPtr()->listCommandbars(); PyObject* pyList = PyList_New(bars.size()); int i=0; diff --git a/src/Gui/Workbench.cpp b/src/Gui/Workbench.cpp index 749278fb0..6617137cd 100644 --- a/src/Gui/Workbench.cpp +++ b/src/Gui/Workbench.cpp @@ -173,6 +173,7 @@ using namespace Gui; * \section moredetails More details and limitations * One of the key concepts of the workbench framework is to load a module at runtime when the user needs some function that it * provides. So, if the user doesn't need a module it never gets loaded into RAM. This speeds up the startup procedure of + * FreeCAD and saves memory. * At startup FreeCAD scans all module directories and invokes InitGui.py. So an item for a workbench gets created. If the user * clicks on such an item the matching module gets loaded, the C++ workbench gets registered and activated. @@ -750,30 +751,26 @@ ToolBarItem* TestWorkbench::setupCommandBars() const // ----------------------------------------------------------------------- -TYPESYSTEM_SOURCE(Gui::PythonWorkbench, Gui::Workbench) +TYPESYSTEM_SOURCE(Gui::PythonBaseWorkbench, Gui::Workbench) -PythonWorkbench::PythonWorkbench() : _workbenchPy(0) +PythonBaseWorkbench::PythonBaseWorkbench() + : _menuBar(0), _contextMenu(0), _toolBar(0), _commandBar(0), _workbenchPy(0) { - _menuBar = StdWorkbench::setupMenuBar(); - _contextMenu = new MenuItem; - _toolBar = StdWorkbench::setupToolBars(); - _commandBar = new ToolBarItem; } -PythonWorkbench::~PythonWorkbench() +PythonBaseWorkbench::~PythonBaseWorkbench() { delete _menuBar; delete _contextMenu; delete _toolBar; delete _commandBar; - if (_workbenchPy) - { + if (_workbenchPy) { _workbenchPy->setInvalid(); _workbenchPy->DecRef(); } } -PyObject* PythonWorkbench::getPyObject() +PyObject* PythonBaseWorkbench::getPyObject() { if (!_workbenchPy) { @@ -786,31 +783,35 @@ PyObject* PythonWorkbench::getPyObject() return _workbenchPy; } -MenuItem* PythonWorkbench::setupMenuBar() const +MenuItem* PythonBaseWorkbench::setupMenuBar() const { return _menuBar->copy(); } -ToolBarItem* PythonWorkbench::setupToolBars() const +ToolBarItem* PythonBaseWorkbench::setupToolBars() const { return _toolBar->copy(); } -ToolBarItem* PythonWorkbench::setupCommandBars() const +ToolBarItem* PythonBaseWorkbench::setupCommandBars() const { return _commandBar->copy(); } -void PythonWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const +DockWindowItems* PythonBaseWorkbench::setupDockWindows() const +{ + return new DockWindowItems(); +} + +void PythonBaseWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const { - StdWorkbench::setupContextMenu(recipient, item); QList items = _contextMenu->getItems(); for (QList::Iterator it = items.begin(); it != items.end(); ++it) { item->appendItem((*it)->copy()); } } -void PythonWorkbench::appendMenu(const std::list& menu, const std::list& items) const +void PythonBaseWorkbench::appendMenu(const std::list& menu, const std::list& items) const { if ( menu.empty() || items.empty() ) return; @@ -841,7 +842,7 @@ void PythonWorkbench::appendMenu(const std::list& menu, const std:: *item << *it; } -void PythonWorkbench::removeMenu(const std::string& menu) const +void PythonBaseWorkbench::removeMenu(const std::string& menu) const { MenuItem* item = _menuBar->findItem(menu); if ( item ) { @@ -850,7 +851,7 @@ void PythonWorkbench::removeMenu(const std::string& menu) const } } -std::list PythonWorkbench::listMenus() const +std::list PythonBaseWorkbench::listMenus() const { std::list menus; QList items = _menuBar->getItems(); @@ -859,7 +860,7 @@ std::list PythonWorkbench::listMenus() const return menus; } -void PythonWorkbench::appendContextMenu(const std::list& menu, const std::list& items) const +void PythonBaseWorkbench::appendContextMenu(const std::list& menu, const std::list& items) const { MenuItem* item = _contextMenu; for (std::list::const_iterator jt=menu.begin();jt!=menu.end();++jt) { @@ -875,7 +876,7 @@ void PythonWorkbench::appendContextMenu(const std::list& menu, cons *item << *it; } -void PythonWorkbench::removeContextMenu(const std::string& menu) const +void PythonBaseWorkbench::removeContextMenu(const std::string& menu) const { MenuItem* item = _contextMenu->findItem(menu); if (item) { @@ -884,12 +885,12 @@ void PythonWorkbench::removeContextMenu(const std::string& menu) const } } -void PythonWorkbench::clearContextMenu() +void PythonBaseWorkbench::clearContextMenu() { _contextMenu->clear(); } -void PythonWorkbench::appendToolbar(const std::string& bar, const std::list& items) const +void PythonBaseWorkbench::appendToolbar(const std::string& bar, const std::list& items) const { ToolBarItem* item = _toolBar->findItem(bar); if (!item) @@ -902,7 +903,7 @@ void PythonWorkbench::appendToolbar(const std::string& bar, const std::listfindItem(bar); if (item) { @@ -911,7 +912,7 @@ void PythonWorkbench::removeToolbar(const std::string& bar) const } } -std::list PythonWorkbench::listToolbars() const +std::list PythonBaseWorkbench::listToolbars() const { std::list bars; QList items = _toolBar->getItems(); @@ -920,7 +921,7 @@ std::list PythonWorkbench::listToolbars() const return bars; } -void PythonWorkbench::appendCommandbar(const std::string& bar, const std::list& items) const +void PythonBaseWorkbench::appendCommandbar(const std::string& bar, const std::list& items) const { ToolBarItem* item = _commandBar->findItem( bar ); if ( !item ) @@ -933,7 +934,7 @@ void PythonWorkbench::appendCommandbar(const std::string& bar, const std::listfindItem(bar); if ( item ) { @@ -942,7 +943,7 @@ void PythonWorkbench::removeCommandbar(const std::string& bar) const } } -std::list PythonWorkbench::listCommandbars() const +std::list PythonBaseWorkbench::listCommandbars() const { std::list bars; QList items = _commandBar->getItems(); @@ -951,3 +952,69 @@ std::list PythonWorkbench::listCommandbars() const return bars; } +// ----------------------------------------------------------------------- + +TYPESYSTEM_SOURCE(Gui::PythonBlankWorkbench, Gui::PythonBaseWorkbench) + +PythonBlankWorkbench::PythonBlankWorkbench() +{ + _menuBar = new MenuItem; + _contextMenu = new MenuItem; + _toolBar = new ToolBarItem; + _commandBar = new ToolBarItem; +} + +PythonBlankWorkbench::~PythonBlankWorkbench() +{ +} + +// ----------------------------------------------------------------------- + +TYPESYSTEM_SOURCE(Gui::PythonWorkbench, Gui::PythonBaseWorkbench) + +PythonWorkbench::PythonWorkbench() +{ + StdWorkbench wb; + _menuBar = wb.setupMenuBar(); + _contextMenu = new MenuItem; + _toolBar = wb.setupToolBars(); + _commandBar = new ToolBarItem; +} + +PythonWorkbench::~PythonWorkbench() +{ +} + +MenuItem* PythonWorkbench::setupMenuBar() const +{ + return _menuBar->copy(); +} + +ToolBarItem* PythonWorkbench::setupToolBars() const +{ + return _toolBar->copy(); +} + +ToolBarItem* PythonWorkbench::setupCommandBars() const +{ + return _commandBar->copy(); +} + +DockWindowItems* PythonWorkbench::setupDockWindows() const +{ + StdWorkbench wb; + return wb.setupDockWindows(); +} + +void PythonWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const +{ + StdWorkbench wb; + wb.setupContextMenu(recipient, item); + PythonBaseWorkbench::setupContextMenu(recipient, item); +} + +void PythonWorkbench::createMainWindowPopupMenu(MenuItem* item) const +{ + StdWorkbench wb; + wb.createMainWindowPopupMenu(item); +} diff --git a/src/Gui/Workbench.h b/src/Gui/Workbench.h index 223766b58..2e74de4a1 100644 --- a/src/Gui/Workbench.h +++ b/src/Gui/Workbench.h @@ -144,6 +144,8 @@ protected: virtual ToolBarItem* setupCommandBars() const; /** Returns a DockWindowItems structure of dock windows this workbench. */ virtual DockWindowItems* setupDockWindows() const; + + friend class PythonWorkbench; }; /** @@ -217,17 +219,17 @@ protected: }; /** - * The PythonWorkbench class allows the manipulation of the workbench from Python. + * The PythonBaseWorkbench class allows the manipulation of the workbench from Python. * Therefore PythonWorkbenchPy provides the required Python interface. * @author Werner Mayer */ -class GuiExport PythonWorkbench : public StdWorkbench +class GuiExport PythonBaseWorkbench : public Workbench { TYPESYSTEM_HEADER(); public: - PythonWorkbench(); - ~PythonWorkbench(); + PythonBaseWorkbench(); + ~PythonBaseWorkbench(); /** * Creates and returns immediately the corresponding Python workbench object. */ @@ -268,8 +270,9 @@ protected: MenuItem* setupMenuBar() const; ToolBarItem* setupToolBars() const; ToolBarItem* setupCommandBars() const; + DockWindowItems* setupDockWindows() const; -private: +protected: MenuItem* _menuBar; MenuItem* _contextMenu; ToolBarItem* _toolBar; @@ -277,6 +280,39 @@ private: Base::PyObjectBase* _workbenchPy; }; +class GuiExport PythonBlankWorkbench : public PythonBaseWorkbench +{ + TYPESYSTEM_HEADER(); + +public: + PythonBlankWorkbench(); + ~PythonBlankWorkbench(); +}; + +/** + * The PythonWorkbench class allows the manipulation of the workbench from Python. + * Therefore PythonWorkbenchPy provides the required Python interface. + * @author Werner Mayer + */ +class GuiExport PythonWorkbench : public PythonBaseWorkbench +{ + TYPESYSTEM_HEADER(); + +public: + PythonWorkbench(); + ~PythonWorkbench(); + + /** Defines the standard context menu. */ + virtual void setupContextMenu(const char* recipient, MenuItem*) const; + virtual void createMainWindowPopupMenu(MenuItem*) const; + +protected: + MenuItem* setupMenuBar() const; + ToolBarItem* setupToolBars() const; + ToolBarItem* setupCommandBars() const; + DockWindowItems* setupDockWindows() const; +}; + } // namespace Gui diff --git a/src/Mod/Sketcher/Gui/Makefile.am b/src/Mod/Sketcher/Gui/Makefile.am index 15f39814f..b18b16032 100644 --- a/src/Mod/Sketcher/Gui/Makefile.am +++ b/src/Mod/Sketcher/Gui/Makefile.am @@ -3,6 +3,7 @@ SUBDIRS=Resources lib_LTLIBRARIES=libSketcherGui.la SketcherGui.la BUILT_SOURCES=\ + moc_SketchOrientationDialog.cpp \ moc_TaskSketcherConstrains.cpp \ moc_TaskSketcherCreateCommands.cpp \ moc_TaskSketcherGeneral.cpp \