Active object manager for the Viewer

This commit is contained in:
jriegel 2014-12-31 15:55:17 +01:00 committed by Stefan Tröger
parent 240f2f7428
commit 16ab7f710b
8 changed files with 225 additions and 62 deletions

View File

@ -0,0 +1,46 @@
/***************************************************************************
* (c) Jürgen Riegel (juergen.riegel@web.de) 2014 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* FreeCAD is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with FreeCAD; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
* Juergen Riegel 2014 *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#endif
#include "ActiveObjectList.h"
using namespace Gui;
void Gui::ActiveObjectList::setObject(App::DocumentObject* obj, const char* name)
{
_ObjectMap[name] = obj;
}
bool Gui::ActiveObjectList::hasObject(const char*name)
{
return _ObjectMap.find(name) != _ObjectMap.end();
}

View File

@ -0,0 +1,72 @@
/***************************************************************************
* (c) Jürgen Riegel (juergen.riegel@web.de) 2014 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* FreeCAD is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with FreeCAD; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
* Juergen Riegel 2014 *
***************************************************************************/
#ifndef GUI_ActiveObjectList_H
#define GUI_ActiveObjectList_H
#include <map>
namespace App {
class DocumentObject;
}
namespace Gui
{
class Document;
/** List of active or special objects
* This class holds a list of objects with a special name.
* Its mainly used to points to something like the active Body or Part in a edit session.
* The class is used the viewer (editor) of a document.
* @see Gui::MDIViewer
* @author Jürgen Riegel
*/
class GuiExport ActiveObjectList
{
public:
template<class T> T* getObject(const char*);
void setObject(App::DocumentObject*, const char*);
bool hasObject(const char*);
protected:
std::map<std::string, App::DocumentObject*> _ObjectMap;
};
template<class T>
T* Gui::ActiveObjectList::getObject(const char* name)
{
auto pos = _ObjectMap.find(name);
if (pos == _ObjectMap.end())
return 0;
else
return dynamic_cast<T*>(pos->second);
}
} //namespace Gui
#endif

View File

@ -232,6 +232,7 @@ public:
PYFUNCDEF_S(sActiveDocument); PYFUNCDEF_S(sActiveDocument);
PYFUNCDEF_S(sSetActiveDocument); PYFUNCDEF_S(sSetActiveDocument);
PYFUNCDEF_S(sActiveView);
PYFUNCDEF_S(sGetDocument); PYFUNCDEF_S(sGetDocument);
PYFUNCDEF_S(sDoCommand); PYFUNCDEF_S(sDoCommand);

View File

