From f4f4d08fab975b71add2c1d4f677e30ec3ec3760 Mon Sep 17 00:00:00 2001
From: Pat Marion <pat.marion@kitware.com>
Date: Thu, 22 Mar 2012 17:04:39 -0400
Subject: [PATCH 1/2] add pqPythonDebugLeaksView widget for tracking vtk
 objects

---
 Applications/ParaView/ParaViewMainWindow.cxx |  10 +++
 Qt/Python/CMakeLists.txt                     |   3 +
 Qt/Python/pqPythonDebugLeaksView.cxx         | 113 +++++++++++++++++++++++++++
 Qt/Python/pqPythonDebugLeaksView.h           |  56 +++++++++++++
 4 files changed, 182 insertions(+)
 create mode 100644 Qt/Python/pqPythonDebugLeaksView.cxx
 create mode 100644 Qt/Python/pqPythonDebugLeaksView.h

diff --git a/Applications/ParaView/ParaViewMainWindow.cxx b/Applications/ParaView/ParaViewMainWindow.cxx
index 379a249..32527f7 100644
--- a/Applications/ParaView/ParaViewMainWindow.cxx
+++ b/Applications/ParaView/ParaViewMainWindow.cxx
@@ -43,6 +43,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "pvStaticPluginsInit.h"
 #endif
 
+#ifdef PARAVIEW_ENABLE_PYTHON
+#include "pqPythonDebugLeaksView.h"
+#define DebugLeaksViewType pqPythonDebugLeaksView;
+#else
+#define DebugLeaksViewType vtkQtDebugLeaksView;
+#endif
+
 class ParaViewMainWindow::pqInternals : public Ui::pqClientMainWindow
 {
 };
