itom 1.1.0
|
00001 /* ******************************************************************** 00002 itom software 00003 URL: http://www.uni-stuttgart.de/ito 00004 Copyright (C) 2013, Institut fuer Technische Optik (ITO), 00005 Universitaet Stuttgart, Germany 00006 00007 This file is part of itom. 00008 00009 itom is free software; you can redistribute it and/or modify it 00010 under the terms of the GNU Library General Public Licence as published by 00011 the Free Software Foundation; either version 2 of the Licence, or (at 00012 your option) any later version. 00013 00014 itom is distributed in the hope that it will be useful, but 00015 WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library 00017 General Public Licence for more details. 00018 00019 You should have received a copy of the GNU Library General Public License 00020 along with itom. If not, see <http://www.gnu.org/licenses/>. 00021 *********************************************************************** */ 00022 00023 #ifndef PYTHONQTCONVERSION_H 00024 #define PYTHONQTCONVERSION_H 00025 00026 //python 00027 // see http://vtk.org/gitweb?p=VTK.git;a=commitdiff;h=7f3f750596a105d48ea84ebfe1b1c4ca03e0bab3 00028 #if (defined _DEBUG) && (!defined linux) 00029 #undef _DEBUG 00030 #include "Python.h" 00031 #define _DEBUG 00032 #else 00033 #include "Python.h" 00034 #endif 00035 00036 #include "pythonDataObject.h" 00037 #include "pythonPlugins.h" 00038 #include "pythonRegion.h" 00039 00040 #include "pythonPCL.h" 00041 00042 #include "../global.h" 00043 00044 #include "opencv/cv.h" 00045 #include <qvariant.h> 00046 #include <qmetaobject.h> 00047 #include <qlist.h> 00048 #include <qbytearray.h> 00049 #include <qpointer.h> 00050 00051 #if ITOM_POINTCLOUDLIBRARY > 0 00052 Q_DECLARE_METATYPE(ito::PCLPointCloud) 00053 Q_DECLARE_METATYPE(ito::PCLPoint) 00054 Q_DECLARE_METATYPE(ito::PCLPolygonMesh) 00055 #endif 00056 00057 Q_DECLARE_METATYPE(QSharedPointer<ito::DataObject>) 00058 Q_DECLARE_METATYPE(QPointer<ito::AddInDataIO>) 00059 Q_DECLARE_METATYPE(QPointer<ito::AddInActuator>) 00060 00061 class PythonQtConversion 00062 { 00063 public: 00064 enum unicodeEncodings { utf_8, latin_1, iso_8859_1, mbcs, ascii, utf_16, utf_16_LE, utf_16_BE, utf_32, utf_32_BE, utf_32_LE, other }; 00065 00067 static PyObject* QStringToPyObject(const QString& str); 00068 00070 static PyObject* QStringListToPyObject(const QStringList& list); 00071 00073 static PyObject* QStringListToPyList(const QStringList& list); 00074 00076 static QString PyObjGetRepresentation(PyObject* val); 00077 00079 static QString PyObjGetString(PyObject* val) { bool ok; QString s = PyObjGetString(val, false, ok); return s; } 00081 static QString PyObjGetString(PyObject* val, bool strict, bool &ok); 00083 static QByteArray PyObjGetBytes(PyObject* val, bool strict, bool &ok); 00085 static int PyObjGetInt(PyObject* val, bool strict, bool &ok); 00087 static qint64 PyObjGetLongLong(PyObject* val, bool strict, bool &ok); 00089 static quint64 PyObjGetULongLong(PyObject* val, bool strict, bool &ok); 00091 static double PyObjGetDouble(PyObject* val, bool strict, bool &ok); 00093 static QVector<double> PyObjGetDoubleArray(PyObject* val, bool strict, bool &ok); 00095 static QVector<int> PyObjGetIntArray(PyObject* val, bool strict, bool &ok); 00097 static bool PyObjGetBool(PyObject* val, bool strict, bool &ok); 00098 00099 #if ITOM_POINTCLOUDLIBRARY > 0 00100 static ito::PCLPointCloud PyObjGetPointCloud(PyObject *val, bool strict, bool &ok); 00101 static ito::PCLPoint PyObjGetPoint(PyObject *val, bool strict, bool &ok); 00102 static ito::PCLPolygonMesh PyObjGetPolygonMesh(PyObject *val, bool strict, bool &ok); 00103 00104 00105 static ito::PCLPointCloud* PyObjGetPointCloudNewPtr(PyObject *val, bool strict, bool &ok); 00106 static ito::PCLPolygonMesh* PyObjGetPolygonMeshNewPtr(PyObject *val, bool strict, bool &ok); 00107 #endif 00108 00109 static ito::DataObject* PyObjGetDataObjectNewPtr(PyObject *val, bool strict, bool &ok); 00110 00111 static QSharedPointer<ito::DataObject> PyObjGetSharedDataObject(PyObject *val, bool &ok); //is always strict, only dataobjects are allowed 00112 00114 static QStringList PyObjToStringList(PyObject* val, bool strict, bool& ok); 00115 00116 00119 static QVariant PyObjToQVariant(PyObject* val, int type = -1); 00120 00121 static QVariant QVariantCast(const QVariant &item, QVariant::Type destType, ito::RetVal &retval); 00122 00123 static QVariant QVariantToEnumCast(const QVariant &item, const QMetaEnum &enumerator, ito::RetVal &retval); 00124 00127 static bool PyObjToVoidPtr(PyObject* val, void **retPtr, int *retType, int type = -1, bool strict = false); 00128 00130 static PyObject* GetPyBool(bool val); 00131 static PyObject* QVariantToPyObject(const QVariant& v); 00132 00133 static PyObject* QVariantMapToPyObject(const QVariantMap& m); 00134 static PyObject* QVariantListToPyObject(const QVariantList& l); 00135 00136 #if ITOM_POINTCLOUDLIBRARY > 0 00137 static PyObject* PCLPointCloudToPyObject(const ito::PCLPointCloud& c); 00138 static PyObject* PCLPointToPyObject(const ito::PCLPoint& c); 00139 static PyObject* PCLPolygonMeshToPyObject(const ito::PCLPolygonMesh& c); 00140 #endif 00141 00142 static PyObject* DataObjectToPyObject(const ito::DataObject& dObj); 00143 00144 static PyObject* ConvertQtValueToPythonInternal(int type, const void* data); 00145 static PyObject* convertPyObjectToQVariant(PyObject *argument, QVariant &qVarArg); 00146 00147 static PyObject* QByteArrayToPyUnicode(const QByteArray &ba, const char *errors = "replace"); 00148 static PyObject* QByteArrayToPyUnicodeSecure(const QByteArray &ba, const char *errors = "replace"); 00149 static PyObject* ByteArrayToPyUnicode(const char* byteArray, Py_ssize_t len = 0, const char *errors = "replace"); 00150 00151 static PyObject* PyUnicodeToPyByteObject(PyObject *unicode, const char *errors = "replace"); 00152 00153 friend class PythonEngine; //such that the pythonEngine can set the encoding values below 00154 00155 private: 00156 static unicodeEncodings textEncoding; 00157 static QByteArray textEncodingName; 00158 00163 static QHash<void*,PyObject*> m_pyBaseObjectStorage; 00164 static void baseObjectDeleterDataObject(ito::DataObject *sharedObject) 00165 { 00166 QHash<void*,PyObject*>::iterator i = m_pyBaseObjectStorage.find((void*)sharedObject); 00167 if(i != m_pyBaseObjectStorage.end()) 00168 { 00169 Py_XDECREF(i.value()); 00170 m_pyBaseObjectStorage.erase(i); 00171 } 00172 00173 delete sharedObject; 00174 } 00175 }; 00176 00177 #endif