@ -145,6 +145,9 @@ PyMethodDef Application::Methods[] = {
{"setActiveDocument", (PyCFunction) Application::sSetActiveDocument,1, {"setActiveDocument", (PyCFunction) Application::sSetActiveDocument,1,
"setActiveDocument(string or App.Document) -> None\n\n" "setActiveDocument(string or App.Document) -> None\n\n"
"Activate the specified document"}, "Activate the specified document"},
{ "activeView", (PyCFunction)Application::sActiveView, 1,
"activeView() -> object or None\n\n"
"Return the active view of the active document or None if no one exists" },
{ "getDocument", (PyCFunction)Application::sGetDocument, 1, { "getDocument", (PyCFunction)Application::sGetDocument, 1,
"getDocument(string) -> object\n\n" "getDocument(string) -> object\n\n"
"Get a document by its name"}, "Get a document by its name"},
@ -175,11 +178,28 @@ PyObject* Gui::Application::sActiveDocument(PyObject * /*self*/, PyObject *args,
Document *pcDoc = Instance->activeDocument(); Document *pcDoc = Instance->activeDocument();
if (pcDoc) { if (pcDoc) {
return pcDoc->getPyObject(); return pcDoc->getPyObject();
} else { }
else {
Py_Return; Py_Return;
} }
} }
PyObject* Gui::Application::sActiveView(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
Document *pcDoc = Instance->activeDocument();
if (pcDoc) {
Gui::MDIView *pcView = pcDoc->getActiveView();
if (pcView)
// already incremented in getPyObject().
return pcView->getPyObject();
}
Py_Return;
}
PyObject* Gui::Application::sSetActiveDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) PyObject* Gui::Application::sSetActiveDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
{ {
Document *pcDoc = 0; Document *pcDoc = 0;

View File

@ -933,10 +933,12 @@ SOURCE_GROUP("Widget" FILES ${Widget_SRCS})
SET(View_CPP_SRCS SET(View_CPP_SRCS
MDIView.cpp MDIView.cpp
GraphvizView.cpp GraphvizView.cpp
ActiveObjectList.cpp
) )
SET(View_HPP_SRCS SET(View_HPP_SRCS
MDIView.h MDIView.h
GraphvizView.h GraphvizView.h
ActiveObjectList.h
) )
SET(View_SRCS SET(View_SRCS
${View_CPP_SRCS} ${View_CPP_SRCS}

View File

@ -26,6 +26,7 @@
#include "View.h" #include "View.h"
#include <QMainWindow> #include <QMainWindow>
#include "ActiveObjectList.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPrinter; class QPrinter;
@ -108,6 +109,8 @@ public:
virtual void setCurrentViewMode(ViewMode mode); virtual void setCurrentViewMode(ViewMode mode);
ViewMode currentViewMode() const { return currentMode; } ViewMode currentViewMode() const { return currentMode; }
ActiveObjectList ActiveObjects;
public Q_SLOTS: public Q_SLOTS:
virtual void setOverrideCursor(const QCursor&); virtual void setOverrideCursor(const QCursor&);
virtual void restoreOverrideCursor(); virtual void restoreOverrideCursor();

View File

@ -64,6 +64,7 @@
#include <App/Document.h> #include <App/Document.h>
#include <App/DocumentObject.h> #include <App/DocumentObject.h>
#include <App/DocumentObjectPy.h>
#include <CXX/Objects.hxx> #include <CXX/Objects.hxx>
using namespace Gui; using namespace Gui;
@ -224,6 +225,10 @@ Py::Object View3DInventorPy::getattr(const char * attr)
throw Py::RuntimeError(s_out.str()); throw Py::RuntimeError(s_out.str());
} }
else { else {
App::DocumentObject *docObj = _view->ActiveObjects.getObject<App::DocumentObject>(attr);
if (docObj){
return Py::Object(docObj->getPyObject());
}else{
Py::Object obj = Py::PythonExtension<View3DInventorPy>::getattr(attr); Py::Object obj = Py::PythonExtension<View3DInventorPy>::getattr(attr);
if (PyCFunction_Check(obj.ptr())) { if (PyCFunction_Check(obj.ptr())) {
PyCFunctionObject* op = reinterpret_cast<PyCFunctionObject*>(obj.ptr()); PyCFunctionObject* op = reinterpret_cast<PyCFunctionObject*>(obj.ptr());
@ -234,6 +239,7 @@ Py::Object View3DInventorPy::getattr(const char * attr)
return obj; return obj;
} }
} }
}
int View3DInventorPy::setattr(const char * attr, const Py::Object & value) int View3DInventorPy::setattr(const char * attr, const Py::Object & value)
{ {
@ -2160,3 +2166,15 @@ Py::Object View3DInventorPy::removeDraggerCallback(const Py::Tuple& args)
throw; throw;
} }
} }
Py::Object View3DInventorPy::setActiveObject(const Py::Tuple& args)
{
App::DocumentObjectPy* docObject = 0;
char* name;
if (!PyArg_ParseTuple(args.ptr(), "sO!", &name, &App::DocumentObjectPy::Type, &docObject))
throw Py::Exception();
if (docObject){
_view->ActiveObjects.setObject(docObject->getDocumentObjectPtr(), name);
}
}

View File

@ -102,6 +102,7 @@ public:
Py::Object hasAxisCross(const Py::Tuple&); Py::Object hasAxisCross(const Py::Tuple&);
Py::Object addDraggerCallback(const Py::Tuple&); Py::Object addDraggerCallback(const Py::Tuple&);
Py::Object removeDraggerCallback(const Py::Tuple&); Py::Object removeDraggerCallback(const Py::Tuple&);
Py::Object setActiveObject(const Py::Tuple&);
private: private:
static void eventCallback(void * ud, SoEventCallback * n); static void eventCallback(void * ud, SoEventCallback * n);