From f94591526f6a9ea8edf5f0034de95020ae1a2011 Mon Sep 17 00:00:00 2001
From: stefan <stefan@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Wed, 21 Apr 2010 13:13:29 +0000
Subject: [PATCH] Adapt remote loader to new sip export fixed get_cpp_qobject
 method (check for None object) fixed get_py_qobject method (check for NULL
 pointer)

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2081 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 .../gui/pymod/export_remote_site_loader.cc    |  5 ++--
 modules/gui/pymod/scene/file_loader.py        |  7 -----
 modules/gui/pymod/sip_handler.hh              | 30 ++++++++++++-------
 3 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/modules/gui/pymod/export_remote_site_loader.cc b/modules/gui/pymod/export_remote_site_loader.cc
index 3446fd6c0..91b80ca77 100644
--- a/modules/gui/pymod/export_remote_site_loader.cc
+++ b/modules/gui/pymod/export_remote_site_loader.cc
@@ -50,9 +50,8 @@ struct WrappedRemoteSiteLoader : public RemoteSiteLoader
     }
 
     virtual QNetworkReply* ById(const QString& id, const QString& selection=QString()){
-      //This hackish code will be changed soon(er or later)
-      unsigned long addr=call_method<unsigned long, std::string>(self, "ByIdAddr", id.toStdString(), selection.toStdString());
-      QNetworkReply* network_reply= reinterpret_cast<QNetworkReply*>(addr);
+      object obj = call_method<object, std::string, std::string>(self, "ById", id.toStdString(), selection.toStdString());
+      QNetworkReply* network_reply= get_cpp_qobject<QNetworkReply>(obj);
       if(network_reply){
         return network_reply;
       }
diff --git a/modules/gui/pymod/scene/file_loader.py b/modules/gui/pymod/scene/file_loader.py
index 105ec8910..522dd6934 100644
--- a/modules/gui/pymod/scene/file_loader.py
+++ b/modules/gui/pymod/scene/file_loader.py
@@ -48,13 +48,6 @@ class BaseRemoteLoader(gui.RemoteSiteLoader):
     else:
       gui.FileLoader.LoadObject(str(file_name),str(selection))
     return None
-
-  #Hack for C++ (will be changed soon)
-  def ByIdAddr(self,id,selection):
-    reply = self.ById(id,selection)
-    if reply is not None:
-      return sip.unwrapinstance(reply)
-    return 0
     
   def IsImg(self):
     return False
diff --git a/modules/gui/pymod/sip_handler.hh b/modules/gui/pymod/sip_handler.hh
index 33436e213..9eb6ac7c4 100644
--- a/modules/gui/pymod/sip_handler.hh
+++ b/modules/gui/pymod/sip_handler.hh
@@ -20,7 +20,7 @@
 #define OST_GUI_SIP_HANDLER_HH
 
 #include <boost/python.hpp>
-
+#include <iostream>
 #include <QWidget>
 
 #include <ost/message.hh>
@@ -36,21 +36,29 @@ namespace ost { namespace gui {
 
 template <class O> object get_py_qobject(O* cpp_object)
 {
-  static object sip_module=import("sip");
-  static object pyqt4_module=import("PyQt4.QtCore");
-  QObject* qobject = qobject_cast<QObject*>(cpp_object);
-  unsigned long addr = reinterpret_cast<unsigned long>(qobject);
-  object py_qobject = pyqt4_module.attr("QObject");
-  object object = sip_module.attr("wrapinstance")(addr, py_qobject);
-  return object;
+  if (cpp_object != NULL){
+    static object sip_module=import("sip");
+    static object pyqt4_module=import("PyQt4.QtCore");
+    QObject* qobject = qobject_cast<QObject*>(cpp_object);
+    unsigned long addr = reinterpret_cast<unsigned long>(qobject);
+    object py_qobject = pyqt4_module.attr("QObject");
+    object object = sip_module.attr("wrapinstance")(addr, py_qobject);
+    return object;
+  }
+  return object();
 };
 
 
 template <class O> O* get_cpp_qobject(object py_object)
 {
-  static object sip_module=import("sip");
-  unsigned long addr = extract<unsigned long>(sip_module.attr("unwrapinstance")(py_object));
-  return reinterpret_cast<O*>(addr);
+  if(py_object.ptr() != Py_None){
+    static object sip_module=import("sip");
+    unsigned long addr = extract<unsigned long>(sip_module.attr("unwrapinstance")(py_object));
+    if(addr){
+      return reinterpret_cast<O*>(addr);
+    }
+  }
+  return NULL;
 };
 
 class SipHandlerBase {
-- 
GitLab