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(sSetActiveDocument);
PYFUNCDEF_S(sActiveView);
PYFUNCDEF_S(sGetDocument);
PYFUNCDEF_S(sDoCommand);

View File

@ -139,13 +139,16 @@ PyMethodDef Application::Methods[] = {
"Open a macro, Inventor or VRML file"},
{"export", (PyCFunction) Application::sExport, 1,
"save scene to Inventor or VRML file"},
{"activeDocument", (PyCFunction) Application::sActiveDocument, 1,
{ "activeDocument", (PyCFunction)Application::sActiveDocument, 1,
"activeDocument() -> object or None\n\n"
"Return the active document or None if no one exists"},
"Return the active document or None if no one exists" },
{"setActiveDocument", (PyCFunction) Application::sSetActiveDocument,1,
"setActiveDocument(string or App.Document) -> None\n\n"
"Activate the specified document"},
{"getDocument", (PyCFunction) Application::sGetDocument, 1,
{ "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(string) -> object\n\n"
"Get a document by its name"},
{"doCommand", (PyCFunction) Application::sDoCommand, 1,
@ -167,17 +170,34 @@ PyMethodDef Application::Methods[] = {
{NULL, NULL} /* Sentinel */
};
PyObject* Gui::Application::sActiveDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
PyObject* Gui::Application::sActiveDocument(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
Document *pcDoc = Instance->activeDocument();
if (pcDoc) {
return pcDoc->getPyObject();
} else {
Py_Return;
}
Document *pcDoc = Instance->activeDocument();
if (pcDoc) {
return pcDoc->getPyObject();
}
else {
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*/)

View File

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

View File

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

View File

@ -64,6 +64,7 @@
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/DocumentObjectPy.h>
#include <CXX/Objects.hxx>
using namespace Gui;
@ -223,15 +224,20 @@ Py::Object View3DInventorPy::getattr(const char * attr)
s_out << "Cannot access attribute '" << attr << "' of deleted object";
throw Py::RuntimeError(s_out.str());
}
else {
Py::Object obj = Py::PythonExtension<View3DInventorPy>::getattr(attr);
if (PyCFunction_Check(obj.ptr())) {
PyCFunctionObject* op = reinterpret_cast<PyCFunctionObject*>(obj.ptr());
if (!pycxx_handler)
pycxx_handler = op->m_ml->ml_meth;
op->m_ml->ml_meth = method_varargs_ext_handler;
}
return obj;
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);
if (PyCFunction_Check(obj.ptr())) {
PyCFunctionObject* op = reinterpret_cast<PyCFunctionObject*>(obj.ptr());
if (!pycxx_handler)
pycxx_handler = op->m_ml->ml_meth;
op->m_ml->ml_meth = method_varargs_ext_handler;
}
return obj;
}
}
}
@ -2116,47 +2122,59 @@ Py::Object View3DInventorPy::addDraggerCallback(const Py::Tuple& args)
Py::Object View3DInventorPy::removeDraggerCallback(const Py::Tuple& args)
{
PyObject* dragger;
char* type;
PyObject* method;
if (!PyArg_ParseTuple(args.ptr(), "OsO", &dragger,&type, &method))
throw Py::Exception();
PyObject* dragger;
char* type;
PyObject* method;
if (!PyArg_ParseTuple(args.ptr(), "OsO", &dragger, &type, &method))
throw Py::Exception();
//Check if dragger is a SoDragger object and cast
void* ptr = 0;
try {
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoDragger *", dragger, &ptr, 0);
}
catch (const Base::Exception&) {
throw Py::Exception("The first argument must be of type SoDragger");
}
//Check if dragger is a SoDragger object and cast
void* ptr = 0;
try {
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoDragger *", dragger, &ptr, 0);
}
catch (const Base::Exception&) {
throw Py::Exception("The first argument must be of type SoDragger");
}
SoDragger* drag = reinterpret_cast<SoDragger*>(ptr);
try {
if (strcmp(type,"addFinishCallback")==0) {
drag->removeFinishCallback(draggerCallback,method);
}
else if (strcmp(type,"addStartCallback")==0) {
drag->removeStartCallback(draggerCallback,method);
}
else if (strcmp(type,"addMotionCallback")==0) {
drag->removeMotionCallback(draggerCallback,method);
}
else if (strcmp(type,"addValueChangedCallback")==0) {
drag->removeValueChangedCallback(draggerCallback,method);
}
else {
std::string s;
std::ostringstream s_out;
s_out << type << " is not a valid dragger callback type";
throw Py::Exception(s_out.str());
}
SoDragger* drag = reinterpret_cast<SoDragger*>(ptr);
try {
if (strcmp(type, "addFinishCallback") == 0) {
drag->removeFinishCallback(draggerCallback, method);
}
else if (strcmp(type, "addStartCallback") == 0) {
drag->removeStartCallback(draggerCallback, method);
}
else if (strcmp(type, "addMotionCallback") == 0) {
drag->removeMotionCallback(draggerCallback, method);
}
else if (strcmp(type, "addValueChangedCallback") == 0) {
drag->removeValueChangedCallback(draggerCallback, method);
}
else {
std::string s;
std::ostringstream s_out;
s_out << type << " is not a valid dragger callback type";
throw Py::Exception(s_out.str());
}
callbacks.remove(method);
Py_DECREF(method);
return Py::Callable(method, false);
}
catch (const Py::Exception&) {
throw;
}
callbacks.remove(method);
Py_DECREF(method);
return Py::Callable(method, false);
}
catch (const Py::Exception&) {
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

@ -101,7 +101,8 @@ public:
Py::Object setAxisCross(const Py::Tuple&);
Py::Object hasAxisCross(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:
static void eventCallback(void * ud, SoEventCallback * n);