@@ -50,6 +57,9 @@ class ParaViewMainWindow::pqInternals : public Ui::pqClientMainWindow
 //-----------------------------------------------------------------------------
 ParaViewMainWindow::ParaViewMainWindow()
 {
+  vtkQtDebugLeaksView* leaksView = new DebugLeaksViewType(this);
+  leaksView->show();
+
   this->Internals = new pqInternals();
   this->Internals->setupUi(this);
 
diff --git a/Qt/Python/CMakeLists.txt b/Qt/Python/CMakeLists.txt
index dc5ca41..1f30b06 100644
--- a/Qt/Python/CMakeLists.txt
+++ b/Qt/Python/CMakeLists.txt
@@ -25,6 +25,7 @@ GET_DIRECTORY_PROPERTY(include_dirs_tmp INCLUDE_DIRECTORIES)
 SET_DIRECTORY_PROPERTIES(PROPERTIES INCLUDE_DIRECTORIES "${MOC_INCLUDE_DIRS}")
 
 QT4_WRAP_CPP(QtPython_MOC_BUILT_SOURCES
+  pqPythonDebugLeaksView.h
   pqPythonDialog.h
   pqPythonMacroSupervisor.h
   pqPythonManager.h
@@ -39,6 +40,8 @@ QT4_WRAP_UI(QtPython_UI_BUILT_SOURCES
 SET_DIRECTORY_PROPERTIES(PROPERTIES INCLUDE_DIRECTORIES "${include_dirs_tmp}")
 
 VTK_ADD_LIBRARY(QtPython
+  pqPythonDebugLeaksView.cxx
+  pqPythonDebugLeaksView.h
   pqPythonDialog.cxx
   pqPythonDialog.h
   pqPythonMacroSupervisor.cxx
diff --git a/Qt/Python/pqPythonDebugLeaksView.cxx b/Qt/Python/pqPythonDebugLeaksView.cxx
new file mode 100644
index 0000000..fd32d88
--- /dev/null
+++ b/Qt/Python/pqPythonDebugLeaksView.cxx
@@ -0,0 +1,113 @@
+/*=========================================================================
+
+   Program: ParaView
+   Module:    pqPythonDebugLeaksView.cxx
+
+   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
+   All rights reserved.
+
+   ParaView is a free software; you can redistribute it and/or modify it
+   under the terms of the ParaView license version 1.2. 
+
+   See License_v1.2.txt for the full ParaView license.
+   A copy of this license can be obtained by contacting
+   Kitware Inc.
+   28 Corporate Drive
+   Clifton Park, NY 12065
+   USA
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=========================================================================*/
+#include "pqPythonDebugLeaksView.h"
+
+#include "pqPythonManager.h"
+#include "pqPythonDialog.h"
+#include "pqPythonShell.h"
+#include "pqApplicationCore.h"
+
+#include "vtkPython.h"
+#include "vtkPythonUtil.h"
+#include "vtkQtDebugLeaksModel.h"
+
+//-----------------------------------------------------------------------------
+class pqPythonShellContext {
+public:
+  pqPythonShellContext()
+  {
+    this->shell()->makeCurrent();
+  }
+
+  ~pqPythonShellContext()
+  {
+    this->shell()->releaseControl();
+  }
+
+  pqPythonShell* shell()
+  {
+    return qobject_cast<pqPythonManager*>(
+             pqApplicationCore::instance()->manager("PYTHON_MANAGER"))->pythonShellDialog()->shell();
+  }
+};
+
+//-----------------------------------------------------------------------------
+pqPythonDebugLeaksView::pqPythonDebugLeaksView(QWidget* p) : vtkQtDebugLeaksView(p)
+{
+
+}
+
+//-----------------------------------------------------------------------------
+pqPythonDebugLeaksView::~pqPythonDebugLeaksView()
+{
+
+}
+
+//-----------------------------------------------------------------------------
+void pqPythonDebugLeaksView::onObjectDoubleClicked(vtkObjectBase* object)
+{
+  pqPythonShellContext context;
+  this->addObjectToPython(object);
+}
+ 
+//-----------------------------------------------------------------------------
+void pqPythonDebugLeaksView::onClassNameDoubleClicked(const QString& className)
+{
+  pqPythonShellContext context;
+  this->addObjectsToPython(this->model()->getObjects(className));
+}
+
+//-----------------------------------------------------------------------------
+void pqPythonDebugLeaksView::addObjectToPython(vtkObjectBase* object)
+{
+  PyObject* mainModule = PyImport_AddModule((char*)"__main__");
+  PyObject* globalDict = PyModule_GetDict(mainModule);
+  PyObject* pyObj = vtkPythonUtil::GetObjectFromPointer(object);
+  PyDict_SetItemString(globalDict, "obj", pyObj);
+  Py_DECREF(pyObj);
+}
+
+//-----------------------------------------------------------------------------
+void pqPythonDebugLeaksView::addObjectsToPython(const QList<vtkObjectBase*>& objects)
+{
+  PyObject* pyListObj = PyList_New(objects.size());
+  for (int i = 0; i < objects.size(); ++i)
+    {
+    PyObject* pyObj = vtkPythonUtil::GetObjectFromPointer(objects[i]);
+    PyList_SET_ITEM(pyListObj, i, pyObj);
+    }
+
+  PyObject* mainModule = PyImport_AddModule((char*)"__main__");
+  PyObject* globalDict = PyModule_GetDict(mainModule);
+  PyDict_SetItemString(globalDict, "objs", pyListObj);
+  Py_DECREF(pyListObj);
+}
diff --git a/Qt/Python/pqPythonDebugLeaksView.h b/Qt/Python/pqPythonDebugLeaksView.h
new file mode 100644
index 0000000..d1cfe97
--- /dev/null
+++ b/Qt/Python/pqPythonDebugLeaksView.h
@@ -0,0 +1,56 @@
+/*=========================================================================
+
+   Program: ParaView
+   Module:    pqPythonDebugLeaksView.h
+
+   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
+   All rights reserved.
+
+   ParaView is a free software; you can redistribute it and/or modify it
+   under the terms of the ParaView license version 1.2. 
+
+   See License_v1.2.txt for the full ParaView license.
+   A copy of this license can be obtained by contacting
+   Kitware Inc.
+   28 Corporate Drive
+   Clifton Park, NY 12065
+   USA
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=========================================================================*/
+#ifndef _pqPythonDebugLeaksView_h
+#define _pqPythonDebugLeaksView_h
+
+#include "QtPythonExport.h"
+#include "vtkQtDebugLeaksView.h"
+
+class QTPYTHON_EXPORT pqPythonDebugLeaksView : public vtkQtDebugLeaksView
+{
+  Q_OBJECT
+
+public:
+
+  pqPythonDebugLeaksView(QWidget* p=0);
+  virtual ~pqPythonDebugLeaksView();
+
+protected:
+
+  virtual void onObjectDoubleClicked(vtkObjectBase* object);
+  virtual void onClassNameDoubleClicked(const QString& className);
+
+  virtual void addObjectToPython(vtkObjectBase* object);
+  virtual void addObjectsToPython(const QList<vtkObjectBase*>& objects);
+};
+
+#endif // !_pqPythonDebugLeaksView_h
-- 
1.7.11.msysgit.1

