From 8991b9e04bbcd4ce5a6dcdf020c2a0fb07280153 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 12 Apr 2015 18:39:01 +0200 Subject: [PATCH] + add Document::sendMsgToFirstView, fix memory leak --- src/Gui/Document.cpp | 20 ++++++++++++++ src/Gui/Document.h | 6 +++++ src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp | 26 +++---------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp index 981c0ab00..92eb0213d 100644 --- a/src/Gui/Document.cpp +++ b/src/Gui/Document.cpp @@ -1163,6 +1163,26 @@ bool Document::sendMsgToViews(const char* pMsg) return false; } +bool Document::sendMsgToFirstView(const Base::Type& typeId, const char* pMsg, const char** ppReturn) +{ + // first try the active view + Gui::MDIView* view = getActiveView(); + if (view && view->isDerivedFrom(typeId)) { + if (view->onMsg(pMsg, ppReturn)) + return true; + } + + // now try the other views + std::list views = getMDIViewsOfType(typeId); + for (std::list::iterator it = views.begin(); it != views.end(); ++it) { + if ((*it != view) && (*it)->onMsg(pMsg, ppReturn)) { + return true; + } + } + + return false; +} + /// Getter for the active view MDIView* Document::getActiveView(void) const { diff --git a/src/Gui/Document.h b/src/Gui/Document.h index 9005e9056..a81387473 100644 --- a/src/Gui/Document.h +++ b/src/Gui/Document.h @@ -150,6 +150,12 @@ public: */ /// send Messages to all views bool sendMsgToViews(const char* pMsg); + /** Sends the message \a pMsg to the views of type \a typeid and stops with + * the first view that supports the message and returns \a ppReturn. The very + * first checked view is the current active view. + * If a view supports the message true is returned and false otherwise. + */ + bool sendMsgToFirstView(const Base::Type& typeId, const char* pMsg, const char** ppReturn); /// Attach a view (get called by the MDIView constructor) void attachView(Gui::BaseView* pcView, bool bPassiv=false); /// Detach a view (get called by the MDIView destructor) diff --git a/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp b/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp index acf9f2422..abeabcdac 100644 --- a/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp +++ b/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp @@ -98,17 +98,7 @@ povViewCamera(PyObject *self, PyObject *args) Gui::Document* doc = Gui::Application::Instance->activeDocument(); if (doc) { - // try active view first - Gui::MDIView* view = doc->getActiveView(); - if (!(view && view->onMsg("GetCamera",&ppReturn))) { - // now try all views - std::list views = doc->getMDIViews(); - for (std::list::iterator it = views.begin(); it != views.end(); ++it) { - if ((*it)->onMsg("GetCamera",&ppReturn)) { - break; - } - } - } + doc->sendMsgToFirstView(Gui::MDIView::getClassTypeId(), "GetCamera", &ppReturn); } else { PyErr_SetString(PyExc_RuntimeError, "No active document found"); @@ -146,6 +136,7 @@ povViewCamera(PyObject *self, PyObject *args) SbVec3f pos = Cam->position.getValue(); float Dist = Cam->focalDistance.getValue(); + Cam->unref(); // free memory // making gp out of the Coin stuff gp_Vec gpPos(pos.getValue()[0],pos.getValue()[1],pos.getValue()[2]); @@ -180,17 +171,7 @@ luxViewCamera(PyObject *self, PyObject *args) Gui::Document* doc = Gui::Application::Instance->activeDocument(); if (doc) { - // try active view first - Gui::MDIView* view = doc->getActiveView(); - if (!(view && view->onMsg("GetCamera",&ppReturn))) { - // now try all views - std::list views = doc->getMDIViews(); - for (std::list::iterator it = views.begin(); it != views.end(); ++it) { - if ((*it)->onMsg("GetCamera",&ppReturn)) { - break; - } - } - } + doc->sendMsgToFirstView(Gui::MDIView::getClassTypeId(), "GetCamera", &ppReturn); } else { PyErr_SetString(PyExc_RuntimeError, "No active document found"); @@ -228,6 +209,7 @@ luxViewCamera(PyObject *self, PyObject *args) SbVec3f pos = Cam->position.getValue(); float Dist = Cam->focalDistance.getValue(); + Cam->unref(); // free memory // making gp out of the Coin stuff gp_Vec gpPos(pos.getValue()[0],pos.getValue()[1],pos.getValue()[2]);